/*
 * 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,

/*
 * 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 clock = document.getElementById("gmt_clock");
                   if (!clock) {
                       return;
                   }

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

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

                   // Get date pieces
                   month   += 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;
               
                   dom.setText(clock, ts);

           },

setLocalTime:function() {

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

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

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

                   // Get date pieces
                   month   += now.getMonth();
                   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(clock, ts);

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

