/**
 * Bluish's Multi-Plugin 
 * Based on SWFObject by Geoff Stearns: http://blog.deconcept.com/swfobject/
 *
 */
DEBUG_MODE = false;
if(typeof bluish == "undefined") var bluish = new Object();
/*
 * PluginEngine - Superclass for all object/embed elements.
 */
bluish.PluginEngine = function() {
	if (!document.createElement || !document.getElementById) { return; }	
	this.params = new Object();
	this.attributes = new Array();
	this.canUsePlugin = false;
}

bluish.PluginEngine.prototype = {
	startup: function(src, id, w, h, min_version, bgcolor) {
		if(src) { this.setAttribute('src', src); }
		if(id) { this.setAttribute('id', id); }
		if(w) { this.setAttribute('width', w); }
		if(h) { this.setAttribute('height', h); }
		if(bgcolor) { this.addParam('bgcolor', bgcolor); }
		this.canUsePlugin = (parseInt(min_version) > 0) ? this.detectVersion(new bluish.PluginVersion(min_version.toString().split("."))) : true;
	},
	setAttribute: function(name, value){
		this.attributes[name] = value;
	},
	getAttribute: function(name){
		return this.attributes[name];
	},
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	detectVersion: function(targetPluginVersion) {
		if(navigator.plugins && navigator.mimeTypes.length){
			return this.detectPlugin(targetPluginVersion);
		} else {
			return this.detectActiveX(targetPluginVersion);
		}
	},
	getHTML: function() {
		var outputHTML = "";
		var params = this.getParams();
		
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // EMBED
			outputHTML = '<embed type="'+ this.getAttribute('MIMEtype') +'" src="'+ this.getAttribute('src') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"';
			outputHTML += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
			for(var key in params){ outputHTML += [key] +'="'+ params[key] +'" '; }
			outputHTML += '/>';
		} else { // Object
			outputHTML = '<object id="'+ this.getAttribute('id') +'" classid="clsid:'+ this.getAttribute('classid') +'" codebase="'+ this.getAttribute('codebase') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'">';
			outputHTML += '<param name="'+ this.getAttribute('object_src_name') +'" value="'+ this.getAttribute('src') +'" />';
			for(var key in params) { outputHTML += '<param name="'+ key +'" value="'+ params[key] +'" />'; }
			outputHTML += "</object>";
		}
		return outputHTML;
	},
	write: function(elementId){
		if(this.canUsePlugin){
			if(DEBUG_MODE) alert("Write: "+this.getHTML());
			var output = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
			output.innerHTML = this.getHTML();
			return true;
		}
		return false;
		
	}
}
bluish.PluginVersion = function(arrVersion){
	this.major = parseInt(arrVersion[0]) != null ? parseInt(arrVersion[0]) : 0;
	this.minor = parseInt(arrVersion[1]) || 0;
	this.rev = parseInt(arrVersion[2]) || 0;
}
bluish.PluginVersion.prototype.versionIsValid = function(target){
	if(this.major < target.major) return false;
	if(this.major > target.major) return true;
	if(this.minor < target.minor) return false;
	if(this.minor > target.minor) return true;
	if(this.rev < target.rev) return false;
	return true;
}

/*
 * QuickTime Movie Object
 */
bluish.QuickTimeMovie = function(src, id, w, h, min_version, bg_color) {
	this.setAttribute('classid', "02BF25D5-8C17-4B23-BC80-D3488ABDDC6B");
	this.setAttribute('codebase', "http://www.apple.com/qtactivex/qtplugin.cab");
	this.setAttribute('pluginspage', "http://www.apple.com/quicktime/download/");
	this.setAttribute('MIMEtype', "video/quicktime");
	this.setAttribute('object_src_name', "src");
	this.detectPlugin = function(targetPluginVersion) {
		var installedPluginVersion = new bluish.PluginVersion([0,0,0]);
		for (var i=0; i < navigator.plugins.length; i++ ) {
			var plugin = navigator.plugins[i];
			if (plugin.name.indexOf("QuickTime") > -1) {
				installedPluginVersion = new bluish.PluginVersion(plugin.name.split(" ")[2].split("."));
				return installedPluginVersion.versionIsValid(targetPluginVersion);
			}
		}
		return installedPluginVersion.versionIsValid(targetPluginVersion);
	};
	this.detectActiveX = function(targetPluginVersion) {
		var installedPluginVersion = new bluish.PluginVersion([0,0,0]);
		var activeX = new ActiveXObject("QuickTimeCheckObject.QuickTimeCheck.1");
		if(activeX.IsQuickTimeAvailable(0)) {
			var versionGestalt = activeX.QuickTimeVersion;
			var major = ((versionGestalt&0xFF000000)/0x1000000).toString(16);
			var minor = ((versionGestalt&0x00F00000)/0x100000).toString(16);
			var rev = ((versionGestalt&0x000F0000)/0x10000).toString(16);
			installedPluginVersion = new bluish.PluginVersion([major,minor,rev]);
		}
		return installedPluginVersion.versionIsValid(targetPluginVersion);
	};
	this.startup(src, id, w, h, min_version, bg_color);
}
bluish.QuickTimeMovie.prototype = new bluish.PluginEngine();

/*
 * Flash Movie Object
 */
bluish.FlashMovie = function(src, id, w, h, min_version, bg_color) {
	this.setAttribute('classid', "D27CDB6E-AE6D-11cf-96B8-444553540000");
	this.setAttribute('codebase', "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab");
	this.setAttribute('pluginspage', "http://www.macromedia.com/go/getflashplayer");
	this.setAttribute('MIMEtype', "application/x-shockwave-flash");
	this.setAttribute('object_src_name', "movie");
	
	this.detectPlugin = function(targetPluginVersion) {
		var installedPluginVersion = new bluish.PluginVersion([0,0,0]);
		var plugin = navigator.plugins["Shockwave Flash"];
		if(plugin && plugin.description) {
			installedPluginVersion = new bluish.PluginVersion(plugin.description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
		}
		return installedPluginVersion.versionIsValid(targetPluginVersion);
	};
	this.detectActiveX = function(targetPluginVersion) {
		var installedPluginVersion = new bluish.PluginVersion([0,0,0]);
		try {
			var activeX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			for (var i=3; activeX!=null; i++) {
				activeX = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i);
				installedPluginVersion = new bluish.PluginVersion([i,0,0]);
			}
		} catch(e) {}
		if (installedPluginVersion.major > targetPluginVersion.major) return installedPluginVersion.versionIsValid(targetPluginVersion);
		if ( ((targetPluginVersion.minor != 0 || targetPluginVersion.rev != 0) && targetPluginVersion.major == installedPluginVersion.major) || installedPluginVersion.major != 6 ) {
			try {
				installedPluginVersion = new bluish.PluginVersion(activeX.GetVariable("$version").split(" ")[1].split(","));
			} catch(e) {}
		}
		return installedPluginVersion.versionIsValid(targetPluginVersion);
	};
	this.startup(src, id, w, h, min_version, bg_color);
}
bluish.FlashMovie.prototype = new bluish.PluginEngine();

/*
 * Create namespace shortcuts
 */
var QuickTimeMovie = bluish.QuickTimeMovie;
var FlashMovie = bluish.FlashMovie;

