/**
 * a tiny flash detection and embedding class
 *
 * @author latchezar tzvetkoff
 *
 * true code - true power!!
 */
Flash = function(fileName, id, width, height, minVer, bgcolor) {
	var version = [0, 0, 0], flash = false;
	if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
		var x = navigator.plugins['Shockwave Flash'];
		if (x && x.description) {
			version = x.description.replace(/([a-zA-Z]|\s)+/, '').replace(/(\s+r|\s+b[0-9]+)/, '.').split('.');
		}
	} else {
		try {
			var 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 = ActiveXObject('ShockwaveFlash.ShockwaveFlash');
			} catch (ex) {
			}
		}
		if (ax && !version[0]) {
			version = ax.GetVariable('$version').split(' ')[1].split(',');
		}
	}

	if (version[0]) {
		flash = true;

		if (minVer) {
			version[0] = parseInt(version[0], 10);
			version[1] = parseInt(version[1], 10);
			version[2] = parseInt(version[2], 10);

			var required = minVer.split('.');
			required[0] = parseInt(required[0], 10);
			required[1] = parseInt(required[1], 10) || 0;
			required[2] = parseInt(required[2], 10) || 0;

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

	return {
		'flash': flash,
		'fileName': fileName,
		'id': id,
		'width': width,
		'height': height,
		'bgcolor': bgcolor,
		'parameters': [],
		'variables': [],
		'addParam': function(name, value) {
			this.parameters[name] = value;
			return this;
		},
		'addVariable': function(name, value) {
			this.variables[name] = escape(value);
			return this;
		},
		'getHtml': function() {
			var flashvars = '', result;
			for (var x in this.variables) {
				if (typeof this.variables[x] == 'string') {
					flashvars += (flashvars.length ? '&' : '') + x + '=' + this.variables[x];
				}
			}

			if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
				result = '<embed id="' + this.id + '" ' +
								'type="application/x-shockwave-flash" ' +
								'src="' + this.fileName + '" ' +
								'width="' + (this.width || 'auto') + '" ' +
								'height="' + (this.height || 'auto') + '" ' +
								'bgcolor="' + (this.bgcolor || 'transparent') + '"';

				for (var x in this.parameters) {
					if (typeof this.parameters[x] == 'string') {
						result += ' ' + x + '="' + this.parameters[x] + '"';
					}
				}

				result += (flashvars.length ? ' flashvars="' + flashvars + '">' : '') + '</embed>';
			} else {
				result = '<object id="' + this.id + '" ' +
								 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
								 'width="' + (this.width || 'auto') + '" ' +
								 'height="' + (this.height || 'auto') + '">';
				result += '<param name="movie" value="' + this.fileName + '" />';

				for (var x in this.parameters) {
					if (typeof this.parameters[x] == 'string') {
						result += '<param name="' + x + '" value="' + this.parameters[x] + '" />';
					}
				}

				result += (flashvars.length ? '<param name="flashvars" value="' + flashvars + '" />' : '') + '</object>';
			}

			return result;
		},
		'write': function(container) {
			if (this.flash) {
				if (typeof container == 'string') {
					container = document.getElementById(container);
				}
				container.innerHTML = this.getHtml(this);
			}
		}
	};
};
