// Multiple Analog Clock Script
// copyright Stephen Chapman, 27th October 2005
// you may copy this clock provided that you retain the copyright notice
var dayname = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');

var am = 'AM';
var pm = 'PM';

var pi = Math.PI;
var d = document;
var pi2 = pi/2;
var clocknum = [[,1,2,3,4,5,6,7,8,9,10,11,12],[,'I','II','III','IIII','V','VI','VII','VIII','IX','X','XI','XII'],[,'&middot;','&middot;','-','&middot;','&middot;','<span style="font-size:60%">|</span>','&middot;','&middot;','-','&middot;','&middot;','<span style="font-size:60%">||</span>']];
var comstyle = 'position:absolute;top:0px;left:0px;visibility:hidden;';
var clocks = [];

function timeZone(now,loc,mtz,dst) {
  if (loc) {
    var dow = now.getDay();
    var second = now.getSeconds();
    var minute = now.getMinutes();
    var hour = now.getHours();
  } else {
    now.setUTCMinutes(now.getUTCMinutes() + (mtz + dst)*60);
    var dow = now.getUTCDay();
    var second = now.getUTCSeconds();
    var minute = now.getUTCMinutes();
    var hour = now.getUTCHours();
  }
  if (hour > 11) {
    moa = pm;
    hour -= 12;
  } else
    moa = am;
  return [dow,moa,hour,minute,second];
}

function clock(num,clocksize,colnumbers,colseconds,colminutes,colhours,numstyle,font_family,localZone,mytimezone,dst,city,country,fix,xpos,ypos)
{
  clocks.push(this);
  this.num = num;
  this.clocksize=clocksize;
  this.colnumbers=colnumbers;
  this.colseconds=colseconds;
  this.colminutes=colminutes;
  this.colhours=colhours;
  this.numstyle = numstyle;
  this.font_family = font_family;
  this.localZone = localZone;
  this.mytimezone = mytimezone;
  this.dst = dst;
  this.city = city;
  this.country = country;
  this.fix = fix;
  this.xpos=xpos;
  this.ypos=ypos;
  this.rad = parseInt(this.clocksize) / 2;
  this.ctrX = parseInt(this.xpos) + this.rad;
  this.ctrY = parseInt(this.ypos) + this.rad;
  this.hourln = 1;
  this.minln = this.secln = 2;
  for(var i = 0; i < (this.rad / 2) + (this.rad / 16); i++) {
    this.hourln += 1;
  }
  for(var i = 0; i < (this.rad / 2) - (this.rad / 8); i++) {
    this.minln += 2;
    this.secln += 2;
  }
  this.font_size = this.rad / 4;
  this.offset = 16;
  if (this.numstyle < 0 || this.numstyle > 2)
    this.numstyle = 0;
}

clock.prototype.updateClock = function() {
  var now = new Date();
  var theTime = timeZone(now,this.localZone,this.mytimezone,this.dst);
  d.getElementById('ampm' + this.num).style.top = (this.ypos + this.rad/3)+'px';
  d.getElementById('ampm' + this.num).innerHTML = theTime[1] + '<br /><br /><br/>' + dayname[theTime[0]];
  d.getElementById('ampm' + this.num).style.visibility = 'visible';
  if (!this.localZone) {
    d.getElementById('zone' + this.num).style.top = (this.ctrY + (this.rad/10))+'px';
    d.getElementById('zone' + this.num).innerHTML = this.city + '<br />' + this.country;
    d.getElementById('zone' + this.num).style.visibility = 'visible';
  }
  var aSecond = pi * theTime[4] / 30 - pi2;
  var aMinute = pi * theTime[3] / 30 - pi2;
  var aHour = pi * theTime[2] / 6 + pi * parseInt(now.getMinutes())/360 - pi2;
  for (var i = 0; i < this.secln; i++) {
    d.getElementById('csec' + this.num + i).style.top = (this.ctrY + i *  Math.sin(aSecond))+'px';
    d.getElementById('csec' + this.num + i).style.left = (this.ctrX + i *  Math.cos(aSecond))+'px';
    d.getElementById('csec' + this.num + i).style.visibility = 'visible';
  }
  for (var i = 0; i < this.minln; i++) {
    d.getElementById('cmin' + this.num + i).style.top = (this.ctrY + i *  Math.sin(aMinute))+'px';
    d.getElementById('cmin' + this.num + i).style.left = (this.ctrX + i *  Math.cos(aMinute))+'px';
    d.getElementById('cmin' + this.num + i).style.visibility = 'visible';
  }
  for (var i = 0; i < this.hourln; i++) {
    d.getElementById('chour' + this.num + i).style.top = (this.ctrY + i * Math.sin(aHour))+'px';
    d.getElementById('chour' + this.num + i).style.left = (this.ctrX + i * Math.cos(aHour))+'px';
    d.getElementById('chour' + this.num + i).style.visibility = 'visible';
  }
}

