var localTime;
var clockOffset;
var clockExpirationLocal;
var clockTimerID = null;
function clockInit(localDateObject, serverDateObject) {
  var origRemoteClock = parseInt(clockGetCookieData("remoteClock"));
  var origLocalClock = parseInt(clockGetCookieData("localClock"));
  var newRemoteClock = serverDateObject.getTime();
  var newLocalClock = localDateObject.getTime();
  var maxClockAge = 60 * 60 * 1000;
  if (newRemoteClock != origRemoteClock) {
    document.cookie = "remoteClock=" + newRemoteClock;
    document.cookie = "localClock=" + newLocalClock;
    clockOffset = newRemoteClock - newLocalClock;
    clockExpirationLocal = newLocalClock + maxClockAge;
    localTime = newLocalClock;
  }
  else
    if (origLocalClock != origLocalClock) {
      clockOffset = null;
      clockExpirationLocal = null;
    }
    else {
      clockOffset = origRemoteClock - origLocalClock;
      clockExpirationLocal = origLocalClock + maxClockAge;
      localTime = origLocalClock;
    }
  var nextDayLocal = (new Date(serverDateObject.getFullYear(),serverDateObject.getMonth(),serverDateObject.getDate() + 1)).getTime() - clockOffset;
  if (nextDayLocal < clockExpirationLocal)
      clockExpirationLocal = nextDayLocal;
}
function clockClearTimeout() {
  if (clockTimerID) {
    clearTimeout(clockTimerID);
    clockTimerID = null;
  }
}
function clockTimeString(inHours, inMinutes, inSeconds) {
  return inHours == null ? "-:--" : ((inHours == 0
    ? "12" : (inHours <= 12 ? inHours : inHours - 12))
      + (inMinutes < 10 ? ":0" : ":") + inMinutes
      + (clockShowSeconds
        ? ((inSeconds < 10 ? ":0" : ":") + inSeconds) : "")
      + (inHours < 12 ? " AM" : " PM"));
}
function clockDisplayTime(inHours, inMinutes, inSeconds) {
  clockWriteToDiv("psClockMod", clockTimeString(inHours, inMinutes, inSeconds));
}
function clockWriteToDiv(divName, newValue) {
  var divObject = simpleFindObj(divName);
  if (divObject && divObject.innerHTML)
    divObject.innerHTML = newValue;
  else
    if (divObject && divObject.document) {
      divObject.document.writeln(newValue);
      divObject.document.close();
    }
}
function clockGetCookieData(label) {
  var c = document.cookie;
  if (c) {
    var labelLen = label.length, cEnd = c.length;
    while (cEnd > 0) {
      var cStart = c.lastIndexOf(';',cEnd-1) + 1;
      while (cStart < cEnd && c.charAt(cStart)==" ")
        cStart++;
      if (cStart + labelLen <= cEnd && c.substr(cStart,labelLen) == label) {
        if (cStart + labelLen == cEnd)
          return "";
        else
          if (c.charAt(cStart+labelLen) == "=")
            return unescape(c.substring(cStart + labelLen + 1,cEnd));
      }
      cEnd = cStart - 1;
    }
  }
  return null;
}
function clockUpdate() {
  var lastLocalTime = localTime;
  localTime = (new Date()).getTime();
  if (clockOffset == null)
    clockDisplayTime(null, null, null);
  else
    if (localTime < lastLocalTime || clockExpirationLocal < localTime) {
      document.cookie = 'remoteClock=-';
      document.cookie = 'localClock=-';
      location.reload();
    }
  else {
    var serverTime = new Date(localTime + clockOffset);
    clockDisplayTime(serverTime.getHours(), serverTime.getMinutes(), serverTime.getSeconds());
    clockTimerID = setTimeout("clockUpdate()", clockIncrementMillis - (serverTime.getTime() % clockIncrementMillis));
  }
}
function simpleFindObj(name,inLayer) {
  return document[name] || (document.all && document.all[name])
    || (document.getElementById && document.getElementById(name))
    || (document.layers && inLayer && document.layers[inLayer].document[name]);
}
