/*** constructor *************************************************************/

var FlashMovie = function () {
	
	// Determine Javscript version.
	this.jsVersion = _jsversion ? _jsversion : 1.0;

	// Declare properties.
	this.debug = false;
	this.xhtml = false;
	this.regex = this.jsVersion >= 1.2;
	this.locReplace = this.jsVersion >= 1.1;

	this.redirect = false;
	this.redirectSwf = "";
	this.redirectAlt = "";

	this.player = null;
	this.installedVersion = 0;
	this.targetVersion = 0;

	this.elementId = "";
	this.altContent = "";

	this.parameters = {};
	this.parameters.url = "";
	this.parameters.bgcolor = "#000000";
	this.parameters.wmode = "opaque";
	this.parameters.width = 0;
	this.parameters.height = 0;
	this.parameters.play = true;
	this.parameters.loop = false;
	this.parameters.menu = true;
	this.parameters.quality = "autohigh";
	this.parameters.scale = "showall";
	this.parameters.align = "";
	this.parameters.salign = "";
	this.parameters.allowscriptaccess = "samedomain";
	this.parameters.seamlesstabbing = true;
	this.parameters.devicefont = false;
	this.parameters.flashvars = {};
	this.parameters.pluginspage = "http://www.macromedia.com/go/getflashplayer";
	this.parameters.type = "application/x-shockwave-flash";
	this.parameters.classid = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
	this.parameters.codebase = "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=%VERSION%,0,0,0";
}

var fmp = FlashMovie.prototype;

/*** private methods *********************************************************/

fmp.writeSwf = function () {
	document.write(this.toString());
	return true;
}

fmp.writeAlt = function () {
	document.write(this.altContent);
	return true;
}

fmp.doRedirect = function () {
	var versionOk = this.player ? (this.installedVersion >= this.targetVersion) : true;
	if (versionOk) {
		if (this.redirectSwf.length) {
			if (this.locReplace) {
				window.location.replace(this.redirectSwf);
			} else {
				window.location = this.redirectSwf;
			}
		} else {
			this.writeSwf();
		}
	} else {
		if (this.locReplace) {
			window.location.replace(this.redirectAlt);
		} else {
			window.location = this.redirectAlt;
		}
	}
	return true;
}

fmp.toString = function () {

	// Define template.
	var template = '';
	template += '<object classid="%CI%" codebase="%CB%" width="%SW%" height="%SH%" id="%ID%" align="%AL%">%NL%';
	template += '%TB%<param name="movie" value="%MV%"%TE%%NL%';
	template += '%TB%<param name="bgcolor" value="%BG%"%TE%%NL%';
	template += '%TB%<param name="wmode" value="%WM%"%TE%%NL%';
	template += '%TB%<param name="play" value="%PL%"%TE%%NL%';
	template += '%TB%<param name="loop" value="%LP%"%TE%%NL%';
	template += '%TB%<param name="menu" value="%MN%"%TE%%NL%';
	template += '%TB%<param name="quality" value="%QU%"%TE%%NL%';
	template += '%TB%<param name="scale" value="%SC%"%TE%%NL%';
	template += '%TB%<param name="salign" value="%SA%"%TE%%NL%';
	template += '%TB%<param name="flashvars" value="%FV%"%TE%%NL%';
	template += '%TB%<param name="devicefont" value="%DF%"%TE%%NL%';
	template += '%TB%<param name="allowscriptaccess" value="%AS%"%TE%%NL%';
	template += '%TB%<param name="seamlesstabbing" value="%ST%"%TE%%NL%';
	template += '%TB%<embed src="%MV%" play="%PL%" loop="%LP%" devicefont="%DF%" quality="%QU%" scale="%SC%" salign="%SA%" ';
	template += 'wmode="%WM%" bgcolor="%BG%" width="%SW%" height="%SH%" name="%ID%" flashvars="%FV%" ';
	template += 'align="%AL%" allowscriptaccess="%AS%" type="%TY%" pluginspage="%PP%"></embed>%NL%';
	template += '</object>%NL%';
	
	// Determine URL (if prior to version 6, needs QS).
	var url = this.parameters.url;
	if (this.targetVersion < 6) {
		var fv = this.getFlashVars();
		if (fv.length) {
			url += "?" + fv;
		}
	}
	
	// Determine replacement values.
	var p = this.parameters;
	var r = {
			"%NL%": "\n",
			"%TB%": "\t",
			"%TE%": (this.xhtml ? " />" : ">"),
			"%MV%": url,
			"%ID%": p.elementId,
			"%SW%": p.width,
			"%SH%": p.height,
			"%BG%": p.bgcolor,
			"%QU%": p.quality,
			"%WM%": p.wmode,
			"%SC%": p.scale,
			"%AL%": p.align,
			"%SA%": p.salign,
			"%PL%": p.play,
			"%LP%": p.loop,
			"%MN%": p.menu,
			"%DF%": p.devicefont,
			"%AS%": p.allowscriptaccess,
			"%ST%": p.seamlesstabbing,
			"%FV%": this.getFlashVars(),
			"%TY%": p.type,
			"%PP%": p.pluginspage,
			"%CB%": p.codebase,
			"%CI%": p.classid
			};

	// Do replacements.
	for (var token in r) {
		if (this.regex) {
			var pattern = new RegExp(token, "gi");
			template = template.replace(pattern, r[token]);
		} else {
			template = template.split(token).join(r[token]);
		}
	}

	return template;
}


