/**
 * Clock Plugin: Shows realtime clock
 * 
 * @file       clock/script.js
 * @brief      Show a clock in wikipage
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Luis Machuca B. <luis.machuca@gulix.cl>
 * @version    1.0
 * 
 *  This file contains the 
 *  default (stylized) visual component for the clock plugin.
 */

var dwClockFace;
var dwClockFace_spans= new Array();
var clock_Pieces= new Array();
var clock_Err= "Clock plugin error: ";
var clock_alerts= 0;

/**
 * @fn    dwclock_rerun
 * @brief Ticks the clock and updates the visual
 */
function dwclock_default_rerun () {

  // use the Date functions
  // to get the current time (hh:mm:ss)
  var msg="";
  // read the clock values
  // dwClockTimer.tick();
  var LH= dwClockTimer.hh;
  var LM= dwClockTimer.mm;
  var LS= dwClockTimer.ss;

  // check if we want local or UTC time
  if (clock_UTC) {
    LH= dwClockTimer.uh;
    msg="UTC<br/>";
    }
  // check if we want 24 hour or AM/PM
  if (clock_hour_mode=="ampm") {
    if (LH>=12 && LH <24) {
      LH= LH - 12;
      msg+="pm";
      }
    else {
      msg+="am";
      }
    } //end hourmode check

  // reformat the values as strings with 2 digits
  var SH= ((LH<10) ? "0" : "") + LH;
  var SM= ((LM<10) ? "0" : "") + LM;
  var SS= ((LS<10) ? "0" : "") + LS;

  clock_Pieces[0].innerHTML= SH;
  clock_Pieces[2].innerHTML= SM;
  clock_Pieces[4].innerHTML= SS;
  clock_Pieces[6].innerHTML= msg;

  setTimeout(dwclock_default_rerun, 1000);
}

/**
 * @fn    dwclock_init
 * @brief Ticks the clock and updates the visual
 */
function dwclock_default_init() {
  // check that we have availability of the JS functions
  // needed to locate and use the Clock Object in DOM
  //if (typeof document.getElementById != "function" ) 
  //  throw new Error (clock_Err + "init failed!\n cause: 'getElementById' not supported.");
  //if (typeof document.getElementsByTagName != 'function' )
  //  throw new Error (clock_Err + "init failed!\n cause: 'getElementsByTagName' not supported.");

  // find the Object we will work on
  // let's check for the needed functions before 
  dwClockDOMObject= $(jsclock_id);
  var clockfacename= '' + jsclock_id + '_face';
  if (dwClockDOMObject) {
    var elems= dwClockDOMObject.getElementsByTagName('div');
    // now we have the list of all children inside the clock object.
    // shall we find the one which has the class "face"
    if (elems.length == 0 ) {
      var errmsg= clock_Err + " no components found.";
      throw new Error (errmsg);
      }
    dwClockFace= $(clockfacename);
    
    // at this point, dwClockFace is the Face component
    // inside the object, there are at least __six__ children
    // with the following sequence:
    // hour  colon  min  colon  sec  <sep>  msg
    clock_Pieces= dwClockFace.getElementsByTagName('span');
    if (clock_Pieces.length < 2) {
      var errmg= clock_Err + "misconstructed clock face.";
      throw new Error (errmsg);
      }
    // clock_Pieces contains the valid (non-Phantom) spans

    // set up a delay for the timer feed to work on
    setTimeout(dwclock_default_rerun, 1000);
    }
  else { 
    // clock init failed
    errmsg= clock_Err + "init failed!";
    errmsg= errmsg+"\n(can't find container: "+jsclock_id+")";
    throw new Error(errmsg);
    }
}

addInitEvent(dwclock_default_init);
// end of clock/modules/default.js


