/**
 * SWFTag
 * @author Isaac Rivera & Jeff Clarke
 * @version 1, November 13, 2007
 *
 * @param id - a document wide unique identifier string
 * @param src - the path to the swf file
 * @param width - the width of the application either in pixels or as a
 					percentage of the html container's width
 * @param height - the height of the application either in pixels or as a
 					percentage of the html container's height
 * @param version - a string representing the swf version being used
 *
 * includes detect + express install.  Adds a name attribute to embed tag.
 */
var SWFTag = function(id, src, width, height, version) {
	if (arguments.length < 5) {
		throw new Error('RequiredArgumetError: new SWFTag(id, src, width, height, version) all arguments are required.');
	}
	for (var i = 0; i < arguments.length; ++i) {
		if (arguments[i] == null) {
			throw new Error('RequiredArgumetError: new SWFTag(id, src, width, height, version) no argument can be null.');
		}
	}
	this.version = (version || SWFTag.V8);
	// attributes container:
	this.attributes = { id:id, name:id, width:width, height:height, align:SWFTag.DEFAULT, src:src };
	// params container:
	this.params = new Object();
	this.setParam("movie", src);
	this.flashvars = null;
	this.useObjectTag = navigator.appName.indexOf("Microsoft") > -1;
	this.alternateContent = '<table style="background-color:#ededef; height: 100%; width: 100%; padding: 15px; "><tr><td style="vertical-align: middle; "><div style="text-align: center; font-family: arial; color:#000000; font-size:12px; font-weight:bold;">Flash Plug-in Required</div>';
      	this.alternateContent += '<div style="text-align: center; font-family: arial; color:#666666; font-size:11px; padding-right:7px; font-weight:normal">The latest Flash Player is required to view this site. Start the Flash Express Install by clicking the link below.</div>';
      	this.alternateContent += '<div style="width: 47px; margin-left: auto; margin-right: auto;"><a href="http://www.adobe.com/products/flashplayer/" target="_blank"><img style="border: 0px; margin: 5px;" src="http://www.aolcdn.com/_media/channels/flash_logo.gif"/></a></div>';
      	this.alternateContent += '<div style="margin-left: auto; margin-right: auto; text-align: center;"><a style="font-family:Arial; color:#2b65b0; font-size:12px; text-decoration:none; font-weight:normal" href="http://www.adobe.com/products/flashplayer/" target="_blank">Install Adobe Flash Player</a></div></td></tr></table>';

	this.expressInstallSrc = SWFTag.EXPRESSINSTALLSRC;
	this.MMPlayerType = "PlugIn";
	this.MMredirectURL = window.location;
	this.MMdoctitle = window.document.title;
};
// Browser constants:
SWFTag.isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
SWFTag.isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
SWFTag.isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
// Boolean constants:
SWFTag.TRUE = "true";
SWFTag.FALSE = "false";
// Shockwave constants:
SWFTag.CLASSID = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
SWFTag.CODEBASE = "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=";
SWFTag.PLUGINSPACE = "http://www.macromedia.com/go/getflashplayer";
SWFTag.TYPE = "application/x-shockwave-flash";
// Script access constants:
SWFTag.SAMEDOMAIN = "samedomain";
SWFTag.ALWAYS = "always";
SWFTag.NEVER = "never";
// Quality constants:
SWFTag.LOW = "low";
SWFTag.MEDIUM = "medium";
SWFTag.HIGH = "high";
SWFTag.AUTOLOW = "autolow";
SWFTag.AUTOHIGH = "autohigh";
SWFTag.BEST = "best";
// Window mode constants:
SWFTag.WINDOW = "window";
SWFTag.OPAQUE = "opaque";
SWFTag.TRANSPARENT = "transparent";
// Scale constants:
SWFTag.SHOWALL = "showall";
SWFTag.NOBORDER = "noborder";
SWFTag.EXACTFIT = "exactfit";
// Networking constants:
SWFTag.ALL = "all";
SWFTag.INTERNAL = "internal";
SWFTag.NONE = "none";
// Alignment constants:
SWFTag.DEFAULT = "middle";
SWFTag.L = "L";
SWFTag.R = "R";
SWFTag.T = "T";
SWFTag.B = "B";
SWFTag.TL = "TL";
SWFTag.TR = "TR";
SWFTag.BL = "BL";
SWFTag.BR = "BR";
// Version constants:
SWFTag.VEXPRESSINSTALL = "6,0,65,0";
SWFTag.V5 = "5,0,0,0";
SWFTag.V6 = "6,0,0,0";
SWFTag.V7 = "7,0,0,0";
SWFTag.V8 = "8,0,0,0";
SWFTag.V9 = "9,0,0,0";
// Default ExpressInstall SRC
SWFTag.EXPRESSINSTALLSRC = "http://cdn.channel.aol.com/_media/channels/expressinstall.swf";
SWFTag.EXPRESSINSTALLMINWIDTH = 220;
SWFTag.EXPRESSINSTALLMINHEIGHT = 145;
SWFTag.createAttribute = function(name, value) {
	if(name != null && typeof(name) == "string" && name.length > 0) {
		if(value != null && typeof(value) == "string" && value.length > 0) {
			var attr = document.createAttribute(name);
			attr.nodeValue = value;
			return attr;
		}
	}
	return null;
};
SWFTag.createParamNode = function(name, value) {
	var p = null;
	if(name != null && name.length > 0 && value != null && value.length > 0) {
		p = document.createElement("param");
		var n = document.createAttribute("name");
		n.nodeValue = name;
		p.setAttributeNode(n);
		var v = document.createAttribute("value");
		v.nodeValue = value;
		p.setAttributeNode(v);
		return p;
	}
	return p;
};
SWFTag.prototype = {
	setAttribute:function(name, value) {
		if(name != null) {
			this.attributes[name] = value;
		}
	},
	setParam:function(name, value) {
		if(name != null) {
			this.params[name] = value;
		}
	},
	setAllowScriptAccess:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.SAMEDOMAIN && value != SWFTag.ALWAYS
													&& value != SWFTag.NEVER) {
			throw new Error('IllegalArgumentValue: allowscriptaccess must be ( samedomain | always | never ).');
		}
		this.setParam("allowScriptAccess", value);
	},
	setAllowFullscreen:function(value){
		value = value.toLowerCase();
		if(value != SWFTag.TRUE && value != SWFTag.FALSE) {
			throw new Error('IllegalArgumentValue: allowFullScreen must be ( true | false ).');
		}
		this.setParam("allowFullScreen", value);
	},
	setAllowNetworking:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.ALL && value != SWFTag.INTERNAL
													&& value != SWFTag.NONE) {
			throw new Error('IllegalArgumentValue: allownetworking must be ( all | internal | none ).');
		}
		this.setParam("allowNetworking", value);
	},
	setBgColor:function(value) {
		if(value.length == 6 && value.charAt(0) != '#') {
       		value = '#' + value;
    	}
		if(value.length != 7) {
			throw new Error('IllegalArgumentValue: bgcolor must be a hex string in the format ( #XXXXXX | XXXXXX ).');
		}
		this.setParam("bgcolor", value);
	},
	setQuality:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.LOW && value != SWFTag.MEDIUM && value != SWFTag.HIGH
						&& value != SWFTag.AUTOLOW && value != SWFTag.AUTOHIGH
						&& value != SWFTag.BEST) {
			throw new Error('IllegalArgumentValue: quality must be ( low | medium | high | autolow | autohigh | best ).');
		}
		this.setParam("quality", value);
	},
	setWmode:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.WINDOW && value != SWFTag.OPAQUE
											&& value != SWFTag.TRANSPARENT) {
			throw new Error('IllegalArgumentValue: wmode must be ( window | opaque | transparent ).');
		}
		this.setParam("wmode", value);
	},
	setMenu:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.TRUE && value != SWFTag.FALSE) {
			throw new Error('IllegalArgumentValue: menu must be ( true | false ).');
		}
		this.setParam("menu", value);
	},
	setPlay:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.TRUE && value != SWFTag.FALSE) {
			throw new Error('IllegalArgumentValue: play must ( true | false ).');
		}
		this.setParam("play", value);
	},
	setLoop:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.TRUE && value != SWFTag.FALSE) {
			throw new Error('IllegalArgumentValue: loop must be ( true | false ).');
		}
		this.setParam("loop", value);
	},
	setScale:function(value) {
		value = value.toLowerCase();
		if(value != SWFTag.SHOWALL && value != SWFTag.NOBORDER
												&& value != SWFTag.EXACTFIT) {
			throw new Error('IllegalArgumentValue: scale must be ( showall | noborder | exactfit ).');
		}
		this.setParam("scale", value);
	},
	addFlashVar:function(name, value) {
		if(name != null) {
			if(this.flashvars == null) {
				this.flashvars = new Object();
			}
			this.flashvars[name] = escape(value);
		}
	},
	addFlashVars:function(value) {
		if(value != null) {
			if(this.flashvars == null) {
				this.flashvars = new Object();
			}
			var fvs = value.split("&");
			if(fvs.length) {
				for(var i = 0; i < fvs.length; i++) {
					var fv = fvs[i].split("=");
					if(fv.length == 2) {
						this.flashvars[fv[0]] = fv[1];
					}
				}
			}
		}
	},
	setId:function(id) {
		if(id != null && id.length > 0) this.setAttribute("id", id);
	},
	getId:function() {
		return this.attributes.id;
	},
	setAlign:function(a) {
		if(a == SWFTag.DEFAULT || a == SWFTag.L || a == SWFTag.R
					|| a == SWFTag.T || a == SWFTag.B) this.setAttribute("align", a);
	},
	setSAlign:function(a) {
		if(a == SWFTag.DEFAULT || a == SWFTag.L || a == SWFTag.R
					|| a == SWFTag.T || a == SWFTag.B || a == SWFTag.TL
					|| a == SWFTag.TR || a == SWFTag.BL || a == SWFTag.BR) {
			this.setAttribute("salign", a);
		}
	},
	setWidth:function(w) {
		if(w != null) this.setAttribute("width", a);
	},
	setHeight:function(h) {
		if(h != null && h.length > 0) this.setAttribute("height", h);
	},
	setSrc:function(src) {
		if(src != null && src.length > 0) this.setAttribute("src", src);
	},
	setVersion:function(v) {
		if(v != null && v.length > 0) this.version = version;
	},
	setAlternateContent:function(content) {
		if(content != null && content.length > 0) this.alternateContent = content;
	},
	setExpressInstallSrc:function(src) {
		if(src != null && src.length > 0) this.expressInstallSrc = src;
	},
	collectFlashVars:function() {
		var value = "";
		for(var i in this.flashvars) {
			value += (i + "=" + this.flashvars[i] + "&");
		}
		return value.substring(0, value.length - 1);
	},
	getEmbedString:function() {
		var value = '<embed';
		var data = this.attributes.data;
		this.attributes.data = null;
		for(var a in this.attributes) {
			if(this.attributes[a] != null && this.attributes[a].length) {
				value += (' ' + a + '="' + this.attributes[a] + '"');
			}
		}
		this.attributes.data = data
		var movie = this.params.movie;
		this.params.movie = null;
		for (var p in this.params) {
			if (this.params[p] != null && this.params[p].length) {
				value += (' '+ p + '="' + this.params[p] + '"');
			}
		}
		this.params.movie = movie;
		if (this.flashvars != null) {
			var fv = this.collectFlashVars();
			if (fv.length > 0) {
				value += ' flashvars="' + fv + '"';
			}
		}
		value += ' version="' + this.version + '"';
		value += ' pluginspage="' + SWFTag.PLUGINSPACE + '">';
		value += '</embed>';
		return value;
	},
	getObjectString:function() {
		var value = '<object classid="' + SWFTag.CLASSID + '" ';
		value += 'codebase="' + SWFTag.CODEBASE + this.version + '" ';
		var src = this.attributes.src;
		this.attributes.src = null;
		for (var a in this.attributes) {
			if (this.attributes[a] != null && this.attributes[a].length) {
				value += (' '+ a + '="' + this.attributes[a] + '"');
			}
		}
		this.attributes.src = src;
		value += '>';
		for (var p in this.params) {
			if (this.params[p] && this.params[p].length) {
				value += '<param name="' + p + '" value="' + this.params[p]+'"/>';
			}
		}
		if (this.flashvars != null) {
			var fv = this.collectFlashVars();
			if (fv.length > 0) {
				value += '<param name="flashvars" value="' + fv + '"/>';
			}
		}
		value += '</object>';
		return value;
	},
	toEmbedString:function() {
		var value = (this.hasPlugin()) ? this.getEmbedString() : this.alternateContent;
		return value;
	},
	toObjectString:function() {
		var value = this.getObjectString(); //(this.hasPlugin()) ? this.getObjectString() : this.alternateContent;
		return value;
	},
	checkSize:function(){
		return (parseInt(this.attributes.width) > SWFTag.EXPRESSINSTALLMINWIDTH) && (parseInt(this.attributes.height) > SWFTag.EXPRESSINSTALLMINHEIGHT);
	},
	hasPlugin:function() {
		var bool = false;
		var minVersion = SWFTag.VEXPRESSINSTALL.split(",");
		var reqVersion = this.version.split(",");
		var hasMinVersion = this.detectFlashVer(minVersion[0], minVersion[1], minVersion[2]);
		var hasReqVersion = this.detectFlashVer(reqVersion[0], reqVersion[1], reqVersion[2]);
		//alert(hasMinVersion+" : "+minVersion[0]+ " : "+minVersion[1]+" : "+ minVersion[2]);
		if(hasMinVersion){
			if(!hasReqVersion){
				this.version = SWFTag.VEXPRESSINSTALL;
				this.attributes.src = this.expressInstallSrc;
				this.params.movie = this.expressInstallSrc;
				if(this.useObjectTag) this.MMPlayerType = "ActiveX";
				this.addFlashVar("MMPlayerType", this.MMPlayerType);
				this.addFlashVar("MMredirectURL", this.MMredirectURL);
				this.addFlashVar("MMdoctitle", this.MMdoctitle);
			}
			bool = true;
			if (!hasReqVersion && !this.checkSize()){
				bool = false;
			}
		}
		return bool;
	},
	fillElementId:function(id) {
		var e = null;
		try {
			e = window.document.getElementById(id);
			e.innerHTML = this.toString();
		} catch(err){}
		return e;
	},
	write:function(doc) {
    	doc.write(this.toString());
	},
	toString:function() {
		if(this.useObjectTag) {
			return this.toObjectString();
		} else {
			return this.toEmbedString();
		}
	},
	
	// Code repurposed from:
	// Flash Player Version Detection - Rev 1.5
	// Detect Client Browser type
	// Copyright(c) 2005-2006 Adobe Macromedia Software, LLC. All rights reserved.
	
	controlVersion: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;
	},
	
	getSwfVer: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];
				if ( descArray[3] != "" ) {
					tempArrayMinor = descArray[3].split("r");
				} else {
					tempArrayMinor = descArray[4].split("r");
				}
				var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
				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 ( this.isIE && this.isWin && !this.isOpera ) {
			flashVer = this.controlVersion();
		}
		return flashVer;
	},
	
	detectFlashVer:function(reqMajorVer, reqMinorVer, reqRevision) {
		var versionStr = this.getSwfVer();
		if (versionStr == -1 ) {
			return false;
		} else if (versionStr != 0) {
			if(this.isIE && this.isWin && !this.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;
		}
	}
};

/* FLASH Content Exposure:
 */
function hideFlashSEO(id, _w, _h){
 	var _e = document.getElementById(id);
 	if ( _e != null ) { 
 		_e.style.width = _w+'px'; 
 		_e.style.height = _h+'px'; 
 		_e.style.overflow = 'hidden';
 	}
 }
