var pendorClock = {

    theDiv: null,
    timer: null,

    aiMonths: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ],
    aiPMonths: [0, 1, 25, 49, 73, 97, 121, 145, 146, 147, 171, 195, 211, 243, 267, 291, 292],
    asMNames: [ "Yestar", "Narrin", "Nenim", "Sulim", "Virta", "Lothess", "Narnya", "Attendes", "Loende", "Cerim", "Urim", "Yavar", "Narquel", "Hiss", "Ring", "Mettare"],
    asWNames: [ "Seren", "Anar", "Noren", "Aldea", "Erwer", "Elenya"],

    init: function() {
        pendorClock.theDiv = document.getElementById('pendordate');
        pendorClock.update();
        pendorClock.timer = setTimeout(pendorClock.update, 1250);
    },

    update: function() {
        var now = new Date();
        var hours = pendorClock.aiMonths[now.getMonth()] + now.getDate();
        if ((now.getMonth() > 2) && (now.getYear() % 4 == 0)) 
        {
            hours++;
        }
        hours = (hours * 24 + now.getHours()) - 16; /* DST Calculation, and wildly
                                              * wrong, but WTF */
        var year = now.getYear() + 16;

        var doy = hours / 30;
        hours = hours % 30;
        
        var seconds = ((now.getSeconds() + (now.getMinutes() * 60)) / 2.25);
        var minutes = seconds / 40;
        seconds = seconds % 40;

        var s = "The time on Pendor is " + hours.toFixed(0) + ":";
        if (minutes < 10) { s = s + "0"; }
        s = s + minutes.toFixed(0) + ':';
        if (seconds < 10) { s = s + "0"; }
        s = s + seconds.toFixed(0);

        for(var k1 = 0; k1 <= 17; k1++) {
            if(pendorClock.aiPMonths[k1] >= doy)
                break;
        }

        var dom = doy - pendorClock.aiPMonths[k1 - 1];
        var dow = (Math.ceil(dom) - 1) % 6;
        s = s + " on " + pendorClock.asWNames[dow] + ", " + 
            pendorClock.asMNames[k1 - 1] + " " + dom.toFixed(0) +
            ", 0" + year.toFixed(0);
        pendorClock.theDiv.innerHTML = s;
        clearTimeout(pendorClock.timer);
        pendorClock.timer = setTimeout(pendorClock.update, 1250);
        }
};

window.onload = function() { pendorClock.init(); };

