/*/////////////////////////////////////////////////////////
// This code is based off of
// "Live Clock Lite" script - Version 1.0
// By Mark Plachetta (astroboy@zip.com.au)
//
// The original script displayed a clock.
// Mark Nielsen modified it to be a countdown timer
// for the lesson module in moodle.
//
//    Below is the code that is used to call this page.
    echo "<script type=\"text/javascript\">\n";
        echo "var starttime = ". $timer->starttime . ";\n";
        echo "var servertime = ". time() . ";\n";
        echo "var testlength = ". $lesson->maxtime * 60 .";\n";
        echo "document.write('<script type=\"text/javascript\" src=\"liveclock_lite.js\"><\/script>');\n";
        echo "window.onload = function () { show_clock(); }";
    echo "</script>\n";
/
// Sarah Ashley has modified Mark Nielsen\s code to be a countup timer
// for the lesson module in Moodle while Wendi Turner stood there with 
// her head tilted to the side with question marks floating over her head as she tried desperately to help. 
// It shows how much time a user has spent in the lesson.
//
// Below is the code that is used to call the modified timer script page.
    echo "<script type=\"text/javascript\">\n";
    echo "var starttime = ". $thestarttime . ";\n";
    echo "var servertime = ". time() . ";\n";
    echo "var mintime = ". $lesson->completiontimespent .";\n";
    echo "var savedtimespent = ". $timealreadyspent .";\n";
    echo "var totaltime = ". $lessontimespent .";\n";
    echo "document.write('<script type=\"text/javascript\" src=\"tst.js\"><\/script>');\n";
    echo "window.onload = function () { show_clock(); }";
    echo "</script>\n";
//
//////////////////////////////////////////////////////////*/

    var stopclock = 0;
    var myclock = '';
    var timespent, hours, minutes, secs;
    var javatimeDate = new Date();
    var javatime = javatimeDate.getTime();
    javatime = Math.floor(javatime / 1000);

    if (typeof(clocksettings) != 'undefined') {
        if (clocksettings.starttime) {
            starttime = parseInt(clocksettings.starttime);
        }
        if (clocksettings.servertime) {
            servertime = parseInt(clocksettings.servertime);
        }
        if (clocksettings.mintime) {
            mintime = parseInt(clocksettings.mintime);
        }
    }

    difference = javatime - servertime;
    starttime = starttime + difference;

    /*function leave() {  // this can run a script when someone leaves a the lesson page
        window.onbeforeunload = window.open('http://www.google.com','','toolbar=no,menubar=no,location=no,height=500,width=500');
    }
    leave();*/

    function show_clock(savedtimespent) {
        currentDate = new Date();
        current = currentDate.getTime();
        current = Math.floor(current / 1000);
         
        var mytime = '';
		var timespent = '';
            myclock = document.getElementById("lesson-timer");	
        timespent = Number(savedtimespent);
		//console.log(savedtimespent);
		//console.log(timespent);
		//savedtimespent++;
        if (timespent >= mintime) { //time already spent is equal to or exceeds the required minimum time, so stop the clock and tell student
        
            mytime += 'Time requirement is now complete';
            stopclock = 1;
        } else { // time already spent is not yet up to the required minimum so RESUME counting from this point
         var j=0;
		 for(var i = timespent; i<=mintime; i++)
		 {
		 
		 (function(i)
		 {
		 
		 setTimeout(function(){
		 
     		hours = Math.floor(i / 3600);
            i = i - (hours * 3600);
            minutes = ("0" + (Math.floor(i / 60))).slice(-2);
            secs = i - (minutes * 60);
			secs = ("0"+secs).slice(-2);
   
             mytime = minutes + ":" + secs;
		 myclock.innerHTML = mytime;
		 
		 },1000*j);
		   
		   
		   }(i));
		   j++;
		   }
        }
        

        if (stopclock == 0) {
            //setTimeout(show_clock(savedtimespent), 5000);
        }
}
