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

//IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
function stateListener(obj) {
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	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 > 6000) {
			savedTimestamp = newD.getTime();
			//video has completed.
			videoPlayer_IsAutoStart = true;
			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.
				$(".movepane").css({'top' : ' 5px'}); // restart the scroll
			}
			playVideo(currentVideoIndex, currentlyShowing);
		}
	}
}

