/**
 * @author lcalandro
 */
$=function(id) {
	return document.getElementById(id);
}

ie=function() {
	return navigator.appVersion.indexOf("MSIE") != -1
}


var Tween = {
	
	updInterval:null,
	frameRate:0,
	tweens:[],
	
	init:function(frameRate) {
		this.frameRate=frameRate;
		this.updInterval=setInterval("Tween.updateTweens()",1000/frameRate);
	},
	
	pause:function() {
		clearInterval(this.updInterval);
	},

	
	updateTweens:function() {
		for (var i in this.tweens) {
			var obj=this.tweens[i].object;
			var props=this.tweens[i].properties;
			var options=this.tweens[i].options;
			var currentStep=(++this.tweens[i].currentStep)/this.tweens[i].steps;
			if (this.tweens[i].currentStep>=0 && this.tweens[i].currentStep<=1) {
				this.tweens[i].initialProperties=Tween.onTweenStart(this.tweens[i].properties,this.tweens[i].object);		
			}
			var initialProperties=this.tweens[i].initialProperties;
				
			if (currentStep>=1) {
				currentStep=1;
				delete this.tweens[i];
				if (options && options.onComplete) {
					options.onComplete();
				}
			}
			
			var easing;
			if (currentStep > 0) {
				
				if (options && options.easing) {
					easing=options.easing;
				} else {
					easing=Easing.linear;
				}
				for (var i in props) {
					var initProp = parseInt(initialProperties[i]);
					var finalProp = parseInt(props[i])-initProp;
					var currentProp=easing(currentStep,initProp,finalProp,1);
					
					if (i=="opacity" && ie()) {
						//obj.style.filter='progid:DXImageTransform.Microsoft.Alpha(Opacity='+ (Math.ceil(currentProp))+')';
					} else {
						obj.style[i] = currentProp + ((options && options.measureUnit)?options.measureUnit:"");
					}
					
				}
			}
		}
	},
	
	addTween:function(object,time,properties,options)	 {
		var t={
			object:object,
			currentStep:(options && options.delay)?-options.delay*this.frameRate:0,
			steps:time*this.frameRate,
			initialProperties:0,
			properties:properties,
			options:options
		}
		this.tweens.push(t);
	},
	clearTweensByObject:function(object) {
		for (var i in this.tweens) {
			if (this.tweens[i].object==object) {
				delete this.tweens[i];
			}
		}
	},
	onTweenStart:function(properties, object){
		var initialProperties={};
		for (var i in properties) {
			if ( i == "opacity" && ie()) {
				/*var splittedstr=object.style.filter.split("Opacity=");
				initialProperties[i] = splittedstr[1].slice(0,splittedstr[1].length-1);
				properties[i]*=100;*/
			} else {
				initialProperties[i] = object.style[i];
			}
		}
		return initialProperties;
	}
	
	
	
}

var Easing= {
	linear: function (t,b,c,d) {
		return c*t/d + b;
	},
	OutQuad: function (t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	InQuad: function (t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	InOutQuad: function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	InCubic: function (t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	OutCubic: function (t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	InOutCubic: function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	InQuart: function (t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	OutQuart: function (t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	InOutQuart: function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint : function (t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	OutQuint : function (t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	InOutQuint : function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	InSine : function (t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	OutSine : function (t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	InOutSine : function (t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	InExpo : function (t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	OutExpo:function (t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	InOutExpo: function (t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	InCirc : function (t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	OutCirc : function (t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	InOutCirc : function (t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	InElastic:function (t, b, c, d) {
		var p=0;
		var a=0;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (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-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	OutElastic:function (t, b, c, d) {
		var p=0;
		var a=0;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (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;
	},
	InOutElastic:function (t,b,c,d) {
		var p=0;
		var a=0;
		if (t==0) return b;
		if ((t/=d/2)==2) return b+c;
		if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	InBack:function (t, b, c, d) {
		var s = 2.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	OutBack:function (t, b, c, d) {
		var s = 2.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	InOutBack:function (t, b, c, d) {
		var s = 2.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	InBounce:function (t, b, c, d) {
		return c - Math.easeOutBounce (d-t, 0, c, d) + b;
	},
	OutBounce:function (t, b, c, d) {
		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;
		}
	},
	InOutBounce:function (t, b, c, d) {
		if (t < d/2) return Math.easeInBounce (t*2, 0, c, d) * .5 + b;
		return Math.easeOutBounce (t*2-d, 0, c, d) * .5 + c*.5 + b;
	}



}
 
 