/*
  script to show a digital clock and a pendulum
*/

var clock = {

digitImage:new Array(), digitId:new Array(),

read:function()
{
  for(var i = 0; i < 10; i++)
  {
    this.digitImage[i] = new Image();

    this.digitImage[i].src = 'images/lcd' + i + '.gif';
  }
},

start:function()
{
  for(var i = 0; i < 6; i++)
    this.digitId[i] = document.getElementById( 'digit'+ i );

  pendulum.start();

  this.set();
},

set:function()
{
  var d = new Date();		

  var hours = d.getHours() % 24;

  var minutes = d.getMinutes();

  var seconds = d.getSeconds();

  this.digitId[0].src = this.digitImage[ Math.floor(hours/10)   ].src;
  this.digitId[1].src = this.digitImage[ hours % 10             ].src;
  this.digitId[2].src = this.digitImage[ Math.floor(minutes/10) ].src;
  this.digitId[3].src = this.digitImage[ minutes % 10           ].src;
  this.digitId[4].src = this.digitImage[ Math.floor(seconds/10) ].src;
  this.digitId[5].src = this.digitImage[ seconds % 10           ].src;

  setTimeout("clock.set()" ,1000);

  pendulum.move();
}
}

var pendulum = {

images:new Array(), xcoor:new Array(), ycoor:new Array(), angle:new Array(),

pendulumId:null, R:1010, index:0,

read:function()
{
  for(var i = -11; i < 12; i++)
  {
    this.images[i] = new Image();

    this.images[i].src = 'images/pendulum'+ i + '.gif';
  }

  var k = -16;

  for(var i = 0; i < 33; i++)
  {
    var x = 200 * Math.sin(Math.PI * k / 32); 
 
    this.xcoor[i] = x;

    this.ycoor[i] = Math.sqrt(this.R * this.R - x * x) - this.R; 
    
    this.angle[i] = Math.round(180 * Math.asin(x/this.R) / Math.PI);

    k++;
  }
},

start:function()
{
  this.pendulumId = document.getElementById('pend');
},

move:function()
{
  this.index = ++this.index % 64;

  var index = Math.min(this.index, 64 - this.index);

  this.pendulumId.style.left = 220 + this.xcoor[index];
  this.pendulumId.style.top  = this.ycoor[index];

  this.pendulumId.src = this.images[this.angle[index]].src;

  if(index > 0 && index < 32)
    setTimeout("pendulum.move()",30);
}
}

clock.read();

pendulum.read();
