/**
 * Media Player JavaScript API Wrapper
 * 
 * @author eBridge Learning Solutions Inc.
 * @version 1.7
 * @modified 18-OCT-2009
 */

var IS_IE = navigator.userAgent.match(/Gecko\//)==null;
var oPlayer = null;		// Windows Media Player ActiveX Object
var oPlaylist = null;	// JS Playlist object

/**
 * Creates a media player object
 * 
 * @param {String} url		location of media file
 * @param {String} id		unique HTML id
 * @param {Integer} width	width of media player object 
 * @param {Integer} height	height of mdeia player object
 * @constructor
 */
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.version = '10.0';
	
	this.params = new Array();
	this.player = null;
	
	// Set default options
	this.URL(url);
	//this.AutoStart(true);		// Start video when player loads
	this.StretchToFit(true);	// Stretch video to player window size
}

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',
		'6.0'	: 'clsid:05589FA1-C356-11CE-BF01-00AA0055595A'
	},
	
	/**
	 * Possible play states
	 */
	PlayStates : 
	{
		0	:	'Undefined',
		1	:	'Stopped',
		2	:	'Paused',
		3	:	'Playing',
		4	:	'ScanForward',
		5	:	'ScanReverse',
		6	:	'Buffering',
		7	:	'Waiting',
		8	:	'MediaEnded',
		9	:	'Transitioning',
		10	:	'Ready',
		11	:	'Reconnecting'
	},
	
	
	
	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);		
	},
	
	/**
	 * Prevent the media item from rewinding at the end of the clip  (version 6.4 and earlier only)
	 * @param {Object} bRewind
	 */
	AutoRewind : function(bRewind)
	{
		this.addParam('autoRewind', bRewind);
	},
	
	
	/**
	 * 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)
			{
				if(this.version <= 6.4) this.player.filename=url;
				else 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;
	},
	
	/**
	 * Get the time remaining on the curret media item (version 7.0+)
	 * @return {Float}	time remainging in seconds (-1 if unsupported)
	 */
	getTimeRemaining : function()
	{
		if(this.version >= 7.0)
		{
			try
			{
				return (this.player.currentMedia.duration - this.player.controls.currentPosition);
			}catch(no_current_media_error)
			{
				return 0;
			}
		}
		
		return -1;
	},
	
	
	events :
	{
		bPlayNext : false,
		
		playState : function(newState)
		{
			switch(oPlayer.PlayStates[newState])
			{
				case 'Playing':	
					//alert("Playing...");
					break;
			
				case 'Paused':
					//alert("Paused!");
					break;
				
				case 'MediaEnded':
					this.bPlayNext = oPlaylist.isSuccessive();
					break;
					
				case 'Stopped':
					if(this.bPlayNext)
					{
						this.bPlayNext=false;
						
						setTimeout(function(){oPlaylist.next();},100);
					}
					
					break;
				case 'Ready':
					
					break;
			}
			window.status += oPlayer.PlayStates[newState] + " ";
			//alert('next');
		},
		
		openState : function(newState)
		{
			//alert(newState);
			//if(newState == 12) oPlayer.player.controls.play();
		},
		
		positionChange : function(oldPosition, newPosition)
		{
			window.status="Old: "+oldPosition+" | New: "+newPosition;
		}
	},
		
	
	/**
	 * 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);
		
		this.player.attachEvent('playStateChange', this.events.playState);
		this.player.attachEvent('openStateChange', this.events.openState);
		this.player.attachEvent('positionChange', this.events.positionChange);
		
		
	},
	
	/**
	 * PRIVATE - add/update 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[this.version]+'" '+
						'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'
	}	
}
