/*
 * Determines the style to display the page in.
 */

// Name of Vana'diel days - note that these days are the value we give to the
// "day" attribute on our time marker to set the day icon.

var vanaDays = [ "fire", "earth", "water", "wind", "ice", "thunder", "light", "dark" ];

var vanadielOffset = 0;
var vanadielLastDay = -1;

{
    // Calculate the offset to convert to Vana'diel time for the current time zone

    // I, unfortunately, have no idea who originally came up with the following.
    // Note that it has since been converted to GMT.
    
    // Earth time:       2002/06/23 15:00:00 GMT
    // Vana'diel time:   0898/02/01 00:00:00 (VST?)

    var vanaTime  = (898 * 360 + 30) * 24 * 60 * 60 * 1000 / 25;
    var baseDay = new Date();
    baseDay.setUTCFullYear(2002, 5, 23); // Set date to 2003-06-23
    baseDay.setUTCHours(15, 0, 0, 0);    // Set time to 15:00:00.0000

    vanadielOffset = baseDay.getTime() - vanaTime;
}

function initBackground() {
    // see if the search string (minus the ?)
    if (window.location.search.length > 0) {
	var elem = window.location.search;
	if (window.location.search.charAt(0) == '?') {
	    elem = elem.substring(1);
	}
	for (e in vanaDays) {
	    if (elem == vanaDays[e]) {
		document.body.setAttribute("class", elem);
		return;
	    }
	}
    }
    updateBackground();
}

function updateBackground() {
    // Vana'diel time runs 25 times faster than our time - this is the time
    // in "Vana'diel milliseconds" actually.
    var vanadielTime = (new Date().getTime() - vanadielOffset) * 25;
    // We only display the day, hour, and minute in the little clock
    var vanadielDay = Math.floor(vanadielTime / 86400000);
    var vanadielDayWeek = vanadielDay % 8;
    document.body.setAttribute("class", vanaDays[vanadielDayWeek]);
    // set a timer to update the page the next day rollover
    // How much time until then?
    var timeout = Math.floor((((vanadielDay + 1) * 86400000) - vanadielTime)/25);
    setTimeout("updateBackground()", timeout);
}

