﻿


// HEADER / FOOTER / MENU

// Periodically refresh header and footer in cases something changes

var intervalTopBottomRefresh = 20;
var updaterTop = new Ajax.PeriodicalUpdater("layer_top", "top.php", { method: "get", frequency: intervalTopBottomRefresh });
var updaterBottom = new Ajax.PeriodicalUpdater("layer_bottom", "bottom.php", { method: "get", frequency: intervalTopBottomRefresh });
var updaterMenu = new Ajax.PeriodicalUpdater("layer_menu", "menu.php", { method: "get", frequency: intervalTopBottomRefresh });
showWelcome();

// CHANGES

// Query for possible changes and in case of a change query for the respective change

var intervalChangesRefresh = 1000;
setInterval(function() { new Ajax.Request("changes.php", { method: "get", onSuccess: onChangesUpdated, onException: onException }) }, intervalChangesRefresh);

// currentView holds what is currently visible in the main layer
// 0 = Welcome
// 1 = Recent Passings
// 2 = Live Classification Results
// 3 = Static Classification Results
// 4 = Certificates
var currentView = 0;
var lastView = -1;

var lastChangeClassificationResults = -1; // -2 means recently updated through showResults and therefore do only set to curreent value and not refresh
var lastChangeRecentPassings = -1;

function onChangesUpdated(http) {
    var json = http.responseText.evalJSON();
    var isRefreshing = false;
        
    if (currentView == 1 && lastChangeRecentPassings != json["recentPassingsChanged"]) 
    {  
        requestRecentPassings();
        lastChangeRecentPassings = json["recentPassingsChanged"];
        isRefreshing = true;
    }
    
    if (lastChangeClassificationResults == -2) 
    {
        lastChangeClassificationResults = json["classificationResultsChanged"];
    } 
    else if (currentView == 2 && lastChangeClassificationResults != json["classificationResultsChanged"])
    {        
        requestLiveClassificationResults(currentClassificationId);
        lastChangeClassificationResults = json["classificationResultsChanged"];
        isRefreshing = true;
    }
    
    lastView = currentView;
    
    if (isRefreshing) 
         $("layer_refreshing").show();
		 
	$("layer_clock").innerHTML = getTimeOfDay();
}

function showWelcome() {
    currentView = 0;
    lastView = 0;
    requestWelcome();
}

function showRecentPassings()
{
    $("layer_refreshing").show();
    currentView = 1;
    lastView = 1;
    requestRecentPassings();
}

function showLiveClassificationResults(classifId)
{
    $("layer_refreshing").show();
    lastChangeClassificationResults = -2;
    currentView = 2;
    lastView = 2;
    currentClassificationId = classifId;
    requestLiveClassificationResults();
}

function showStaticClassificationResults(classifId)
{
    $("layer_refreshing").show();
    lastChangeClassificationResults = -2;
    currentView = 3;
    lastView = 3;
    currentClassificationId = classifId;
    requestStaticClassificationResults();
}

function showCertificates() {
    currentView = 4;
    lastView = 4;
    requestCertificates();
}

// WELCOME

function requestWelcome() {
    new Ajax.Request("welcome.php", { method: "get", onSuccess: onWelcomeUpdated, onException: onException });
}

function onWelcomeUpdated(http) 
{
    var content = http.responseText;
    $("layer_main").innerHTML = content;
    $("layer_refreshing").hide();
}

// LIVE AND STATIC RESULTS

var currentClassificationId = 1;
var rowsToShowPerGroup = 10;

function requestLiveClassificationResults() {
    new Ajax.Request("results.php?classifId=" + currentClassificationId, { method: "get", onSuccess: onLiveClassificationResultsUpdated, onException: onException });
}

function requestStaticClassificationResults() {
    new Ajax.Request("results.php?classifId=" + currentClassificationId, { method: "get", onSuccess: onLiveClassificationResultsUpdated, onException: onException });
}

