/*
 * FlashObject embed
 * http://blog.deconcept.com/2004/10/14/web-standards-compliant-javascript-flash-detect-and-embed/
 *
 * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
 *
 * v1.0.7 - 11-17-2004
 *
 * Create and write a flash movie to the page, includes detection
 *
 * Usage:
 *
 *	myFlash = new FlashObject("path/to/swf.swf", "swfid", "width", "height", flashversion, "backgroundcolor");
 *	myFlash.altTxt = "Upgrade your Flash Player!";                // optional
 *	myFlash.addParam("wmode", "transparent");                     // optional
 *	myFlash.addVariable("varname1", "varvalue");                  // optional
 *	myFlash.addVariable("varname2", getQueryParamValue("myvar")); // optional
 *	myFlash.write();
 *
 */

//Check if already defined...
if (typeof FlashObject == 'undefined') {

FlashObject = function(swf, id, w, h, type, align) {
	this.swf = swf;
	this.id = id;
	this.width = w;
	this.height = h;
	this.version = 7; // default to 7
	this.align = align || "left";
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	//this.altTxt = "Please <a href='http://www.macromedia.com/go/getflashplayer'>upgrade your Flash Player</a>.";
	this.altTxt;
	this.bypassTxt = "<p>Already have Flash Player? <a href='?detectflash=false&"+ this.sq +"'>Click here if you have Flash Player "+ this.version +" installed</a>.</p>";
	this.params = new Object();
	this.variables = new Object();
	//if (c) this.color = this.addParam('bgcolor', c);
	this.addParam('quality', 'high'); // default to high
//	this.doDetect = getQueryParamValue('detectflash');
	this.doDetect = "false";
	this.embedType = type || "flash";
	
	//Additional attributes
	this.addParam('AutoStart','true');
	this.addParam('WindowLess','false');
	this.addParam('VideoBorderWidth','0');
	this.addParam('VideoBorderColor','ffffff');
	this.addParam('ShowControls','true');
	
}

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

FlashObject.prototype.getParams = function() {
    return this.params;
}

FlashObject.prototype.getParam = function(name) {
    return this.params[name];
}

FlashObject.prototype.addVariable = function(name, value) {
	this.variables[name] = value;
}

FlashObject.prototype.getVariable = function(name) {
    return this.variables[name];
}

FlashObject.prototype.getVariables = function() {
    return this.variables;
}

FlashObject.prototype.getParamTags = function() {
	var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}

FlashObject.prototype.getHTML = function() {

    if (this.embedType=='slideShow'){
		imageDir=this.getParam('imageDir');
		url = "http://sitebuilder.warwick.ac.uk/sitebuilder2/render/tocImages.htm?sbrPage="+imageDir+"&showGallery=off"+"&rn="+new Date().getTime();
		//url = "http://www2.warwick.ac.uk/contentstore/subcontent/images.do?filedir="+imageDir+"&showGallery=off"+"&rn="+new Date().getTime();
		this.addVariable('xmlPath',unescape(url));
    }

    var flashHTML = "";
    if (window.ActiveXObject && navigator.userAgent.indexOf('Mac') == -1) { // PC IE
	if (this.embedType=="flash"|| this.embedType=='slideShow'){
     	
	flashHTML += '<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 + '" id="' + this.id + '">';
//	flashHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">';
	        flashHTML +='<param name="allowScriptAccess" value="sameDomain" />';
		flashHTML += '<param name="movie" value="' + this.swf + '" />';
		flashHTML += '<param name="quality" value="high" />';
        	if (this.getParamTags() != null) {
	            flashHTML += this.getParamTags();
	        }
	        if (this.getVariablePairs() != null) {
        	    flashHTML += '<param name="flashVars" value="' + this.getVariablePairs() + '" />';
	        }
	        flashHTML += '</object>';
	}
	else if (this.embedType=="mediaplayer"){
		flashHTML = this.getMplayerCode();

	} else {
		flashHTML += "<p>embed type " + this.embedType +" not recognised!</p>";
	}


    }
    else { // Everyone else
        if (this.embedType=="flash" || this.embedType=='slideShow'){
		
		
		
		flashHTML += '<embed allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + this.swf + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '" name="' + this.id + '"';
	       	for (var param in this.getParams()) {
        	    flashHTML += ' ' + param + '="' + this.getParam(param) + '"';
	       	}

        	if (this.getVariablePairs() != null) {
	       	    flashHTML += ' flashVars="' + this.getVariablePairs() + '"';
	        }
	       	flashHTML += '></embed>';
	}

	else if (this.embedType=="mediaplayer"){
		flashHTML = this.getMplayerCode();

	} else {
		flashHTML += "<p>embed type " + this.embedType +" not recognised!</p>";
	}

    }
//	document.getElementById('output').innerHTML=flashHTML;

    flashHTML = '<div align="' + this.align + '">' + flashHTML + '</div>';

    return flashHTML;	
}


FlashObject.prototype.getMplayerCode = function() {

		
		var flashHTML = "<object id=\""+ this.id +"\" ";
		flashHTML += "width=\"" + this.width + "\" ";
		flashHTML += "height=\"" + this.height + "\" ";
		flashHTML += "vspace=\"0\" standby=\"Loading Microsoft&amp;#65533;Windows&amp;#65533; Media Player components...\" ";
		flashHTML += "codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\" ";
		flashHTML += "classid=\"CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95\" ";
		flashHTML += "type=\"application/x-oleobject\" ";
		flashHTML += "hspace=\"0\"> ";
		flashHTML += "<param name=\"Filename\" value=\"" + this.swf + "\" /> ";
				
		for (var param in this.getParams()) {
        	flashHTML += "<param name=\"" +param + "\" value=\"" + this.getParam(param) +" \">";
    	}
		
		

		//flashHTML += "<param name=\"AutoStart\" value=\""+this.getParam('AutoStart')+"\" /> ";
		//flashHTML += "<param name=\"DisplaySize\" value=\"0\" /> ";
		//flashHTML += "<param name=\"AutoSize\" value=\"True\" /> ";
		//flashHTML += "<param name=\"ShowControls\" value=\"True\" /> ";
		//flashHTML += "<param name=\"ShowStatusBar\" value=\"True\" /> ";
		flashHTML += "<embed name=\""+this.id+"\" ";
		flashHTML += "type=\"application/x-mplayer2\" ";
		flashHTML += "pluginspage=\"http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/\" ";
		flashHTML += "width=" + this.width +" ";
		flashHTML += "height=" + this.height + " ";
		flashHTML += "hspace=\"0\" vspace=\"0\" ";
		flashHTML += "filename=\"" + this.swf + "\" ";
		
		for (var param in this.getParams()) {
			value = this.getParam(param);
			if (value=='true' || value=='false'){
				flashHTML += param + "=" + (value=='true'?'1':'0') +" ";
			} else {
	        	flashHTML += param + "=" + value +" ";
			}
    	}

		
		
		//flashHTML += " autostart=" +  (this.getParam('AutoStart')=='true'?'1':'0');
		//flashHTML += " displaysize=0";
		//flashHTML += " autosize=1";
		//flashHTML += " showcontrols=1";
		//flashHTML += " showstatusbar=1>";
		flashHTML += "</embed></object>";
//		document.getElementById('output').innerHTML=flashHTML;
		return flashHTML
}


FlashObject.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}

