// time management

// save time in preferences
// save time in variable
// stop time on widget off
// clear time on reset
// save time since submit

if (window.widget){
    widget.onhide = onhide;
    widget.onshow = onshow;
}

/*





var timer;



*/
var counttimer = null;
function startCounting(){
counttimer = setInterval("addSecond()", 1000);
}

function onhide(){
    if(counttimer != 0){
        clearInterval(counttimer);
//        counttimer = null;
    }
}

function onshow(){
	
   if(counttimer != 0){
	       clearInterval(counttimer);
	}
   counttimer = setInterval("addSecond()", 1000);
}

var seconds = 0;
var localtime = 0;
/*
function showRuntime(){
	showInformation('<br> I have been running for '+seconds+' seconds',true, 1, true); // text, fade in, extraicon, closebutton			
}
*/
function addSecond(){
	seconds++;
	localtime++;
	document.getElementById("debug_window").innerHTML = seconds;	
	if(localtime == 60*60){
		// if you have been playing for an hour
		localtime = 0;
		showInformation('<br> I have been running for '+time(seconds*1000)+'. Time for a break?',true, 1, true); // text, fade in, extraicon, closebutton								
//		showPlaytime()
	}
	
}

function showPlaytime(){

	showInformation('<br> I have been running for: '+time(seconds*1000),true, 1, true); // text, fade in, extraicon, closebutton								
}

function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function time(ms) {
var sec = Math.floor(ms/1000)

var min = Math.floor(sec/60)
sec = sec % 60
t = two(sec) ;

var hr = Math.floor(min/60)
min = min % 60
t = two(min) + ":" + t

var day = Math.floor(hr/60)
hr = hr % 60
t = two(hr) + ":" + t
t = day + ":" + t

return t
}