/*______________________________________

  mizFlapFlash #061110 (#001)
  
  Copyright (C) 2006 Mizuyari All rights reserved.
  Script written by Mahiro Komura.
  http://mizuyari.jp/
______________________________________*/

function flapFlash(parentElementId, src, width, height, params) {
 if (!mizuyari) return;
 if (!parentElementId || !src || !width || !height) return;
 this.params = new Object();
 this.attributes = new Object();
 this.parentElementId = parentElementId;
 this.src    = src;
 this.width  = width;
 this.height = height;
 if (params && typeof params == "object") this.params = params;
 this.initialize();
}

flapFlash.prototype = {

  initialize : function() {
   this.flashVersion = mizuyari.getFlashVersion();
   this._codebaseURL = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab";
   this._initFlag = true;
   this._locked = false;
   // mizuyari.addOnLoadEvent( function() { return mcObj.start(); });
  },

  addParam : function(name, value) {
   this.params[name] = value;
  },

  getParams: function() {
   return this.params;
  },

  setAttribute: function(name, value) {
   this.attributes[name] = value;
  },

  getAttribute: function(name){
   return this.attributes[name];
  },

  start: function() {
   if (!this._initFlag || this._locked ) return false;
   this._locked = true;
   var ua = mizuyari.ua;
   var params = this.getParams();
   if (!params.menu) params.menu = "false";
   var contentVersion = this.getAttribute("contentVersion");
   var flashCanPlay = (!contentVersion) ? true : this.flashVersion >= contentVersion;
   //trace("flashVersion: %d, contentVersion: %d", this.flashVersion, contentVersion);
   if (flashCanPlay || mizuyari.ua.isWinIE) {
    var tempHTML = "";
    tempHTML = (mizuyari.ua.isWinIE) ? this.setObjectElementForWinIE() : (mizuyari.ua.isMacIE) ? this.setEmbedElement() : this.setObjectElement();
    if (document.getElementById) {
     var parentElmt = document.getElementById(this.parentElementId);
     parentElmt.innerHTML = tempHTML;
    } else {
     document.open();
     document.write(tempHTML);
     document.close();
    }
   } else {
    this.redirect();
   }
  },
  
  redirect : function() {
   var redirectTimer;
   var redirectURL  = this.getAttribute('redirectURL'); if (!redirectURL) return;
   var redirectWait = (this.getAttribute('redirectWait')) ? this.getAttribute('redirectWait') : 5000;
   redirectTimer = setTimeout("location.href='" + redirectURL + "'", redirectWait);
  },

  setObjectElement: function() {
   var params = this.getParams();
   var temp = "";
   temp += '<object type="application/x-shockwave-flash"';
   temp += ' data="' + this.src +'" width="' + this.width + '" height="' + this.height + '"';
   if (this.getAttribute('id'))    temp += ' id="'    + this.getAttribute('id') + '"';
   if (this.getAttribute('class')) temp += ' class="' + this.getAttribute('class') + '"';
   if (this.getAttribute('align')) temp += ' align="' + this.getAttribute('align') + '"';
   temp += '>';
   temp += '<param name="width" value="' + this.width + '" \/>';
   temp += '<param name="height" value="' + this.height + '" \/>';
   for (var key in params) temp += '<param name="' + key + '" value="' + params[key] + '" \/>';
   temp += '<\/object>';
   return temp;
  },

  setObjectElementForWinIE : function() {
   var params = this.getParams();
   var temp = "";
   var codebaseVersion = function() { return this.getAttribute('codebaseVersion'); }
   if (codebaseVersion) this._codebaseURL += '#version=' + codebaseVersion;
   temp += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="' + this._codebaseURL + '"';
   temp += ' width="' + this.width + '" height="' + this.height + '"';
   if (this.getAttribute('id'))    temp += ' id="'    + this.getAttribute('id') + '"';
   if (this.getAttribute('class')) temp += ' class="' + this.getAttribute('class') + '"';
   if (this.getAttribute('align')) temp += ' align="' + this.getAttribute('align') + '"';
   temp += '>';
   temp += '<param name="movie" value="' + this.src + '" \/>';
   for (var key in params) temp += '<param name="' + key + '" value="' + params[key] + '" \/>';
   return temp;
  },

  setEmbedElement : function() {
   var params = this.getParams();
   var temp = "";
   var pluginspageURL = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
   temp += '<embed src="' + this.src + '" width="' + this.width + '" height="' + this.height + '"';
   if (this.getAttribute('id'))    temp += ' id="'    + this.getAttribute('id') + '"';
   if (this.getAttribute('class')) temp += ' class="' + this.getAttribute('class') + '"';
   if (this.getAttribute('align')) temp += ' align="' + this.getAttribute('align') + '"';
   for (var key in params) temp += ' ' + key + '="' + params[key] + '"';
   temp += ' type="application/x-shockwave-flash" pluginspage="'+ pluginspageURL +'"><\/embed>';
   return temp;
  }
}
