/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */
function HectorAnimator (duration, interval, from, to, callback) {
	this.duration = duration;
	this.interval = interval;
	this.oncomplete = null;
	this.timer = null;
	this.animations = new Array();
	
	this._isFirst = true;
		
	this.animate = function (_self) {
	  var time = new Date();
	  time = Math.min((time.getTime() - _self.startTime) / _self.duration, 1);
	  time = Math.max(0, time);
	  var pos = 0.5 - (0.5 * Math.cos(Math.PI * time));
	  var done = time == 1;
	  for (var i = 0; i < _self.animations.length; i++) {
	    _self.animations[i].doFrame(_self, pos, _self._isFirst, done, time);	  	
	  }
	  _self.isFirst = false;
	  if (done) {
	    _self.stop();
	    if (_self.oncomplete) {
	      _self.oncomplete();
	    }
	  }
	}

	if (from !== undefined && to !== undefined && callback !== undefined) {
		this.addAnimation(new HectorAnimation (from, to, callback));
	}
}

HectorAnimator.prototype.start = function () {
  if (this.timer == null) {
    var time = new Date();
    this.startTime = time.getTime() - this.interval;
    var _self = this;
    this.timer = setInterval(function() {
      _self.animate(_self);
    }, this.interval);
  }
}

HectorAnimator.prototype.stop = function () {
  if (this.timer != null) {
    clearInterval(this.timer);
  }
}

HectorAnimator.prototype.addAnimation = function (animation) {
  this.animations.push(animation);
}

//
// Animation class
//

function HectorAnimation (from, to, callback) {
  this.from = from;
  this.to = to;
  this.callback = callback;
  this.now = from;
  this.time = 0;
  this.ease = 0;
}

HectorAnimation.prototype.doFrame = function (animator, ease, first, done, time) {
	this.now = this.from + (this.to - this.from) * ease;
	if (done) {
	  this.now = this.to;
	}
	this.ease = ease;
	this.time = time;
	this.callback(animator, this.now, first, done); 
}

//Rect class

function HectorRect (left, top, right, bottom) {
	this.left = left;
	this.top = top;
	this.right = right;
	this.bottom = bottom;
}

function HectorRectAnimation (from, to, callback)
{
	this.from = from;
	this.to = to;
	this.callback = callback;
	this.now = from;
	this.ease = 0;
	this.time = 0;
}

HectorRectAnimation.prototype = new HectorAnimation (0, 0, null);

HectorRectAnimation.prototype.doFrame = function (animation, ease, first, done, time) {
	
	var now;
	
	if (done)
		now = this.to;
	else
		now = this.from;
		
	this.now = now;
	this.ease = ease;
	this.time = time;
	this.callback (animation, now, first, done);
}


