
/*
MediaPlayer Object
Version		: 1.0
Created		: 01/12/06
Created By	: Martin Webb
Modified	: 01/12/06

Class to embed a media played into a web page based on the type of file to be displayed.
Will distiguish between Windows Media files and quicktime files.
This will not detect the player within the host browser.

*/
function MediaPlayer(pSource, pID, pWidth, pHeight, pAutoStart, pController, pStatusBar){
	this._source = pSource;
	this._playerID = pID;
	this._width = pWidth;
	this._height = pHeight;
	this._autoStart = pAutoStart;
	this._controller = pController;
	this._statusBar = pStatusBar;

	this.isWin = navigator.userAgent.toLowerCase().indexOf("windows") != -1;
	
	this._players = [{		
		name					:	"Windows Media Player",
		controllerHeightWin		:	45,
		controllerHeightOther	:	45,
		statusBarHeightWin		:	19,
		statusBarHeightOther	:	19,
		fileTypes				:	["wmv", "avi", "wma"],
		classID					:	"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6",
		codeBase				:	"http:\/\/activex.microsoft.com\/activex\/controls\/mplayer\/en\/nsmp2inf.cab#Version=6,4,7,1112",
		pluginSpace				:	"http:\/\/www.microsoft.com\/windows\/windowsmedia\/download\/AllDownloads.aspx\/",
		pathVar					:	"url",
		autoPlayVar				:	"AutoStart",
		showControllerVar		:	"ShowControls",
		showStatusBarVar		:	"ShowStatusBar"
	},
	{
		name					:	"QuickTime Player",
		controllerHeightWin		:	16,
		controllerHeightOther	:	16,
		statusBarHeightWin		:	0,
		statusBarHeightOther	:	0,
		fileTypes				:	["mov", "mp4", "qt", "aif"],
		classID					:	"CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B",
		codeBase				:	"http:\/\/www.apple.com\/qtactivex\/qtplugin.cab",
		pluginSpace				:	"http:\/\/www.apple.com\/quicktime\/download\/",
		pathVar					:	"src",
		autoPlayVar				:	"autoplay",
		showControllerVar		:	"controller",
		showStatusBarVar		:	""
	}
	];
	
	this._player = this.getPlayer();	
}

MediaPlayer.prototype.getPlayer = function(){
	if(this._source.length){
		var pos = this._source.lastIndexOf(".");
		if(pos > -1){
			var ext = this._source.substring(pos+1, this._source.length); // get our file extention
			// loop through our defined players to find a matched file extention
			for(var i=0; i<this._players.length; i++){
				var fileTypes = this._players[i].fileTypes;
				for(var j=0; j<fileTypes.length; j++){
					if(fileTypes[j] == ext.toLowerCase()){
						return this._players[i];
					}
				}
			}
		}

	}
	return {name : "UNKNOWN"};
};

// windows media player likes its booleans as 1 | 0
MediaPlayer.prototype.boolToNum = function(pInput){
	if(this._player.name == "Windows Media Player"){
		if(pInput){
			return 1;
		}else{
			return 0;
		}
	}else{
		return pInput;
	}
};

MediaPlayer.prototype.write = function(){
	if(this._player.name != "UNKNOWN"){
		var playerInsertHeight = this._height;
		if(this._controller){
			this.isWin ? playerInsertHeight += this._player.controllerHeightWin : playerInsertHeight += this._player.controllerHeightOther;
		}
		if(this._statusBar){
			this.isWin ? playerInsertHeight += this._player.statusBarHeightWin : playerInsertHeight += this._player.statusBarHeightOther;
		}
		document.writeln("<object id=\"" + this._playerID + "\" width=\"" + this._width + "\" height=\"" + playerInsertHeight + "\"");
		document.writeln("classid=\"" + this._player.classID + "\"");
		document.writeln("codebase=\"" + this._player.codeBase + "\">");
		document.writeln("<param name=\"" + this._player.pathVar + "\" value=\"" + this._source + "\" \/>");
		document.writeln("<param name=\"" + this._player.autoPlayVar + "\" value=\"" + this.boolToNum(this._autoStart) + "\" \/>");
		document.writeln("<param name=\"" + this._player.showControllerVar + "\" value=\"" + this.boolToNum(this._controller) + "\" \/>");
		if(this._player.showStatusBarVar.length){
			document.writeln("<param name=\"" + this._player.showStatusBarVar + "\" value=\"" + this.boolToNum(this._statusBar) + "\" \/>");
		}
		document.writeln("<embed pluginspage=\"" + this._player.pluginSpace + "\"");
		document.writeln("src=\"" + this._source + "\"");
		document.writeln("width=\"" + this._width + "\"");
		document.writeln("height=\"" + playerInsertHeight + "\"");
		document.writeln(this._player.autoPlayVar + "=\"" + this.boolToNum(this._autoStart) + "\"");
		document.writeln(this._player.showControllerVar + "=\"" + this.boolToNum(this._controller) + "\"");
		if(this._player.showStatusBarVar.length){
			document.writeln(this._player.showStatusBarVar + "=\"" + this.boolToNum(this._statusBar) + "\"");
		}
		document.writeln("<\/embed><\/object>");
	}else{
		document.writeln("<strong>ERROR: Can not insert media player. Unkown file format.</strong>");
	}
};

