	var Separ='';
  var Url = location.href;
Url = Url.replace(/.*\?(.*?)/,"$1");
Variables = Url.split ("&");

for (i = 0; i < Variables.length; i++) {
if(Variables.length!=2){break;}
       Separ = Variables[i].split("=");
       eval ('var '+Separ[0]+'="'+Separ[1]+'"');
}

//alert(Separ[1]);
/* *** LISTEN FOR EVENTS *** */


c=null; //used for scrolling the carousel back once the playlist is completed 



/* 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");
	} else {
		setTimeout("addListeners()",500);
	}
}

	  
 
var count = 0;
function contadora() {
    count ++;
    return count;
    
}
  
//IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
function stateListener(obj) {
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
	

	if ((currentState == "COMPLETED")&&(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 cambia si viene de la URL. Si viene de la URL y pasa por segunda vez por la funcion vuelve a currentVideoIndex normal. 
		
		var cuenta = contadora();
		
		   if (Separ[1]!=null){
      
      if(cuenta>=2){
          currentVideoIndex = currentVideoIndex +1;
      }
      else{
        var addone =  parseInt(Separ[1]) + 1;
      	currentVideoIndex = addone;
      }
            
      }else {
		
     currentVideoIndex = currentVideoIndex +1;
     }




			var contentLength = 0;
			
			if(currentlyShowing == 'content'){
				contentLength = contentArray.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.
				playVideo(0, currentlyShowing);
				

				
      c.scroll(1);    //restart the carousel

			}// end final de currentVideoIndex >= contentLength
			

			//alert('final: '+currentVideoIndex);
			playVideo(currentVideoIndex, currentlyShowing);
		
		}
	}
} 
