/*______________________________________

  mizFlipFlash #080601 (version 0.2.6a)
  
  Copyright (C) 2007-2008 mizuyari All rights reserved.
  Script written by Mahiro Komura <mahirok@mizuyari.jp>
  http://mizuyari.jp/
______________________________________*/

function flipFlash(parentElementId, src, width, height, version, params) {
 if (!mizuyari) return;
 if (!parentElementId || !src || !width || !height || !version) return;
 this.params = new Object();
 this.attributes = new Object();
 this.variables = new Object();
 this.parentElementId = parentElementId;
 this.src    = src;
 this.width  = width;
 this.height = height;
 var tempVer = version.match(/^[0-9]+\.[0-9]+\.[0-9]+$/) ? version.split(".") : [version,0,0];
 this.setAttribute("id","mizmc" + Math.floor(Math.random()*10000));
 this.setVariable("contentVersion",eval(tempVer[0]+"."+tempVer[1]));
 this.setVariable("contentVerRev",eval(tempVer[2]));
 this.setVariable("expressInstall",false);
 this.setVariable("expressInstallSrc","playerProductInstall.swf");
 this.setVariable("expressInstallRedirectURL",window.location);
 if (params && typeof params == "object") this.params = params;
 this.initialize();
}

flipFlash.prototype = {

 initialize : function() { // initialize #080531
  var getFlashVersion = mizuyari.getFlashVersion(1);
  this.flashVersion = (getFlashVersion == -1) ? -1 : eval(getFlashVersion[0]+"."+getFlashVersion[1]);
  this.flashVerRev  = (getFlashVersion == -1) ? -1 : eval(getFlashVersion[2]);
  this.canExpressInstall = (mizuyari.ua.isWin || mizuyari.ua.isMac) && (this.flashVersion >= 7 || (this.flashVersion == 6 && this.flashVerRev >= 65)); 
  this._codebaseURL = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab";
  this._initFlag = true;
  this._locked = false;
  this._finished = false;
 },

 addParam : function(name, value) {
  this.params[name] = value;
 },

 getParam: function(name) {
  return this.params[name];
 },

 getParams: function() {
  return this.params;
 },

 setAttribute: function(name, value) {
  this.attributes[name] = value;
 },

 getAttribute: function(name) {
  return this.attributes[name];
 },

 getAttributes: function() {
  return this.attributes;
 },

 setVariable: function(name, value) {
  this.variables[name] = value;
 },

 getVariable: function(name) {
  return this.variables[name];
 },

 enableExpressInstall: function(src) {
  this.setVariable("expressInstall", true);
  if (src) this.setVariable("expressInstallSrc", src);
 },

 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.getVariable("contentVersion");
  var contentVerRev = this.getVariable("contentVerRev");
  var flashCanPlay = ((this.flashVersion > contentVersion) || (this.flashVersion == contentVersion && this.flashVerRev >= contentVerRev));
  // trace("flashVersion: %d, contentVersion: %d", this.flashVersion, contentVersion);
  if (flashCanPlay) {
    this.putflash();
  } else if (this.getVariable("expressInstall") && this.canExpressInstall ) {
    var MMPlayerType = (mizuyari.ua.isWinIE == true) ? "ActiveX" : "PlugIn";
    var MMredirectURL = (!this.getVariable("expressRedirectURL")) ? window.location : this.getVariable("expressRedirectURL");
    this.src = this.getVariable("expressInstallSrc");
    var tempFlashVars = (this.getParam("FlashVars")) ? this.getParam("FlashVars") : "";
    tempFlashVars += '&MMredirectURL='+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+document.title
    this.addParam("FlashVars", tempFlashVars);
    params = this.getParams();
    this.putflash();
  } else if (mizuyari.ua.isWinIE && this.flashVersion == -1) {
    this.putflash();
  } else if (mizuyari.ua.isFirefox && this.flashVersion == -1) {
    this.putflash();
  } else {
   this.redirect();
  }
 },
 
 redirect : function() {
  var redirectTimer;
  var redirectURL  = this.getVariable('redirectURL'); if (!redirectURL) return;
  var redirectWait = (this.getVariable('redirectWait') >= 0) ? this.getVariable('redirectWait') : 10000;
  redirectTimer = setTimeout("location.href='" + redirectURL + "'", redirectWait);
 },

 putflash : function() { // putflash #080531
  if (!document.getElementById) {
   document.open();
   document.write(this.createEmbedElement());
   document.close();
   return;
  }
  if (mizuyari.ua.isWinIE) {
   var parentElmt = miz$(this.parentElementId);
   this.createObjectElementForWinIE();
  } else if (mizuyari.ua.isMacIE) {
   this.createEmbedElement();
  } else {
   this.createObjectElement();
  }
 },

 createObjectElement: function() { // createObjectElement #080531
  var parentElmt = miz$(this.parentElementId);
  var params = this.getParams();
  var attributes = this.getAttributes();
  var objectElmt = document.createElement('object');
  objectElmt.setAttribute("type", "application/x-shockwave-flash");
  objectElmt.setAttribute("data", this.src);
  objectElmt.setAttribute("width", this.width);
  objectElmt.setAttribute("height", this.height);
  objectElmt.setAttribute("data", this.src);
  for (var key in attributes) {
   objectElmt.setAttribute(key, attributes[key]);
  }
  objectElmt.innerHTML = parentElmt.innerHTML;

  var paramElmt = document.createElement('param');
  paramElmt.setAttribute("name", "width");
  paramElmt.setAttribute("value", this.width);
  objectElmt.appendChild(paramElmt);

  var paramElmt = document.createElement('param');
  paramElmt.setAttribute("name", "height");
  paramElmt.setAttribute("value", this.height);
  objectElmt.appendChild(paramElmt);

  for (var key in params) {
   var paramElmt = document.createElement('param');
   paramElmt.setAttribute("name", key);
   paramElmt.setAttribute("value", params[key]);
   objectElmt.appendChild(paramElmt);
  }
  parentElmt.innerHTML = "";
  parentElmt.appendChild(objectElmt);
  this._finished = true;
 },

 createObjectElementForWinIE : function() { // createObjectElementForWinIE #080531
  var parentElmt = miz$(this.parentElementId);
  var params = this.getParams();
  var attributes = this.getAttributes();
  var temp = "";
  var codebaseVersion = this.getVariable('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 + '"';
  for (var key in attributes) temp += ' '+ key +'="'    + attributes[key] + '"';
  temp += '>';
  temp += parentElmt.innerHTML;
  temp += '<param name="movie" value="' + this.src + '" \/>';
  for (var key in params) temp += '<param name="' + key + '" value="' + params[key] + '" \/>';
  parentElmt.innerHTML = temp;
  this._finished = true;
 },

 createEmbedElement : function() { // createEmbedElement #080531
  var parentElmt = miz$(this.parentElementId);
  var params = this.getParams();
  var attributes = this.getAttributes();
  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 + '"';
  for (var key in attributes) temp += ' ' + key +'="' + attributes[key] + '"';
  for (var key in params) temp += ' ' + key + '="' + params[key] + '"';
  temp += ' type="application/x-shockwave-flash" pluginspage="'+ pluginspageURL +'">';
  temp += '<noembed>' + parentElmt.innerHTML + '<\/noembed>';
  temp += '<\/embed>';
  parentElmt.innerHTML = temp;
  this._finished = true;
 },

 setNewWidth : function(newWidth) { // setNewWidth #080601
  if (!this._finished) return;
  if (!isNaN(parseInt(newWidth,10)))
  var elmtId = this.getAttribute("id");
  miz$(elmtId).style.width = newWidth + "px";
 },

 setNewHeight : function(newHeight) { // setNewHeight #080601
  if (!this._finished) return;
  if (!isNaN(parseInt(newHeight,10)))
  var elmtId = this.getAttribute("id");
  miz$(elmtId).style.height = newHeight + "px";
 } 

}

