/**
 * A JS representation of the JWPlayer
 * 
 * @author jmutsaerts <jmutsaerts@zigwebsoftware.nl>
 * @version 1.0 <jmutsaerts@zigwebsoftware.nl>
 * @since 27 mei 2010 14:12:29
 * @copyright Copyright (c) 2010, Zig Websoftware
 */
var JWPlayer = Class.create
({
	// Constants for possible states
	STATE_BUFFERING: 'BUFFERING',
	STATE_PLAYING: 'PLAYING',
	STATE_IDLE: 'IDLE',
	STATE_COMPLETED: 'COMPLETED',
	
	// Constants for possible commands
	COMMAND_PLAY: 'PLAY',	
	COMMAND_PLAY_NEXT: 'NEXT',	
	COMMAND_PLAY_PREVIOUS: 'PREV',	
	COMMAND_STOP: 'STOP',
	COMMAND_SET_VOLUME: 'VOLUME',
	COMMAND_SET_CURRENT_ITEM: 'ITEM',
	COMMAND_ADD_ITEM: 'LOAD',
	COMMAND_SEEK: 'SEEK',
	COMMAND_MUTE: 'MUTE',
	
	// Model events
	EVENT_MODEL_BUFFER: 'BUFFER',
	EVENT_MODEL_ERROR: 'ERROR',
	EVENT_MODEL_LOADED: 'LOADED',
	EVENT_MODEL_META: 'META',
	EVENT_MODEL_STATE: 'STATE',
	EVENT_MODEL_TIME: 'TIME',
	
	// View events
	EVENT_VIEW_FULLSCREEN: 'FULLSCREEN',
	EVENT_VIEW_ITEM: 'ITEM',
	EVENT_VIEW_LINK: 'LINK',
	EVENT_VIEW_LOAD: 'LOAD',
	EVENT_VIEW_MUTE: 'MUTE',
	EVENT_VIEW_NEXT: 'NEXT',
	EVENT_VIEW_PLAY: 'PLAY',
	EVENT_VIEW_PREV: 'PREV',
	EVENT_VIEW_REDRAW: 'REDRAW',
	EVENT_VIEW_SEEK: 'SEEK',
	EVENT_VIEW_STOP: 'STOP',
	EVENT_VIEW_TRACE: 'TRACE',
	EVENT_VIEW_VOLUME: 'VOLUME',
	
	// Controller events
	EVENT_CONTROLLER_ERROR: 'ERROR',
	EVENT_CONTROLLER_ITEM: 'ITEM',
	EVENT_CONTROLLER_MUTE: 'MUTE',
	EVENT_CONTROLLER_PLAY: 'PLAY',
	EVENT_CONTROLLER_PLAYLIST: 'PLAYLIST',
	EVENT_CONTROLLER_RESIZE: 'RESIZE',
	EVENT_CONTROLLER_SEEK: 'SEEK',
	EVENT_CONTROLLER_STOP: 'STOP',
	EVENT_CONTROLLER_VOLUME: 'VOLUME',
	
	/**
	 * Contains the DOM element of the Flash Movie
	 * @var HTMLElement
	 */
	_oElement: null,
	
	/**
	 * All video descriptions in the order of the playlist
	 * @var Array
	 */
	_aDescriptions: null,
	
	/**
	 * DOM Element to show description in (in innerHTML)
	 * @var HTMLElement
	 */
	_oDescriptionElement: null,
	
	/**
	 * Current state of the player
	 * @var string
	 */
	state: this.STATE_IDLE,
	
	/**
	 * Item that is currently played
	 * @var int
	 */
	item: null,

	/**
	 * @param string	sPlayerID				ID of the DOM element of the Flash Movie
	 * @param string	sInstanceName			Name of instance i.e. 'oPlayer' in var oPlayer = new JWPlayer(...
	 * @param Array		aDescriptions			All video descriptions in the order of the playlist
	 * @param int		sDescriptionElementID	ID of the DOM element that displays video descriptions
	 */
	initialize: function(sPlayerID, sInstanceName, aDescriptions, sDescriptionElementID)
	{
		this._oElement = $(sPlayerID);
		this._oDescriptionElement = $(sDescriptionElementID);
		this._aDescriptions = aDescriptions;
		
		if(sInstanceName)
		{
			sInstanceName += '.';
		}
		
		this._oElement.addModelListener(this.EVENT_MODEL_STATE, sInstanceName + 'stateListener');
		this._oElement.addViewListener(this.EVENT_VIEW_LINK, sInstanceName + 'viewListener');
		this._oElement.addControllerListener(this.EVENT_CONTROLLER_ITEM, sInstanceName + 'controllerListener');
	},
	
	/**
	 * @return void
	 */
	play: function()
	{
		this.sendCommand(this.COMMAND_PLAY);
	},
	
	/**
	 * @return void
	 */
	playNext: function()
	{
		this.sendCommand(this.COMMAND_PLAY_NEXT);
	},
	
	/**
	 * @return void
	 */
	playPrev: function()
	{
		this.sendCommand(this.COMMAND_PLAY_PREVIOUS);
	},
	
	/**
	 * @return void
	 */
	playItem: function(iNumber)
	{
		this.sendCommand(this.COMMAND_SET_CURRENT_ITEM, iNumber);
	},
	
	/**
	 * @return void
	 */
	stop: function()
	{
		this.sendCommand(this.COMMAND_STOP)
	},
	
	/**
	 * @return void
	 */
	seek: function(iPosition)
	{
		this.sendCommand(this.COMMAND_SEEK, iPosition)
	},
	
	addItem: function(sURL)
	{
		this.sendCommand(this.COMMAND_ADD_ITEM, sURL)
	},
	
	/**
	 * @return void
	 */
	setVolume: function(iVolume)
	{
		this.sendCommand(this.COMMAND_SET_VOLUME, iVolume)
	},

	/**
	 * @return void
	 */
	mute: function()
	{
		this.sendCommand(this.COMMAND_MUTE)
	},
	
	/**
	 * Send an event to the DOM element (handled in Flash JWPlayer) to request a command 
	 * @param string	sCommand	Command name
	 * @param mixed		mValue		Value if command takes one (setters)
	 * @return void
	 */
	sendCommand: function(sCommand, mValue)
	{		
		this._oElement.sendEvent(sCommand, mValue)
	},

	/**
	 * @return void
	 */
	updateDescription: function()
	{
		if(this._oDescriptionElement)
		{
			this._oDescriptionElement.innerHTML = this._aDescriptions[this.item];
		}
	},
	
	/**
	 * @param object oStateInfo
	 * @return void
	 */
	stateListener: function(oStateInfo)
	{
		this.state = oStateInfo.newstate;
		switch(this.state)
		{
			case this.STATE_COMPLETED:
				this.playNext();				
			break;
		}
		this.updateDescription();
	},
	
	/**
	 * @param object oModelInfo
	 * @return void
	 */
	modelListener: function(oModelInfo)
	{
		this.updateDescription();		
	},
	
	/**
	 * @param object oViewInfo
	 * @return void
	 */
	viewListener: function(oViewInfo)
	{
		this.updateDescription();
	},
	
	/**
	 * @param object oControllerInfo
	 * @return void
	 */
	controllerListener: function(oControllerInfo)
	{	
		if(this.item !== oControllerInfo.index)
		{
			this.item = oControllerInfo.index;
			this.updateDescription();
		}
	}
});




