/*
 * CLOCK FORMAT: Jan. 05, 2010 08:18:00
 * Javascript object for implementing realtime clocks in a webpage.
 * A GMT clock is implemented if an element with id="gmt_clock" is present in
 * the webpage.
 * A system clock is implemented if an element with id="local_clock" is
 * present in the webpage.
 */

clock={

    // Properties
    gmtTimeout: 0,
    localTimeout: 0,

    // Array mapping month names
    months: ['Jan', 
        'Feb', 
        'Mar', 
        'Apr', 
        'May', 
        'Jun', 
        'Jul', 
        'Aug', 
        'Sep', 
        'Oct', 
        'Nov', 
        'Dec'],

/*
 * init()
 * Checks for W3C DOM support and allows for subsequent helper methods to
 * be loaded.
 */
init:function() {

         // Make sure we're W3C DOM compliant
         if (!document.getElementById || !document.getElementsByTagName) {
             return;
         }

         /* Display the GMT clock if an element with id="gmt_clock" is present
          * in the html markup.
          */
         var gmtClock = document.getElementById("gmt_clock");
         if (gmtClock) {
             clock.gmtTimeout = setInterval('clock.setGMTTime()', 1000);
         }

         /* Display the local clock if an element with id="local_clock" is
          * present in the html markup.
          */
         var localClock = document.getElementById("local_clock");
         if (localClock) {
             clock.localTimeout = setInterval('clock.setLocalTime()', 1000);
         }

     },

setGMTTime:function() {

                   var jsClock = document.getElementById("gmt_clock");
                   if (!jsClock) {
                       alert("no clock found!");
                       return;
                   }

                   // Initialize the data object
                   var now = new Date();

                   //var month = 1; // zero-based
                   //var day, year, hour, min, sec;
                   var month, day, year, hour, min, sec;

                   // Get date pieces
                   monthNum = now.getUTCMonth();
                   month   = clock.months[now.getUTCMonth()];
                   day     = now.getUTCDate();
                   year    = now.getUTCFullYear();
                   hour    = now.getUTCHours();
                   minutes = now.getUTCMinutes();
                   seconds = now.getUTCSeconds();

                   // Prepend necessary zeros
                   //month   = month < 10 ? "0" + month : month;
                   day     = day < 10 ? "0" + day : day;
                   hour    = hour < 10 ? "0" + hour : hour;
                   minutes = minutes < 10 ? "0" + minutes : minutes;
                   seconds = seconds < 10 ? "0" + seconds : seconds;

                   // Create time string
                   var ts = month + ". " + day + ", " + year;
                   ts += " " + hour + ":" + minutes + ":" + seconds + " UTC";
               
                   dom.setText(jsClock, ts);

           },

setLocalTime:function() {

                   var jsClock = document.getElementById("local_clock");
                   if (!jsClock) {
                       return;
                   }

                   // Initialize the data object
                   var now = new Date();

                   //var month = 1; // zero-based
                   var month, day, year, hour, min, sec;

                   // Get date pieces
                   monthNum = now.getUTCMonth();
                   month   = clock.months[now.getUTCMonth()];
                   day     = now.getDate();
                   year    = now.getFullYear();
                   hour    = now.getHours();
                   minutes = now.getMinutes();
                   seconds = now.getSeconds();

                   // Prepend necessary zeros
                   //month   = month < 10 ? "0" + month : month;
                   day     = day < 10 ? "0" + day : day;
                   hour    = hour < 10 ? "0" + hour : hour;
                   minutes = minutes < 10 ? "0" + minutes : minutes;
                   seconds = seconds < 10 ? "0" + seconds : seconds;

                   // Create time string
                   var ts = month + ". " + day + ", " + year;
                   ts += " " + hour + ":" + minutes + ":" + seconds;
               
                   dom.setText(jsClock, ts);

           }
}
// Intialize the object once the page has loaded.
dom.addEvent(window, 'load', clock.init, false);


