// Kaltura functions

 function get(el) {
	 return document.getElementById(el);
 }
 
 function onKdpReady(playerId) {
	 window.myKdp=get(playerId);
	 //get("Player_State").innerHTML="<br>&nbsp; READY (Id=" + playerId + ")";
	// get("nowPlaying").innerHTML=(myKdp.evaluate('{entryId}'));
	//alert('11 '+playerId) ;
	 getDuration();
	 attachKdpEvents();
	 addKdpListners();
 }
 
 function onKdpEmpty(playerId) {
	 //get("Player_State").innerHTML="<br>&nbsp; READY (Content not specified or not found; Id=" + playerId + ")";
 }
 
 function attachKdpEvents() {
	get("kdpPlay").onclick=function(){myKdp.dispatchKdpEvent('doPlay');return false;}
	get("kdpPause").onclick=function(){myKdp.dispatchKdpEvent('doPause');return false;}
	get("kdpStop").onclick=function(){myKdp.dispatchKdpEvent('doStop');myKdp.dispatchKdpEvent('stopFastForward');return false;}
	get("kdpFFX8").onclick=function(){myKdp.dispatchKdpEvent('fastForward',8);return false;}
	get("kdpMute").onclick=function(){myKdp.dispatchKdpEvent('volumeChange');return false;}
	get("kdpJumpTo").onclick=function(){myKdp.dispatchKdpEvent('doSeek',get('goSeek').value);return false;}
	get("kdpLoadVideo").onclick=function(){kdpDoLoadVideo();return false;}
	get("kdpShare").onclick=function(){kdpDoGigya();return false;}
 }
 
 function kdpDoLoadVideo() {
	//get("Player_State").innerHTML="<br>&nbsp;Stopped";
	myKdp.insertMedia("-1",get('loadVideoEntry').value,'true');
	myKdp.addJsListener("initMedia","kdpDoOnLoadVideo");
 }
 
 function kdpDoOnLoadVideo() {
	myKdp.dispatchKdpEvent('doPlay');
	//get("Player_State").innerHTML="<br>&nbsp;New Video Loaded";
	//get("nowPlaying").innerHTML=(myKdp.evaluate('{entryId}'));
	myKdp.removeJsListener("initMedia");
 }
 
 function kdpDoGigya() {
	kdpDoOnPause();
	myKdp.dispatchKdpEvent('gigyaPopup');
	myKdp.addJsListener("closePopup","kdpDoOnCloseGigya");
	alert("detach events to prevent play button etc. from functioning");
 }
 
 function kdpDoOnCloseGigya() {
	 alert("re-attach events");
	 alert("remove listner to 'closePopup'");
 }
 
 function addKdpListners() {
	 myKdp.addJsListener("doPlay","kdpDoOnPlay");
	 myKdp.addJsListener("doPause","kdpDoOnPause");
	 myKdp.addJsListener("doStop","kdpDoOnStop");
	 myKdp.addJsListener("fastForward","kdpDoOnFF");
	 myKdp.addJsListener("playerPlayEnd","kdpDoOnEnd");
 }
 
 function kdpDoOnPlay() {
	 //get("Player_State").innerHTML="<br>&nbsp;Playing";
	 startKdpTimer();
 }
 
 function kdpDoOnPause() {
	 //get("Player_State").innerHTML="<br>&nbsp;Paused";
	 myKdp.dispatchKdpEvent('stopFastForward');
	 stopKdpTimer();
 }
 
 function kdpDoOnStop() {
	 //get("Player_State").innerHTML="<br>&nbsp;Stopped";
	 myKdp.dispatchKdpEvent('doSeek',0);
	 get("entryPosition").value=0.0;
	 stopKdpTimer();
 }
 
 function kdpDoOnFF() {
	 //get("Player_State").innerHTML="<br>&nbsp;Fast forwarding";
	 startKdpTimer();
 }
 
 function kdpDoOnEnd() {
	 //get("Player_State").innerHTML="<br>&nbsp;Reached end";
	 stopKdpTimer();
 }
 
 function getDuration() {
	window.kdpMovieDuration=(myKdp.evaluate('{duration}'));
	//get("movieDuration").innerHTML=kdpMovieDuration;
	//get("entryPosition").innerHTML="0.00";
 }
 
 function startKdpTimer() {
	var timer_element=get("entryPosition"); // the html element (object) that will display the current videohead position
	window.kdpTimerInterval=setInterval(function(){doTimer(timer_element)},250); // make it a global variable so that we can kill it later
	doTimer(timer_element);
 }
 
 function doTimer(timer_element) {
	var kdpHeadPosition=myKdp.getMediaSeekTime();
	if(kdpHeadPosition>kdpMovieDuration) {
		kdpHeadPosition=kdpMovieDuration;
		myKdp.dispatchKdpEvent('stopFastForward');
		kdpDoOnEnd();
	}
	kdpHeadPosition+=""; // convert to string
	var seconds=kdpHeadPosition.split(".")[0];
	try{var fractions=kdpHeadPosition.split(".")[1].substr(0,3)} catch(err){fractions=0};
	timer_element.innerHTML=seconds+"."+fractions;
 }
 
 function stopKdpTimer() {
	 clearInterval(kdpTimerInterval);
 }
 
 function GetEmbedCode() {
	 var emb=get("GiveEmbedCode");
	 emb.value=get("myPlayer").innerHTML;
 	 emb.select()
 }

