// JavaScript Document
GLB.namespace("GLB.common");
GLB.common.animation = function(){
	/*
		.version: 1.0
		
		.date:
		20/09/2007
		
		.usage:
		var anim = new GLB.common.animation.Tween(inivalue, endvalue, duration);
		anim.setAnimation(GLB.common.animation.bounceEquation);
		anim.init();				
	*/
	
	/*
	Static Function
	*/
	var _easingEquation = function(t,b,c,d,a,p)
	{
		return c/2 * ( Math.sin( Math.PI * (t/d-0.5) ) + 1 ) + b;
	}
	var _bounceEquation = function(t,b,c,d,a,p)
	{
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	}
	var _elasticEquation = function(t,b,c,d,a,p)
	{
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);
	}
	/*
		Class		
	*/
	var $anim = function(inivalue, endvalue, duration, args){
		this._duration 	= duration;
		this._inicial 	= inivalue;	
		this._final 	= endvalue;
		this._args		= args;		
	}
	$anim.prototype = {
		init:function(){
			if(typeof this._inicial === "array" && typeof this._final === "array" && this._inicial.length != this._final.length)return;
			
			var _self = this;
		
			this._iniTime = new Date().getTime();
			this._interval = setInterval(function(){
				_self.setAnimation();
			}, 5);
		},
		setAnimation:function(){
			var curTime = new Date().getTime()-this._iniTime;	
			
			if (curTime >= this._duration){
				  this.onEndAnimation && this.onEndAnimation(this._final, this._args || {});
				 clearInterval(this._interval);
				 return;
			}
			
			if(typeof this._inicial === "object"){
				var _tempArr = [];
				for(var i=0; i<this._inicial.length; i++){
					_tempArr.push(this.easingEquation(curTime, this._inicial[i], parseInt(this._final[i]-this._inicial[i]), this._duration));
				}
				this.onAnimation && this.onAnimation(_tempArr, this._args || {});
			}else
				this.onAnimation && this.onAnimation(this.easingEquation(curTime, this._inicial, parseInt(this._final-this._inicial), this._duration), this._args || {});
		},
		easingEquation:_easingEquation
	}
	
	return {
		Tween			: $anim,
		easingEquation	: _easingEquation,
		bounceEquation	: _bounceEquation,
		elasticEquation	: _elasticEquation
	}
}();
