/**
 * Class Player
 * 
 * Uses the serverGeneratedUniqueDivId and the mediaCollection to start a player instance.
 * 
 * According to the media inside the mediaCollection the class checks which plugins and
 * media is available. The DOM is also configured according to this, using the domConfiguration
 * object (also adds playerformats and mediaquality to the control panel)
 * 
 * The player tries to use the the plugins in this hierarchy:
 * 1. flash
 * 2. silverlight
 * 3. windows media plugin
 * 
 * The prefs can be safed via session cookie. The cookie prefs are used, if available.
 * Else the best prefs will be choosen automatically.
 * 
 * The underscore is used to declare classes, attributes and methods 
 * as private by naming convention.
 * 
 * Created:		2009_10-01	
 * Modified:	2009_10-01
 *
 * @version 0.1
 * @author Jan V�lker
 * @copyright ARD.de
 */

/** 
 * Class constructor
 * @access public
 * @param string Player div id
 * @param object MediaCollection
 * @return Player
 */
var Player = function(playerDivId, width, height, mc) {
	this._id = playerDivId;
	this._mc = mc;
	this._pluginDetection = new PluginDetection(mc);
	// Object for player configuration
	this._pc = new Object();
	this._pc.width = width;
	this._pc.height = height;
	this._pc.hqWidth = 960;
	this._pc.hqHeight = 544;
	this._pc.xqWidth = 1280;
	this._pc.xqHeight = 720;
	this._pc.startTime = 0;
	this._pc.endTime = 0;
	this._pc.autoplay = "AUTOPLAY" // for no autoplay choose "NOTHING"
	this._pc.options = true;
	this._pc.rememberTime = true;
	
	this._pc.currentPlugin = false;
	this._pc.currentQuality = false;
	
	/** 
	 * Set the currentPlugin and currentQuality variables
	 * @access public
	 * @param Number Plugin 0 - flash, 1 - silverlight, 3 - windows media
	 * @param Number Quality of the stream 0 - small or 1 - medium
	 * @return MediaCollection
	 */
	this._pc.setCurrentPlaybackPrefs = function(plugin, quality) {
		this.currentPlugin = plugin;
		this.currentQuality = quality;
	};
	this._pc.getCurrentPlugin = function() {
		return this.currentPlugin;
	};
	this._pc.getCurrentQuality = function() {
		return this.currentQuality;
	};
};
	
Player.prototype = {
	/** 
	 * Init the player
	 * @access public
	 * @param void
	 * @return void
	 */
	init: function() {
		var id = this._id;
		var mc = this._mc;
		if (this._pluginDetection.pluginFound() == true) {
			var pc = this._pc;
			var currentPlayer = false;
			var currentStream = false;
			
			// Add the inital DOM structure for a player
			$j().addPlayerDiv(id);
			if(pc.options == true)
			{
				$j().addControlPanelDiv(id);
				$j().addButtonDiv(id);
				$j().addHqVideoDiv(id);
			}
				
			// addFunction so save and to delete the saved prefs (set a session cookie when clicked or delete)
			$j().addSaveFunction(id, pc);
			$j().addDeleteSaveFunction(id, mc, pc);
			
			// Show the buttons for download and podcast
			var download = mc.getDownload();
			var podcast = mc.getPodcast();
			if(download != false) {
				$j().showDownloadButton(id, download);
			}
			if(podcast != false) {
				$j().showPodcastButton(id, podcast);
			}
			
			// check if cookie is set and the prefs are available
			if($j().checkCookiePrefAvailable(mc)) {
				currentPlayer = $j().getCookiePlugin();
				currentStream = $j().getCookieQuality();
			}
			// No cookie set
			else {
				// Select the default player
				currentPlayer = mc.getBestMediaNumber();
				// Select default media
				currentStream = mc.getBestStreamNumber(currentPlayer);
			}
			
			// show buttons available and add the click functions
			$j().showFormatButtonsAvailable(id, pc, mc);
			$j().showQualityButtonsAvailable(id, currentPlayer, pc, mc);
			$j().addHqCloseClickFunctions(id, currentStream, currentPlayer, pc, mc);
			// select the format and quality buttons the inital player is using
			$j().selectFormatButton(id, currentPlayer);
			$j().selectQualityButton(id, currentStream);
			
			// Start Player
			$j().injectPlayer(id, currentPlayer, currentStream, pc, mc, 0);	
		}
		else {
			$j().showErrorMsg(id, mc.getRecommendedStreamNumber());
			//alert("No or no usable plugin found.");
		}	
	},
	
	/** 
	 * Set a start and end time for the playback
	 * @access public
	 * @param number Start after n seconds (0 for normal start)
	 * @param number Stop video after n seconds (0 for normal end)
	 * @return void
	 */
	setStartStopTime: function(start, stop) {
		this._pc.startTime = start;
		this._pc.endTime = stop;
	},
	
	/** 
	 * Set the width and height for the HQ-Layer
	 * @access public
	 * @param number Width
	 * @param number Height
	 * @return void
	 */
	setHqDimensions: function(width, height) {
		this._pc.hqWidth = width;
		this._pc.hqHeight = height;
	},
	
	/** 
	 * Set the width and height for the XQ-Layer
	 * @access public
	 * @param number Width
	 * @param number Height
	 * @return void
	 */
	setXqDimensions: function(width, height) {
		this._pc.xqWidth = width;
		this._pc.xqHeight = height;
	},
	
	/** 
	 * Get the div id, where the player is injected
	 * @access public
	 * @param void
	 * @return String Id of the div
	 */
	getId: function() {
		return this._id;
	},
	
	/** 
	 * Prevents the Options Layer for the player
	 * @access public
	 * @param void 
	 * @return void
	 */
	dontShowOptions: function() 
	{
		this._pc.options = false;
	},
	
	/** 
	 * Prevents that current play time is remebered on quality switch
	 * @access public
	 * @param void 
	 * @return void
	 */
	dontRememberCurrentTime: function() 
	{
		this._pc.rememberTime = false;
	},
	
	/** 
	 * Set the current play time.
	 * The function only works, if the "dontRememberCurrentTime" is 
	 * NOT used.
	 * @access public
	 * @param number The current time in sconds 
	 * @return void
	 */
	setCurrentTime: function(time) 
	{
		//global Array holding current times of several players
		playerIdCurrentTimeArray[this._id] = time;
	},
	/** 
	 * Get the current play time.
	 * @access public
	 * @param void 
	 * @return void
	 */
	getCurrentTime: function() 
	{
		//global Array holding current times of several players
		return playerIdCurrentTimeArray[this._id];
	},
	
	/** 
	 * Prevents the player to start onload
	 * @access public
	 * @param void 
	 * @return void
	 */
	dontAutoPlay: function()
	{
		this._pc.autoplay = "NOTHING";
	}
};
