/*
 * Flash detection
 * (c) Stendahls.net
 *
 * Set querystring parameter 'showflash' for added control:
 *         true = Flash movie will always be inserted (IE will prompt user to install if Flash does not exist)
 *        false = Flash movie is not inserted
 *  unspecified = Flash movie is inserted if Flash is installed (default)
 */

// Detect Client Browser type
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

var flashParams;
var flashVars;
function initSWF() {
	flashParams = new Array();
	flashVars = new Array();	
}

// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
      // NS/Opera version >= 3 check for Flash plugin in plugin array
      if (navigator.plugins != null && navigator.plugins.length > 0) {
            if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
                  var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
                        var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
                        descArray = flashDescription.split(" ");
                        tempArrayMajor = descArray[2].split(".");
                        versionMajor = tempArrayMajor[0];
                  if ( descArray[3] != "" ) {
                        tempArrayMinor = descArray[3].split("r");
                  } else {
                        tempArrayMinor = descArray[4].split("r");
                  }
                        versionMinor = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
                        flashVer = parseFloat(versionMajor + "." + versionMinor);
            } else {
                  flashVer = -1;
            }
      }
      // MSN/WebTV 2.6 supports Flash 4
      else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
      // WebTV 2.5 supports Flash 3
      else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
      // older WebTV supports Flash 2
      else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
      // Can't detect in all other cases
      else {
            
            flashVer = -1;
      }
      return flashVer;
}

// If called with no parameters this function returns a floating point value 
// which should be the version of the Flash Player or 0.0 
// ex: Flash Player 6r65 returns 6.65
function detectFlashVer(reqMajorVer, reqMinorVer) 
{
      reqVer = parseFloat(reqMajorVer + "." + reqMinorVer);
      // loop backwards through the versions until we find the newest version      
      for (i=25;i>0;i--) {      
            if (isIE && isWin && !isOpera) {;
                  versionStr = VBGetSwfVer(i);
            } else {
                  versionStr = JSGetSwfVer(i);
            }
            if (versionStr == -1) {
					return false;
			  } else if (versionStr != 0) {
                  if(isIE && isWin && !isOpera) {
                        tempArray = versionStr.split(" ");
                        tempString = tempArray[1];
                        versionArray = tempString .split(",");
                        
                        versionMajor = versionArray[0];
                        versionMinor   = versionArray[2];
                        
                        versionString = versionMajor + "." + versionMinor;
                        versionNum = parseFloat(versionString);
                  } else {
                         versionNum = versionStr;
                  }
                  return (versionNum >= reqVer ? true : false );            
            }
      }
      
      return (reqVer ? false : 0.0);
}

// get value of querystring param
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return "";
	}
}

// Check if required Flash version is installed and inserts movie if ok
function checkSWF(elementId, url, width, height, reqMajVer, reqMinVer) {
	var showflash = getQueryParamValue('showflash');
	if(showflash == 'false')
		return;

	var hasRightVersion = detectFlashVer(reqMajVer, reqMinVer);
	if(hasRightVersion || showflash=='true')
		insertSWF(elementId, url, width, height);
}

function urlEncodeParam(value) {
		var v = value == null ? "": value;
		v = v.replace(/\%/g, "%25");
		v = v.replace(/\r/g, "%0D");
		v = v.replace(/\n/g, "%0A");
		v = v.replace(/\t/g, "%09");
		v = v.replace(/\&amp;/g, "&");
		v = v.replace(/\&/g, "%26");
		v = v.replace(/\=/g, "%3D");
		v = v.replace(/\"/g, "%22");
		v = v.replace(/\+/g, "%2B");
		v = v.replace(/ /g, "+");
		return v;
}

function getFlashVarPairs() {
	var pairs = new Array();
	for(var key in flashVars)
		pairs.push(key +"="+ this.urlEncodeParam(flashVars[key]));
	return pairs;
}

// Inserts the Flash movie tags
function insertSWF(elementId, url, width, height) {
	if(!flashParams['bgcolor'])
		flashParams['bgcolor'] = '#000000';

	var flashvars = getFlashVarPairs().join('&');

	var oeTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
		+ ' width="'+width+'" height="'+height+'"'
		+ ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
		+ '<param name="movie" value="'+url+'" /><param name="menu" value="false" /><param name="quality" value="best" />';

	for(var p in flashParams)
		oeTags += '<param name="'+p+'" value="'+flashParams[p]+'" />';

	if(flashvars.length > 0)
		oeTags += '<param name="flashvars" value="'+flashvars+'" />';
		
	oeTags += '<embed src="'+url+'" menu="false" quality="best"'
		+ ' width="'+width+'" height="'+height+'" name="'+elementId+'swf" align="middle"'
		+ ' play="true" quality="high"';
	
	for(var p in flashParams)
		oeTags += ' '+p+'="'+flashParams[p]+'"';

	if(flashvars.length > 0)
		oeTags += ' flashvars="'+flashvars+'"';
		
	oeTags += ' allowScriptAccess="sameDomain"'
		+ ' type="application/x-shockwave-flash"'
		+ ' pluginspage="http://www.macromedia.com/go/getflashplayer">'
		+ '<\/embed>'
		+ '<\/object>';
	
	var div = (document.getElementById) ? document.getElementById(elementId) : ((document.all) ? document.all[elementId] : null)
	if(div) div.innerHTML = oeTags;
}

/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}
