/**************************************************
 * CFlashObj, version 0.10
 * Copyright 2007 Ivengi BV. Benelux
 **************************************************/
/* 
  Comments :
  include flash.js in your webpage :
  <script language=javascript src="flash.js" type=text/javascript></script>
  
  Create a CFlashObj to display the flash object :
  <script language="JavaScript" type="text/JavaScript">
    var Flash = new CFlashObj("my.swf", 300, 300, "http://www.ivengi.com/pics/my.jpg");
    Flash.Write();
  </script>
  
  Use AddParam("Key", "Value") to add additional param values
*/
 
  
/**************************************************************************************
 *  CFlashDetect                                                                      *
 **************************************************************************************/

function CFlashDetect(RequiredFlashVersion)
{
	this.bFlashInstalled = false;
  this.bOldBrowserDetected = false;
  this.DetectedFlashVersion = 0;
  this.RequiredFlashVersion = RequiredFlashVersion;
		
  if (navigator.mimeTypes) {
    if (navigator.mimeTypes["application/x-shockwave-flash"]) {
      var Plugin = navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin;
      if (Plugin) {
        var words = navigator.plugins["Shockwave Flash"].description.split(" ");
        for (var i = 0; i < words.length; ++i) {
          if (!isNaN(parseInt(words[i]))) {
            this.DetectedFlashVersion = words[i];
            this.bFlashInstalled = true;
            break;
          }
        }
      }
    }
  } 

  if (!this.bFlashInstalled) {
    if (navigator.appVersion.indexOf("MSIE") != -1) {
      if (navigator.appVersion.indexOf("Win") != -1) {
        FlashCanPlay = false;
        if (navigator.appVersion.indexOf("Win") != -1) {
          document.write('<scr' + 'ipt LANGUAGE=VBScript\> \n');
          document.write('on error resume next \n');
          document.write('FlashCanPlay = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + this.RequiredFlashVersion + '")))\n');
          document.write('</scr' + 'ipt\> \n');
        }
        if (FlashCanPlay) {
          this.bFlashInstalled = true;
          this.DetectedFlashVersion = this.RequiredFlashVersion;
        }
      }
    }
  }
  
  if (this.bCheckForRecentIeVersion) {
    if (this.IsInternetExplorer()) {
      if (!IsInterExplorerVersion(5.5)) {
        this.bOldBrowserDetected = true;
      }
    }
  }	
}

CFlashDetect.prototype.IsInterExplorerVersion = function(MinimalVersion)
{
  if (navigator.appVersion.indexOf("MSIE") != -1) {
		Version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		if (Version >= MinimalVersion) {
		  return true;
		}
	}
	return false; 
}

/**************************************************************************************
 *  IfFlashRedirect                                                                         *
 **************************************************************************************/

function IfFlashRedirect(RedireclUrl)
{
  var DetectFlash = new CFlashDetect(5);
  
  if (DetectFlash.bFlashInstalled) {
  	document.write('<scr' + 'ipt language="JavaScript" type="text/JavaScript"\> \n');
  	document.write('window.location.href="' + RedireclUrl + '";');
  	document.write('</scr' + 'ipt\>\n');
  }
  return;
}

/**************************************************************************************
 *  CFlashObj                                                                         *
 **************************************************************************************/
 
function CFlashObj(Movie, Width, Height, ImgSrc)
{
  // For testing
  this.bForceNoFlashDetected = false;
  
  this.bInit = false;
  this.bFlashInstalled = false;
  this.bOldBrowserDetected = false;
  this.DetectedFlashVersion = 0;
  this.RequiredFlashVersion = 5;  
  
  this.Id = "";
  this.Align = "";
  this.Movie = Movie;
    
  this.Width = Width;
  this.Height = Height;
  this.ImgSrc = ImgSrc;
  this.Movie = Movie;
  
  this.aParam = new Array();
  this.aParam[0] = new Array();
  this.aParam[0][0] = "quality";
  this.aParam[0][1] = "high";
  
  this.aParam[1] = new Array();
  this.aParam[1][0] = "menu";
  this.aParam[1][1] = "false";
  
  this.aParam[2] = new Array();
  this.aParam[2][0] = "scale";
  this.aParam[2][1] = "noscale";
  
  this.aParam[3] = new Array();
  this.aParam[3][0] = "wmode";
  this.aParam[3][1] = "transparent";
  
  this.aParam[4] = new Array();
  this.aParam[4][0] = "allowScriptAccess";
  this.aParam[4][1] = "always";

}  
  
CFlashObj.prototype.IsInternetExplorer = function()
{
  if (navigator.appVersion.indexOf("MSIE") != -1) {
    return true;
  }
  return false;
}

CFlashObj.prototype.Check = function()
{
	var DetectFlash = new CFlashDetect(this.RequiredFlashVersion);
	this.bFlashInstalled = DetectFlash.bFlashInstalled;
  this.bOldBrowserDetected = DetectFlash.bOldBrowserDetected;
  this.DetectedFlashVersion = DetectFlash.DetectedFlashVersion;
  this.bInit = true;
}

CFlashObj.prototype.AddParam = function(Key, Value)
{
  var bFound = false;
  for (var i=0; i < this.aParam.length; i++) {
    if (this.aParam[i][0] == Key) {
      this.aParam[i][1] = Value;
      bFound = true;
    }
  }
  
  if (!bFound) {
    this.aParam[this.aParam.length-1] = new Array();
    this.aParam[this.aParam.length-1][0] = Key;
    this.aParam[this.aParam.length-1][1] = Value;
  }
}

CFlashObj.prototype.Write = function()
{
  if (!this.bInit) {
    this.Check();
  }
  
  var Out;
  
  if (!this.bOldBrowserDetected && this.bFlashInstalled && (this.DetectedFlashVersion >= this.RequiredFlashVersion) && !this.bForceNoFlashDetected) {
  	
    Out = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" width=\"" + this.Width + "\" height=\"" + this.Height + "\"";
    
    if (this.Id != "") {
     Out += " id=\"" + this.Id + "\"";
    }
  	
  	if (this.Align != "") {
  	 Out += " align=\"" + this.Align + "\"";
  	}
  	
  	Out += ">";
		Out += "<param name=\"movie\" value=\"" +  this.Movie + "\" />";
		
		var EmbedParams = "";
		
    for (var i=0; i < this.aParam.length; i++) {
      Out += "<param name=\"" + this.aParam[i][0] + "\" value=\"" +  this.aParam[i][1] + "\" />";
      EmbedParams += " " + this.aParam[i][0] + "=\"" + this.aParam[i][1] + "\""; 
    }
    
		Out += "<embed src=\"" + this.Movie + "\"" + EmbedParams + " width=\"" + this.Width + "\" height=\"" + this.Height + "\"";
		
		if (this.Id != "") {
			Out += " name=\"" + this.Id + "\"";
		} else {
			Out += " name=\"SwfMovie\"";
		}
			
		if (this.Align != "") {
			Out += " align=\"" + this.Align + "\"";
		}
		
		Out += " type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
    
    Out += "</object>";
  } else {
    Out = "<img src=\"" + this.ImgSrc + "\" width=\"" + this.Width + "\" height=\"" + this.Height + "\" />";
  }
  // alert(Out);
  document.write(Out);
}