function onLiveClassificationResultsUpdated(http) 
{
    var json = http.responseText.evalJSON();
    var table = "<table class=\"results\">";
    var numberRows = json.rows ? json.rows.length : 0;
    var numberColumns = json.columns.length; // includes as last argument groups
    var lastGroups = "";
    var rowsInGroup = 0;
    var groupLinks = "<span id=\"groupLinks\">Gehe zu: </span>";
    for (var i=0; i<numberRows; i++) {
        // Check if normal row or new group first
        var currentGroup = json.rows[i][numberColumns - 1]
        if (lastGroups != currentGroup) {
            table += "<tr><td colspan=\"" + (numberColumns - 3) +  "\" class=\"group\" id=\"" + currentGroup + "\">" + currentGroup + "</td><td class=\"up\"><a href=\"#layer_top\" colspan=\"2\"><img src=\"images/up.png\" border=\"0\"></a></td></tr>";
            lastGroups = currentGroup;
            rowsInGroup = 0;
            groupLinks += "<a href=\"#" + currentGroup + "\">" + currentGroup + "</a>   |   ";
        }       
		if (currentView == 3 || rowsInGroup < rowsToShowPerGroup) {
            table += "<tr class=\"" + (i % 2 == 0 ? "zebra" : "nonzebra") + "\">";
            for (var j=1; j<numberColumns - 1; j++) {
               table += "<td class=\"" + json.columns[j] +  "\">" + json.rows[i][j]  + "</td>";  
            }  
        }
        table += "</tr>";
        rowsInGroup++;
    }
    table += "</table>"
    table = groupLinks + "<br>" + table;
    $("layer_main").innerHTML = table;
    $("layer_refreshing").hide();
}

// RECENT PASSINGS

function requestRecentPassings() {
    new Ajax.Request("recentpassings.php", { method: "get", onSuccess: onRecentPassingsUpdated, onException: onException });
}

function onRecentPassingsUpdated(http) 
{
    var json = http.responseText.evalJSON();
    var table = "<table class=\"results\">";
    var numberRows = json.rows ? json.rows.length : 0;
    var numberColumns = json.columns.length; 
    var lastGroups = "";
    var rowsInGroup = 0;
    for (var i=0; i<numberRows; i++) {
		var color = json.rows[i][numberColumns];
        table += "<tr class=\"" + (i % 2 == 0 ? "zebra" : "nonzebra") + "\" style=\"" + (color != "" ? "background-color:" + color : "") + "\">";
        for (var j=0; j<numberColumns; j++) {
           table += "<td class=\"" + json.columns[j] +  "\">" + json.rows[i][j]  + "</td>";  
        }  
        table += "</tr>";
        rowsInGroup++;
    }
    table += "</table>"
    $("layer_main").innerHTML = table;
    $("layer_refreshing").hide();
}

// CERTIFICATES

function requestCertificates() {
    new Ajax.Request("certificates.php", { method: "get", onSuccess: onCertificatesUpdated, onException: onException });
}

function onCertificatesUpdated(http) 
{
    var content = http.responseText;
    $("layer_main").innerHTML = content;
    $("layer_refreshing").hide();
}


// Assorted

function onException(http, ex) {
    throw(ex); //(ex.message + "\n\n" + ex.stack);
}

// Clock

function getTimeOfDay() {
	var hours, minutes, seconds;
	var intHours, intMinutes, intSeconds;
	var today;

	today = new Date();
	intHours = today.getHours();
	intMinutes = today.getMinutes();
	intSeconds = today.getSeconds();

	hours = intHours+":";
	if (intMinutes < 10) {minutes = "0"+intMinutes;}
	else {minutes = intMinutes;}
	/* minutes = minutes + ";"
	if (intSeconds < 10) {seconds = "0"+intSeconds+" ";}
	else {seconds = intSeconds+" ";} */
	return hours+minutes;
}