FlashObject.prototype.write = function(elementId) {
	if(detectFlash(this.version) || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			
			document.location.replace(this.redirect);
		} else if (this.altTxt) {
			if (elementId) {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}		
}

}

function getFlashVersion() {
	var flashversion = 0;
	if (navigator.plugins && navigator.plugins.length) {
		var x = navigator.plugins["Shockwave Flash"];
		if(x){
			if (x.description) {
				var y = x.description;
	   			flashversion = y.charAt(y.indexOf('.')-1);
			}
		}
	} else {
		result = false;
	    for(var i = 15; i >= 3 && result != true; i--){
   			execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
   			flashversion = i;
   		}
	}
	return flashversion;
}

function detectFlash(ver) {	
	if (getFlashVersion() >= ver) {
		return true;
	} else {
		return false;
	}
}

// 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 "";
	}
}

/* add Array.push if needed */
if(Array.prototype.push == null){
	Array.prototype.push = function(item){
		this[this.length] = item;
		return this.length;
	}
}

if (String.prototype.toAbsoluteUrl == null) {
	String.prototype.toAbsoluteUrl = function() {
		var absoluteUrl = this;
		if (absoluteUrl.indexOf(':') < 0) {
			var path = '' + window.location.href;
			var bases = document.getElementsByTagName('base');
			if (bases.length > 0) {
				path = bases[0].href;
			}
			if (absoluteUrl.indexOf('/') === 0) {
				absoluteUrl = absoluteUrl.substring(1);
				path = path.substring(0, path.indexOf('/', 7));
			}
			if (path.charAt(path.length - 1) != '/') {
				path += '/';
			}
			absoluteUrl = path + absoluteUrl;
		}
		return absoluteUrl;
	};
}

if (typeof FlashVersionDetector == 'undefined') {
//Flash Player Version Detection - Rev 1.6
//Detect Client Browser type
//Copyright(c) 2005-2006 Adobe Macromedia Software, LLC. All rights reserved.
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 FlashVersionDetector = {
getActiveXControlVersion : function()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
},

//JavaScript helper required to detect Flash Player PlugIn version information
getVersion : function(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	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;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// 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;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = FlashVersionDetector.getActiveXControlVersion();
	}	
	return flashVer;
},

isClientHasVersionString : function(versionString) {
	versionArray = versionString.replace(/,/g,'.').split(".");
	if (versionArray.length == 3) {
		return FlashVersionDetector.isClientHasVersion(versionArray[0],versionArray[1],versionArray[2]);
	} else if (versionArray.length == 4) {
		return FlashVersionDetector.isClientHasVersion(versionArray[0],versionArray[1],versionArray[3]);
	} else {
		return false;
	}
},

//When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
isClientHasVersion : function(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = FlashVersionDetector.getVersion();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']


		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

     	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}
}

}
