/* LMS Functions for Timed viewing (ie scoring) */
var LMSMode = false;
var VideoLengthSeconds=0;
var UpdateLMSIntervalSeconds = 120;

//Make them watch at least 80% of the video to complete
var CompletionThreshold = 0.8;  

var API = getAPIHandle();
if (API != null) {
	LMSMode= true;
}

if (LMSMode) {
	doLMSInitialize();
	
	//testing only
	//doLMSSetValue('cmi.core.lesson_status','incomplete');
	//doLMSSetValue('cmi.core.score.raw',0);
	//doLMSCommit();
	
}
else {
	//alert("No LMS API Present");
}

function StartTimer()
{
	
//top.window.status="Timer Started";

	//When video player buffers it runs StartTimer several times in quick succession
	//we only want one SessionTimer running at a time.  
	//Trick: Use the child reference SessionTimer (window.SessionTimer) so we can successfully check for null variable
	if (window.SessionTimer != null) {
		clearTimeout(SessionTimer);
	}
	
	SessionTimer = setTimeout('UpdateLMS()',UpdateLMSIntervalSeconds*1000);
}

function StopTimer()
{
//top.window.status="Timer Stopped";
	clearTimeout(SessionTimer); 
}

function UpdateLMS()
{
	// Calculate the value of UpdateLMSIntervalSeconds/VideoLengthSeconds on a 0-1 scale
	// This value will be added to cmi.progress_measure each time this function is called.
	var ProgressMeasureIncrementor =  UpdateLMSIntervalSeconds/VideoLengthSeconds;
	
	// Add in the Incrementor value associated with UpdateLMSIntervalSeconds
	var CurrentProgressMeasure = parseFloat(doLMSGetValue('cmi.core.score.raw'));
	CurrentProgressMeasure = CurrentProgressMeasure + ProgressMeasureIncrementor;
//alert(CurrentProgressMeasure);
	doLMSSetValue('cmi.core.score.raw',CurrentProgressMeasure);
	
	// cmi.core.session_time can be called multiple times during session and will add up in cmi.core.total_time 
	// cmi.core.total_time keeps track off all time used in all SCO's in the course
	// doc says this shouldn't work - may be non-standard for this LMS
	doLMSSetValue('cmi.core.session_time',ConvertSecondsToHMS(UpdateLMSIntervalSeconds));
	doLMSCommit();
	
	
	if (CurrentProgressMeasure >= CompletionThreshold) {
		strStatus = 'completed';
		doLMSSetValue('cmi.core.lesson_status',strStatus);
		doLMSCommit();
	}
//alert(doLMSGetValue('cmi.core.score.raw'));
	StartTimer();
}

function convertHMStoSeconds(strHMS) {
	var arrHMS = strHMS.split(":")
	var seconds = parseInt(arrHMS[0])*3600 + parseInt(arrHMS[1])*60 + parseInt(arrHMS[2]);
	return seconds;
}

function ViewTime()
{
	var CurrentProgressMeasure = parseFloat(doLMSGetValue('cmi.core.score.raw'));
	return "View time: " + ConvertSecondsToHMS(CurrentProgressMeasure * VideoLengthSeconds) + " (" + CurrentProgressMeasure + ")";
}

/*
this function will convert seconds into hours, minutes, and seconds in
CMITimespan type format - HHHH:MM:SS.SS (Hours has a max of 4 digits and
Min of 2 digits
*/
function ConvertSecondsToHMS(ts)
{
   var sec = (ts % 60);

   ts -= sec;
   var tmp = (ts % 3600);  //# of seconds in the total # of minutes
   ts -= tmp;              //# of seconds in the total # of hours

   // convert seconds to conform to CMITimespan type (e.g. SS.00)
   sec = Math.round(sec*100)/100;

   var strSec = new String(sec);
   var strWholeSec = strSec;
   var strFractionSec = "";

   if (strSec.indexOf(".") != -1)
   {
      strWholeSec =  strSec.substring(0, strSec.indexOf("."));
      strFractionSec = strSec.substring(strSec.indexOf(".")+1, strSec.length);
   }

   if (strWholeSec.length < 2)
   {
      strWholeSec = "0" + strWholeSec;
   }
   strSec = strWholeSec;

   if (strFractionSec.length)
   {
      strSec = strSec+ "." + strFractionSec;
   }


   if ((ts % 3600) != 0 )
      var hour = 0;
   else var hour = (ts / 3600);
   if ( (tmp % 60) != 0 )
      var min = 0;
   else var min = (tmp / 60);

   if ((new String(hour)).length < 2)
      hour = "0"+hour;
   if ((new String(min)).length < 2)
      min = "0"+min;

   var rtnVal = hour+":"+min+":"+strSec;

   return rtnVal;
}
