_animations = [];

function Animation(target,duration,fps) {
	// fps: frames per second.
	// duration: in seconds
	this.id = _animations.length;
	this._fps = fps;
	this._totalFrames = duration*fps;
	this._frametimeout = 1000/fps;
	this._target = target;
	_animations.push(this);
}
Animation.prototype = new Object();
Animation.prototype._fps = 12;
Animation.prototype._totalFrames = 1;
Animation.prototype._currentFrame = 1;

Animation.prototype.start = function () {
	if (this._target.onStart) this._target.onStart();
	this.next();
}
Animation.prototype.stop = function () {
	if (this._target.onEnterFrame) this._target.onEnterFrame(this._currentFrame/this._totalFrames);
	if (this._target.onStop) this._target.onStop();
}
Animation.prototype.next = function() {
	if (this._target.onEnterFrame) this._target.onEnterFrame(this._currentFrame/this._totalFrames);
	this._currentFrame++;
	if (this._currentFrame >= this._totalFrames) {
		this.stop();
	} else {
		setTimeout('_animations['+this.id+'].next()',this._frametimeout);
	}
}
Animation.prototype.previous = function() {
	if (this._target.onEnterFrame) this._target.onEnterFrame(this._currentFrame/this._totalFrames);
	this._currentFrame--;
	if (this._currentFrame <= 1) {
		this.stop();
	} else {
		setTimeout('_animations['+this.id+'].previous()',this._frametimeout);
	}
}
