/*
 * xAnalogClock.js, 1.5, 2005-05-16
 *
 * Credits:
 * ***************************************************************
 * *  Analog clock script- By Kurt (kurt.grigg@virgin.net)       *
 * *  Script featured on Dynamic Drive                           *
 * *  Visit http://www.dynamicdrive.com for this script and more *
 * ***************************************************************
 *
 * Copyright (c) 2002-2005 Roumen Petrov.  All rights reserved.
 *
 * Notes:
 * - Do not put script in HTML head section - NS4 and frames problem
 * - Works on:
 *   - Gecko 0.9.x/1.x (Mozilla, Firefox, Netscape 6x/7x, Galeon)
 *   - IE 5.x/6.x
 *   - Netscape 4.7x
 *   - Opera 5.x(*)/6.x/7.x/8.x
 *   - Konqueror 2.2.x/3.x
 * (*) Opera 5.x cannot handle AnalogClock on two pages and result is
 *   clock crash on second page.
 */

//TODO: check symbol '#' in colours for old IE(4?)
AnalogClock.prototype.faceColour    = '#444444';
AnalogClock.prototype.secondsColour = '#FF0000';
AnalogClock.prototype.minutesColour = '#444444';
AnalogClock.prototype.hoursColour   = '#444444';
AnalogClock.prototype.clockHeight   = 30;
AnalogClock.prototype.clockWidth    = 30;
AnalogClock.prototype.use_pixel_prop = 0;


function AnalogClock () {
  this.Ypos=0;
  this.Xpos=0;

  this.digits  = new Array(12);
  this.hours   = new Array(3);
  this.minutes = new Array(4);
  this.seconds = new Array(5);

  var i;
  if (document.getElementById || document.all) {
    for (i=0; i < this.digits.length ; i++)
      document.write('<div id="acDigits' +i+'" style="position:absolute;top:-40px;left:0px;width:30px;height:30px;font-family:Arial;font-size:10px;color:'+this.faceColour+';text-align:center;padding-top:10px">'+(i+1)+'</div>');
    for (i=0; i < this.minutes.length; i++)
      document.write('<div id="acMinutes'+i+'" style="position:absolute;top:-40px;left:0px;width:2px;height:2px;font-size:2px;background:'+this.minutesColour+'"></div>');
    for (i=0; i < this.hours.length  ; i++)
      document.write('<div id="acHours'  +i+'" style="position:absolute;top:-40px;left:0px;width:2px;height:2px;font-size:2px;background:'+this.hoursColour+'"></div>');
    for (i=0; i < this.seconds.length; i++)
      document.write('<div id="acSeconds'+i+'" style="position:absolute;top:-40px;left:0px;width:2px;height:2px;font-size:2px;background:'+this.secondsColour+'"></div>');
  } else if (document.layers) {
    for (i=0; i < this.digits.length ; i++)
      document.write('<layer name="acDigits' +i+'" top=0 left=0 height=30 width=30><center><font face=Arial size=1 color='+this.faceColour+'>'+(i+1)+'</font></center></layer>');
    for (i=0; i < this.minutes.length; i++)
      document.write('<layer name="acMinutes'+i+'" top=0 left=0 bgcolor='+this.minutesColour+' clip="0,0,2,2"></layer>');
    for (i=0; i < this.hours.length  ; i++)
      document.write('<layer name="acHours'  +i+'" top=0 left=0 bgcolor='+this.hoursColour+' clip="0,0,2,2"></layer>');
    for (i=0; i < this.seconds.length; i++)
      document.write('<layer name="acSeconds'+i+'" top=0 left=0 bgcolor='+this.secondsColour+' clip="0,0,2,2"></layer>');
  }

  for (i=0; i < this.digits.length ; i++) { this.digits[i]  = this.getElementStyle("acDigits" +i); }
  for (i=0; i < this.minutes.length; i++) { this.minutes[i] = this.getElementStyle("acMinutes"+i); }
  for (i=0; i < this.hours.length  ; i++) { this.hours[i]   = this.getElementStyle("acHours"  +i); }
  for (i=0; i < this.seconds.length; i++) { this.seconds[i] = this.getElementStyle("acSeconds"+i); }
  if (document.layers == undefined &&
      this.digits[0].pixelWidth != undefined) {
    this.use_pixel_prop = 1;
  }
}


