function addFOLoadEvent(func) {
  var foOldOnLoad = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      foOldOnLoad();
      func();
    }
  }
}

if(typeof DF == "undefined") var DF = new Object();
if(typeof DF.FObjectUtil == "undefined") DF.FObjectUtil = new Object();

//Optional params: idToWrite, will override automatic ID gen'd on write
DF.FObject = function(swfPath, w, h, ver, play, loop, idToWrite){
	if (!document.createElement || !document.getElementById) { return; }
	this.params = new Object();
	this.variables = new Object();
	this.attributes = new Array();
	if(swfPath) { this.setAttribute('swfPath', swfPath); }
	
	if(w) { this.setAttribute('width', w); }
	if(h) { this.setAttribute('height', h); }
	if(ver) { this.setAttribute('version', new DF.PlayerVersion(ver.toString().split("."))); }
	this.installedVer = DF.FObjectUtil.getPlayerVersion(this.getAttribute('version'));
	
	var id = idToWrite ? idToWrite : '';
	this.setAttribute('idToWrite', id);
	
	var isPlayOnLoad = (play) ? play.toString().toLowerCase() : 'true';
	var isLoop = (loop) ? loop.toString().toLowerCase() : 'false';

	this.addVariable('relativepath', swfPath.substr(0,swfPath.lastIndexOf('/')+1) );
	
	this.addParam('play', isPlayOnLoad);
	this.addParam('loop', isLoop);
	this.addParam('quality', 'high');
	this.addParam('bgcolor', '#ffffff');
	this.addParam('wmode', 'opaque');
}

DF.FObject.prototype = {
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	addVariable: function(name, value){
		this.variables[name] = value;
	},
	getVariable: function(name){
		return this.variables[name];
	},
	getVariables: function(){
		return this.variables;
	},
	getVariablePairs: function(){
		var variablePairs = new Array();
		var key;
		var variables = this.getVariables();
		for(key in variables){
			variablePairs.push(key +"="+ variables[key]);
		}
		return variablePairs;
	},
	getSWFHTML: function() {
		var swfNode = "";
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
			swfNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swfPath') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"';
			swfNode += ' id="'+ this.getAttribute('idToWrite') +'" name="'+ this.getAttribute('idToWrite') +'" ';
			var params = this.getParams();
			 for(var key in params){ swfNode += [key] +'="'+ params[key] +'" '; }
			var pairs = this.getVariablePairs().join("&");
			 if (pairs.length > 0){ swfNode += 'flashvars="'+ pairs +'"'; }
			swfNode += '/>';
		} else { // PC IE
			swfNode = '<object id="'+ this.getAttribute('idToWrite') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
			swfNode += '<param name="movie" value="'+ this.getAttribute('swfPath') +'" />';
			var params = this.getParams();
			for(var key in params) {
			 swfNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
			}
			var pairs = this.getVariablePairs().join("&");
			if(pairs.length > 0) {swfNode += '<param name="flashvars" value="'+ pairs +'" />';}
			swfNode += "</object>";
		}
		return swfNode;
	},
	//promptForUpgrade is optional bool
	write: function(elementId, skipUpgradePrompt, backupHtml){
	
		if(!this.getAttribute('idToWrite')){
			this.setAttribute('idToWrite', 'FObject:'+elementId); //if no special id for inserted node specified, prepend parent's id
		}
		var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
		if(this.installedVer.versionIsValid(this.getAttribute('version'))){
			
			n.innerHTML = this.getSWFHTML();
			return true;
		}else{
			if (skipUpgradePrompt && (typeof backupHtml == 'string')){
				n.innerHTML = backupHtml;
				return true;
			}
			else if (!skipUpgradePrompt){ this.showUpgradeMsg(); }
		}
		return false;
	},
	showUpgradeMsg: function(){
		var win = window;
		var uidName = DF.FlashMsgId;
        var nwin = win.open("", uidName, "dependent,resizable,height=115,width=250");
		if (nwin != null && typeof nwin != "undefined" && nwin) {
			nwin.document.write(
			   '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
			   + '"http://www.w3.org/TR/html4/loose.dtd">'
			   + '<html><head><title>'
			   + uidName
			   + '</title></head>'
			   + '<body>'
			   + '<br/><br/><a href="http://www.macromedia.com/go/getflash/" onclick="window.open(\'http://www.macromedia.com/go/getflash/\',\'\',\'\');return false;">Get the latest Flash plug-in here</a>'
			   +'</body></html>'
        	);
        	nwin.document.close();
			return;	
		}
        
		//pop-up blocked
		addFOLoadEvent(DF.WriteGetFlashDiv);
	}
}

DF.FObjectUtil.getPlayerVersion = function(reqVer){
	var PlayerVersion = new DF.PlayerVersion([0,0,0]);
	if(navigator.plugins && navigator.mimeTypes.length){
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			PlayerVersion = new DF.PlayerVersion(x.description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
	}else{
		try{
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			for (var i=3; axo!=null; i++) {
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i);
				PlayerVersion = new DF.PlayerVersion([i,0,0]);
			}
		}catch(e){}
		if (reqVer && PlayerVersion.major > reqVer.major) return PlayerVersion; // version is ok, skip minor detection
		if (!reqVer || ((reqVer.minor != 0 || reqVer.rev != 0) && PlayerVersion.major == reqVer.major) || PlayerVersion.major != 6) {
			try{
				PlayerVersion = new DF.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
			}catch(e){}
		}
	}
	return PlayerVersion;
}
DF.PlayerVersion = function(arrVersion){
	this.major = parseInt(arrVersion[0]) != null ? parseInt(arrVersion[0]) : 0;
	this.minor = parseInt(arrVersion[1]) || 0;
	this.rev = parseInt(arrVersion[2]) || 0;
}
DF.PlayerVersion.prototype.versionIsValid = function(fv){
	if(this.major < fv.major) return false;
	if(this.major > fv.major) return true;
	if(this.minor < fv.minor) return false;
	if(this.minor > fv.minor) return true;
	if(this.rev < fv.rev) return false;
	return true;
}
DF.WriteGetFlashDiv = function(){
    var win = window;
    var doc = win.document;
	this.doc = doc;
	
	var msgDiv = doc.getElementById(DF.FlashMsgId);
	var existing_msgDiv = !!msgDiv;
	if (existing_msgDiv){ //already exists
		msgDiv.style.display = "inline";
	}
	else{
		msgDiv = doc.createElement("div");
		msgDiv.id = DF.FlashMsgId;
		msgDiv.style.display = "inline";
		msgDiv.innerHTML = '<a href="http://www.macromedia.com/go/getflash/" onclick="window.open(\'http://www.macromedia.com/go/getflash/\',\'\',\'\');return false;">Get the latest Flash plug-in here</a>';
		doc.body.appendChild(msgDiv);
	}
}
DF.FlashMsgId = "Flash_Upgrade";

/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

