/*
 * Julppu: the scoreboard script, 
 * automatically refreshes the page with the page of the next comp. class. 
 * 
 */

window.onload = julppuSetup; 
window.onunload = julppuCleanup; 

var JULPPU = {
    /*
    play_src:         "pics/play.gif", 
    play_hover_src:   "pics/play_hover.url", 
    pause_src:        "pics/pause.gif", 
    pause_hover_src:  "pics/pause_hover.gif",
    reload_src:       "pics/refresh.gif",
    reload_hover_src: "pics/refresh_hover.gif",
    */
}; 


function julppuSetup() {

    // Static stuff 
    JULPPU.timer_interval = 1000; // in ms 
    // ... 

    // Current status variables
    JULPPU.timer_counter = 0; 
    JULPPU.current_url = 0; 
    JULPPU.play = true; 

    JULPPU.play_button = document.getElementById("play_button"); 
    JULPPU.reload_button = document.getElementById("reload_button"); 

    // Set up event handler and timers
    updateClock(); 

    JULPPU.play_button.onclick = playClicked; 
    JULPPU.play_button.onmouseover = playMouseover; 
    JULPPU.play_button.onmouseout = playMouseout; 

    JULPPU.reload_button.onclick = reloadClicked; 
    JULPPU.reload_button.onmouseover = reloadMouseover; 
    JULPPU.reload_button.onmouseout = reloadMouseout; 

    JULPPU.timer = setInterval("julppuTimer()", JULPPU.timer_interval); 
    //JULPPU.timer = setTimeout("julppuTimer()", JULPPU.timer_interval); 
}


function julppuCleanup() {
    // Any cleanup that we need to do? 
    // Event handlers of the play & refresh buttons? 
}


function playClicked() {
    if (JULPPU.play) {
	JULPPU.play_button.src = JULPPUCONF.pics_path + "play_hover.gif"
	JULPPU.play = false; 
    }
    else {
	JULPPU.play_button.src = JULPPUCONF.pics_path + "pause_hover.gif"
	JULPPU.play = true; 
    }
    //alert("PLAY")
}


function playMouseover() {
    if (JULPPU.play) {
	JULPPU.play_button.src = JULPPUCONF.pics_path + "pause_hover.gif"
    }
    else {
	JULPPU.play_button.src = JULPPUCONF.pics_path + "play_hover.gif"
    }
}


function playMouseout() {
    if (JULPPU.play) {
	JULPPU.play_button.src = JULPPUCONF.pics_path + "pause.gif"
    }
    else {
	JULPPU.play_button.src = JULPPUCONF.pics_path + "play.gif"
    }
}


function reloadClicked() {
    window.location.reload();
}


function reloadMouseover() {
    JULPPU.reload_button.src = JULPPUCONF.pics_path + "reload_hover.gif"
}


function reloadMouseout() {
    JULPPU.reload_button.src = JULPPUCONF.pics_path + "reload.gif"
}


function getCurrentTimeStamp() {
    // Returns the current timestamp as hh:mm:ss
    var date = new Date(); 
    var iSecs  = date.getSeconds(); 
    var iMins  = date.getMinutes(); 
    var iHours = date.getHours(); 
    var sSecs  = "" + ((iSecs > 9)  ? iSecs  : "0" + iSecs);
    var sMins  = "" + ((iMins > 9)  ? iMins  : "0" + iMins);
    var sHours = "" + ((iHours > 9) ? iHours : "0" + iHours);
    return sHours + ":" + sMins + ":" + sSecs;
}


function updateClock() {
    document.getElementById("julppu-clock").innerHTML = getCurrentTimeStamp(); 
}


function loadNextPage() {
    window.location.replace(JULPPUCONF.next_url);
}


function julppuTimer() {
    var progress_id = ""; 
    var progress_num = 0; 
    var progress_step_len = 0; 
    var pic; 

    if (JULPPU.play) {

	JULPPU.timer_counter += 1; 

	progress_step_len = JULPPUCONF.refresh_interval / 
	    JULPPUCONF.progress_steps; 
	progress_num = 
	    Math.floor(JULPPU.timer_counter / progress_step_len).toFixed(0);

	// try to update all the previous ones in case there are more 
	// than one progress bar steps per our timer interval
	for (var i = 1; i <= progress_num; i++) {
	    progress_id = "progress" + i.toString(); 
	    pic = document.getElementById(progress_id); 
	    if (pic) {
		pic.src = JULPPUCONF.pics_path + "progress_on.gif"; 
	    }
	}

	// for testing purposes, remove later
	//document.getElementById("j-counter").innerHTML = JULPPU.timer_counter; 

	if (JULPPU.timer_counter * JULPPU.timer_interval 
	    >= JULPPUCONF.refresh_interval * 1000) {
	    loadNextPage();
	    JULPPU.timer_counter = 0; 
	}
    }

    updateClock(); 

    //alert("Terve taas!"); 
    //window.location.reload();

    //document.getElementById("otsikko").style.color = "#ff0000";
    //document.writeln("<p>Terve</p>");
}


