// ************************************************************************************************
// *** Constructs the ig player object (It wraps the general player object)
// ************************************************************************************************

function PlayerEmbeded(playerName)
{
	// ************************************************************************************************
	// *** Interface
	// ************************************************************************************************	
	
	// Fields
	// ******
	this.playerName = playerName;
	this.positionSlider = null;
	
	// Methods
	// *******
	this.fullScreen = player_FullScreen;
	this.next = player_Next;
	this.play = player_Play;
	this.playItem = player_PlayItem;
	this.previous = player_Previous;
	this.getCurrentMediaId = player_GetCurrentMediaId;
	this.getCurrentPlaylist = player_GetCurrentPlaylist;
	this.setCurrentPlaylist = player_SetCurrentPlaylist;
	this.switchMute = player_SwitchMute;
	//this.switchQuality = player_SwitchQuality;
	this.syncVolume = player_SyncVolume;
	this.updatePositionSlider = player_UpdatePositionSlider;

	// ************************************************************************************************
	// *** Implementation
	// ************************************************************************************************
	
	// Fields
	// ******
	//this.cookieManager = new CookieManager('megaplayer_session', null, window.location.toString().match('^(http://)?([^/]+)')[2]);
	this.currentPlaylist = null;
	this.player = new PlayerWm(this.playerName + '.player', document.getElementById('playerObj'));
	this.playState = new PlayState();
	this.quality = new Quality();
	this.tools = new Tools();
	
	// Methods
	// *******
	this.setVolume = player_SetVolume;
	this.volumeUp = player_volumeUp;
	this.volumeDown = player_volumeDown;
	this.setPosition = player_SetPosition;
	this.updateMute = player_UpdateMute;
	this.updateStatusMessage = player_UpdateStatusMessage;
	this.updateTimeInfo = player_UpdateTimeInfo;	
	this.updateBanner = player_UpdateBanner;
	this.updatePlayPauseButton = player_UpdatePlayPauseButton;
	
	// Bind event handlers
	// *******************	
	this.player.onCurrentMediaChange.add(this.playerName, 'updateTimeInfo');
	this.player.onCurrentMediaChange.add(this.playerName, 'updateBanner');
	this.player.onCurrentPositionChange.add(this.playerName, 'updateTimeInfo');
	this.player.onCurrentPositionChange.add(this.playerName, 'updatePositionSlider');
	this.player.onMuteChange.add(this.playerName, 'updateMute');
	this.player.onPlayerStateStringChange.add(this.playerName, 'updateStatusMessage');
	this.player.onPlayStateChange.add(this.playerName, 'updatePlayPauseButton');
	
	// Forces default quality as high
	this.player.setQuality(this.quality.high);
	//var currentQuality = this.player.getQuality();
	//this.player.setQuality(currentQuality == this.quality.high) ? this.quality.low : this.quality.high);
	
	//this.player.setQuality(this.quality.high);
	//alert(currentQuality);
	
	// Enable full screen mode
	function player_FullScreen() {
		this.player.fullScreen();
	}
	
	// Plays the next playlist item
	function player_Next() {
		this.player.next();
	}
	
	// Plays a specific playlist item
	function player_PlayItem(index) {
		this.player.playItem(index);
	}
	
	// Plays the current media item
	function player_Play()
	{
		if(this.player.getPlayState() == this.playState.playing)
		{
			this.player.pause();
		}
		else
		{
			this.player.play();
			if(! started)
			{
				startPlayer();
			}
		}
	}
	
	// Plays the previous playlist item
	function player_Previous() {
		this.player.previous();
	}
	
	// Gets current playlist
	function player_GetCurrentPlaylist() {
		return this.currentPlaylist;
	}
	
	// Sets current playlist
	function player_SetCurrentPlaylist(pl) {
		this.currentPlaylist = pl;
		this.player.setCurrentPlaylist(pl.playlist);
	}
	
	// Switches the mute flag
	function player_SwitchMute() {
		var bMute = this.player.getMute();
		
		// Set mute player mute state
		this.player.setMute(!bMute);
	}
	
	// Switches the quality
	/*function player_SwitchQuality() {
		
		// Gets the previous state
		var state = this.player.getPlayState();
		
		// Gets the previous position
		var pos = -1;
		var currentMedia = this.player.getCurrentMedia();
		var currentPlaylist = this.player.getCurrentPlaylist();

		// Set the current media item in the playlist
		if(currentMedia != null && currentPlaylist != null)
			pos = currentPlaylist.indexOf(currentMedia);
	
		var currentQuality = this.player.getQuality();
		var imgObj = document.getElementById('qualityImg');

		// Change image
		if(imgObj != null)
			imgObj.src = (currentQuality == this.quality.high) ? 'image/labelBroadband.gif' : 'image/labelDialUp.gif';		
			
		// Set quality
		this.player.setQuality((currentQuality == this.quality.high) ? this.quality.low : this.quality.high);
		
		// Starts playback, if necessary
		if(pos>=0 && state == this.playState.playing)
			this.player.playItem(pos);
	}*/
	
	// Sets the current position
	function player_SetPosition() {		
		this.player.setPosition(this.positionSlider.getValue() * this.player.getCurrentMediaDuration());
	}
	
	// Sets the player's volume
	function player_SetVolume() {
		this.player.setVolume(this.volumeSlider.getValue() * 100);
	}
	
	// Sets status message
	function player_UpdateStatusMessage() {
		var statusTag = document.getElementById('playerStatus');
		
		if(statusTag != null)
			statusTag.innerHTML = this.player.getStatus();		
	}
	
	// Updates time info
	function player_UpdateTimeInfo() {
		var timeInfoTag = document.getElementById('playerTimeInfo');
		var duration = this.player.getCurrentMediaDuration();
		var currentPosition = (!this.player.getPosition()) ? 0 : this.player.getPosition();
		
		if(timeInfoTag != null)
			timeInfoTag.innerHTML = this.tools.formatSeconds(currentPosition) + ' | ' + this.tools.formatSeconds(duration);
	}
	
	// Syncronizes the slider with player volume
	function player_SyncVolume() {
		this.volumeSlider.setValue(this.player.getVolume() / 100);
	}
	
	// Update position slider
	function player_UpdatePositionSlider() {
		var currentMediaDuration;
		
		// Move the position slider only if it is not being dragged
		if(!this.positionSlider.isDragging) {
			currentMediaDuration = this.player.getCurrentMediaDuration();
			this.positionSlider.setValue((currentMediaDuration > 0) ? this.player.getPosition() / currentMediaDuration : 0);
		}
	}
	
	function player_volumeDown()
	{
		this.player.setVolume(this.player.getVolume() - 14);
		update_volume(this.player.getVolume());
	}
	
	function player_volumeUp()
	{
		if(this.player.getVolume() < 98)
		{
			this.player.setVolume(this.player.getVolume() + 14);
		}
		update_volume(this.player.getVolume());
	}
	function update_volume(volume)
	{
		volume_width = volume / 2;
		//document.getElementById("output").innerHTML = volume_width;
		document.getElementById("volume_full").style.width = volume_width + "px";		
	}
	
	// Update mute button
	function player_UpdateMute() {
		var obj;
		var bMute = this.player.getMute();
		
		// Get the volume table object
		obj = document.getElementById('btnMute');
		
		// Set the image		
		if(obj != null)
			obj.className = bMute ? 'muteOn' : 'muteOff';
	}
	
	// Update play/pause button
	function player_UpdatePlayPauseButton() {
		var playState = this.player.getPlayState();
		var imgObj = document.getElementById('imgPlayPause');
		
		if(imgObj == null) // If we cannot find the play/pause image, do nothing
			return;
		
		if(playState != this.playState.playing && imgObj.src.indexOf('image/buttonPlay.gif') == -1)
			imgObj.src = 'image/buttonPlay.gif';
		else if(playState == this.playState.playing && imgObj.src.indexOf('image/buttonPause.gif') == -1)
			imgObj.src = 'image/buttonPause.gif';
	}	
	
	function player_GetCurrentMediaId()
	{
		
		var currentMedia = this.player.getCurrentMedia();
		if(currentMedia)
		{
			return(currentMedia.contentId);
		}
		return(0);
	}
	
	// Updates banner
	function player_UpdateBanner() 
	{
		if(this.player)
		{
			var currentMedia = this.player.getCurrentMedia();
			if(currentMedia)
			{
				if(currentMedia.bannerUrl)
				{
					var frameAds = document.getElementById('frameAds');
					if(frameAds)
					{
						if(frameAds.src!=currentMedia.bannerUrl)
						{
							frameAds.src=currentMedia.bannerUrl;
						}
					}
				}
			}
		}
	}
}