/* *** LISTEN FOR EVENTS *** */

/* the player is now ready */
function playerReady(thePlayer) 
{
	player = document.getElementById(thePlayer.id);
	addListeners();
}

/* listen for events from the flash player */
function addListeners() 
{
	if (player) 
	{ 
		player.addModelListener("STATE", "stateListener");
		player.addControllerListener("ITEM", "itemListener"); 
	} 
	else 
	{
		setTimeout("addListeners()",100);
	}
}

//IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
function stateListener(obj) 
{
	currentState = obj.newstate; 
	previousState = obj.oldstate;
	
	if (sacadirecto == 1){
		var fin = "<div id='fondofin'><a href='#' onclick='javascript:cClass(6);switchDirecto();return false;'><img id='fin' src='img/fin.png' height='100' width='400' /></a></div>";
		$("#final").html(fin);
		sacadirecto = 0;
		
		return;
	}

	if ((currentState == "IDLE")&&(previousState == "PLAYING")) 
	{
		//Lets do a timestamp check, in firefox the completed state fires twice.
		//so we check we are not the second event by skipping if we're a complete event in less than 5 seconds.
		var newD = new Date();
		var timestamp =  newD.getTime();
		timestamp = (timestamp - savedTimestamp);
		
		/*if(timestamp > 5000)
		{*/
			savedTimestamp = newD.getTime();
			//video has completed.
			videoPlayer_IsAutoStart = true;
			
			currentVideoIndex = currentVideoIndex +1;
			var contentLength = 0;
			
			if(currentlyShowing == 'content')
			{
				contentLength = contentArray.length;
			}
			
			if(currentlyShowing == 'schcontent')
			{
				contentLength = schArray.length;
			}
			
			if(currentlyShowing == 'search')
			{
				contentLength = searchContentArray.length;
			}
			
			if(currentVideoIndex >= contentLength)
			{
				//restarting the playlist, have we come to the end of the current playlist?
				currentVideoIndex = 0; // restart the playlist.
				if(currentlyShowing != 'schcontent')
				{
					playVideo(0, currentlyShowing);
				}
				else
				{
					$.ajax({
						type: "POST",
						url: "vzService/hasSchedule.php",
						data: "clientID="+thisChannel.ClientID+"&channelID="+thisChannel.ChannelID+"",
						contentType: CONTENT_TYPE,
					
						success: function(msg){ 
						  	try{
								eval(msg);
							
								if (hasSchedule == hasScheduleInit) {
									cambio = 0;
								}else{
									cambio = 1;
									hasScheduleInit = hasSchedule;
								}
							}catch(exception){
								//alert("Exception 1");
							}
						}
					});
					
					if (cambio == 1)
					{
						switchDirecto();
					}
					else
					{	
						playVideoDirecto(0, currentlyShowing);
					}
					currentlyShowing="schcontent";
 				}
   			}
   			else if(currentlyShowing != 'schcontent')
			{
				playVideo(currentVideoIndex, currentlyShowing);
			}
			else
			{
				$.ajax({
					type: "POST",
					url: "vzService/hasSchedule.php",
					data: "clientID="+thisChannel.ClientID+"&channelID="+thisChannel.ChannelID+"",
					contentType: CONTENT_TYPE,
				
					success: function(msg){ 
					  	try{
							eval(msg);
						
							if (hasSchedule == hasScheduleInit) {
								cambio = 0;
								hasScheduleInit = hasSchedule;
							}else{
								cambio = 1;
								hasScheduleInit = hasSchedule;
							}
						}catch(exception){
							//alert("Exception 2");
						}
					}
				});
				
				if (cambio == 1)
				{
					switchDirecto();
				}
				else
				{	
					playVideoDirecto(currentVideoIndex, currentlyShowing);
				}
				currentlyShowing="schcontent";
			}
		/*}*/
	}
}

//////////////////////////
function stateListenerKey(key) 
{
	currentState = "IDLE"; 
	previousState = "PLAYING"; 
	
	//alert("Viendo: "+currentlyShowing);

	if ((currentState == "IDLE")&&(previousState == "PLAYING")) 
	{
		//Lets do a timestamp check, in firefox the completed state fires twice.
		//so we check we are not the second event by skipping if we're a complete event in less than 5 seconds.
		var newD = new Date();
		var timestamp =  newD.getTime();
		timestamp = (timestamp - savedTimestamp);
		
		/*if(timestamp > 5000)
		{*/
			savedTimestamp = newD.getTime();
			//video has completed.
			videoPlayer_IsAutoStart = true;
			var contentLength = 0;
			if(currentlyShowing == 'content')
			{
				contentLength = contentArray.length;
			}
			
			if(currentlyShowing == 'schcontent')
			{
				contentLength = schArray.length;
			}
			
			if(currentlyShowing == 'search')
			{
				contentLength = searchContentArray.length;
			}
			
			if (key == -1)
			{
				//Tiene que saltar -3
				//currentVideoIndex = currentVideoIndex -1;
				currentVideoIndex = currentVideoIndex -3;
				if (currentVideoIndex < 0)
				{
					//currentVideoIndex = contentLength-1;
					currentVideoIndex = contentLength-2;
				}
			}
			else
			{
				currentVideoIndex = currentVideoIndex +1;
			}
			
			if(currentVideoIndex >= contentLength)
			{
				//restarting the playlist, have we come to the end of the current playlist?
				currentVideoIndex = 0; // restart the playlist.
				if(currentlyShowing != 'schcontent')
				{
					playVideo(0, currentlyShowing);
				}
				else
				{
					playVideoDirecto(0, currentlyShowing);
					currentlyShowing="schcontent";
 				}
   			}
   			else if(currentlyShowing != 'schcontent')
			{
				playVideo(currentVideoIndex, currentlyShowing);
			}
			else
			{
				playVideoDirecto(currentVideoIndex, currentlyShowing);
				currentlyShowing="schcontent";
			}
		/*}*/
	}
} 
 
function itemListener(obj) 
{
	if (obj.index != currentItem) 
	{
 		previousItem = currentItem;
		currentItem = obj.index;
	}
}

/*function hasSchedule()
{
	$.ajax({
		type: "POST",
		url: "vzService/hasSchedule.php",
		data: "clientID="+thisChannel.ClientID+"&channelID="+thisChannel.ChannelID+"",
		contentType: CONTENT_TYPE,
	
		success: function(msg){ 
		  	try{
				eval(msg);
			
				if (hasSchedule == 1){
					return 1;
				}else if (hasSchedule == 0){
					return 0;
				}
			}catch(exception){
				alert("Exception");
			}
		}
	});
}*/
