var dom = (document.getElementById) ? 1 : 0;

function animation(id)
{
  this.element = (!dom) ? document[id] : document.getElementById(id).style;
  this.active = 0;
  this.timer = null;
  this.timer2 = null;
  this.path = null;
  this.pathsize = null;
  this.num = null;
  this.num2 = null;
  
  this.name = id + "Var";
  eval(this.name + " = this");

  this.animate = animate;
  this.step = step;
  this.sizeStep = sizeStep;
  this.show = show;
  this.hide = hide;
  this.left = left;
  this.width = width;
  this.top = top;
  this.resize = resize;
  this.moveTo = moveTo;
  this.slideBy = slideBy;
  this.slideTo = slideTo;
  this.sizeBy = sizeBy;
  this.sizeTo = sizeTo;
  this.sizeItTo = sizeItTo;
  this.circle = circle;
}

function pos(x, y) {
  this.x = Math.round(x);
  this.y = Math.round(y);
}

function show() {
  this.element.visibility = "visible";
}

function hide() {
  this.element.visibility = "hidden";
}

function width()
{
  tmp = this.element.width; 
  if(tmp.indexOf('px'))tmp = tmp.substr(0,tmp.length-2);
  tmp = parseInt(tmp);
  return tmp;
}

function left() {
  return parseInt(this.element.left);
}

function top() {
  return parseInt(this.element.top);
}

function moveTo(x, y) {
  this.element.left = x;
  this.element.top = y;
}

function step() {
  this.moveTo(this.path[this.num].x, this.path[this.num].y);  
  if (this.num >= this.path.length - 1) {
    clearInterval(this.timer);
    this.active = 0;
    if (this.statement)
      eval(this.statement);
  } else {
    this.num++;
  }
}


function animate(interval) {
  if (this.active) return;
  this.num = 0;
  this.active = 1;
  this.timer = setInterval(this.name + ".step()", interval);
}


function sizeStep() {
  this.sizeItTo(this.pathsize[this.num2]);  
  if (this.num2 >= this.pathsize.length - 1) 
  {
    clearInterval(this.timer2);
    this.active2 = 0; 
   } else {
     this.num2++;
  }
}

function resize(interval)
{

 if (this.active2) return;
  this.num2 = 0;
  this.active2 = 1;
  this.timer2 = setInterval(this.name + ".sizeStep()", interval);
}


function sizeItTo(tx)
{
this.element.width = tx+"px";	
}


function sizeBy(dx, steps, interval) 
{
  var fx = this.width(); 
  var tx = fx - dx; 

  this.sizeTo(tx, steps, interval);

}

function sizeTo(tx, steps, interval) 
{

  var fx = this.width();

  var dx = tx - fx;
  var sx = dx / steps;
  //alert(sx);
  
    var ar = new Array();
  for (var i = 0; i < steps; i++) 
  {
    fx += sx;
    ar[i] = Math.round(fx);
    
  }  
  this.pathsize = ar;
  this.resize(interval);
}




function slideBy(dx, dy, steps, interval, statement) {
  var fx = this.left();
  var fy = this.top();
  var tx = fx + dx;
  var ty = fy + dy;
  this.slideTo(tx, ty, steps, interval, statement);

}

function slideTo(tx, ty, steps, interval, statement) {
 var fx = this.left();
  var fy = this.top();
   
  var dx = tx - fx;
  var dy = ty - fy;
  var sx = dx / steps;
  var sy = dy / steps;
  
    var ar = new Array();
  for (var i = 0; i < steps; i++) {
    fx += sx;
    fy += sy;
    ar[i] = new pos(fx, fy);
    
  }  
  this.path = ar;

  this.statement = (statement) ? statement : null;
  this.animate(interval);
}

function circle(radius, angle0, angle1, steps, interval, statement) {
  var dangle = angle1 - angle0;
  var sangle = dangle / steps;
  var x = this.left();
  var y = this.top();
  var cx = x - radius * Math.cos(angle0 * Math.PI / 180);
  var cy = y + radius * Math.sin(angle0 * Math.PI / 180);

  var ar = new Array();
  for (var i = 0; i < steps; i++) {
    angle0 += sangle;
    x = cx + radius * Math.cos(angle0 * Math.PI / 180);
    y = cy - radius * Math.sin(angle0 * Math.PI / 180);
    ar[i] = new pos(x, y);
  }
  this.path = ar;

  this.statement = (statement) ? statement : null;
  this.animate(interval);
}
