/**  Switcher: Changes switcher background automatically, on a timer. **/
var Switcher = {
	timerOn: true, 
	timerRunning: null,

	items: null,
	imageSrc: null,

	current: null,

	/* Times 8 seconds before running function to automatically rotate.
	   Will only start a new timer if the previous one has finished. */
	timerStart: function(){
		if(Switcher.timerRunning == null){ Switcher.timerRunning = setTimeout("Switcher.rotateSwitch()",10000);  }
	},

	/* Needs to be run when a timer completes. 
	   Resets timerRunning to its default state. */
	clearTimer: function(){
		clearTimeout(Switcher.timerRunning);
		Switcher.timerRunning = null;
	},

	/* If the timer is set to on, rotates switcher background, and kicks timer off again. 
	   If timer is off, halts rotation. */
	rotateSwitch: function(el){
		result = Switcher.imageSrc.indexOf(Switcher.current); //Searches to see if there is an image currently being displayed. Returns the number of the image.
		if(result == -1){ Switcher.setSwitch(Switcher.items[0]); Switcher.current = Switcher.imageSrc[0]; }
		else if(Switcher.imageSrc[result + 1] == undefined) { Switcher.setSwitch(Switcher.items[0]); Switcher.current = Switcher.imageSrc[0]; }
		else { Switcher.setSwitch(Switcher.items[result + 1]); Switcher.current = Switcher.imageSrc[result + 1]; }

		if(Switcher.timerOn){
			Switcher.clearTimer();
			Switcher.timerStart();
		}

		else { Switcher.timerStart(); }
	},

	setSwitch: function(el){	
		if(el = el.getParent('li')){ 
			Switcher.hideAll();
			el.fade('in');

			span = el.getChildren('span');
			if(span[0]) { 
				var fx = new Fx.Tween(span[0]);
				fx.start.pass(['top', -40], fx).delay(2000); 
			}
		}
	},

	getSwitches: function(){
		switcherItem = $$('ul#switcher li > img');
		return switcherItem; 
	},
	
	getSrc: function(switcherImage){
		imageSrc = [];
		switcherImage.each(function(img){
			imageSrc.push(img.get('src'));
		});
	
		return imageSrc;
	},

	hideAll: function(){
		var items = $$('ul#switcher li');
		items.each(function(item){
			item.fade('out');

			span = item.getChildren('span');
			if(span[0]){ span[0].setStyle('top', 0); }
		});
	},

	/* Sets up mouseenter and mouseleave behaviour on switches, as well as mootools tooltips. 
	   Also starts initial timer. Executed on 'domready'. */
	start: function(){ 
		Switcher.items = Switcher.getSwitches();
		Switcher.imageSrc = Switcher.getSrc(Switcher.items);
		Switcher.current = $$('li.switcherActive > img')[0].get('src');

		var items = $$('ul#switcher li');
		items.each(function(item){
			if(!item.hasClass('switcherActive')){ item.setStyle('opacity', '0'); }
		});
	

		Switcher.timerStart();
	}
};	

