/**
 * A tiny flash detection and embedding class
 *
 * @author Latchezar Tzvetkoff
 *
 * true code - true power!!
 */

// the flash object itself..
var Flash = function(src, id, width, height, bgcolor, minVer) {
	this.src = src;
	this.id = id || 'flash_' + parseInt(Math.random()*10) + parseInt(Math.random()*10) + parseInt(Math.random()*10) + parseInt(Math.random()*10) + parseInt(Math.random()*10) + parseInt(Math.random()*10);
	this.width = width || 'auto';
	this.height = height || 'auto';
	this.bgcolor = bgcolor || 'transparent';
	this.minVer = minVer;

	this.params = {};
	this.flashvars = {};
};

// execute this upon initialization - we need to know current flash version..
Flash.version = (function() {
	var version = [0, 0, 0];

	if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
		var x = navigator.plugins['Shockwave Flash'];
		if (x && x.description) {
			version = x.description.replace(/[a-z\s]+/ig, '').replace(/(\s+r|\s+b[0-9]+)/, '.').split('.');
		}
	} else {
		var ax;
		try {
			ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
		} catch (ex) {
			try {
				ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
				ax.AllowScriptAccess = 'always';
				version = [6, 0, 21];
			} catch (ex) {
			}
			try {
				ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
			} catch (ex) {
			}
		}
		if (ax && !version[0]) {
			version = ax.GetVariable('$version').split(' ')[1].split(',');
		}
	}

	return [
		parseInt(version[0]),
		parseInt(version[1]),
		parseInt(version[2])
	];
})();

// flash object methods..
Flash.prototype = {
	// check flash version..
	checkVersion: function(version) {
		var flashver = Flash.version,
			result = false;

		if (!version) {
			return true;
		}

		if (flashver[0]) {
			result = true;

			if (typeof version == 'string') {
				version = version.split('.');
				version[0] = parseInt(version[0], 10) || 0;
				version[1] = parseInt(version[1], 10) || 0;
				version[2] = parseInt(version[2], 10) || 0;
			}

			if (flashver[0] < version[0]) {
				result = false;
			} else if (flashver[0] == version[0]) {
				if (flashver[1] < version[1]) {
					result = false;
				} else if (flashver[1] == version[1]) {
					if (flashver[2] < version[2]) {
						flash = false;
					}
				}
			}
		}

		return result;
	},

	// escape a parameter..
	escapeParam: function(value) {
		return value.replace('"' , '%22', 'g').
					 replace('\'', '%27', 'g');
	},

	// add a parameter..
	addParam: function(name, value) {
		this.params[name] = this.escapeParam(value + '');
		return this;
	},

	// escape a flashvar..
	escapeVariable: function(value) {
		return value.replace('%' , '%25', 'g').
					 replace('"' , '%22', 'g').
					 replace('\'', '%27', 'g').
					 replace('&' , '%26', 'g').
					 replace('=' , '%3D', 'g').
					 replace('?' , '%3F', 'g');
	},

	// add a flashvar..
	addVariable: function(name, value) {
		this.flashvars[name] = this.escapeVariable(value + '');
		return this;
	},

	// returns the html code for the flash object..
	getHtml: (function() {
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
			return function() {
				if (!this.params.flashvars) {
					var flashvars = [];

					for (var key in this.flashvars) {
						if (this.flashvars.hasOwnProperty(key)) {
							flashvars.push(key += '=' + this.flashvars[key]);
						}
					}

					this.params.flashvars = flashvars.join('&');
				}

				var result = '<embed type="application/x-shockwave-flash" ' +
									'id="' + this.id + '" ' +
									'src="' + this.src + '" ' +
									'width="' + this.width + '" ' +
									'height="' + this.height + '" ' +
									'bgcolor="' + this.bgcolor + '"';

				for (var key in this.params) {
					if (this.params.hasOwnProperty(key)) {
						result += ' ' + key + '="' + this.params[key] + '"';
					}
				}

				result += '/>';

				return result;
			}
		}

		return function() {
			if (!this.params.flashvars) {
				var flashvars = [];

				for (var key in this.flashvars) {
					if (this.flashvars.hasOwnProperty(key)) {
						flashvars.push(key += '=' + this.flashvars[key]);
					}
				}

				this.params.flashvars = flashvars.join('&');
			}

			var result = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
								 'id="' + this.id + '" ' +
								 'width="' + this.width + '" ' +
								 'height="' + this.height + '">';

			result += '<param name="movie" value="' + this.src + '"/>';
			result += '<param name="bgcolor" value="' + this.bgcolor + '"/>';

			for (var key in this.params) {
				if (this.params.hasOwnProperty(key)) {
					result += '<param name="' + key + '" value="' + this.params[key] + '"/>';
				}
			}

			result += '</object>';

			return result;
		};
	})(),

	// embed flash object in a container..
	write: function(container) {
		if (typeof container == 'string') {
			container = document.getElementById(container);
		}

		if (container && container.nodeType == 1) {
			if (this.checkVersion(this.minVer)) {
				container.innerHTML = this.getHtml();
			}
		}
	}
};

