﻿
/******************************************************************************************************************************
** 
** Filename: timer.js
**
/******************************************************************************************************************************


	/*******************************************************************************
	** timer constuctor
	*******************************************************************************/
		
		function timer(){
				
				/** timer methods 
				************************************************/ 
					try {
						this.getCurrentTime=timer_getCurrentTime;	
						this.startTimer=timer_startTimer;
						this.computeTime=timer_computeTime;
						this.convertTotalSeconds=timer_convertTotalSeconds;
					}catch(e){errorReporter(e, errorLocation, "timer.js  [function: methods]");}
				
	
				/** timer properties 
				************************************************/
					try {
						this.startTime;			
						this.currentTime;
						this.elapsedSeconds = 0;
						this.elapsedMinutes = 0;
						this.secondsRemaining;
						this.minutesBeforePopup = 60;
						this.formattedTime = "00:00:00.0";
					}catch(e){errorReporter(e, errorLocation, "timer.js  [function: properties]");}
			}

		


	/*******************************************************************************
	** getCurrentTime()	
	*******************************************************************************/


		function timer_getCurrentTime(){
			try{
				return new Date().getTime();	
			}catch(e){errorReporter(e, errorLocation, "timer.js  [function: getCurrentTime()]");}
		}
		

	/*******************************************************************************
	** startTimer()	
	*******************************************************************************/


		function timer_startTimer(){
			try{
					_scormHelper.retrieveSuspendData();
					if(_scormHelper.getSuspendDataElement("session_time") > 0){
						var elapsedTime = parseInt(this.getCurrentTime()) - parseInt(_scormHelper.getSuspendDataElement("session_time"));
					}else{
						var elapsedTime = 0;
					}
					
					var millisecondsforPopup = parseInt((this.minutesBeforePopup * 60) * 1000);
					var millisecondsRemaining = parseInt(millisecondsforPopup - elapsedTime);
					setTimeout("displayTimerAlert()",millisecondsRemaining);
					
			}catch(e){errorReporter(e, errorLocation, "timer.js  [function: startTimer()]");}
		}
		

		function displayTimerAlert(){
			try{
			
				alert("You have been working on this course for over an hour.\n\nWhy not take a short break now and return to the training refreshed.\n\nThe programme will remain open at this screen ready for you to return.");
				
				_scormHelper.retrieveSuspendData();
				var currentTime = _timer.getCurrentTime();
				_scormHelper.setSuspendDataElement("session_time", currentTime);
				_scormHelper.commitSuspendData();
				_scorm.doLMSCommit();
				/*var elapsedTime = parseInt(_timer.getCurrentTime()) - parseInt(_scormHelper.getSuspendDataElement("session_time"));
				var millisecondsforPopup = parseInt((_timer.minutesBeforePopup * 60) * 1000);
				var millisecondsRemaining = parseInt(millisecondsforPopup - elapsedTime);
				setTimeout("displayTimerAlert()",millisecondsRemaining);*/
				_timer.startTimer();
				
			
			}catch(e){errorReporter(e, errorLocation, "timer.js  [function: displayTimerAlert()]");}
		}

	/*******************************************************************************
	** computeTime()
	*******************************************************************************/
			
		function timer_computeTime(startDate){
			try{
				var formattedTime = "00:00:00.0";
				var currentDate = this.getCurrentTime();
      			var elapsedSeconds = ( (currentDate - startDate) / 1000 );
      			formattedTime = this.convertTotalSeconds( elapsedSeconds );
				return formattedTime;	
			}catch(e){errorReporter(e, errorLocation, "timer.js  [function: computeTime()]");}	
		} 
		

	/*******************************************************************************
	** convertTotalSeconds(totalSeconds)
	*******************************************************************************/		
		

		function timer_convertTotalSeconds(ts){
			try{	
				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
				if ( (ts % 3600) != 0 ) var Hour = "00" ;
				else var Hour = ""+ (ts / 3600);
				if ( (tmp % 60) != 0 ) var Min = "00";
				else var Min = ""+(tmp / 60);
				Sec=""+Sec
				Sec=Sec.substring(0,Sec.indexOf("."))
				if (Hour.length < 2)Hour = "0"+Hour;
				if (Min.length < 2)Min = "0"+Min;
				if (Sec.length <2)Sec = "0"+Sec;
				var rtnVal = Hour+":"+Min+":"+Sec;
				return rtnVal;
			}catch(e){errorReporter(e, errorLocation, "timer.js  [function: convertTotalSeconds()]");}
		}






		

		


		

