
ANIMATION_TIMEOUT = 10; //in milliseconds
ANIMATORQUEUE = new Array();

function RUNANIMATORS() {
  for ( var i in ANIMATORQUEUE )
    ANIMATORQUEUE[i].run();
  window.setTimeout(RUNANIMATORS,ANIMATION_TIMEOUT);
}
function STARTANIMATORS() {
  window.setTimeout(RUNANIMATORS,ANIMATION_TIMEOUT);
}
function QUEUEANIMATOR(animator) {
  ANIMATORQUEUE.push(animator);
}
function REMOVEANIMATOR(animator) {
  for(var i=0; i<ANIMATORQUEUE.length;i++ )
    if(ANIMATORQUEUE[i]==animator)
      ANIMATORQUEUE.splice(i,1); 
}
function ISANIMATORQUEUEEMPTY() {
  return (ANIMATORQUEUE.length == 0);
}


// GENERAL ANIMATOR OBJECT

function Animator() {
}
Animator.prototype.queue = function(delay) { //duration in milliseconds
  this.startTime = new Date().getTime() + delay; 
  this.isWaiting = true;
  QUEUEANIMATOR(this);  
} 
Animator.prototype.start = function() { 
  this.startTime = new Date().getTime(); 
  this.isWaiting = true;
  QUEUEANIMATOR(this);  
} 
Animator.prototype.run = function() {
  if (this.duration == 0) {
    if (new Date().getTime() > this.startTime) {
      REMOVEANIMATOR(this);
      this.finalize();
    }
  }
  else {
    lambda = (new Date().getTime() - this.startTime) / this.duration;
    if (lambda >= 1) { 
      REMOVEANIMATOR(this);
      this.finalize();
    }
    else if (lambda >= 0) {
      if (this.isWaiting) {
        this.prepareFirstRun();
        this.isWaiting = false;
      }
      this.animate(lambda);
    }
  }
}

Animator.prototype.prepareFirstRun = function() {}
Animator.prototype.animate = function(lambda) {alert("invalid animator.");}
Animator.prototype.finalize = function() {}

/// MOVEANIMATOR

function MoveAnimator(id,originalX,originalY,newX,newY,duration,acceleration) {
  this.duration = duration;
  this.acceleration = acceleration;
  this.id = id;
  this.newX = newX;
  this.newY = newY;
  this.originalX = originalX;
  this.originalY = originalY;
}
MoveAnimator.prototype = new Animator();
MoveAnimator.prototype.constructor=MoveAnimator; 

MoveAnimator.prototype.position = function(x,y) {
  obj = document.getElementById(this.id);
  if (y !== false ) obj.style.top = y + "px";
	if (x !== false ) obj.style.left = x + "px";
}
MoveAnimator.prototype.prepareFirstRun = function() {
  if (this.originalY === false)
    this.originalY = document.getElementById(this.id).offsetTop;
  if (this.originalX === false)
    this.originalX = document.getElementById(this.id).offsetLeft;
}

MoveAnimator.prototype.animate = function(lambda) {
  if (this.acceleration < 0) //lassuló
    lambda2 = (1-(1-lambda)*(1-lambda));
  else if (this.acceleration > 0) //gyorsuló
    lambda2 = lambda*lambda;
  else
    lambda2 = lambda;
    
  x = (this.newX === false) ? false : this.originalX * (1-lambda2) + this.newX * lambda2;
  y = (this.newY === false) ? false : this.originalY * (1-lambda2) + this.newY * lambda2;
  this.position(x,y);
}
MoveAnimator.prototype.finalize = function() {
  this.position( this.newX, this.newY);
}

/// TRANSPARENCY ANIMATOR

function TransparencyAnimator(id, originalTransparency, newTransparency, duration) {
  this.duration = duration;
  this.originalTransparency = originalTransparency;
  this.newTransparency = newTransparency;
  this.id = id;
}
TransparencyAnimator.prototype = new Animator();
TransparencyAnimator.prototype.constructor=TransparencyAnimator; 

TransparencyAnimator.prototype.setTransparency = function(t) {
  obj = document.getElementById(this.id);
	obj.style.visibility = (t > 0) ? "visible" : "hidden";
	obj.style.opacity = t;
	obj.style.filter = "alpha(opacity="+t*100+")";
}

TransparencyAnimator.prototype.prepareFirstRun = function() {
  //this.setTransparency(this.originalTransparency);
}
TransparencyAnimator.prototype.animate = function(lambda) {
  t = this.originalTransparency * (1-lambda) + this.newTransparency * lambda; 
  this.setTransparency(t);
}
TransparencyAnimator.prototype.finalize = function() {
  this.setTransparency(this.newTransparency);
}

/// DELAYEDACTION ANIMATOR

function DelayedActionAnimator(actionString) {
  this.actionString = actionString;
  this.duration = 0;
}
DelayedActionAnimator.prototype = new Animator();
DelayedActionAnimator.prototype.constructor=DelayedActionAnimator(); 

DelayedActionAnimator.prototype.finalize = function() {
  eval(this.actionString);
}

