/*
	Function to communicate with the player via javascript. 
	At the moment, methods like playVideo() and stopVideo are supported.
 */

function callPlayer( playerCommand ){
	
	//get flash player
	if(checkFlashSupport()){
		var app = swfobject.getObjectById("tvnextplayer_flash");
		
		switch(playerCommand){
			
			case "playVideo":
				app.playVideo();
				break;
			
			case "pauseVideo":
				app.pauseVideo();
				break;
				
			/*
				stop Button not supported yet
				case "stopVideo":
				app.stopVideo();
				break;*/
				
			default:
				//REMOVE ALERT before publishing -> for debug only
				alert("Error in callPlayer on unknown command -> " + playerCommand);
		}
	}
	//get html5 player
	else
	{
		var app = document.getElementById('tvnextplayer_html5');
		
		switch(playerCommand){
			
			case "playVideo":
				app.play();
				break;
			
			case "pauseVideo":
				app.pause();
				break;
				
			/*	
				stopButton not supported yet
				case "stopButton":
				app.stopVideo();
				break;*/
				
			default:
				//REMOVE ALERT before publishing -> for debug only
				alert("Error in callPlayer on unknown command -> " + playerCommand);
		}
	}
}


/*
	Function for player to communicate with the webpage. Player throws event if it changes his state, f.e.
		-> HTML5-Player: 	- Video play
							- Video pause
							- Video on end
							
		-> Flash Player: 	- SWF loading
							- Video player ready (SWF load complete)
							- Video buffering
							- Video playing
							- Video paused
	
	At example page, Player update their State in a DIV-Field
	
*/
function updatePlayerStateInGui(actionName){
	//flash player
	if(checkFlashSupport()){
		switch(actionName){
			
			//MediaPlayerState.PLAYING == "playing"
			case "playing":
				$("div#currentPlayerState").text("video plays");
				break;
			
			//MediaPlayerState.PAUSED == "paused"
			case "paused":
				$("div#currentPlayerState").text("video paused");
				break;
			
			//MediaPlayerState.READY == "ready"
			case "ready":
				 $("div#currentPlayerState").text("player ready");
				break;
				
			//MediaPlayerState.BUFFERING == "buffering"
			case "buffering":
				 $("div#currentPlayerState").text("player buffers");
				break;
				
			//MediaPlayerState.LOADING == "loading"
			case "loading":
				 $("div#currentPlayerState").text("player loading");
				break;
			
			default:
				alert("error with actionName -> " + actionName);
		
		}
	}
	//html5 player
	else{
		switch(actionName){	
		
			case "playVideo":
				$("div#currentPlayerState").text("video plays");
				break;
			
			case "pauseVideo":			
				$("div#currentPlayerState").text("video paused");
				break;
			
			case "videoOnEnd":
				 $("div#currentPlayerState").text("video on end");
				break;
				
			default:
				alert("error with actionName -> " + actionName);
		
		}
	}
	
	
}

/*
	Function to check if flash is supported. 
	playerVersion.major returns 0 if no flash was found
*/
function checkFlashSupport(){
	var playerVersion = swfobject.getFlashPlayerVersion(); 
	if(playerVersion.major == 0){
		return false;
	}
	return true;
}
