/**
 * Tween Class
 * 
 * Animation multiple objects and properties with one js object and timer
 * 
 * An adaptation of an adaptation of Philippe Maegerman's JS Tween class for WPF/E
 * done by Richard Leggett, AKQA - richardleggett.co.uk.
 * and done again by Lenny Burdette, Schematic - lburdette@schematic.com
 * 
 * Usage :
 * 
 * new Tween(params, duration, options);
 * - params is a hash or an array of hashes
 * - duration is a length of time in seconds
 * - options is a hash
 * 
 * t = new Tween({ obj: someObject, prop: someProperty, 
 * 					func: tweenEquation, begin: value, 
 * 					end: value, suffix: string }, duration[, options]);
 * 
 * or (this is where the magic happens)
 * 
 * t = new Tween([
 *  	{ obj: someObject, prop: someProperty, 
 * 			func: tweenEquation, begin: value, 
 * 			end: value, suffix: string },
 *  	{ obj: someObject, prop: someProperty, 
 * 			begin: value, end: value, suffix: string }
 *  ], duration[, options]);
 * 
 * If you leave out any value in a subsequent object, it will default to the 
 * last used value. So if your first property requires "px" as a suffix, but 
 * your second doesn't, you'll have to manually overwrite it 
 * 
 * You can add an event handler to the whole Tween with addListener(type, callback);
 * 
 */
 
if ( !window.Loc ) { window.Loc = {}; }

Loc.Tween = function(params, duration, options) {
	this.setDuration(duration);
	this.options = options || {};
	this.init(params);
	this._listeners = {};
	return this;
}
Loc.Tween.prototype = {
	/*************************
	/ 		Initialize
	/************************/
	init : function(params) {
		this.objects = [];
		if (params instanceof Array) {
			for (var i = 0, j = params.length; i < j; i++) {
				this.initOne(params[i]);
			}
		} else {
			this.initOne(params);
		}
	},
	
	initOne : function(object) {
		this.objects.push(new Loc.TweenObject(object, this.mostRecent()));
	},
	
	mostRecent : function() {
		if (this.objects.length < 1) { return {}; }
		return this.objects[this.objects.length - 1];
	},
	
	setDuration : function(d) {
		this.duration = (d == null || d <= 0) ? 1 : d;
	},
	
	/*************************
	/ 		Play Control
	/************************/
	start : function() {
		this.goTo(0);
		this.initTimer();
		this.update();
		this.startEnterFrame();
		this.broadcast('onMotionStarted');
	},
	
	startFromHere : function() {
		for (var i = 0, j = this.objects.length; i < j; i++) {
			this.objects[i].getCurrentValue();
		}
		this.start();
	},
	
	stop : function() {
		this.isPlaying = false;
		this.broadcast('onMotionStopped', { halted : true });
	},
	
	loop : function() {
		this.options.loop = true;
		this.start();
	},
	
	resume : function() {
		this.initTimer();
		this.update();
		this.startEnterFrame();
		this.broadcast('onMotionResumed');
	},
	
	ffoward : function() {
		this.stop();
		this.time = this.duration;
		this.fixTime();
		this.update();
	},
	
	rewind : function(to) {
		this.stop();
		this.time = (to === undefined) ? 0 : to;
		this.initTimer();
		this.update();
	},
	
	/*************************
	/ 		Timer
	/************************/
	initTimer : function() {
		this.startTime = this.getTimer() - this.time * 1000;
	},
	
	goTo : function(time) {
		if (time > this.duration) {
			if (this.options.loop) {
				this.time = 0;
				this.loops = (this.loops === undefined) ? 1 : this.loops + 1;
				this.broadcast('onMotionLooped', { times : this.loops });
			} else {
				this.time = this.duration;
				this.isPlaying = false;
				this.broadcast('onMotionFinished');
			}
		} else if (time < 0) {
			this.time = 0;
		} else {
			this.time = time;
		}
	},
	
	nextFrame : function() {
		this.goTo((this.getTimer() - this.startTime) / 1000);
	},
	
	getTimer : function(){
		return new Date().getTime() - this.time;
	},
	
	/*************************
	/ 		Move
	/************************/
	update : function() {
		for (var i = 0, j = this.objects.length; i < j; i++) {
			this.objects[i].update(this.time, this.duration);
		}
		this.broadcast('onMotionChanged');
	},
	
	/*************************
	/ 		Playback
	/************************/
	startEnterFrame : function() {
		this.isPlaying = true;
		this.onEnterFrame();
	},
	
	onEnterFrame : function() {
		if (this.isPlaying) {
			this.nextFrame();
			this.update();
			var me = this;
			setTimeout(function() { me.onEnterFrame(); }, 0);
		}
	},
	
	/*************************
	/ 		Listeners
	/************************/
	addListener : function(type, callback) {
		if (! this._listeners[type]) {
			this._listeners[type] = [];
		}
		return this._listeners[type].push(callback);
	},

	broadcast : function(type, params) {
		params = params || {};
		params.type = type;
		params.tween = this;
		if (this._listeners[type]) {
			for (var i = 0, j = this._listeners[type].length; i < j; i++) {
				this._listeners[type][i](params);
			}
		}
	}
}

