/**
 * Class MediaCollection
 * 
 * Every player instance uses one mediaCollection (virdeo or audio, isLive, isGeoblocked. 
 * A media collection has several media objects for the media available (flash, silverlight, wm).
 * Each media has several streams in different quality (s, m, l). 
 * 
 * The underscore is used to declare classes, attributes and methods 
 * as private by naming convention.
 * 
 * Created:		2009_09-25	
 * Modified:	2009_09-28
 *
 * @version 0.1
 * @author Jan V�lker
 * @copyright ARD.de
 */

/** 
 * Class constructor
 * @access public
 * @param string Type video or audio
 * @param boolean The mediaCollection is a live stream
 * @param boolean The mediaCollection is geoblocked 
 * @return Media
 */
var MediaCollection = function(type, isLive, isGeoblocked) {
	this._type = type;
	this._isLive = isLive;
	this._isGeoblocked = isGeoblocked;
	
	this._download = false;
	this._podcast = false;
	this._audioImage = "";
	this._previewImage = "";
	this._subtitleUrl = "";

	this._mediaArray = new Array(false, false, false);
	this._pluginArray = new Array(false, false, false);
};

MediaCollection.prototype = {
	/** 
	 * Add a media object to the mediaCollection
	 * @access public
	 * @param string Plugin 0 - flash, 1 - silverlight, 3 - windows media
	 * @return MediaCollection
	 */
	addMedia: function (plugin) {
		this._mediaArray[plugin] = new Media(plugin);
		return this;
	},
	/** 
	 * Add a mediaStream object to a media mediaObject
	 * @access public
	 * @param string Plugin 0 - flash, 1 - silverlight, 3 - windows media
	 * @param string Quality of the stream 0 - small, 1 - medium or 2 - large
	 * @param string Streamingserver URL
	 * @param string Stream location
	 * @return MediaCollection
	 */
	addMediaStream: function (plugin, quality, server, stream) {
		mediaStream = new MediaStream(quality, server, stream);
		this._mediaArray[plugin].addMediaStream(mediaStream);
		return this;
	},
	/** 
	 * Enables a plugin for a media
	 * The pluginDetection enables a plugin if client has a plugin installed
	 * and if the media source is offerd for this plugin
	 * @access public
	 * @param string Plugin 0 - flash, 1 - silverlight, 3 - windows media
	 * @param string Version of the plugin
	 * @return void
	 */
	enablePlugin: function(plugin, version) {
		this._pluginArray[plugin] = version;
	},
	/**
	 * Getter for a plugin by name
	 * @access public
	 * @param string Plugin 0 - flash, 1 - silverlight, 3 - windows media
	 * @return boolean/string Plugin found/plugin version
	 */
	getMediaAndPluginAvailableArray: function() {
		return this._pluginArray;
	},
	/** 
	 * Getter for the media object my pluginName
	 * @access public
	 * @param string Plugin 0 - flash, 1 - silverlight, 3 - windows media
	 * @return Media
	 */
	getMediaArray: function() {
		return this._mediaArray;
	},
	/** 
	 * Returns the best media number
	 * 1. Flash, 2. Silverlight, 3. Window Media Plugin
	 * @access public
	 * @param void
	 * @return number Nubmer of the Arrayfield
	 */
	getBestMediaNumber: function() {
		var mediaAndPluginAvailableArray = this.getMediaAndPluginAvailableArray();
		for (i in mediaAndPluginAvailableArray) {
			if (mediaAndPluginAvailableArray[i] != false) {
				bestMediaNumber = Number(i);
				break;
			}
		}
		return bestMediaNumber;
	},
	/** 
	 * Returns the best mediaStream number
	 * 1. m-quality, 2. s-quality / Fallback: 3. hq-quality, 4. xq-quality
	 * @access public
	 * @param string Plugin 0 - flash, 1 - silverlight, 3 - windows media
	 * @return number Number of the Arrayfield
	 */
	getBestStreamNumber: function(plugin) {
		var mediaStreamArray = this.getMediaArray()[plugin].getMediaStreamArray();
		if (mediaStreamArray[1] != false) {
			bestStreamNumber = 1;
		}
		else {
			if (mediaStreamArray[0] != false) {
				bestStreamNumber = 0;
			}
			else
			{
				// If only HQ or XQ Media is available, play this in the small player
				// this is only to prevent exception, if only hq or xq is available, but 
				// no other quality is defined
				if (mediaStreamArray[2] != false) {
					bestStreamNumber = 2;
				} 
				else 
				{
					if (mediaStreamArray[3] != false) {
						bestStreamNumber = 3;
					}
				}
			}
		}
		return bestStreamNumber;
	},
	/** 
	 * Returns the recommended media number, if  the client must install a plugin
	 * 1. Flash 0, 2. Silverlight 1, 3. Window Media Plugin 2
	 * @access public
	 * @param void
	 * @return number Number of the Arrayfield
	 */
	getRecommendedStreamNumber: function() {
		for (i in this._mediaArray) {
			if (this._mediaArray[i] != false) {
				recommendedStreamNumber = Number(i);
				break;
			}
			else {
				recommendedStreamNumber = 0;
			}
		}
		return recommendedStreamNumber;
	},
	/** 
	 * Set a download url
	 * @access public
	 * @param string URL
	 * @return MediaCollection
	 */
	setDownload: function(url) {
		this._download = url;
		return this;
	},
	/**
	 * Getter for the download url
	 * @access public
	 * @param void
	 * @return String download url
	 */
	getDownload: function() {
		return this._download;
	},
	/** 
	 * Set a podcast url
	 * @access public
	 * @param string URL
	 * @return MediaCollection
	 */
	setPodcast: function(url) {
		this._podcast = url;
		return this;
	},
	/**
	 * Getter for the podcast url
	 * @access public
	 * @param void
	 * @return String podcast url
	 */
	getPodcast: function() {
		return this._podcast;
	},
	/** 
	 * Set a audio image url
	 * @access public
	 * @param string URL
	 * @return MediaCollection
	 */
	setAudioImage: function(url) {
		this._audioImage = url;
		return this;
	},
	/**
	 * Getter for the audio image url
	 * @access public
	 * @param void
	 * @return String podcast url
	 */
	getAudioImage: function() {
		return this._audioImage;
	},
	/** 
	 * Set a image url
	 * @access public
	 * @param string URL
	 * @return MediaCollection
	 */
	setPreviewImage: function(url) {
		this._previewImage = url;
		return this;
	},
	/**
	 * Getter for the image url
	 * @access public
	 * @param void
	 * @return String podcast url
	 */
	getPreviewImage: function() {
		return this._previewImage;
	},
	
	/** 
	 * Set a subtitle url to a subtitle file
	 * @access public
	 * @param string URL
	 * @return MediaCollection
	 */
	setSubtitleUrl: function(subtitleUrl) {
		this._subtitleUrl = subtitleUrl;
		return this;
	},
	/**
	 * Getter for the subtitle url
	 * @access public
	 * @param void
	 * @return String subtitle url
	 */
	getSubtitleUrl: function() {
		return this._subtitleUrl;
	},
	
	/**
	 * Getter for the type
	 * @access public
	 * @param void
	 * @return String video or audio
	 */
	getType: function() {
		return this._type;
	},
	
	/**
	 * Getter for the isLive status
	 * @access public
	 * @param void
	 * @return Boolean
	 */
	getIsLive: function() {
		return this._isLive;
	}
};
