/*
* AnimationController
* ----------------------------------------
* @last_modified: 2009-02-22
* @author: Svetovoe Oborudovanie <svet@svetpro.ru>
*/

function AnimationController(iInterval) {	
	
	this.aAnimations = [];	
	this.bProcessing = false;
	this.iTimer = null;
	this.iInterval = iInterval;
	this.fStopCallbackFunction = null;	
	
}

AnimationController.prototype = {		
	
	setStopCallbackFunction : function(fStopCallbackFunction) {
		
		this.fStopCallbackFunction = fStopCallbackFunction;
		
	},
	
	addAnimation : function(oAnimation) {
		
		if(!(oAnimation instanceof Animation)) {
			return;
		}
		
		oAnimation.setController(this);
	
		this.aAnimations.push(oAnimation);		
	
	},
	
	removeAnimation : function(oAnimation) {
		
		this.aAnimations.remove(oAnimation);			
		
	},
	
	start : function() {
		
		if(this.bProcessing) {
			return false;	
		}
		
		this.bProcessing = true;
			
		var oThis = this;
			
		this.iTimer = setInterval(
			function() {
					
				oThis.process();
					
			},
			this.iInterval
			);				
			
		return true;
	
	},
	
	process : function() {
		
		if(this.aAnimations.length > 0) {
		
			for(var i = 0; i < this.aAnimations.length; i++) {
				this.aAnimations[i].process();
			}
			
			return;		
			
		}		
		
		this.bProcessing = false;
		
		clearInterval(this.iTimer);
		
		if(this.fStopCallbackFunction) {
			this.fStopCallbackFunction();
		}			
	
	}
	
}
