/**************************************************************************************
  Description: Class for the embedding and detection of Microsoft Windows Media Player.
		 Author: Roarke Lynch
		   Date: 19-Apr-05
		  Notes: none
 Dependencies: Browser.js
**************************************************************************************/

/* Constructor */
MediaPlayer = function(source, width, height) 
{
	 this.source = ((typeof source) == 'undefined' ? "" : source)
    this.width  = ((typeof width)  == 'undefined' ? "100%" : width);
    this.height = ((typeof height) == 'undefined' ? "100%" : height)
    this.id = "";
    this.params = new Object();	
};

/* Static Members */
MediaPlayer.isInstalled = function()
{
	if( (typeof Browser) == 'undefined' ) {
		alert("***Browser.js not found****");
		return;
	}
	
	return Browser.detectControl("Windows Media", "MediaPlayer.MediaPlayer.1");
};

MediaPlayer.embed = function(source, width, height, params, parent_id)
{
    var player = new MediaPlayer(source, width, height);
    
    for(var i in params) {
        player.setParam(i, params[i]);
    }
    
	try {
		document.write(player);
    }
    catch(e) {
        var canvas = document.getElementById(parent_id);
        var playerElement = player.toXmlElement();
        canvas.appendChild(playerElement);
    }
};

/* Instance Members */
MediaPlayer.prototype.getSource = function()
{
    return this.source;
};

MediaPlayer.prototype.setSource = function(source)
{
    this.source = source;
};

MediaPlayer.prototype.getWidth = function()
{
    return this.width;
};

MediaPlayer.prototype.setWidth = function(w)
{
    this.width = w;
};

MediaPlayer.prototype.getHeight = function()
{
    return this.height;
};

MediaPlayer.prototype.setHeight = function(h)
{
    this.height = h;
};

MediaPlayer.prototype.getId = function()
{
    return this.id;
};

MediaPlayer.prototype.setId = function(id)
{
    this.id = id;
};

MediaPlayer.prototype.getParam = function(name)
{
    return this.params[name];
};

MediaPlayer.prototype.getParams = function()
{
    return this.params;
};

MediaPlayer.prototype.setParam = function(name, value)
{
    this.params[name] = value;
};

MediaPlayer.prototype.getParamTags = function()
{
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
};

MediaPlayer.prototype.toString = function ()
{
	var wmHTML = "";
	if (window.ActiveXObject && (navigator.userAgent.indexOf('Mac') == -1)) { // PC IE
		
		wmHTML += '<object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject" width="' + this.getWidth() + '" height="' + this.getHeight() + '" id="' + this.getId() + '">';
		wmHTML += '<param name="FileName" value="' + this.getSource() + '" />';
		if (this.getParamTags() != null) {
			wmHTML += this.getParamTags();
		}
		wmHTML += '</object>';
		
	}
	else { // Everyone else
		wmHTML += '<embed type="application/x-mplayer2" src="' + this.getSource() + '" width="' + this.getWidth() + '" height="' + this.getHeight() + '" id="' + this.getId() + '"';
		for (var param in this.getParams()) {
			wmHTML += ' ' + param + '="' + this.getParam(param) + '"';
		}
		wmHTML += '></embed>';
    }
    
    return wmHTML;
};

MediaPlayer.prototype.toXmlElement = function () {
	var xmlElement;
	var paramElement;
	if (window.ActiveXObject && (navigator.userAgent.indexOf('Mac') == -1)) { // PC IE
		xmlElement = document.createElementNS("http://www.w3.org/1999/xhtml", "object");
		xmlElement.setAttribute("classid", "CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95");
		xmlElement.setAttribute("codebase", "http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701");
		xmlElement.setAttribute("standby", "Loading Microsoft Windows Media Player components...");
		xmlElement.setAttribute("type", "application/x-oleobject");
		xmlElement.setAttribute("width", this.getWidth());
		xmlElement.setAttribute("height", this.getHeight());
		xmlElement.setAttribute("id", this.getId());
		
		paramElement = document.createElementNS("http://www.w3.org/1999/xhtml", "param");
		paramElement.setAttribute("name", "FileName");
		paramElement.setAttribute("value", this.getSource());
		xmlElement.appendChild(paramElement);
		
		for (var param in this.getParams()) {
			paramElement = document.createElementNS("http://www.w3.org/1999/xhtml", "param");
			paramElement.setAttribute("name", param);
			paramElement.setAttribute("value", this.getParam(param));
			xmlElement.appendChild(paramElement);
	    }
	}
	else { // Everyone else
		xmlElement = document.createElementNS("http://www.w3.org/1999/xhtml", "embed");
		xmlElement.setAttribute("type", "application/x-mplayer2");
		xmlElement.setAttribute("src", this.getSource());
		xmlElement.setAttribute("width", this.getWidth());
		xmlElement.setAttribute("height", this.getHeight());
		xmlElement.setAttribute("id", this.getId());
		
		for (var param in this.getParams()) {
			xmlElement.setAttribute(param, this.getParam(param))
		}
    }
	
	return xmlElement;
}
