/*
    Original:  Tomleung (lok_2000_tom@hotmail.com) This tag should not be removed
    Server time ticking clock v2.0 Updated by js-x.com

    -------

    Updated & rewritten for internal use by RL.SE in April/May 2009:

    o   Clock unnecessarily waited 1s before first display of time. Fixed,

    o   Removed unused functions MakeArrayday() & MakeArraymonth()

    o   Unused variable runTime removed

    o   Uses universal 24 hour format instead of ancient AM/PM

    o   Code beautified / indented / made more readable

    o   Added rl_adjust() for setting/adjusting the time from
        outside of JavaScript. The calling routine in e.g. PHP
        should await the beginning of the second before calling
        rl_adjust()

    o   Added showPCtime() to also show client's own time

    o   Tested FF3.1 and FF3.5 (and IE8, if someone still uses IE...)

    o   Added larger clock as a popup for those with low vision

*/

var     hours;
var     minutes;
var     seconds;
var     timer = null;
var     timer2 = null;


function twoDigit(v)                                // Prepend units with a zero
{
    if(v<10)
        v="0"+v;

    return v;
}


function showPCtime()                               // Plain vanilla clock
{
    var ct = new Date();                            // Get PC's own time
    var ch = ct.getHours();
    var cm = ct.getMinutes();
    var cs = ct.getSeconds();

    ch = twoDigit(ch);                              // Format nicely...
    cm = twoDigit(cm);
    cs = twoDigit(cs);

    var rs = ch + ":" + cm + ":" + cs ;             // Resulting string

    document.getElementById("pctime").innerHTML = rs;
}



function work()                                     // Called by timer every second
{
    var shours = hours;
    var sminutes = minutes;
    var sseconds = seconds;
    var movingtime;

    if (!document.layers && !document.all && !document.getElementById)
        return;                                     // I give up...

    sminutes = twoDigit(sminutes);
    sseconds = twoDigit(sseconds);
    shours   = twoDigit(shours);

    //  Assemble time, forcing it into a string
    //
    movingtime = "" + shours + ":" + sminutes + ":" + sseconds+"" ;

    document.getElementById("clock").innerHTML = movingtime;

    if(++seconds>59)                                // Handle rollovers
    {
        seconds=0;                                  // New minute

        if(++minutes>59)                            // New hour
        {
            minutes=0;

            if(++hours>23)                          // New day
                hours=0;
        }
    }
}


function rl_adjust(hh, mm, ss)                      // Call from PHP on page reload
{
    hours = hh;                                     // Feed counters w/ real time
    minutes = mm;
    seconds = ss;

    if (timer)                                      // Is there an old timer alive?
    {
        clearInterval(timer);                       // If so, get rid of it
        timer=null;
    }

    timer = setInterval("work();", 1000);           // Start a fresh 1 sec timer

    work();                                         // Don't wait, show now!

    if (timer2)                                     // The same for PC's clock
    {
        clearInterval(timer2);
        timer2=null;
    }

    timer = setInterval("showPCtime();", 333);      // Start a fresh 1/3 sec timer
}



function bautapopup()                               // A "slightly" bigger clock
{
    if (! window.focus)                             // WTF..?
        return true;

    var hojd   = screen.height ;
    var bredd  = screen.width ;
    var opts, wh;

    opts = ",scrollbars=no,status=0 ";

    var wparam = 'width=' + bredd + ',height=' + hojd + opts;

    wh = window.open('stor-klocka.php', 'Yadayada', wparam);
    wh.moveTo(0,0);
    return false;
}
