/*
 * Animator Class
 *	Version: 1.0.2
 *	Desc:	Responsible for delegating all animation.
 *		Only class to use setTimeout()
 *
 *	Notes:	This class is intended to collaborate with
 *		Tween Class instances nested within other
 *		types of class objects.
 */

var Animator = function() {

	// Class Properties
	var classObj	= this;
	var playBack	= null;
	var fps		= null;
	var elapsed	= null;
	var objList	= null;

	// Import Moving Objects
	function importList(a) {
		if(a.length > fps) {
			objList = a.slice(0, fps);
		} else {
			objList = a.slice();
		}
	}

	// Add Moving Object
	function addObject(t) {
		objList.push(t);
	}

	// Delete Moving Object
	function delObject(t) {
		objList.splice(t, 1);
	}

	// Animate Tween List
	function animate() {
		if(objList.length < 1) {
			clearTimeout(playBack);								// stop animating
			return;
		} else {
			for(var n=0;n<objList.length;n++) {
				objList[n].render(elapsed);				// play frame "elapsed" of tween "t"
				if(objList[n].getState() == false) {
					delObject(n);					// remove tween "t"
					break;						// advance to next tween
				}
			}
			if(objList.length) {
				nextFrame();
				playBack = setTimeout(function(){ classObj.animate() }, (1000 / fps));	// keep animating
			}
		}
	}

	// Set Framerate
	function setFps(f) {
		f = (f < 1) ? 1 : f;
		f = (f > 60) ? 60 : f;
		fps = f;
	}

	// Export Framerate
	function getFps() {
		return fps;
	}

	// Set Elapsed Time
	function setElapsed(e) {
		elapsed = e;
	}

	// Get Elapsed Time
	function getElapsed() {
		return elapsed;
	}

	// Increment Time
	function nextFrame() {
		(elapsed == null || typeof elapsed == "undefined") ? null : elapsed++;
	}

	// Initialize
	function init(e, f) {
		setElapsed(e);
		setFps(f);
	}
	function initScene() {
		for(var n=0;n<objList.length;n++) {
			objList[n].placeInScene();
		}
	}

	// Class Construction
	this.objList = objList;
	this.playBack = playBack;
	this.elapsed = elapsed;
	this.addObject = addObject;
	this.delObject = delObject;
	this.importList = importList;
	this.getFps = getFps;
	this.setFps = setFps;
	this.setElapsed = setElapsed;
	this.getElapsed = getElapsed;
	this.nextFrame = nextFrame;
	this.animate = animate;
	this.init = init;
	this.initScene = initScene;

}