var AnimationDirector = new Class({
	/* The Director controls multiple Scenes */
	Implements: [Options,Events, Chain], 						  

	options: {
		/* CSS Selector */
		stage : '',
		/* randomise scenes? */
		random : true
	}, 
	
	log: function(){
		return false;
		//if('console' in window && 'log' in window.console) console.log.apply(console, arguments);
	},
	
	initialize: function(options){ 
		this.setOptions(options);
		//this.log('this.options=',this.options);
		
		/* list of all scenes */
		this.Scenes = [];
		this.currentScene = 0;
		/* periodical */
		this.periodId = 0;
		/* time delay between scenes (in ms)*/
		this.sceneDelay = 5000;
		/* init */
		this.createStage();
		
		var scenes = document.id(this.Stage).getElements('.scene');
		
		if(this.options.random) scenes.shuffle();
		
		scenes.each(
			/* add all scenes */
			function(item, index, array){
				this.addScene({'scene' : item,'index':index});
			},this
		);		
		
		/* Random */
		//--------------------- if(this.options.random) this.Scenes.shuffle();
		
		this.createNav();
		/* pad out first chain */
		//this.chain(this.startTimer);
		//this.chain( this.Scenes[0].stepAnimation.bind(this.Scenes[0], {'addEvent':false}) );
		//this.NavLinkArray[0].fireEvent('click');
		
		/* fade in */
		this.Scenes[0].stepAnimation({'addEvent':false});
		///this.chain(this.startTimer);
		/* fade out */
		this.chain( this.Scenes[0].stepAnimation.bind(this.Scenes[0], {'addEvent':true}) );
		
		//this.NavLinkArray[0].fireEvent('click');
		/* delay */
		///this.chain(this.startTimer);
		/* queue up fade out */
		//this.chain( this.Scenes[0].stepAnimation.bind(this.Scenes[0], {'addEvent':true}) );
		
		/* fade in */
		//this.Scenes[0].stepAnimation({'addEvent':true});
		
		/* periodical */
		this.startTimer();
		
		
	},
	
	

	
	 /*
	Creates the Stage for Animation
	$params[''] () : unnused
	*/
	createStage : function (params){
		this.Stage = new AnimationStage({'stage':this.options.stage});
		/* add delay, then restart interval */
		document.id(this.Stage).addEvent('mouseleave', 
			this.startTimer.bind(this)
		);
		document.id(this.Stage).addEvent('mouseenter', 
			this.clearTimer.bind(this)
		);
				
	},
	
	 /*
	Adds a Scene. Multiple scenes comprise a finished animation
	$params['scene'] (str) : CSS selector
	*/
	addScene : function (params){
		var scene = false;
		if( scene = new AnimationScene({'stage': this.Stage, 'scene': params.scene}) ){
			/* event when a Scene completes */
			scene.addEvent('sceneComplete',function(){this.callChain();}.bind(this));
			
			/* add to list */
			this.Scenes.push( scene );
		}
	},
	
	/*
	creates the animation sequence via Chain
	params['index'] (int, zero-based, optional) : which scene?
	params['addEvent'] (bool) : add event handler
	*/
	resetChain : function (params){
		/* sequence of scenes 
			function is chained twice because all Actors only have a chain with 2 steps
		*/
		
		//this.log('resetChain params=',params);
		
		/* empty sequence */
		this.clearChain();
		
		/* temp list of scenes */
		var sortedScenes = this.Scenes;
		
		var addEvent = (params.addEvent) ? true : false;
		
		if(params && params.index > 0){
			var firstHalf = this.Scenes.slice(0,params.index);
			var lastHalf = this.Scenes.slice(params.index);
			
			/* swap */
			sortedScenes = lastHalf.concat(firstHalf);
			
			
			
		}
		
		sortedScenes.each(
			function(item, index, array){
				
				//this.log('resetChain Scene = ',document.id(item),'params=',params);
				/* fade in */
				this.chain( item.stepAnimation.bind(item, {'addEvent':false}) );
				/* fade out */
				this.chain( item.stepAnimation.bind(item, {'addEvent':addEvent}) );
				//this.log('Scene=',document.id(item));
			},this
		);
		
	},

	/*
	
	params['index'] (int, zero-based, optional) : which scene?
	*/
	nextScene : function (params){
		
		//this.log('nextScene() this.currentScene=',this.currentScene,'params=',params);
		
		if(params && this.currentScene != params.index){
			this.log('params=', params);
			if(params.clickFromUser){
				this.clearTimer();
			}
			
			/* fade out */
			this.callChain();
			
			/* skip straight to scene */
			this.log('A');
			/* fade in */
			this.chain( this.Scenes[params.index].stepAnimation.bind(this.Scenes[params.index], {'addEvent':false}) );
			/* chain delay */
			
			//this.chain(this.startTimer);
		
			this.log('B');
			/* fade out */
			this.chain( this.Scenes[params.index].stepAnimation.bind(this.Scenes[params.index], {'addEvent':true}) );
			
			this.currentScene = params.index;
		
		}
		
		
		
		
	},
	
	/*
	create interactive nav to control slideshow
	$params[''] () : unused
	*/
	createNav : function (){
		
		this.Nav = new Element('div',{'class':'home-feature-nav'});
		this.NavLinkArray = [];
		this.Scenes.each(
			function(item, index, array){
				var link = new Element('a',{'text':index, 'href':'javascript:;'});
				if(index==0) {
					link.addClass('active');
				}
				
				/* handler */
				link.addEvent('click', function(){
						var clickFromUser = false;
						

						
						if(arguments.length){
							//this.log('link click arguments=',arguments);
							if(arguments.clickFromUser) clickFromUser = true;
						}
						
						
						var el;
						
						if(el=this.Nav.getElement('.active')){
							el.removeClass('active');
						}
						
						link.addClass('active'); 
						this.nextScene({'index':index,'clickFromUser':clickFromUser});
						
					}.bind(this, {'clickFromUser' : true}) );
				/* store */
				this.NavLinkArray.push(link);
				this.Nav.grab(link);
			},this
		);
		
		document.id(this.Stage).grab(this.Nav);
	},
	
	/*
	starts a periodical that auto-changes the slideshow
	$params[''] () : unused
	*/
	startTimer : function (){
		var delayLength = this.sceneDelay;
		if(arguments.delayLength){
			//this.log('startTimer arguments=',arguments);
			delayLength = arguments.delayLength;
		}else{
			
		}
		
		//this.log('delayLength=',delayLength);
		
		
		//this.log('startTimer this.currentScene=',this.currentScene);
		
		//if(this.periodId){
			//this.log('this.periodId=',this.periodId);
			//clearInterval(this.periodId);
		//}
		
		this.periodId = function(){
			var index = this.currentScene + 1;
			if(index == this.Scenes.length){
				/* reset */
				index=0;
			}
			this.NavLinkArray[index].fireEvent('click',{'clickFromUser':false});
		}.periodical(delayLength, this);
		
		//this.log('this.periodId=',this.periodId);
	},
	/*
	clears the auto timer
	$params[''] () : 
	*/
	clearTimer : function(){
		if(this.periodId) clearInterval(this.periodId);	
	}
	
	
});
