﻿/**
 * SWFTag & SWFManager
 * @author Isaac Rivera & Justin Akin
 * @version 1.6.7, June 14, 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
 *
 * note: v 1.6.7 includes detect + express install
 */
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, width:width, height:height, align:SWFTag.L, 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; width: 240px; padding: 6.5px; "><tr><td style="vertical-align: middle; "><div style="text-align: center; font-family: arial; color:#000000; font-size:12px; font-weight:bold;">Install Adobe Flash Player</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/ch_sports/flash_logo.gif"/></a></div></td></tr></table>';
     
	this.expressInstallSrc = SWFTag.EXPRESSINSTALLSRC;
	this.MMPlayerType = "PlugIn";
	this.MMredirectURL = window.location;
	this.MMdoctitle = window.document.title;
};
// 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.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);
	},
	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;
		return value;
	},
	toObjectString:function() {
		var value = (this.hasPlugin()) ? this.getObjectString() : this.alternateContent;
		return value;
	},
	hasPlugin:function() {
		var bool = false;
		var minVersion = SWFTag.VEXPRESSINSTALL.split(",");
		var reqVersion = this.version.split(",");
		var hasMinVersion = DetectFlashVer(minVersion[0], minVersion[1], minVersion[2]);
		var hasReqVersion = 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;
		}
		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();
		}
	}
};

// Flash Player Version Detection - Rev 1.5
// 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;

function ControlVersion() {
	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
function GetSwfVer(){
	// 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 ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) {
	versionStr = GetSwfVer();
	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;
	}
}

function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
    var str = '';
    if (isIE && isWin && !isOpera)
    {
  		str += '<object ';
  		for (var i in objAttrs)
  			str += i + '="' + objAttrs[i] + '" ';
  		for (var i in params)
  			str += '><param name="' + i + '" value="' + params[i] + '" /> ';
  		str += '></object>';
    } else {
  		str += '<embed ';
  		for (var i in embedAttrs)
  			str += i + '="' + embedAttrs[i] + '" ';
  		str += '> </embed>';
    }

    document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
      case "id":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}


