/**
 * SWFTag
 * @author Isaac Rivera
 * @version 1.0
 */

var SWFTag = function(id, src, width, height, version) {	
	if (arguments.length < 5) {
		throw new Error('RequiredArgumetError: You must pass in an id, src, width, height, and version when creating a FLash object.');
	}

	for (var i = 0; i < arguments.length; ++i) {
		if (arguments[i] == undefined || arguments[i] == null) {
			throw new Error('RequiredArgumetError: All constructor arguments must have values: ' + i);
		}
	}
	
	// required:
	this.id = id;
	this.src = src;
	this.width = width;
	this.height = height;
	// defaults:
	this.classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
	this.codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=";
	this.version = (version || "7,0,0,0");
	this.align = "middle";
	// params container:
	this.params = new Object();
	this.flashvars = null;
	this.useObjectTag = (navigator.appName.indexOf ("Microsoft") != -1) ? true : false;
};

SWFTag.prototype = {

	setParam:function(name, value) {
		this.params[name] = value;
	},
	
	setAllowScriptAccess:function(value) {
		value = value.toLowerCase();
		if(value != "samedomain" && value != "always" && value != "never") {
			throw new Error('IllegalArgumentValue: allowscriptaccess can only be "samedomain", "always", or "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 7 or 6 character hex string in the format "#XXXXXX or XXXXXX."');
    }
		this.setParam("bgcolor", value);
	},

	setQuality:function(value) {
		value = value.toLowerCase();
		if(value != "low" && value != "medium" && value != "high" 
							&& value != "autolow" && value != "autohigh" && value != "best") {
			throw new Error('IllegalArgumentValue: quality can only be "low", "medium", "high", "autolow", "autohigh", or "best".');
		}
		this.setParam("quality", value);
	},

	setWmode:function(value) {
		value = value.toLowerCase();
		if(value != "window" && value != "opaque" && value != "transparent") {
			throw new Error('IllegalArgumentValue: wmode can only be "window", "opaque", or "transparent".');
		}
		this.setParam("wmode", value);
	},

	setMenu:function(value) {
		value = value.toLowerCase();
		if(value != "true" && value != "false") {
			throw new Error('IllegalArgumentValue: menu can only be "true", or "false".');
		}
		this.setParam("menu", value);
	},

	setPlay:function(value) {
		value = value.toLowerCase();
		if(value != "true" && value != "false") {
			throw new Error('IllegalArgumentValue: play can only be "true", or "false".');
		}
		this.setParam("play", value);
	},

	setLoop:function(value) {
		value = value.toLowerCase();
		if(value != "true" && value != "false") {
			throw new Error('IllegalArgumentValue: loop can only be "true", or "false".');
		}
		this.setParam("loop", value);
	},

	setScale:function(value) {
		value = value.toLowerCase();
		if(value != "showall" && value != "noborder" && value != "exactfit") {
			throw new Error('IllegalArgumentValue: scale can only be "showall", "noborder",  or "exactfit".');
		}
		this.setParam("scale", value);
	},

	addFlashVar:function(name, value) {
		if(this.flashvars == null) {
			this.flashvars = new Object();
		}
		this.flashvars[name] = escape(value);
	},
	
	addFlashVars:function(value) {
		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];
				}
			}
		}
	},
	
	objectToFlashVars:function(obj) {
		if(this.flashvars == null) {
			this.flashvars = new Object();
		}
		if(obj != null) {
			for(var prop in obj) {
				this.flashvars[prop] = obj[prop];
			}
		}
	},
	
	setId:function(id) {
		this.id = id;
	},
	
	setAlign:function(a) {
		this.align = a;
	},
	
	setWidth:function(w) {
		this.width = a;
	},
	
	setHeight:function(h) {
		this.height = h;
	},
	
	setSrc:function(src) {
		this.src = src;
	},
	
	setVersion:function(v) {
		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);
	},
	
	toString:function() {
		if(this.useObjectTag) {
			return this.toObjectTag();
		} else {
			return this.toEmbedTag();
		}
	},
	
	write:function(doc) {
    	doc.write(this.toString());
	},
	
	toEmbedTag:function() {
		var value = '<embed src="' + this.src + '"';
		value += ' width="' + this.width + '"';
		value += ' height="' + this.height + '"';
		value += ' type="application/x-shockwave-flash"';
		if (this.id != null) {
				value += ' id="' + this.id + '"';
		}
		for (var p in this.params) {
				if (this.params[p] && this.params[p].length) {
						value += (' '+ p + '="' + this.params[p] + '"');
				}
		}
		if (this.flashvars != null) {
				var fv = this.collectFlashVars();
				if (fv.length > 0) {
						value += ' flashvars="' + fv + '"';
				}
		}
		value += ' pluginspage="http://www.macromedia.com/go/getflashplayer">';
		value += '</embed>';
		
		return value;
	},
	
	toObjectTag:function() {
		var value = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
		if (this.id != null) {
				value += 'id="' + this.id + '" ';
		}
		value += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + this.version + '" ';
		value += 'width="' + this.width + '" ';
		value += 'height="' + this.height + '" ';
		value += 'align="' + this.align + '">';
		value += '<param name="movie" value="' + this.src + '"/>';

		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 = document.getElementById(id);
		if(e) {
			e.innerHTML = this.toString();
		}
	},
	
	fillElement:function(e) {
		if(e) {
			e.innerHTML = this.toString();
		}
	},
	
	appendToElementId:function(id) {
		var e = document.getElementById(id);
		if(e) {
			e.innerHTML += this.toString();
		}
	},
	
	appendToElement:function(e) {
		if(e) {
			e.innerHTML += this.toString();
		}
	}
};





