﻿if(typeof thefarm == "undefined") var thefarm = new Object();
if(typeof thefarm.util == "undefined") thefarm.util = new Object();

thefarm.MPObject = function(url, id, w, h, autoStart, showControls, detectByMimeType, errMsg){
	if (!document.createElement || !document.getElementById) { return; } //check for browser compat

	this.params = new Object();
	this.variables = new Object();
	
	this.plugin = ""; //the name of the plugin found
	this.markup = ""; //the output of the markup rendered
	
	this.width = 100;
	this.height = 100;
	this.id = "MPObject";
	this.url = "";
	this.autoStart = true;
	this.showControls = true;
	this.detectByMimeType = false;
	
	var msg="There was an error attempting to load the Windows Media Player plugin.\n\n";
	msg+="Please see the instructions listed on the screen to install the latest version\n\n";	
	msg+="Click OK to continue.\n\n";
	this.errMsg = msg;
	
	if(errMsg) { this.errMsg = errMsg; }
	if(detectByMimeType) { this.detectByMimeType = (detectByMimeType=="false"?false:true); }
	if(url) { this.url = url; }
	if(id) { this.id = id; }
	if(w) { this.width = w; }
	if(h) { this.height = h; }
	if(autoStart) { this.autoStart = (autoStart=="false"?false:true); }
	if(showControls) { this.showControls = (showControls=="false"?false:true); }	

}
thefarm.MPObject.prototype = {	
	addParam: function(name, value){
		this.params[name] = value;
	},
	getParams: function(){
		return this.params;
	},
	getMPHTML: function() {
		var mpNode = "";
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
			mpNode = '<embed type="application/x-mplayer2" autostart="'+ this.autoStart +'" src="'+ this.url +'" width="'+ this.width +'" height="'+ this.height +'"';
			mpNode += ' id="'+ this.id +'" name="'+ this.id +'" ';
			if (!this.showControls)
				mpNode += ' showcontrols="0" ';
			var params = this.getParams();
			 for(var key in params){ mpNode += [key] +'="'+ params[key] +'" '; }
			mpNode += '/>';
		} 
		else { // PC IE
			mpNode = '<object id="'+ this.id +'" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" standby="Loading Microsoft® Windows® Media Player components..." type="application/x-oleobject" width="'+ this.width +'" height="'+ this.height +'">';
			mpNode += '<param name="FileName" value="'+ this.url +'" />';
			mpNode += '<param name="AutoStart" value="'+ this.autoStart +'" />';
			if (!this.showControls)
				mpNode += '<param name="uiMode" value="none" />';
			var params = this.getParams();
			for(var key in params) {
			 mpNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
			}
			mpNode += "</object>";
		}
		this.markup = mpNode;
		return mpNode;
	},
	checkPlugin: function(pluginName) {
		alert("Looking for plugin: " + pluginName);
		var found = false;
		if (navigator.plugins) {
          for (i=0; i < navigator.plugins.length; i++ ) {
				//alert(navigator.plugins[i] + " = " + pluginName);
               if (navigator.plugins[i].name.indexOf(pluginName) >= 0)
                {
					found = true;
					this.plugin = navigator.plugins[i].name; //store the found plugin name
				}
            }
        }
        alert("Plugin found? " + found);
        return found;
	},
	write: function(elementId){
		var player;
		
	
		//This will attempt to locate a useable plugin for Windows Media
		
		try 
		{	
			//if it's not IE
			if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
			{
				if (this.detectByMimeType) {					
					try {
						if (!player){
							player = navigator.mimeTypes["application/x-mplayer2"];
							if (player)
								player = player.enabledPlugin;
							if (player)
								this.plugin = "Found by MimeType";
						}
					}
					catch (e) {							
						player = false; //some browsers will throw an error if this doesn't work.
					}					
				}				
			
				if (!player){ //check for new firefox plugin
					player = this.checkPlugin("Windows Media Player Firefox Plugin");
				}
				if (!player){ //check for old firefox plugin
					player = this.checkPlugin("Windows Media Player Plug-in Dynamic Link Library");
				}				
				if (!player){ //check for old Windows Media Plugin for Mac
					player = this.checkPlugin("Windows Media Plugin");
				}
				if (!player){ //check for QuickTime plugin
					player = this.checkPlugin("QuickTime");
					if (player){ //if they have QuickTime, check for new QuickTime Windows Media Plugin
						player = this.checkPlugin("Flip4Mac Windows Media Plugin");
					}
				}
				
			}
			else 
			{
				if (!player){
					player = new ActiveXObject("MediaPlayer.MediaPlayer.1");
					if (player)
						this.plugin = "MediaPlayer.MediaPlayer.1";
				}
			}
			
			//if the player is still not found and the browser supports GeckoActiveXObject browser...
			if (!player){
				alert("Attempting to load GeckoActiveXObject");
				player = new GeckoActiveXObject("6BF52A52-394A-11d3-B153-00C04F79FAA6"); //player version 9
				if (player)
					this.plugin = "6BF52A52-394A-11d3-B153-00C04F79FAA6";
			}			
			
			alert("using plugin: " + this.plugin);
			
		}
		catch(e)
		{
			if (this.errMsg!="")
				alert(this.errMsg);
		}	
		
		if (player)
		{
			var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
			n.innerHTML = this.getMPHTML();
			return true;
		}
		else
			return false;		
	}
}


/* ---- get value of query string param ---- */
thefarm.util = {
	getRequestParameter: function(param){
		var q = document.location.search || document.location.hash;
		if(q){
			var startIndex = q.indexOf(param +"=");
			var endIndex = (q.indexOf("&", startIndex) > -1) ? q.indexOf("&", startIndex) : q.length;
			if (q.length > 1 && startIndex > -1) {
				return q.substring(q.indexOf("=", startIndex)+1, endIndex);
			}
		}
		return "";
	}
}

/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* add some aliases for ease of use/backwards compatibility */
var getQueryParamValue = thefarm.util.getRequestParameter;
var MPObject = thefarm.MPObject; // for legacy support   	        
