﻿/**
 * SWFTag & SWFManager
 * @author Isaac Rivera
 * @version 1.6.1.m, April 5, 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
 */
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";
// 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.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);
	},
	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", w);
	},
	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();
		}
	}
};