/*
 * ACCIDENT GALLERY.COM
 *
 * Class: tween.js
 * Version: 1.0.1
 * Desc:	Custom class used to perform smooth 
 *		animation defined by an instance of
 *		the Spline class.
 */

var Tween = function(pFrom, pTo, pStart, pCount, tCurve, pFunc) {
	var inScene	= null;
	var from	= null;
	var to		= null;
	var delta	= null;
	var fStart	= null;
	var fCount	= null;
	var frame	= null;
	var timeCV	= null;
	var execAfter	= null;
	var retVal	= null;
	
	// CONSTRUCTOR
	function _construct(pFrom, pTo, pStart, pCount, tCurve, pFunc) {
		setValues(pFrom, pTo);
		calcDelta();
		placeInScene();
		setTimeline(pStart, pCount);
		setTimeCurve(tCurve);
		setExecuteAfter(pFunc);
	}
	
	// METHODS
	function setValues(pFrom, pTo) {
		from = pFrom;
		to = pTo;
	}
	function calcDelta() {
		delta = to - from;
	}
	function placeInScene() {
		inScene = true;
	}
	function removeFromScene() {
		inScene = false;
	}
	function getState() {
		return inScene;
	}
	function getFrom() {
		return from;
	}
	function setTimeline(pStart, pCount) {
		fStart = pStart;
		fCount = pCount;
	}
	function setTimeCurve(tCurve) {
		timeCV = tCurve;
	}
	function setExecuteAfter(pFunc) {
		if(pFunc != null) {
			execAfter = pFunc;
		}
	}
	function getExecuteAfter() {
		return execAfter;
	}
	function interpolate(eTime) {
		
		frame = eTime - fStart;

		var frameNext = frame + 1;
		
		if(eTime >= fStart) {
			if(frameNext < fCount) {
				retVal = from + (timeCV.calc(frame) * delta);
				return retVal;
			} else {
				removeFromScene();
			}
		}
	}

	// CLASS PROPERTIES
	this.from = from;
	this.to = to;
	this.delta = delta;
	this.fStart = fStart;
	this.fCount = fCount;
	this.frame = frame;
	this.timeCV = timeCV;
	this.retVal = retVal;

	// CLASS METHODS
	this._construct = _construct;
	this.placeInScene = placeInScene;
	this.getState = getState;
	this.getFrom = getFrom;
	this.getExecuteAfter = getExecuteAfter;
	this.interpolate = interpolate;

	_construct(pFrom, pTo, pStart, pCount, tCurve, pFunc);
}