/**
 * Tween v 1.0
 *
 * Eric R. Shinn
 * The Canvas Project 2006
 *
 */

var Tween = {
	
	vault: new Array(),
	
	create: function (targetElement, targetProperty, unitMeasurement, startValue, endValue, timeSeconds, fps, tweenType, timeDelay, extra1, extra2) {
		var tweenz = Tween.vault.slice(0);
		for (var i=0; i<tweenz.length; i++) {
			if ((tweenz[i].targetElement == targetElement) && (tweenz[i].targetProperty == targetProperty)) {
	//			alert("Found one! "+tweenz[i].intervalId);
				clearInterval(tweenz[i].intervalId);
				tweenz[i] = new Tween.tween(targetElement, targetProperty, unitMeasurement, startValue, endValue, timeSeconds, fps, tweenType, timeDelay, extra1, extra2);
				Tween.vault = tweenz;
				return tweenz[i];
			}
		}
	//	alert("Tween length: "+Tween.tweens.length);
		var n = tweenz.length;
		tweenz[n] = new Tween.tween(targetElement, targetProperty, unitMeasurement, startValue, endValue, timeSeconds, fps, tweenType, timeDelay, extra1, extra2);
		Tween.vault = tweenz;
	//	alert("Tween length: "+Tween.tweens.length);
		return Tween.vault[n];
	},
	
	tween: function (targetElement, targetProperty, unitMeasurement, startValue, endValue, timeSeconds, fps, tweenType, timeDelay, extra1, extra2) {
//		alert("Called: Tween.create()");
//		alert(targetProperty);
		var _this = this;
		this.targetElement = targetElement;
		this.targetProperty = targetProperty;
		this.unitMeasurement = (unitMeasurement) ? unitMeasurement : '';
		this.startValue = startValue;
		this.endValue = endValue;
		this.timeStart = new Date().getTime();
		this.timeDest = this.timeStart + (timeSeconds * 1000)
		this.timeDelay = (timeDelay) ? timeDelay : 0
		this.fps = (fps) ? fps : 15;
		this.tweenType = (tweenType == undefined || tweenType == '') ? "easeOutExpo" : tweenType;
		this.intervalId = setInterval(function() {Tween.update(_this);}, (1000/this.fps));
		this.extra1 = extra1;
		this.extra2 = extra2;
		return this;
	},
	
	update: function (targetTween) {
		var now = new Date().getTime();
		if ((targetTween.timeStart + targetTween.timeDelay*1000) <= now) {
			if ((targetTween.timeDest + targetTween.timeDelay*1000) <= now) {
				// Kill it. It's gone too far.
				if (targetTween.isDone) {
					//Set to its destination value.
					if (targetTween.targetProperty == "opacity" && navigator.userAgent.toLowerCase().indexOf("ie")+1) targetTween.targetElement.filter = "alpha(opacity="+targetTween.endValue*100+")";
					else targetTween.targetElement[targetTween.targetProperty] = targetTween.endValue+targetTween.unitMeasurement;
//					alert("Completed Tween");
					clearInterval(targetTween.intervalId);
				} else {
//					clearInterval(targetTween.intervalId);
				}
//				alert("Setting to done");
				targetTween.isDone = true;
			} else {
				targetTween.isDone = false;
				// Apply value to said property...
				var value = Tween.processValue(targetTween);//Math.round(Tween.processValue(targetTween));
				if (targetTween.targetProperty == "opacity" && navigator.userAgent.toLowerCase().indexOf("ie")+1) targetTween.targetElement.filter = "alpha(opacity="+value*100+")";
				else targetTween.targetElement[targetTween.targetProperty] = value+targetTween.unitMeasurement;
//				targetTween.targetElement[targetTween.targetProperty] = value+targetTween.unitMeasurement;
			}
		}
	},
	
	destroy: function () {
		// End the animation abruptly.
	},
	
	processValue: function (tween) {
		//alert("Processing...");
		var timeNow = new Date().getTime();
		var t = (timeNow - (tween.timeDelay * 1000)) - tween.timeStart;
		var b = tween.startValue;
		var c = tween.endValue - tween.startValue;
		var d = tween.timeDest - tween.timeStart;
		var a = tween.extra1;
		var p = tween.extra2;
		var s = tween.extra1;
		
		switch (tween.tweenType.toLowerCase()) {
			
			// Simple
			case "linear" : // Simple linear tweening.					No easing.
				return c*t/d + b;
			// Quadratic		
			case "easeinquad" : // Quadratic (t^2) easing in.				Accelerating from zero velocity.
				return c*(t/=d)*t + b;
			case "easeoutquad": // Quadratic (t^2) easing out.			Decelerating to zero velocity
				return -c *(t/=d)*(t-2) + b;
			case "easeinoutquad": // Quadratic (t^2) easing in/out.			Acceleration until halfway, then deceleration
				if ((t/=d/2) < 1) return c/2*t*t + b;
				return -c/2 * ((--t)*(t-2) - 1) + b;
				
			// Cubic		
			case "easeincubic": // Cubic (t^3) easing in.				Accelerating from zero velocity
				return c*(t/=d)*t*t + b;
			case "easeoutcubic": // Cubic (t^3) easing out.				Decelerating to zero velocity
				return c*((t=t/d-1)*t*t + 1) + b;
			case "easeinoutcubic": // Cubic (t^3) easing in/out.			Acceleration until halfway, then deceleration
				if ((t/=d/2) < 1) return c/2*t*t*t + b;
				return c/2*((t-=2)*t*t + 2) + b;
			case "easeinquart": // Quartic (t^4) easing in.				Accelerating from zero velocity
				return c*(t/=d)*t*t*t + b;
			case "easeoutquart": // Quartic (t^4) easing out.				Decelerating to zero velocity
				return -c * ((t=t/d-1)*t*t*t - 1) + b;
			case "easeinoutquart": // Quartic (t^4) easing in/out.			Acceleration until halfway, then deceleration
				if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
				return -c/2 * ((t-=2)*t*t*t - 2) + b;
			case "easeinquint": // Quintic (t^5) easing in.				Accelerating from zero velocity
				return c*(t/=d)*t*t*t*t + b;
			case "easeoutquint": // Quintic (t^5) easing out.				Decelerating to zero velocity
				return c*((t=t/d-1)*t*t*t*t + 1) + b;
			case "easeinoutquint": // Quintic (t^5) easing in/out.			Acceleration until halfway, then deceleration
				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;
			
			// Sinusoidal
			case "easeinsine": // Sinusoidal (sin(t)) easing in.			Accelerating from zero velocity
				return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
			case "easeoutsine": // Sinusoidal (sin(t)) easing out.			Decelerating to zero velocity
				return c * Math.sin(t/d * (Math.PI/2)) + b;
			case "easeinoutsine": // Sinusoidal (sin(t)) easing in/out.		Acceleration until halfway, then deceleration
				return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
			
			// Exponential
			case "easeinexpo": // Exponential (2^t) easing in.			Accelerating from zero velocity
				return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
			case "easeoutexpo": // Exponential (2^t) easing out.			Decelerating to zero velocity
				return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
			case "easeinoutexpo": // Exponential (2^t) easing in/out.		Acceleration until halfway, then deceleration
				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;
			
			// Circular
			case "easeincirc": // Circular (sqrt(1-t^2)) easing in.			Accelerating from zero velocity
				return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
			case "easeoutcirc": // Circular (sqrt(1-t^2)) easing out.			Decelerating to zero velocity
				return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
			case "easeinoutcirc": // Circular (sqrt(1-t^2)) easing in/out.		Acceleration until halfway, then deceleration
				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;
			
			// Elastic
			case "easeinelastic": // Elastic (exponentially decaying sine wave)
				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;
			case "easeoutelastic": // elastic (exponentially decaying sine wave)
				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;
			case "easeinoutelastic": // elastic (exponentially decaying sine wave)
				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;
			
		// Robert Penner's explanation for the s parameter 
		// (overshoot ammount): s controls the amount of 
		// overshoot: higher s means greater overshoot
		// s has a default value of 1.70158, which produces 
		// an overshoot of 10 percent s==0 produces cubic 
		// easing with no overshoot
			
			case "easeinback": // back (overshooting cubic easing:			Backtracking slightly, then reversing 
						 // (s+1)*t^3 - s*t^2) easing in.			direction and moving to target.
				if (s == undefined) s = 1.70158;
				return c*(t/=d)*t*((s+1)*t - s) + b;
			case "easeoutback": // back (overshooting cubic easing:			Easing out - moving towards target, 
						   // (s+1)*t^3 - s*t^2)					overshooting it slightly, then reversing and coming back to target
				if (s == undefined) s = 1.70158;
				return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
			case "easeinoutback": // back (overshooting cubic easing:		Backtracking slightly, then reversing direction and moving to target,
						     // (s+1)*t^3 - s*t^2) easing in/out.		then overshooting target, reversing, and finally coming back to target
				if (s == undefined) s = 1.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;
		// This were changed a bit by me (since I'm not using 
		// Penner's own Math.* functions). So I changed it to call 
		// findTweenValue() instead (with some different arguments)
			
			case "easeinbounce": // Bounce (exponentially decaying parabolic bounce) easing in
				return c - processValue (0, c, 0, d-t, d, "easeOutBounce") + b;
			case "easeoutbounce": // Bounce (exponentially decaying parabolic bounce) easing out
				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;
				}
			case "easeinoutbounce": // Bounce (exponentially decaying parabolic bounce) easing in/out
				if (t < d/2) return processValue (0, c, 0, t*2, d, "easeInBounce") * .5 + b;
				return processValue(0, c, 0, t*2-d, d, "easeOutBounce") * .5 + c*.5 + b;
		}
	}
		
};