/*/////////////////////////////////////////////////////////
// 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->timelimit .";\n";
//        echo "document.write('<script type=\"text/javascript\" src=\"liveclock_lite.js\"><\/script>');\n";
//        echo "window.onload = function () { show_clock(); }";
//    echo "</script>\n";
//
//////////////////////////////////////////////////////////*/

    var stopclock = 0;
    var myclock = '';
    var timeleft, hours, minutes, secs, clockTimer;
    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.testlength) {
            testlength = parseInt(clocksettings.testlength);
        }
    }
//console.log(JSON.stringify(clocksettings));
    difference = javatime - servertime;
    starttime = starttime + difference;

    /*function leave() {  // feable attempt to run a script when someone leaves a timed test early, failed so far
        window.onunload = window.open('http://www.google.com','','toolbar=no,menubar=no,location=no,height=500,width=500');
        
    }
    leave();*/

    function show_clock() {

        currentDate = new Date();
        current = currentDate.getTime();
        current = Math.floor(current / 1000);

        var mytime = '',
           // myclock = document.getElementById("lesson-timer");
           myclock = document.getElementById("countdown");
           timereq = document.getElementById("timereq");
        if (current > starttime + testlength) {
            mytime += 'Time requirement is completed';//M.util.get_string('timeisup', 'lesson');
            stopclock = 1;
        } else {
            timeleft = starttime + testlength - current;
            if (timeleft < 60) {
                myclock.className = "timeleft1";
            }
            hours = Math.floor(timeleft / 3600);
            timeleft = timeleft - (hours * 3600);
            minutes = Math.floor(timeleft / 60);
            secs = timeleft - (minutes * 60);

            if (secs < 10) {
                secs = "0" + secs;
            }
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if(hours==0){
                mytime +=  minutes + ":" + secs;
            }else{
                mytime += hours + ":" + minutes + ":" + secs;
            }
        }
console.log(mytime+"=="+stopclock);
        myclock.innerHTML = mytime;
        timereq.innerHTML = msToTime(mintime);

        if (stopclock == 0) {
            clockTimer = setTimeout("show_clock()", 1000);
        }
}
function successful_listener(e) {
  console.log("would successfully fire!");
  //Error here Clear Time out is not happening at all and timer is still running
  clearTimeout(clockTimer);
}

function msToTime(duration) {
	var milliseconds = parseInt((duration%1000)/100)
		, seconds = parseInt((duration/1000)%60)
		, minutes = parseInt((duration/(1000*60))%60)
		, hours = parseInt((duration/(1000*60*60))%24);
			
	hours = (hours < 10) ? "0" + hours : hours;
	minutes = (minutes < 10) ? "0" + minutes : minutes;
	seconds = (seconds < 10) ? "0" + seconds : seconds;
					
	return minutes + ":" + seconds ;
}

// both ways to register both beforeunload And unload events would work with window
window.addEventListener("beforeunload", successful_listener);
window.addEventListener("unload", successful_listener);
window.onbeforeunload = successful_listener;
window.onunload = successful_listener;


;