/*
* Flash Plugin detection and embed - http://computerscorp.com/swfplacer/
*
* SWFPlacer v0.1 - is (c) 2006 cybernetix@gmail.com
*
* This code is licenced under the so-called MIT licence http://www.opensource.org/licenses/mit-license.php
*
*/
var SWFPlacer = function() {
	this.required    = {file:true, width:true, height:true, version:true};
	this.defaults    = ["quality:high", "bgcolor:#FFFFFF", "loop:false", "menu:false", "wmode:opaque", "allowscriptaccess:samedomain"];
	this.parameters  = this.attributes = new Array();
	this.create(arguments);
}
SWFPlacer.prototype = {
	getFlashVersion: function() {
		this.flashVersion = [0,0,0,0];
		try {
			this.flashVersion = new ActiveXObject("ShockwaveFlash.ShockwaveFlash").GetVariable("$version").match(/\d+/g);
			this.embedSWF     = false;
		} catch (e) {}
		try {
			this.flashVersion = navigator.plugins["Shockwave Flash"].description.match(/\d+/g);
			this.embedSWF     = true;
			this.defaults[this.defaults.length] = "swliveconnect:true";
		} catch (e) {}
	},
	create: function(args) {	
		this.getFlashVersion();
		this.writeString = '', this.errorText = '', this.missingParams = '';
		for (var i = 0, px = ""; i < args.length; i++)
			if (!this.empty(param = args[i])) {
				px = param.split(":");
				if (this.required[px[0]])
				    this.attributes[px[0]] = px[1];
				else
				    this.parameters[px[0]] = px[1];				
			}
		for (var zx in this.required)
			if (!this.attributes[zx])
				this.missingParams += " <b>" + zx + "</b> ;";
		if (!this.empty(this.missingParams))
			this.flashVersion = [-1,0,0,0];
		if (this.validVersion()) {		
			for (var i = 0, px = ""; i < this.defaults.length; i++) {
				 px = this.defaults[i].split(":");
				 if (!this.parameters[px[0]])
				      this.parameters[px[0]] = px[1];
			}
			if (!this.parameters.id)
				 this.parameters.id = this.parameters.file.split(".")[0];
			if (this.embedSWF) {
				this.writeString += '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ';
	        	this.writeString += 'name="'+this.parameters.id+'" width="'+this.attributes.width+'" height="'+this.attributes.height+'" ';
				this.writeString += 'src="'+this.attributes.file+'" ';
				for (var zx in this.parameters)
					 this.writeString += zx +'="'+ this.parameters[zx] +'" ';
				this.writeString += '>';
			}
			else {
			    this.writeString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this.attributes.version+'" ';
	        	this.writeString += 'id="'+this.attributes.id+'" width="'+this.attributes.width+'" height="'+this.attributes.height+'"> ';
				this.writeString += '<param name="movie" value="'+this.attributes.file+'" /> ';
				for (var zx in this.parameters)
					 this.writeString += '<param name="'+ zx +'" value="'+ this.parameters[zx] +'" /> ';
				this.writeString += '</object>';
			}
		}
		else {
			this.writeString += '<div style="width:'+this.attributes.width+'px; height:'+this.attributes.height+'px; border:1px solid black; background-color: white; padding: 10px; overflow: auto;">';			
			if (this.flashVersion[0] == -1) {
				this.errorText  = "FLASH OBJECT<br />Missing parameter(s) :";
				this.errorText += this.missingParams;
			}
			else {
				this.errorText    = "You don't have an appropriate version of Macromedia Flash Player that should play the content we've created. Please click button to install latest version of Macromedia Flash Player then back to this page and refresh it.";
				this.writeString += '<a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&promoid=BIOW" target="_blank"><img align="left" src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" style="border:0px; margin-right: 10px;" /></a>';				
			}
			this.writeString += '<p style="text-align:justify; font-family: Arial; font-size: 11px; color: black;">'+this.errorText+'</p></div>';
		}
		document.write(this.writeString);
		this.writeString = this.errorText = this.missingParams = undefined;
	},
	validVersion: function() {
		var requiredVer = this.attributes.version.match(/\d+/g).slice(0,3);
		var actualVer   = this.flashVersion.slice(0, requiredVer.length);
		return this.validFlashVersion = parseInt(actualVer.join("")) >= parseInt(requiredVer.join(""));
	},
	empty: function(val) {
		return (val == null || val == undefined || val == "" || val == "undefined");
	}	
}
