<!--

// timer.js library
// author: philip pang
// date created: 26 Jun 2007
// history:
// - 26/6/07 originally created
// - 17/7/07 prefixZero() replaced with simpler timerPrefixZero() to be self-contained
// - 17/7/07 timerGetNow() supports format 2

var _timerhdl = null;
var _timerTagID = "";
var _timerCountdownSecs = 0;
var _timerCountdownCBfunc = "";
var _timerElapseSecs = 0; // accumulated elapsed time in seconds
var _timerEpochSecs = null; // elapsed time in seconds since 1 Jan 1970


function timerClockStart (tagID) {
  _timerTagID = tagID;
  timerShow ("--:--:--");
  timerStart ("timerClockUpdate()");
}

function timerElapseStart(tagID) {
  _timerTagID = tagID;
  timerGetElapseSecs ("start");
  timerShow ("00:00:00");
  timerStart ("timerElapseUpdate()");
}

function timerCountdownStart (tagID,cbfunc,initialTimeSecs) {
  _timerTagID = tagID;
  _timerCountdownSecs = initialTimeSecs;
  _timerCountdownCBfunc = cbfunc;
  timerGetElapseSecs ("start");
  timerShow (timerFmtTime(initialTimeSecs,1));
  timerStart ("timerCountdownUpdate()");
}

function timerStart (timerEventHandler) {
  if (_timerhdl == null) {
    _timerhdl = setInterval (timerEventHandler,1000);
  }
}

function timerStop () {
  if (_timerhdl != null) {
    clearInterval (_timerhdl);
    _timerhdl = null;
  }
}

function timerShow (str) {
  document.getElementById(_timerTagID).innerHTML = str;
}

function timerClockUpdate() {
  timerShow (timerGetNow());
}

function timerElapseUpdate() {
  _timerElapseSecs += timerGetElapseSecs();
  timerShow (timerFmtTime(_timerElapseSecs,1));
}

function timerCountdownUpdate() {
  _timerCountdownSecs -= timerGetElapseSecs();
  timerShow (timerFmtTime(_timerCountdownSecs,1));
  if (_timerCountdownSecs <= 0) {
    timerStop ();
    if (_timerCountdownCBfunc != "") {
      eval (_timerCountdownCBfunc);
      _timerCountdownCBfunc = "";
    }
  }
}

//fmt 1: hh:mm:ss
//fmt 2: dd/month/yyyy hh:mm:ss
//fmt 3: yyyymodd
//fmt 4: yyyymodd@hhmm
function timerGetNow (fmt) {
  var tstamp = new Date();
  var hh = tstamp.getHours();
  var mm = tstamp.getMinutes();
  var ss = tstamp.getSeconds();
  var dd = tstamp.getDate();
  var mo = tstamp.getMonth()+1;
  var yy = tstamp.getFullYear();
  hh = timerPrefixZero (hh);
  mm = timerPrefixZero (mm);
  ss = timerPrefixZero (ss);
  dd = timerPrefixZero (dd);
  mo = timerPrefixZero (mo);
  if (fmt==1) {
    return hh + ":" + mm + ":" + ss;
  }
  if (fmt==2) {
    return dd + "/" + mo + "/" + yy + " " + hh + ":" + mm + ":" + ss;
  }
  if (fmt==3) {
    return "" + yy + mo + dd;	
  }
  if (fmt==4) {
    return "" + yy + mo + dd + "@" + hh + mm;	
  }
  return "-unknown time format-";
}

// initially: timerGetElapseSecs ("start");
// subsequent: var timeInSecs = timerGetElapseSecs () or timerGetElapseSecs (null);
// unit: seconds
function timerGetElapseSecs (flag) {
  var epochTime = (new Date()).getTime();
  if (flag!=null) {
    _timerEpochSecs = epochTime;
    return 0;
  }
  var tElapsed = epochTime - _timerEpochSecs;
  _timerEpochSecs = epochTime;
  return Math.floor(tElapsed/1000);
}

// fmt 1 => HH:MM:SS
// ss is in seconds
function timerFmtTime(ss,fmt) {
  var hh,mm;
  if (fmt==1) {
    hh = Math.floor (ss / 3600);
    ss = ss % 3600;
    mm = Math.floor (ss / 60);
    ss = ss % 60;

    hh = timerPrefixZero (hh);
    mm = timerPrefixZero (mm);
    ss = timerPrefixZero (ss);

    return hh + ":" + mm + ":" + ss;
  }
  return s;
}

// n is supposed to be either 1- or 2-digit only
// result is returned as a string
function timerPrefixZero (n) {
  if (n > 9) return "" + n;
  if (n < 10) return "0" + n;	
}

if (typeof(gsw_shout)!='undefined') alert ("timer.js ok");

//-->
