/* $Id: timegoeson.js 411 2008-10-01 15:04:11Z thz $ */
/*
 * Time Goes On
 * Copyright © 2008 by Tim Huetz. All rights reserved.
 *
 * The copyright to the contents herein is the property of the person listed above (further known
 * as 'copyright holder'). The contents may be used and/or copied only with the written permission of
 * the copyright holders, or in accordance with the terms and conditions stipulated in the 
 * agreement/contract under which the contents have been supplied.
 **/
var g_tgoWatchingSeconds = 0;
var g_tgoVersion = "1st October 2008";
 
/**
 * \brief Timer method to update all fiels of the page.
 * \author Tim Huetz
 * \date 09/29/2008
 */
function updateWorldEvents() {
    /* loop through all entries we have inside our database and update their counter */
    for( var i = 0; i < g_tgoEntries; i++ ) {
        g_tgoDataset[ i ][ TGO_DATABASE_INDEX_COUNTDOWN ]--;
        if( g_tgoDataset[ i ][ TGO_DATABASE_INDEX_COUNTDOWN ] <= 0 ) {
            g_tgoDataset[ i ][ TGO_DATABASE_INDEX_COUNTDOWN ] = g_tgoDataset[ i ][ TGO_DATABASE_INDEX_XSECONDS ]; // reset the counter to its initial value
            g_tgoDataset[ i ][ TGO_DATABASE_INDEX_OCCURRED ]++; // increment the count of this event
            
            /* update the current entry on the page */
            var elementToUpdate = document.getElementById( "infoEntry_" + i );
            var countElementToUpdate = null;
            if( !elementToUpdate || !( countElementToUpdate = elementToUpdate.childNodes[ 0 ] ) ) {
                window.clearInterval( g_updateWorldEventsId );
                window.clearInterval( g_updateWatchingTimeId );
                alert( "There was an error during updating element number " + (i+1) + ". The web-application was stopped!" );
                return;
            }
            countElementToUpdate.firstChild.data = "... " + g_tgoDataset[ i ][ TGO_DATABASE_INDEX_OCCURRED ];
        }
    }
}

/**
 * \brief Timer method to update the time the user is watching the current page.
 * \author Tim Huetz
 * \date 09/29/2008
 */
function updateWatchingTime() {
    g_tgoWatchingSeconds++;
    
    /* calculate the hours, minutes and seconds */
    var watchedHours = Math.floor( g_tgoWatchingSeconds / 3600 );
    var watchedMinutes = Math.floor( ( g_tgoWatchingSeconds % 3600 ) / 60 );
    var watchedSeconds = Math.floor( g_tgoWatchingSeconds - ( 60 * watchedMinutes + 3600 * watchedHours )  );
    
    /* try to get the element which is used to display the time the user is watching this page  */
    var rootElement = document.getElementById( "running" );
    if( !rootElement ) {
        window.clearInterval( g_updateWorldEventsId );
        window.clearInterval( g_updateWatchingTimeId );
        alert( "ERROR" );
    }
    
    /* update the time the user is watching this page */
    rootElement.firstChild.data = watchedHours + " hours, " + watchedMinutes + " minutes, " + watchedSeconds + " seconds";
}

/**
 * \brief Create the basic layout of all database entries.
 * \author Tim Huetz
 * \date 09/29/2008
 */
function initializePage() {
    /* try to get the root div-element which will wraped around all other data-entry elements and some other elements we need */
    var rootElement = document.getElementById( "what_happened" );
    var datasetVersionElement = document.getElementById( "dataset_version" );
	var scriptVersionElement = document.getElementById( "script_version" );
    if( !rootElement || !datasetVersionElement || !scriptVersionElement ) {
        window.clearInterval( g_updateWorldEventsId );
        window.clearInterval( g_updateWatchingTimeId );
        alert( "It seems that there was an error during the initialization process. The web-application cannot be executed in the currently used browser." );
    }
    
    /* update some other fields of the page */
    datasetVersionElement.firstChild.data = "Last database update on " + g_tgoDatabaseVersion + ". ";
	scriptVersionElement.firstChild.data = "Last script update on " + g_tgoVersion + ". ";
    
    /* add div-layers for all entries inside our database */
    for( var i = 0; i < g_tgoEntries; i++ ) {
        rootElement.innerHTML += "<div id=\"infoEntry_" + i + "\" />";
        rootElement.lastChild.innerHTML += "<span name=\"count\">... " + g_tgoDataset[ i ][ TGO_DATABASE_INDEX_OCCURRED ] + "</span>";
        rootElement.lastChild.innerHTML += "<span name=\"event_text\">&nbsp;" + g_tgoDataset[ i ][ TGO_DATABASE_INDEX_EVENT ] + "</span>";
        rootElement.lastChild.innerHTML += "<span name=\"occurs_every\">&nbsp;(every " + g_tgoDataset[ i ][ TGO_DATABASE_INDEX_XSECONDS ] + " seconds)</span>";
        rootElement.lastChild.innerHTML += "<span name=\"source\" style=\"visibility:hidden;\">" + g_tgoDataset[ i ][ TGO_DATABASE_INDEX_SOURCE ] + "</span>";
    }
}

/* initialize the timer to update all fields of the page */
var g_updateWorldEventsId = window.setInterval( updateWorldEvents, 1000 );
var g_updateWatchingTimeId = window.setInterval( updateWatchingTime, 1000 );