/*** public methods **********************************************************/

fmp.execute = function () {
	if (this.redirect) {
		this.doRedirect();
		return;
	}
	var versionOk = (this.installedVersion >= this.targetVersion);
	if (versionOk) {
		this.writeSwf();
	} else {
		this.writeAlt();
	}
}

fmp.setRedirect = function (swfUrl, altUrl) {
	// Instructs the object to redirect to different URLs
	// based on the flash player version. If an empty string
	// is passed in the 'swfUrl' argument, redirection will
	// only take place for alternate content.
	this.redirect = true;
	this.redirectSwf = swfUrl;
	this.redirectAlt = altUrl;
}

fmp.setAltRedirect = function (altUrl) {
	// Instructs the object to redirect to a different URL
	// if a sufficient flash player was not detected. 
	this.redirect = true;
	this.redirectSwf = "";
	this.redirectAlt = altUrl;
}

fmp.unsetRedirect = function () {
	// Removes the redirect instruction altogether.
	this.redirect = false;
	this.redirectSwf = false;
	this.redirectAlt = false;
}

/*** setter methods **********************************************************/

fmp.useDetection = function () {
	//if (FlashPlayer == undefined) return false; // error in Mac IE: 'undefined' is undefined
	//if (!FlashPlayer) return false;
	this.player = FlashPlayer;
	this.installedVersion = this.player.maxInstalledVersion;
	return true;
}

fmp.setVersion = function (v) {
	v = parseInt(v);
	if (this.regex) {
		if (!/\d/i.test(v)) return false;
	}
	this.targetVersion = Math.abs(parseInt(v));
	// Update the codebase parameter.
	var codebaseVersion = this.player ? this.installedVersion : this.targetVersion;
	var token = "%VERSION%";
	if (this.regex) {
		var pattern = new RegExp(token, "gi");
		this.parameters.codebase = this.parameters.codebase.replace(pattern, codebaseVersion);
	} else {
		this.parameters.codebase = this.parameters.codebase.split(token).join(codebaseVersion);
	}
	return true;
}

fmp.setUrl = function (u) {
	if (!u.length) return false;
	this.parameters.url = u;
	this.setElementId(); 
	return true;
}

fmp.setElementId = function (e) {
	//if (e == undefined || !e.length) { // error in Mac IE: 'undefined' is undefined
	if (!e) {
		var u = this.parameters.url;
		if (!u.length) return false;
		var lastSlash = u.lastIndexOf("/");
		var lastDot = u.lastIndexOf(".");
		this.parameters.elementId = u.substring(lastSlash + 1, lastDot);
	} else {
		this.parameters.elementId = e;
	}
	return true;
}

fmp.setBgColor = function (bg) {
	// Accepts the following (strings): 1a|#1a|1a2|#1a2|1a2b3c|#1a2b3c
	if (this.regex) {
		var rx2 = /^#?[0-9a-f]{2}$/i;
		var rx3 = /^#?[0-9a-f]{3}$/i;
		var rx6 = /^#?[0-9a-f]{6}$/i;
		if (!rx2.test(bg) && !rx3.test(bg) && !rx6.test(bg)) return false;
		if (rx2.test(bg)) {
			bg = bg.replace(/([0-9a-f]{2})/gi, "#$1$1$1"); // might add additional "#"
		}
		if (rx3.test(bg)) {
			bg = bg.replace(/([0-9a-f])([0-9a-f])([0-9a-f])/gi, "#$1$1$2$2$3$3"); // might add additional "#"
		}
	}
	if (bg.length < 7) bg = "#" + bg;
	this.parameters.bgcolor = bg.toLowerCase();
	return true;
}

