// ************************************************************************************************
// *** Player Object.
// ************************************************************************************************

function PlayerVideo(objName, windowID, context)
{
	// ************************************************************************************************
	// *** Fields ***
	// ************************************************************************************************
	this.objName = objName;
	this.playerType = new PlayerType(); 
	this.player = null; //variable to store current player	
	this.windowID = windowID; //variable to store object ID	
	this.playState = new PlayState(); //enumeration with player play state
	this.tools = new FormatTools();	
	this.playerFW = null;
	this.context = context; //variable to store deployment context
	
	//this.type = null; 
	
	//Configuration parameters
	this.width 			= 320; 
	this.height 		= 280;	
	this.autostart 		= new Boolean(false);
	this.repeat 		= new Boolean(false);
	this.mute 			= new Boolean(false);
	this.volume			= 50;
	this.idVideo = 0;
	
	this.updateProgressBar = null;
	this.updateControllBar = null;

	this.videoStart = 0;
	this.videoPosition = 0;	
	this.videoSize = -1; //Utilizzato per il timeshifting: -1 streaming normale // != -1 impostata la durata del video
	
	this.videoTimeStart = ""; //Utilizzato per il timeshifting 
	
	this.imgPath = "";
	this.streamingElement = ""; //Elemento grafico per l'immagine di waiting
	
	this.selectedButton	= null;	
	
	this.manualStop = true;
	
	// *** interface fields ***

	this.type = null; 
	
	
	
	
	// ************************************************************************************************
	// *** Methods ***
	// ************************************************************************************************
	
	this.setConfiguration = player_SetConfiguration; 
	this.getConfiguration = player_GetConfiguration; 
/*	
	this.setSource = player_SetSource;
	this.getSource = player_GetSource;
	
	this.play   = player_Play;
	this.stop   = player_Stop;
	this.pause  = player_Pause;
	
	this.fullscreen = player_FullScreen;
	
    this.setMute      = player_SetMute;
	this.getMuteState = player_GetMuteState;
	
	this.setVolume = player_SetVolume;
	
	this.increaseVolume = player_IncreaseVolume;
	this.decreaseVolume = player_DecreaseVolume;
	
	this.getCurrentMediaDuration = player_GetCurrentMediaDuration;
	this.setPosition = player_SetPosition;
	this.getPosition = player_GetPosition;
*/
	this.loadPlayerObjectByType = player_LoadPlayerObject;
		
	this.loadWindowsMediaPlayerObject = player_LoadWindowsMediaPlayerObject;
	this.drawWindowsMedia = player_DrawWindowsMedia;
	
	this.loadRealPlayerObject = player_LoadRealPlayerObject;
	this.drawReal = player_DrawReal;
	
	this.loadJWPlayerObject = player_LoadJWPlayerObject;
	this.drawJW = player_DrawJW;
	
	//this.tick = player_Tick;
	//this.manualStop = true;
	
	//this.getPlayState = player_GetPlayState;
	
/*	
	this.videoStart = 0;
	this.videoPosition = 0;	
	this.videoSize = -1; //Utilizzato per il timeshifting: -1 streaming normale // != -1 impostata la durata del video
	
	this.videoTimeStart = ""; //Utilizzato per il timeshifting 
	
	this.imgPath = "";
	this.streamingElement = ""; //Elemento grafico per l'immagine di waiting
	
	this.selectedButton	= null;		
*/
	
	
	// *** interface methods
	
	this.setSource = player_SetSource;
	this.getSource = player_GetSource;
	
	this.play   = player_Play;
	this.stop   = player_Stop;
	this.pause  = player_Pause;
	
	this.fullscreen = player_FullScreen;
	
    this.setMute      = player_SetMute;
	this.getMuteState = player_GetMuteState;
	
	this.setVolume = player_SetVolume;
	
	this.increaseVolume = player_IncreaseVolume;
	this.decreaseVolume = player_DecreaseVolume;
	
	this.getCurrentMediaDuration = player_GetCurrentMediaDuration;
	this.setPosition = player_SetPosition;
	this.getPosition = player_GetPosition;	
	
	this.getPlayState = player_GetPlayState;
	
	/*
	this.getCDNConf = player_GetCDNConf;
	this.setCDNConf = player_SetCDNConf;
	*/
	
	//*************************************************************************************************
	//*** Events
	//*************************************************************************************************
	
	// *** interface events*** 
	this.tick = player_Tick;
	
	
	// ************************************************************************************************
	// *** Methods implementation
	// ************************************************************************************************		
	
	function player_SetConfiguration(configuration)	
	{				
	  	  	
	  this.width 			= configuration['videoWidth'];
	  this.height 			= configuration['videoHeight'];
	  this.autostart 		= configuration['videoAutostart'];;
	  this.repeat 			= configuration['videoRepeat'];
	  this.mute 			= configuration['videoMuting'];	  
	  this.volume			= configuration['volume'];
	  this.videoStart   	= configuration['timeCode'];
	  this.imgPath			= configuration['imgPath'];
	  this.streamingElement	= configuration['streamingElement'];
	  this.idVideo = configuration['idVideo'];
	  // TODO perchè mancano gli altri campi  ?
	  if(this.autostart)
		  this.manualStop = false;	  

	  
	}
		
	function player_GetConfiguration()
	{
	  var cfg = {                
                width:this.width,
                height:this.height,                
                autostart:this.autostart,
                repeat:this.repeat,
                mute:this.mute,   
          	  	idVideo:this.idVideo
          	  	// TODO perchè mancano gli altri campi  ?
              };
	  return cfg;
	}

	
	function player_LoadPlayerObject(playerTypeVar)
	{		
		switch(playerTypeVar)
		{
			case this.playerType.WindowsMedia:
				return this.loadWindowsMediaPlayerObject("");
			case this.playerType.Real:
				  return this.loadRealPlayerObject("");				  
			case this.playerType.JWPlayer:				
				return this.loadJWPlayerObject();
		}
	}
	
	function player_LoadWindowsMediaPlayerObject(source)
	{			
		//Element.update("playerObject_"+ this.windowID, this.drawWindowsMedia());
		//document.getElementById("playerObject_"+ this.windowID).innerHTML = this.drawWindowsMedia(source);
		
		if (navigator.userAgent.indexOf('MSIE') == -1){ 			
			var $jWmPlayer = jQuery.noConflict();		
			$jWmPlayer("div#playerObject_"+ this.windowID).html(this.drawWindowsMedia(source));
		}
		else{
			document.getElementById("playerObject_"+ this.windowID).innerHTML = this.drawWindowsMedia(source);
		}
		
		var wmPlayer = document.getElementById("playerWindowsMedia" + this.windowID);
		this.player = null;
		this.player = new PlayerWm("wmPlayer", wmPlayer);	
		this.type = this.playerType.WindowsMedia;
				
		this.playerJW = null;
		
		return (this.player);
	}
		
	function player_DrawWindowsMedia(source)
	{    
		var playerHtml = "";
		if (navigator.userAgent.indexOf('MSIE') != -1) 			
			playerHtml = '<OBJECT id=\"playerWindowsMedia' + this.windowID + '\" name=\"playerWindowsMedia' + this.windowID + '\" CLASSID=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" width=\"' + this.width + '\" height=\"' + this.height + '\">\n';
		else
			playerHtml = '<OBJECT id=\"playerWindowsMedia' + this.windowID + '\" name=\"playerWindowsMedia' + this.windowID + '\" type=\"application/x-ms-wmp\" width=\"' + this.width + '\" height=\"' + this.height + '\">\n';		
		playerHtml += '<param name=\"URL\" value=\"' + source + '\">\n';
		playerHtml += '<param name=\"AutoStart\" value=\"' + this.autostart + '\">\n';
		playerHtml += '<param name=\"TransparentAtStart\" value=\"0\">\n';
		playerHtml += '<param name=\"ShowStatusBar\" value=\"0\">\n';
		playerHtml += '<param name=\"ShowDisplay\" value=\"0\">\n';		
		//playerHtml += '<param name=\"windowlessVideo\" value=\"1\">\n';
		 
			
		if(this.mute)
		{		  
		  playerHtml += '<param name=\"Volume\" value=\"0\">\n';		  
		}
		else
		{
		  playerHtml += '<param name=\"Volume\" value=\"'+this.volume+'\">\n';		 
		}
		
		if(this.repeat)
		  playerHtml += '<param name=\"PlayCount\" value=\"10000\">\n';  
		
		playerHtml += '<param name=\"UImode\" value=\"none\">\n';
		playerHtml += '<param name=\"AnimationAtStart\" value=\"1\">\n';
		playerHtml += '<param name=\"StretchToFit\" value=\"1\">\n';
		playerHtml += '<param name=\"EnableContextMenu\" value=\"True\">\n';
		playerHtml += '<param name=\"currentPosition\" value=\"'+ this.videoStart + '\">\n';		
		playerHtml += '</OBJECT>\n';	
		
		
		
		
		return (playerHtml);
	}
	
	function player_LoadRealPlayerObject(source)
	{						
		//Element.update("playerObject_"+ this.windowID, this.drawReal(source));
		document.getElementById("playerObject_"+ this.windowID).innerHTML = this.drawReal(source);
		
		var realPlayer = document.getElementById("playerReal" + this.windowID);
		this.player = null;
		this.player = new PlayerReal("realPlayer", realPlayer);		
		this.type = this.playerType.Real;

		this.playerJW = null;		
		return (this.player);
	}
		
	function player_DrawReal(source)
	{
		var playerHtml;
		
		playerHtml = "<embed id=\"playerReal" + this.windowID+"\" name=\"playerReal" + this.windowID+"\" type=\"audio/x-pn-realaudio-plugin\"";
		playerHtml += "width=\""+this.width+"\" height=\""+this.height+"\"\n";
		playerHtml += "src=\'"+source;
		if(this.videoStart != 0){
			playerHtml += "?start="+this.videoStart;
		}		
		playerHtml += "\'\n";
		playerHtml += "autostart=\""+this.autostart+"\"\n";
		playerHtml += "loop=\""+this.repeat+"\"\n";					
		playerHtml += "controls=\"imagewindow\" />\n";		
		
		
		return (playerHtml);
	}

	/**
	 * 
	 * @param source contiene URL nella forma streamer,nome_file
	 * @return
	 */
	
	
	function player_LoadJWPlayerObject(source){
		if (navigator.userAgent.indexOf('MSIE') == -1){ 			
			var $jwPlayer = jQuery.noConflict();		
			$jwPlayer("div#playerObject_"+ this.windowID).html(this.drawJW(source));
		}
		else{
			document.getElementById("playerObject_"+ this.windowID).innerHTML = this.drawJW(source);
		}
		
		// source 
		var sourceCfg = decodeSource(source);
		
		var idContainer = "playerJW_"+this.windowID;
		//alert(document.getElementById(idContainer).id);
		var startFrom = 0;
		if(this.videoStart != 0){
			startFrom=this.videoStart;
		}
		
		//---
		//console.log("PLAYER::LOAD_JW:start:"+source+"-"+startFrom);
		
		jwplayer(idContainer).setup({
			flashplayer: this.context+"/js/player/JWPlayer/player.swf",
			// TODO repeat va messo  ?
			streamer:sourceCfg['base'],
			file: sourceCfg['file'],
			height: this.height,
			width: this.width,
			autostart :this.autostart,
			start:startFrom,
			controlbar:'none',
			allowscriptaccess:'always',
			'rtmp.subscribe': (sourceCfg['cdn']!=null ? true : false),
			plugins: {
				viral: { functions: 'embed' }
			}
			
		});
		
		var objPlayer = jwplayer(idContainer);
		//objPlayer['viral.functions']='embed';
		this.player = null;
		this.player = new PlayerJW("JWPlayer", objPlayer);	
		this.type = this.playerType.JWPlayer;
				
		//this.playerJW = null;
		
		return (this.player);
	}
	
	
	function player_DrawJW(source){	
		//var d = document.createElement("div");
		//d.setAttribute("id","playerJW_"+ this.windowID);
		
		
		var playerHtml="<div id='playerJW_"+this.windowID+"'></div>";
		return (playerHtml);
	}	
	/*
	function player_LoadJWPlayerObject(source){
		if (navigator.userAgent.indexOf('MSIE') == -1){ 			
			var $jwPlayer = jQuery.noConflict();		
			$jwPlayer("div#playerObject_"+ this.windowID).html(this.drawJW(source));
		}
		else{
			document.getElementById("playerObject_"+ this.windowID).innerHTML = this.drawJW(source);
		}
		var splits = source.split(",");
		
		var idContainer = "playerJW_"+this.windowID;
		//alert(document.getElementById(idContainer).id);
		var startFrom = 0;
		if(this.videoStart != 0){
			startFrom=this.videoStart;
		}
		
		//---
		console.log("PLAYER::LOAD_JW:start:"+startFrom);
		
		var flashvars = { streamer:splits[0] , file:splits[1] ,autostart :this.autostart,start:startFrom,controlbar:'none'};
		var params = { allowfullscreen:'true', allowscriptaccess:'always',wmode:'opaque' };
		var attributes = { id:'playerJW_'+this.windowID };
		
		swfobject.embedSWF(this.context+'/js/player/JWPlayer/player.swf', idContainer, this.width, this.height, '9.0.115', 'false',
				 flashvars, params, attributes, this.setPlayerObj);



		

		this.type = this.playerType.JWPlayer;	
		//this.playerJW = null;
		
		//return (this.player);
	}
	
	function flashLoaded(e) {console.log("FLASH LOADED");
		var objPlayer = e.ref;
		//objPlayer['viral.functions']='embed';
		setP(objPlayer);
			
	}
	
	function setP(obj){
		this.player = null;
		this.player =  new PlayerJW("JWPlayer", obj.ref);	
	}
	
	
	function player_DrawJW(source){	
		//var d = document.createElement("div");
		//d.setAttribute("id","playerJW_"+ this.windowID);
		
		
		var playerHtml="<div id='playerJW_"+this.windowID+"'></div>";
		return (playerHtml);
	}

	*/
// ------------------------------------------------------------------------
  function player_SetSource(source)
	{		
		if(this.player != null)
		{
			try{
				this.player.setSource(source);
			}
			catch(e){
				;
			}
		}
	}
	
	function player_GetSource()
	{		
		if(this.player != null)
		{
			try{
				return this.player.getSource();
			}
			catch(e){
				return null;
			}
		}
		
		return null;
	}

  function player_Play()
	{
		if(this.player != null)
		{
			try{
				this.player.play();
			}
			catch(e){
				;
			}
		}
	}
	
	function player_Stop()
	{
		if(this.player != null)
		{
			try{
				this.player.stop();
			}
			catch(e){
				;
			}
			this.manualStop = true;
		}
	}
	
	function player_Pause()
	{
		if(this.player != null)
		{
			try{
				this.player.pause();
			}
			catch(e){
				;
			}
		}
	}

	function player_FullScreen(fullscreen)
	{
		if(this.player != null)
		{
			try{
				this.player.fullscreen(fullscreen);
			}
			catch(e){
				;
			}
		}
	}
	
	function player_SetMute(mute)
	{
		if(this.player != null)
		{		
			//alert(mute + " - " + this.player.getMuteState());
			if(mute != this.player.getMuteState())
			{		
				this.mute = mute;
				try{
					this.player.setMute(mute);					
					if(!mute){
						this.setVolume(this.volume);
					}	
				}
				catch(e){
					;
				}
			}
		}
	}
	
	function player_GetMuteState()
	{
		if(this.player != null)
		{
			try{
				return this.player.getMuteState();
			}
			catch(e){
				;
			}
		}
	}
	
	function player_SetVolume(volume){
		if(this.player != null){
			try{
				this.player.setVolume(volume);
			}
			catch(e){
				;
			}
			this.volume = volume;
		}
	}
	
	
		
	function player_IncreaseVolume()
	{
		if(this.player != null)
		{		  
			var thVolume = document.getElementById("thVolume_" + this.windowID);
			var volume = new Number(this.player.getVolume());
      
			if(volume < 100)
			{
				volume = volume + 10;
				thVolume.style.width = volume + "px";					
				this.player.setVolume(volume);
			}
			else 
			{
				thVolume.style.width = "100px";				
			}
			
			this.volume = volume;
			this.mute = false;			
		}
	}	
	
	function player_DecreaseVolume()
	{
		if(this.player != null)
		{
			var thVolume = document.getElementById("thVolume_" + this.windowID);
			var volume = this.player.getVolume();
			
			if(volume > 0)
			{
				volume = volume - 10;
				thVolume.style.width = volume + "px";				
				this.player.setVolume(volume);
			}
			else
			{
				thVolume.style.width = "1px";				
				this.player.setVolume(0);
			}
				
			this.volume = volume;
			this.mute = false;
		}
	}
	
	
	function player_GetCurrentMediaDuration() 
	{
		if(this.player != null)
		{
			if(this.videoSize != -1){
				return this.videoSize;
			}
			else{
				try{
					return this.player.getCurrentMediaDuration();
				}
				catch(e){
					return 0;
				}
			}
			
		}	
	}	
	
	function player_GetPosition()
	{
		if(this.player != null){
			try{
				return (this.player.getPosition());
			}
			catch(e){
				return 0;
			}
		}
		return 0;		
	}
	
	function player_SetPosition(position)
	{
		if(this.player != null)
		{
			try{
				this.player.setPosition(position);
			}catch(e){
				;
			}
		}
	}
	
	function player_GetPlayState()
	{
		try{
			if(this.player != null)
				return (this.player.getPlayState());
			
			else 
				return (this.playState.undefined);
		}catch(e){
			return (this.playState.undefined);;
		}
	}
	

/*	
	function player_SetVolume(volume){
		if(this.player != null){
			try{
				this.player.setVolume(volume);
			}
			catch(e){
				;
			}
			this.volume = volume;
		}
	}

	function player_GetCurrentMediaDuration() 
	{
		if(this.player != null)
		{
			if(this.videoSize != -1){
				return this.videoSize;
			}
			else{
				try{
					return this.player.getCurrentMediaDuration();
				}
				catch(e){
					return 0;
				}
			}
			
		}	
	}	
	
	function player_GetPosition()
	{
		if(this.player != null){
			try{
				return (this.player.getPosition());
			}
			catch(e){
				return 0;
			}
		}
		return 0;		
	}
	
	function player_SetPosition(position)
	{
		if(this.player != null)
		{
			try{
				this.player.setPosition(position);
			}catch(e){
				;
			}
		}
	}
		
	function player_GetPlayState()
	{
		try{
			if(this.player != null)
				return (this.player.getPlayState());
			
			else 
				return (this.playState.undefined);
		}catch(e){
			return (this.playState.undefined);;
		}
	}
*/
	
	/*
	function player_GetCDNConf{
		try{
			if(this.player != null)
				return (this.player.getCDNConf());
			
			else 
				return (this.playState.undefined);
		}catch(e){
			return (this.playState.undefined);;
		}
	}
	
	function player_SetCDNConf{
		
	}
	*/
	// ************************************************************************************************
	// *** Events
	// ************************************************************************************************
	//var count =0;
	function player_Tick()
	{	
		//console.log("srving timeout :"+count);
		//count++;
		//if (this.player == null) alert("player NULL:"+count);
		
		if(this.player != null)
		{
		  
			
			var currentMediaDuration = this.player.getCurrentMediaDuration();
			
			var value = ((currentMediaDuration > 0) ? ((this.player.getPosition()*100) / currentMediaDuration) : 0);
			value += 1;
									
			var duration = this.player.getCurrentMediaDuration();

			var currentPosition = (!this.player.getPosition()) ? 0 : this.player.getPosition();

			
			if(this.videoSize == -1){ // No Timeshifting
				
				if(this.type != this.playerType.Real)
					this.videoPosition = parseInt(currentPosition);
				else
					this.videoPosition = parseInt(currentPosition) + this.videoStart;
				
			}else{	// Timeshifting				
					this.videoPosition = parseInt(currentPosition) + this.videoStart;				
			}
			
			//$jPlayer('#posProgess').html(this.videoPosition + " - " + this.videoStart);
			
			this.updateProgressBar(this.videoPosition, this.getCurrentMediaDuration(), this.windowID);
			
			
			
			//document.getElementById("playerState"+this.windowID).innerHTML = this.getPlayState();			
			if(this.streamingElement != ""){
				
				if(this.getPlayState() == this.playState.playing || 
					this.getPlayState() == this.playState.paused || 
					this.getPlayState() == this.playState.stopped){													
					if(this.updateControllBar != null){						
						displayStreamingWait(false, this.streamingElement, this.imgPath);						
						if(this.getPlayState() == this.playState.stopped)
							this.updateControllBar("Stop");	
						else if(this.getPlayState() == this.playState.playing)
							this.updateControllBar("Play");	
						else if(this.getPlayState() == this.playState.paused)
							this.updateControllBar("Pause");	
					}
				}
				else{
					if(this.updateControllBar != null && this.getPlayState() != this.playState.undefined)
						displayStreamingWait(true, this.streamingElement, this.imgPath);
				}
			}
				
		}
				
		//console.log("set timeout:"+this.objName+"-"+this.videoPosition+"-"+count);
		setTimeout(this.objName + '.tick();', 1000);
	}
	//console.log("set timeout ext:"+this.objName+"-"+this.videoPosition+"-"+count);
	setTimeout(this.objName + '.tick();', 3000);
}
	