clock.prototype.display = function() {
  var ctx = '';
  if (this.fix)
    ctx += '<div style="position:relative;margin:auto;width:'+(this.clocksize+this.offset*2)+'px;height:'+(this.clocksize+this.offset*2)+'px;overflow:visible;">';
  for (var i = 1; i < 13; i++) {
    ctx += '<div id="cnum' + this.num + i + '" style="'+comstyle+'width:'+(this.offset*2)+'px;height:'+(this.offset*2)+'px;font-family:' + this.font_family + ';font-size:' + this.font_size + 'px;color:#4a4a4a' + this.colnumbers + ';text-align:center;padding-top:10px;z-index:1000;">' + clocknum[this.numstyle][i] + '<\/div>';
  }
  for (var i = 0; i < this.minln; i++) {
    ctx += '<div id="cmin' + this.num + i + '" style="'+comstyle+'left:0px;width:1px;height:1px;font-size:1px;background:#' + this.colminutes + ';z-index:997;"><\/div>';
  }
  for (var i = 0; i < this.hourln; i++) {
    ctx += '<div id="chour' + this.num + i + '" style="'+comstyle+'width:2px;height:2px;font-size:2px;background:#' + this.colhours + ';z-index:998;"><\/div>';
  }
  for (var i = 0; i < this.secln; i++) {
    ctx += '<div id="csec' + this.num + i + '" style="'+comstyle+'width:1px;height:1px;font-size:1px;background:#' + this.colseconds + ';z-indez:999;"><\/div>';
  }
  ctx += '<div id="ampm' + this.num + '" style="'+comstyle+'width:'+((this.xpos+this.rad)*2)+'px;font-family:' + this.font_family + ';font-size:' + (this.font_size*1) + 'px;color:#4a4a4a' + this.colnumbers + ';text-align:center;padding-top:10px;z-index:990;"><\/div><div id="zone' + this.num + '" style="'+comstyle+'width:'+((this.xpos+this.rad)*2)+'px;font-family:' + this.font_family + ';font-size:' + (this.font_size*2/3) + 'px;color:#4a4a4a' + this.colnumbers + ';text-align:center;padding-top:10px;z-index:990;"><\/div>';
  if (this.fix)
    ctx += '<\/div>';
  d.getElementById('clock_'+this.num).innerHTML = ctx;
}

clock.prototype.start = function() {
  if (!d.getElementById) return;
  this.display();
  for (var i = 1; i < 13; i++) {
    d.getElementById('cnum' + this.num + i).style.top = (this.ctrY - this.offset + this.rad * Math.sin(i * pi / 6 - pi2))+'px';
    d.getElementById('cnum' + this.num + i).style.left = (this.ctrX - this.offset + this.rad * Math.cos(i * pi / 6 - pi2))+'px';
    d.getElementById('cnum' + this.num + i).style.visibility = 'visible';
  }
}

function updateClocks() {
  for (var i = clocks.length -1; i >=0; i--) {
    clocks[i].updateClock();
  }
}

function setClocks() {
  for (var i = clocks.length -1; i >=0; i--) {
    clocks[i].start();
  }
  setInterval('updateClocks()',100);
}

window.onload = setClocks;

// code to adjust for daylight saving time if applicable (localzone = 0)
var lnz = 1;
var dtz = -4;
var dst = new Date()
//if ((dst.getMonth()>3 && dst.getMonth()<10) || (dst.getMonth()=3 && dst.getDate()>=30) || (dst.getMonth()=10 && dst.getDate()<=26))
//	lnz = 1;
//if ((dst.getMonth()>3 && dst.getMonth()<11) || (dst.getMonth()=3 && dst.getDate()>=9) || (dst.getMonth()=11 && dst.getDate()<=2))
//	dtz = -4;

// code to handle clock positioning (fix = 0)

var facecol = 'FFFFFF' // '000000' //
//name,clocksize,colnumbers,colseconds,colminutes,colhours,numstyle,font_family,localZone,mytimezone,dst,city,country,fix,x,y
new clock('sh',70,facecol,'660000','990000','CC0000',0,'',0,+8,0,'','',1,16,0);
new clock('ln',70,facecol,'660000','990000','CC0000',0,'',0,lnz,0,'','',1,16,0);
new clock('dt',70,facecol,'660000','990000','CC0000',0,'',0,dtz,0,'','',1,16,0);

