﻿/**
 * SWFTag & SWFManager
 * @author Isaac Rivera
 * @version 1.6, October 11, 2006
 * 
 * @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
 */
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.DEFAULT, src:src };
	// params container:
	this.params = new Object();
	this.setParam("movie", src);
	this.flashvars = null;
	this.useObjectTag = navigator.appName.indexOf("Microsoft") > -1;
};
// 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";
// 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.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";
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);
	},
	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;
	},
	collectFlashVars:function() {
		var value = "";
		for(var i in this.flashvars) {
			value += (i + "=" + this.flashvars[i] + "&");
		}
		return value.substring(0, value.length - 1);
	},
	toEmbedString: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;
	},
	toObjectString: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;
	},
	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();
		}
	}
};
SWFManager = new Object();
SWFManager.SWFS = new Object();
SWFManager.OBSERVERS = new Object();
SWFManager.LAYERS = new Array();
SWFManager.FLASH_CALLBACK = "flashEventReceiverMethodName";
SWFManager.JS_CALLBACK = "jsEventReceiverMethodName";
SWFManager.getSWF = function(id) {
	return SWFManager.SWFS[id];
};
SWFManager.sendEvent = function(evt) {
	for(var id in SWFManager.SWFS) {
		if(id != evt.source) {
			try {
				document.getElementById(id).onJSEvent(evt);
			} catch(err){}
		}
	}
	for(var id in SWFManager.OBSERVERS) {
		try {
			if(id != evt.source) SWFManager.OBSERVERS[id].onEvent(evt);
		} catch(err){}
	}
	SWFManager.onloadTasks();
};
SWFManager.onSWFEvent = function(evt) {
	SWFManager.sendEvent(evt);
};
SWFManager.addObserver = function(id, o) {
	SWFManager.OBSERVERS[id] = o
};
SWFManager.newSWF = function(id, src, width, height, version) {
	var swf;
	try {
		swf = new SWFTag(id, src, width, height, version);
		swf.addFlashVar(SWFManager.FLASH_CALLBACK, "onJSEvent");
		swf.addFlashVar(SWFManager.JS_CALLBACK, "window.SWFManager.onSWFEvent");
		swf.addFlashVar("jsId", id);
		SWFManager.SWFS[id] = swf;
	} catch(err){}
	return swf;
};
SWFManager.fillElementId = function(id, swf) {
	var e = null;
	try {
		e = swf.fillElementId(id);
		SWFManager.LAYERS.push(e);
	} catch(err){}
	SWFManager.checkWindowEvents();
};

SWFManager.originalOnunload = new Function();
SWFManager.originalOnbeforeunload = new Function();
SWFManager.originalOnload = new Function();

SWFManager.onunload = function() {
	//alert("SWFManager.onunload()");
	
	window.__flash_unloadHandler = function(){};
	window.__flash_savedUnloadHandler = function(){};
	window.__flash_setupUnloadHandler = function(){};
	
	try {
		SWFManager.originalOnunload();
	} catch(err){}
	
	var _objects = window.document.getElementsByTagName("OBJECT");
	if(_objects == null || _objects.length == 0) _objects = window.document.getElementsByTagName("object");
	if(_objects == null || _objects.length == 0) _objects = window.document.getElementsByTagName("EMBED");
	if(_objects == null || _objects.length == 0) _objects = window.document.getElementsByTagName("embed");
	if(_objects != null || _objects.length > 0) {
		for(var _obj in _objects) {
			var _parent = _objects[_obj].offsetParent;
			var _thisobj = _objects[_obj];
			if(typeof(_thisobj) != "undefined") {
				if(typeof(_thisobj.innerHTML) != "undefined" && typeof(_thisobj.innerHTML.length) == "number") {
					try {
						_thisobj.innerHTML = "";
						} catch(e){}
				}
				if(typeof(_thisobj.style) != "undefined") _thisobj.style.display = "none";
			}
			if(typeof(_parent) != "undefined") {
				if(typeof(_parent.innerHTML) != "undefined" && typeof(_parent.innerHTML.length) == "number") {
					try {
						_parent.innerHTML = "";
					} catch(e){}
				}
				if(typeof(_parent.style) != "undefined") _parent.style.display = "none";
			}
		}
	}
	delete SWFManager.OBSERVERS;
	delete SWFManager.LAYERS;
	delete SWFManager.SWFS;
	SWFManager.OBSERVERS = {};
	SWFManager.SWFS = {};
	SWFManager.LAYERS = [];
};

SWFManager.onbeforeunload = function() {
	window.__flash_unloadHandler = function(){};
	window.__flash_savedUnloadHandler = function(){};
	window.__flash_setupUnloadHandler = function(){};
	try {
		SWFManager.originalOnbeforeunload();
	} catch(err){}
	if(window.onunload != SWFManager.onunload) {
		SWFManager.originalOnunload = window.onunload;
	}
	window.onunload = function() {
		SWFManager.onunload();
	};
};

SWFManager.onload = function() {
	//alert("SWFManager.onload()");
	window.__flash_unloadHandler = function(){};
	window.__flash_savedUnloadHandler = function(){};
	window.__flash_setupUnloadHandler = function(){};
	try {
		SWFManager.originalOnload();
	} catch(err){}
	SWFManager.onloadTasks();
};

SWFManager.onloadTasks = function() {
	window.__flash_unloadHandler = function(){};
	window.__flash_savedUnloadHandler = function(){};
	window.__flash_setupUnloadHandler = function(){};
	if(window.onbeforeunload != SWFManager.onbeforeunload) {
		SWFManager.originalOnbeforeunload = window.onbeforeunload;
	}
	if(window.onunload != SWFManager.onunload) {
		SWFManager.originalOnunload = window.onunload;
	}
	window.onbeforeunload = SWFManager.onbeforeunload;
	window.onunload = SWFManager.onunload;
};

SWFManager.checkWindowEvents = function() {
	window.__flash_unloadHandler = function(){};
	window.__flash_savedUnloadHandler = function(){};
	window.__flash_setupUnloadHandler = function(){};
	if(window.onload != SWFManager.onload) {
		SWFManager.originalOnload = window.onload;
	}
	window.onload = SWFManager.onload;
};
