// This script will put a clock on your page that shows a visitor the
// time in your timezone, rather than the visitor's timezone. This
// makes it easier for people to tell if it's an appropriate time to
// call you.
//
// Just include a line like this in your HTML:
//
//   <script src="http://eugenia.tumanova.org/clock.js?tz=-5"></script>
//
// The "tz=-5" indicates Eastern Time, which is 5 hours behind GMT. Just
// change the -5 to whatever your timezone offset is.
//
// The clock will display where you put a tag like this:
//
//   <div id="current-time">(time)</div>
//
// To change your available hours, use the "open" and "close" parameters.
// For example, if you start at 7am and end at 6pm:
//   .../clock.js?tz=-5&open=7&close=18
//
// If you want to display seconds, add &seconds=yes to your script src,
// something like:
//   .../clock.js?tz=-5&seconds=yes
//
// If you need a different ID for the clock element, use something like:
//   .../clock.js?tz=-5&field=myclock

// Run the timer!
function clock_timer()
{
    var options=clock_config();

    var field=document.getElementById(options.field);

    var date=new Date();
    var hours=date.getUTCHours();
    var minutes=date.getUTCMinutes();
    var seconds=date.getUTCSeconds();

    // Second Sunday in March to first Sunday in November
    var beginDST=new Date();
    var endDST=new Date();
    beginDST.setMonth(2);
    beginDST.setDate(1);
    beginDST.setDate(beginDST.getDate()+((7-beginDST.getDay())%7)+7);
    endDST.setMonth(10);
    endDST.setDate(1);
    endDST.setDate(endDST.getDate()+((7-endDST.getDay())%7));

    if(beginDST<=date && date<=endDST) options.tz++;

    hours=(hours+options.tz+24)%24;
    var ap=hours<12?"am":"pm";

    // After midnight is 12 AM, after noon is 12 PM
    var aphour=hours%12;
    if (aphour==0) aphour=12;

    // Display the time
    field.firstChild.nodeValue=(aphour<10?"  ":"")+aphour +":"
        +(minutes<10?"0":"")+minutes
        +(options.seconds?(":"+(seconds<10?"0":"")+seconds):"")
        + " " + ap;

    // Change the class if we're during off-hours
    if(hours<options.open || hours>options.close)
        document.getElementById("current-time").className="offhours";

    // Update the timer once a minute, or once a second if they're displayed
    setTimeout('clock_timer()', (options.seconds?1:60)*1000);
}

// Read options from the script tag's src attribute
function clock_config() {
    var config={
        seconds: false,
        field: 'current-time',
        open: 5,
        close: 19
    };

    var query;
    var scripts=document.getElementsByTagName("script");
    for(var i=0;i<scripts.length;i++) {
        if(scripts[i].src.match(/tumanova\.org\/clock\.js/)) {
            query=scripts[i].src.replace(/^.*clock\.js\?/, '');
            break;
        }
    }

    var options=query.split(/&/);
    var option;
    for(var i=0;i<options.length;i++) {
        option=options[i].split(/=/);
        if(option[1]=="yes")   option[1]=true;
        if(option[1]=="no")    option[1]=false;
        if(option[0]=="tz")    option[1]=parseInt(option[1]);
        if(option[0]=="open")  option[1]=parseInt(option[1]);
        if(option[0]=="close") option[1]=parseInt(option[1]);
        config[option[0]]=option[1];
    }
    return config;
}

// Simon Willison's addLoadEvent method
function clock_addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

// Queue the timer up to run when the body loads
clock_addLoadEvent(clock_timer);

