/* 
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, addition){
	this.target=elemTarget;
	this.time=0;
	this.duration=iDuration;
	this.position=iStartValue;
	this.finish=parseFloat(iEndValue);
	
	/* private properties */
	if (!elemTarget.style)
		elemTarget.style = '';
	this._style=elemTarget.style;
	this._prop=sStyleProperty;
	this._ease=oEaseType;
	this._start=parseFloat(iStartValue);
	this._change=this.finish - this._start;
	this._animate=false;
	this._addition = addition ? addition : false;
	
	this.start();
}

Tween.aStack=[];
Tween._idPoll=null;
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._startPoll=function(){
	this._idPoll=setInterval('Tween._poll()', 30);
}

Tween._stopPoll=function(){
	clearInterval(this._idPoll);
	this._idPoll=null;
}

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._style[this._prop]=this.position + (this._addition ? this._addition : '');
			}
			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);
		}
	}
	if(!this.constructor._idPoll)
		this.constructor._startPoll();
}

Tween.prototype.stop=function(){
	this._animate=false;
	this.constructor._remove(this);
	
	if(!this.constructor.aStack.length)
		this.constructor._stopPoll();
}

Tween.prototype.resume=function(){
	this._animate=true;
	if(!this.constructor._inStack(this))
		this.constructor._add(this);
}
