/**
 * @projectDescription Media Player JavaScript API Wrapper
 * 
 * @author Gareth Stephenson
 * @version 1.0
 */


/**
 * Creates a media player object
 * 
 * @param {String} url
 * @param {String} id
 * @param {Integer} width
 * @param {Integer} height
 * @constructor
 */

var IS_IE = navigator.userAgent.match(/Gecko\//)==null;


 MediaPlayer = function(url, id, width, height)
 {
 	// Initialize default parameters
 	this.id = (id?id:'MediaPlayer');
	this.width = (width?width:460);
	this.height = (height?height:410);
	
	this.params = new Array();
	this.player = null;
	
	// Set default options
	this.URL(url);
	this.AutoStart(true);	
 }
 
 MediaPlayer.prototype=
 {
	/**
	 * Class Versions
	 */
	Version :
	{
		'10.0'	: 'clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'9.0'	: 'clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6',		
		'7.0'	: 'clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'6.4'	: 'clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95'
	},
	
	SetSize : function(width, height)
	{
		this.width = width;
		this.height = height;
	},
	
		 
 	/**
 	 * Start playing video file when player loads
 	 * @param {Boolean} bStart	true=start now, false=do not start immediately
 	 */
 	AutoStart : function(bStart)
	{
		this.addParam('autoStart', bStart);		
	},
	
	
	/**
	 * Set the user-interface mode for the player
	 * @param {Object} sMode
	 */
	SetUIMode : function(sMode)
	{
		this.addParam('uiMode', sMode);
	},
	
	/**
	 * Sets if an animation should show while the file loads
	 * @param {Object} bStart
	 */
	StartOnLoad : function(bStart)
	{
		this.addParam('AnimationAtStart', bStart);
	},
	
	/**
	 * Render the video in the webpage rather than using video overlay
	 * @param {Object} bActive
	 */
	RenderInPage : function(bActive)
	{
		this.addParam('WindowlessVideo', bActive);		
	},
	
	/**
	 * Stretch the video to fit the player size
	 * @param {Boolean} bStreatch
	 */
	StretchToFit : function(bStreatch)
	{
		this.addParam('StretchToFit', bStreatch);
	},
	
	/**
	 * Show player controls 
	 */
	ShowControls : function(bShow)
	{
		this.addParam('ShowControls', bShow);
	},
	
	/**
	 * Show media player status bar
	 */
	ShowStatusBar : function(bShow)
	{
		this.addParam('ShowStatusBar', bShow);
	},
	
	/**
	 * Set the location and filename of the clip to be played
	 * @param {String} url	file location (local or remote)
	 */
	URL : function(url)
	{
		if(this.player == null)
			this.addParam('URL', url);
		else {
			if(IS_IE) this.player.URL = url;
			else {
				// Firefox fix (requires command to be executed twice)
				this.player.src = url;
				this.player.src = url;
			}
		}
			
	},	
	
	/**
	 * Gets reference to the player object
	 * @return {Object}	a reference to the media player DOM object
	 */
	getReference : function()
	{
		return this.player;
	},	
	
	/**
	 * Inserts the Media Player object into the webpage
	 * @param {String} id
	 */
	write : function(id)
	{
		var node = document.getElementById(id);
		
		if(node) node.innerHTML += this.getHTML();
		else document.write(this.getHTML());
		
		this.player = document.getElementById(this.id);
		
	},
	
	/**
	 * PRIVATE - add an object parameter to the media player object class
	 * @param {String} name		name of the parameter
	 * @param {String,Boolean,Integer} value	value of the corresponding parameter
	 */
 	addParam : function(name, value)
	{
		this.params[name] = value;
	},
	
	/**
	 * Builds the relavant HTML tags for a valid DOM object
	 * @return {String}	formatted html tags
	 */
 	getHTML : function()
	{
		var ie_html = "";
		var attributes = "";
		
		ie_html += '<object id="'+this.id+'" '+
						'classid="'+this.Version['10.0']+'" '+
						'width="'+this.width+'" '+
						'height="'+this.height+'">';
		
		for(var name in this.params)
		{
			ie_html += '<param name="'+name+'" value="'+this.params[name]+'" />';
			if(name != 'URL')
				attributes += " "+name+'="'+(this.params[name]?1:0)+'"';
			else
				attributes += " "+name+'="'+this.params[name]+'"';				
		}
		ie_html += '</object>';
		
		
		var non_ie_html = '<embed id="'+this.id+'" '+
						'type="application/x-mplayer2" '+ 
						'pluginspage="http://www.microsoft.com/Windows/MediaPlayer/download/default.asp" '+
						'width="'+this.width+'" '+
						'height="'+this.height+'" '+
						attributes+
						'></embed>';		
		
		if(IS_IE) return ie_html;
		
		return non_ie_html;
	},
	
	
	uiModes :
	{
		invisible 	: 'invisible',
		full		: 'full',
		small		: 'small',
		none		: 'none'
	}	
 }