if (document.getElementById) {
  AnalogClock.prototype.getElementStyle = function (_name) {
    return document.getElementById(_name).style;
  }
} else if (document.all) {
  AnalogClock.prototype.getElementStyle = function (_name) {
    return document.all[_name].style;
  }
} else if (document.layers) {
  AnalogClock.prototype.getElementStyle = function (_name) {
    return document.layers[_name]; // rumen: only first level !!!
  }
}


if (document.layers) {
  AnalogClock.prototype.pixel = function (_val) {
    return _val;
  }
}
else {
  AnalogClock.prototype.pixel = function (_val) {
    return _val+'px';
  }
}



AnalogClock.prototype.draw = function () {
  var time = new Date ();
  var sec  = -1.57 + Math.PI * time.getSeconds()/30;
  var min  = -1.57 + Math.PI * time.getMinutes()/30;
  var hrs  = -1.57 + Math.PI * time.getHours()/6 + Math.PI*parseInt(time.getMinutes())/360;
  var split=(Math.PI+Math.PI)/this.digits.length;

  if (window.pageYOffset != undefined) {
    this.Ypos = window.pageYOffset;
    this.Xpos = window.pageXOffset;
  } else {
    this.Ypos = document.body.scrollTop ;
    this.Xpos = document.body.scrollLeft;
  }
  if (window.innerHeight != undefined) {
    this.Ypos += window.innerHeight;
    this.Xpos += window.innerWidth ;
  } else {
    this.Ypos += window.document.body.clientHeight;
    this.Xpos += window.document.body.clientWidth ;
  }
  this.Ypos -= this.clockHeight+30;
  this.Xpos -= this.clockWidth +30;

  var fontDelta = 5; // depend from body font(line size)
  var digitsTop  = this.Ypos-15-fontDelta;
  var digitsLeft = this.Xpos-15;
  if (document.layers) {
    digitsTop += 10+fontDelta; // NS4 correction !!!
  }

  var handHeightFactor = this.clockHeight/4.1;
  var handWidthFactor  = this.clockWidth /4.1;
  var i;
  if (this.use_pixel_prop != 1) {
    for (i=0; i < this.digits.length; i++) {
      this.digits[i].top  = this.pixel(digitsTop +this.clockHeight*Math.sin(-1.045+i*split));
      this.digits[i].left = this.pixel(digitsLeft+this.clockWidth *Math.cos(-1.045+i*split));
    }
    for (i=0; i < this.seconds.length; i++) {
      this.seconds[i].top =this.pixel(this.Ypos+i*handHeightFactor*Math.sin(sec));
      this.seconds[i].left=this.pixel(this.Xpos+i*handWidthFactor *Math.cos(sec));
    }
    for (i=0; i < this.minutes.length; i++) {
      this.minutes[i].top =this.pixel(this.Ypos+i*handHeightFactor*Math.sin(min));
      this.minutes[i].left=this.pixel(this.Xpos+i*handWidthFactor *Math.cos(min));
    }
    for (i=0; i < this.hours.length; i++) {
      this.hours[i].top   =this.pixel(this.Ypos+i*handHeightFactor*Math.sin(hrs));
      this.hours[i].left  =this.pixel(this.Xpos+i*handWidthFactor *Math.cos(hrs));
    }
  } else {
    for (i=0; i < this.digits.length; ++i) {
      this.digits[i].pixelTop =digitsTop +this.clockHeight*Math.sin(-1.045+i*split)
      this.digits[i].pixelLeft=digitsLeft+this.clockWidth *Math.cos(-1.045+i*split)
    }
    for (i=0; i < this.seconds.length; i++) {
      this.seconds[i].pixelTop =this.Ypos+i*handHeightFactor*Math.sin(sec);
      this.seconds[i].pixelLeft=this.Xpos+i*handWidthFactor *Math.cos(sec);
    }
    for (i=0; i < this.minutes.length; i++) {
      this.minutes[i].pixelTop =this.Ypos+i*handHeightFactor*Math.sin(min);
      this.minutes[i].pixelLeft=this.Xpos+i*handWidthFactor *Math.cos(min);
    }
    for (i=0; i < this.hours.length; i++) {
      this.hours[i].pixelTop   =this.Ypos+i*handHeightFactor*Math.sin(hrs);
      this.hours[i].pixelLeft  =this.Xpos+i*handWidthFactor *Math.cos(hrs);
    }
  }
}