fmp.setWindowMode = function (w) {
	w = w.toLowerCase();
	if (w != "" &&
		w != "window" &&
		w != "opaque" &&
		w != "transparent") return false;
	if (w == "") w = "window";
	this.parameters.wmode = w;
	return true;
}

fmp.setSize = function (w, h) {
	if (this.regex) {
		var rx = /^\d+%?$/;
		if (!rx.test(w) || !rx.test(h)) return false;
	}
	this.parameters.width = w.toString();
	this.parameters.height = h.toString();
	return true;
}

fmp.setWidth = function (w) {
	if (this.regex) {
		var rx = /^\d+%?$/;
		if (!rx.test(w)) return false;
	}
	this.parameters.width = w.toString();
	return true;
}

fmp.setHeight = function (h) {
	if (this.regex) {
		var rx = /^\d+%?$/;
		if (!rx.test(h)) return false;
	}
	this.parameters.height = h.toString();
	return true;
}

fmp.setPlay = function (p) {
	this.parameters.play = Boolean(p);
	return true;
}

fmp.setLoop = function (l) {
	this.parameters.loop = Boolean(l);
	return true;
}

fmp.setMenu = function (m) {
	this.parameters.menu = Boolean(m);
	return true;
}

fmp.setQuality = function (q) {
	q = q.toLowerCase();
	if (q != "" &&
		q != "best" &&
		q != "high" &&
		q != "autohigh" &&
		q != "medium" &&
		q != "low" &&
		q != "autolow") return false;
	if (q == "") q = "high";
	this.parameters.quality = q;
	return true;
}

fmp.setScaleMode = function (s) {
	s = s.toLowerCase();
	if (s != "" &&
		s != "showall" &&
		s != "exactfit" &&
		s != "noscale" && // is this valid?
		s != "noborder") return false;
	if (s == "") s = "showall";
	this.parameters.scale = s;
	return true;
}

fmp.setAlignment = function (a) {
	a = a.toLowerCase();
	if (this.regex && a.length) {
		// accepts: l|r|t|b|(empty)
		var rx = /^[lrtb]$/i;
		if (!rx.test(a)) return false;
	}
	this.parameters.align = a;
	return true;
}

fmp.setSAlignment = function (a) {
	a = a.toLowerCase();
	if (this.regex && a.length) {
		// accepts: l|r|t|b|tl|tr|bl|br|(empty)
		var rx = /^(l|r|t|b|tl|tr|bl|br){1}$/gi;
		if (!rx.test(a)) return false;
	}
	this.parameters.salign = a;
	return true;
}

fmp.setScriptAccess = function (s) {
	s = s.toLowerCase();
	if (s != "" &&
		s != "samedomain" &&
		s != "always" &&
		s != "never") return false;
	if (s == "") s = "samedomain";
	this.parameters.allowscriptaccess = s;
	return true;
}

fmp.setSeamlessTabbing = function (s) {
	this.parameters.seamlesstabbing = Boolean(s);
	return true;
}

fmp.setDeviceFont = function (d) {
	this.parameters.devicefont = Boolean(s);
	return true;
}

fmp.setFlashVar = function (name, value) {
	this.parameters.flashvars[name] = value;
	return true;
}

fmp.setFlashVarsQS = function (qs) {
	// Accepts a query-format string, and reads name/value pairs from it.
	this.clearFlashVars();
	if (!qs.length) return true;
	var pairs = qs.split("&");
	while (pairs.length) {
		var pair = pairs.shift().split("=");
		this.setFlashVar(pair[0], pair[1]);
	}
	return true;
}

fmp.clearFlashVars = function () {
	this.parameters.flashvars = {};
}


/*** getter methods **********************************************************/

fmp.getFlashVars = function () {
	var fv = [];
	for (var p in this.parameters.flashvars) {
		fv[fv.length] = p + "=" + this.parameters.flashvars[p];
	}
	return fv.join("&");
}

delete fmp;