Loc.TweenObject = function(params, defaults) {
	this.obj = params.obj === undefined ? defaults['obj'] : params.obj;
	this.prop = params.prop === undefined ? defaults['prop'] : params.prop;
	if (! this.obj || ! this.prop) { return; }
	if (params.func === undefined) {
		this.func = defaults['func'] === undefined ? Loc.Tween.linearTween : defaults['func'];
	} else {
		this.func = params.func;
	}
 	if (params.begin === undefined) {
		this.begin = parseInt(this.obj[this.prop]) || 0;
	} else {
		this.begin 	= params.begin;
	}	
	if (params.end === undefined) {
		this.end = defaults['end'] || 0;
	} else {
		this.end = params.end;
	}
	if (params.suffix === undefined) {
		this.suffix = defaults['suffix'] || '';
	} else {
		this.suffix = params.suffix;
	}
}
Loc.TweenObject.prototype = {
	change : function() {
		return this.end - this.begin;
	},
	
	update : function(time, duration) {
		var newValue = this.func(time, this.begin, this.change(), duration);
		this.obj[this.prop] = newValue + ((this.suffix == '') ? 0 : this.suffix);
	},
	
	getCurrentValue : function() {
		this.begin = this.obj[this.prop];
	}
}

/**********************************************************************
TERMS OF USE - EASING EQUATIONS
Open source under the BSD License.
Copyright (c) 2001 Robert Penner
JavaScript version copyright (C) 2006 by Philippe Maegerman
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

   * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
   * Neither the name of the author nor the names of contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*****************************************/
Loc.Tween.backEaseIn = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158;
	return c*(t/=d)*t*((s+1)*t - s) + b;
}
Loc.Tween.backEaseOut = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158;
	return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
Loc.Tween.backEaseInOut = function(t,b,c,d,a,p){
	if (s == undefined) var 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;
}
Loc.Tween.elasticEaseIn = 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-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	
}
Loc.Tween.elasticEaseOut = 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);
	}
Loc.Tween.elasticEaseInOut = function (t,b,c,d,a,p){
	if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) var p=d*(.3*1.5);
	if (!a || a < Math.abs(c)) {var 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;
}

Loc.Tween.bounceEaseOut = 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;
	}
}
Loc.Tween.bounceEaseIn = function(t,b,c,d){
	return c - Tween.bounceEaseOut (d-t, 0, c, d) + b;
	}
Loc.Tween.bounceEaseInOut = function(t,b,c,d){
	if (t < d/2) return Tween.bounceEaseIn (t*2, 0, c, d) * .5 + b;
	else return Tween.bounceEaseOut (t*2-d, 0, c, d) * .5 + c*.5 + b;
	}

Loc.Tween.strongEaseInOut = function(t,b,c,d){
	return c*(t/=d)*t*t*t*t + b;
	}

Loc.Tween.regularEaseIn = function(t,b,c,d){
	return c*(t/=d)*t + b;
	}
Loc.Tween.regularEaseOut = function(t,b,c,d){
	return -c *(t/=d)*(t-2) + b;
	}

Loc.Tween.regularEaseInOut = function(t,b,c,d){
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
	}
Loc.Tween.strongEaseIn = function(t,b,c,d){
	return c*(t/=d)*t*t*t*t + b;
	}
Loc.Tween.strongEaseOut = function(t,b,c,d){
	return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}

Loc.Tween.strongEaseInOut = 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;
	}
Loc.Tween.linearTween = function (t, b, c, d) {
    return c*t/d + b;
};
Loc.Tween.easeInQuad = function (t, b, c, d) {
    return c*(t/=d)*t + b;
};
Loc.Tween.easeOutQuad = function (t, b, c, d) {
    return -c *(t/=d)*(t-2) + b;
};
Loc.Tween.easeInOutQuad = function (t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t + b;
    return -c/2 * ((--t)*(t-2) - 1) + b;
};
Loc.Tween.easeInCubic = function (t, b, c, d) {
    return c*(t/=d)*t*t + b;
};
Loc.Tween.easeOutCubic = function (t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
};
Loc.Tween.easeInOutCubic = 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;
};
Loc.Tween.easeInQuart = function (t, b, c, d) {
    return c*(t/=d)*t*t*t + b;
};
Loc.Tween.easeOutQuart = function (t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
};
Loc.Tween.easeInOutQuart = 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;
};
Loc.Tween.easeInQuint = function (t, b, c, d) {
    return c*(t/=d)*t*t*t*t + b;
};
Loc.Tween.easeOutQuint = function (t, b, c, d) {
    return c*((t=t/d-1)*t*t*t*t + 1) + b;
};
Loc.Tween.easeInOutQuint = 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;
};

/**
 * Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
 */
Loc.Tween.easeInExpo = function(t, b, c, d) {
	return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
}
/**
 * Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
 */
Loc.Tween.easeOutExpo = function(t, b, c, d) {
	return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
}
/**
 * Easing equation function for an exponential (2^t) easing in/out: acceleration until halfway, then deceleration.
 */
Loc.Tween.easeInOutExpo = 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;
}

/**
 * Easing equation function for an exponential (2^t) easing out/in: deceleration until halfway, then acceleration.
 */
Loc.Tween.easeOutInExpo = function(t, b, c, d) {
	if (t < d/2) return easeOutExpo (t*2, b, c/2, d);
	return easeInExpo((t*2)-d, b+c/2, c/2, d);
}

 
