var already_alerted = false;
var unrendered = new Array();
var page_loaded = false;

var oldOnLoad = window.onload;

var newOnLoad = function(){
				page_loaded = true;
				
				for(var i=0; i < unrendered.length; i++){
					unrendered[i].elm.innerHTML = unrendered[i].html;
				}
			};

if(typeof(oldOnLoad) != "function"){
	window.onload = newOnLoad;
}
else{
	window.onload = function(){ 
		if(oldOnLoad){
			oldOnLoad(); 
		}
		
		newOnLoad();
	} 
}

//this function writes a flash object to a html page
//nm = object name
//l = swf physical location
//n = flash object name
//w = object width
//h = object height
//v = min flash player version required
function SwfWriter(nm, l, n, w, h, v){	
	//add requied version
	this.version = v;
	this.obj_name = nm;
	this.flash_name = n;
	this.location = l;
	this.width = w;
	this.height = h;
	this.extra_params = arguments[6];
	this.wait_for_load = false;
	
	//create the object parameter holder objects
	this.obj_atr = new Object();
	this.params = new Object();
	this.emb_atr = new Object();
	
	//error message
	this.error = 
		'Our site requires flash player be installed to work properly.<br/>'
		+ 'Please click the button below and install flash as well as restarting the browser once install is complete.<br/>'
		+ '<a href="http://www.adobe.com/go/getflash" target="_blank">Get Flash</a>';
	
	this.init = function(){
		this.add("align", "middle");
		this.add("height", this.height);
		this.add("id", this.flash_name);
		this.add("width", this.width);
		this.add("classid", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000");
		this.add("codebase", "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0");
		this.add("allowScriptAccess", "sameDomain");
		this.add("movie", this.location);
		this.add("quality", "high");
		this.add("src", this.location);
		this.add("name", this.flash_name);
		this.add("type", "application/x-shockwave-flash");
		this.add("pluginspage", "http://www.macromedia.com/go/getflashplayer");
		
		if(this.extra_params && this.extra_params.elm){
			this.elm = document.getElementById(this.extra_params.elm);
		}
		else{
			document.write('<div id="' + this.flash_name + '_holder" style="width:' + this.width + 'px; height:' + this.height + 'px; overflow:hidden; visibility:visible;" align="center"></div>');
			
			this.elm = document.getElementById(this.flash_name + "_holder");
		}
	}
	
	this.add = function(n, v){
		switch(n.toLowerCase()){
			case "error":
				this.error = v;
				break;
			case "classid":
			case "codebase":
			case "onblur":
			case "onclick":
			case "ondblClick":
			case "ondrag":
			case "ondragend":
			case "ondragenter":
			case "ondragleave":
			case "ondragover":
			case "ondrop":
			case "onfinish":
			case "onfocus":
			case "onmousedown":
			case "onmouseup":
			case "onmouseover":
			case "onmousemove":
			case "onmouseout":
			case "onkeypress":
			case "onkeydown":
			case "onkeyup":
			case "onload":
			case "onpropertychange":
			case "onreadystatechange":
			case "onstart":
			case "onscroll":
			case "onactivate":
			case "onbeforedeactivate":
			case "ondeactivate":
				this.obj_atr[n] = v;
				break;
			case "bgcolor":
			case "movie":
			case "scale":
			case "wmode":
			case "flashvars":
			case "allowscriptaccess":
				this.params[n] = v;
				break;
			case "pluginspage":
			case "src":
			case "type":
			case "name":
				this.emb_atr[n] = v;
	        	break;
			case "width":
			case "height":
			case "align":
			case "vspace": 
			case "hspace":
			case "class":
			case "title":
			case "type":
			case "accesskey":
			case "tabindex":
			case "id":
				this.obj_atr[n] = v;
				this.emb_atr[n] = v;
				break;
			default:
				this.emb_atr[n] = v;
				this.params[n] = v;
		}
	}
	
	this.isGood = function(req, cur){
		try{
			for(var i=0; i < cur.length; i++){
				if(cur[i] > req[i]){
					return true;
				}
				else if(cur[i] < req[i]){
					return false;
				}
			}
			
			return true;
		}
		catch(e){
			return false;
		}
	}
	
	this.playerIsCurrent = function(){
		this.isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
		this.isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
		//var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
		
		// IE flash detection.
		if(this.isIE){
			try{
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				v = axo.GetVariable("$version");
				temp_ary = v.split(" ");// ["WIN", "9,0,16,0"]
				temp_strn = temp_ary[1];// "9,0,16,0"
				v_ary = temp_strn.split(",");	// ['9', '0', '16', '0']
				
				return this.isGood(this.version, v_ary);
			}
			catch(e){
				if(this.isWin){
					var o_put = 
						'<SCR' + 'IPT LANGUAGE="VBScript">' +
						'\n on error resume next' +
						'\n If (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.' + this.version[0] + '"))) Then' +
						'\n ' + this.elm.id + '.InnerHTML = "Flash Is Good!"' +
						'\n End If' +
						'\n </SCR' + 'IPT>';
					
					document.write(o_put);
				}
				
				return false;
			}
		}
		else{
			if(navigator.plugins && 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];
					
					if( descArray[3] != "" ){
						tempArrayMinor = descArray[3].split("r");
					}
					else{
						tempArrayMinor = descArray[4].split("r");
					}
					var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
					
					return this.isGood(this.version, new Array(versionMajor, versionMinor, versionRevision));
				}
			}
		}
		
		//incase flash isn't recognized
		return false;
	}
	
	this.addAttributes = function(obj){
			var temp = "";
			
			for(var key in obj){
				temp += key + '="' + obj[key] + '" ';
			}
			
			return temp;
	}
	
	this.make = function(){		
		if(this.playerIsCurrent() || this.elm.innerHTML == "Flash Is Good!"){
			//make embed
			var emb = document.createElement("embed");
			
			//add params as embed attributes
			for(var key in this.params){
				emb.setAttribute(key, this.params[key]);
			}
			
			//add embed attributes
			for(var key in this.emb_atr){
				emb.setAttribute(key, this.emb_atr[key]);
			}
			
			if(this.isIE){
				//make object tab
				this.html = '<object ' + this.addAttributes(this.obj_atr) + '>';
		
				//make object params
				for(var key in this.params){
					this.html += '<param name="' + key + '" value="' + this.params[key] + '" />';
				}
		
				//close object tag
				this.html += '</object>';
								
				if(this.wait_for_load && !page_loaded){
					unrendered.push(this);
				}
				else{
					this.elm.innerHTML = this.html;
				}
			}
			else{
				if(this.wait_for_load && !page_loaded){
					var div = document.createElement("div");
					div.appendChild(emb);
					
					this.html = div.innerHTML;
					
					unrendered.push(this);
				}
				else{
					this.elm.innerHTML = "";
				
					this.elm.appendChild(emb);
				}
			}
		}
		else{			
			this.elm.innerHTML = this.error;
			
			this.flash = false;
			
			if(!already_alerted){
				already_alerted = true;
				
				alert_result = confirm("Our site requires flash player 9 or higher be installed to work properly.\nClick OK to download flash and take full advantage of all our site has to offer.");
			
				if(alert_result){
					open("http://www.adobe.com/go/getflash");
				}
			}
		}
	}
	
	this.doubleCheck = function(){
		if(!this.flash && this.isGood(this.version, arguments)){
			if(this.error){
				this.error.innerHTML = "";
			}
			
			document[this.obj_atr.id].width = this.width;
			document[this.obj_atr.id].height = this.height;
		}
	}
}