/* 
Quick port of Adobe Flash Tween class
Requires easing_equations.js

Author: Sergey Chikuyonok (gonarch@design.ru)
17.09.2006
*/ 

function Tween(elemTarget, sStyleProperty, oEaseType, iStartValue, iEndValue, iDuration){
	this.target=elemTarget;
	this.time=0;
	this.duration=iDuration;
	this.position=iStartValue;
	this.finish=parseFloat(iEndValue);
	
	/* private properties */
	this._style=elemTarget.style;
	this._prop=sStyleProperty;
	this._ease=oEaseType;
	this._start=parseFloat(iStartValue);
						 
	this._change=this.finish - this._start;
	this._animate=false;
	
	this.start();
}

Tween.aStack=[];
Tween._poll=function(){
	for(var i=0; i<this.aStack.length; i++){

		this.aStack[i]._onMotion();
	}
}

Tween._add=function(obj){
	this.aStack[this.aStack.length]=obj;
}

Tween._remove=function(obj){
	for(var i=0; i < this.aStack.length; i++){
		if(this.aStack[i] == obj){
			this.aStack.splice(i,1);
			break;
		}
	}
}

Tween._inStack=function(obj){
	for(var i=0; i<this.aStack.length; i++){
		if(this.aStack[i] == obj)
			return true;
	}
	return false;
}

Tween.prototype._onMotion=function(){
	if(this._animate){
		if(this.time < this.duration){
			this.time++;

			this.position=this._ease(this.time, this._start, this._change, this.duration, 50, 1.06);
			if(this._prop)
				this._style[this._prop]= parseInt(this.position)+'px';
//				alert(this._style[this._prop]);
			this.onMotionChanged();
		}
		else{
			this.onMotionFinished();
			this.stop();
		}
	}
}

Tween.prototype.onMotionChanged=function(){
	return;
}

Tween.prototype.onMotionFinished=function(){
	return;
}

Tween.prototype.start=function(){

	if(!this._animate){
		this._animate=true;
		this.time=0;
		if(!this.constructor._inStack(this)){
			this.constructor._add(this);
		}
	}
}

Tween.prototype.stop=function(){
	this._animate=false;
	this.constructor._remove(this);
}

Tween.prototype.resume=function(){
	this._animate=true;
	if(!this.constructor._inStack(this))
		this.constructor._add(this);
}

setInterval('Tween._poll()', 20);
