/**  Switcher: Changes switcher background automatically, on a timer. 
 *   If a switch gets mouse focus, it becomes the switcher background. Also, a mootools tooltip is displayed.
 *   When a switch loses focus, the automatic rotation continues, from that selected switch.
 *   HREFs in links should be set to "#", for mootools tooltips. **/


	var cl = 1; //Global variable used to set the active switch.
	var timerOn = true; //Global variable used to toggle automatic rotation.
	var timerRunning = null; //Global variable to store timer id if running.
	
	var Site = {
		//Times 4 seconds before running function to automatically rotate.
		//Will only start a new timer if the previous one has finished.
		timerStart: function(){
		    if(timerRunning == null){ timerRunning = setTimeout("Site.rotateImage()",4000);  }
		},

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

		//If the timer is set to on, rotates switcher background, and kicks timer off again. If timer is off, halts rotation.
		rotateImage: function(){
		    if(timerOn){
		    	if(cl == 4){ var nextImage = 1; }
		    	else { var nextImage = cl + 1; }
		    	cl = nextImage;
			
		    	$('switcher').setStyle("background", "url('assets/switcher/fulls/" + nextImage + ".gif?') no-repeat");
			Site.clearTimer();
			Site.timerStart();
		    }
 		},
		
		//Sets up mouseenter and mouseleave behaviour on switches, as well as mootools tooltips. Also starts initial timer. Executed on 'domready'.
		switcher: function(){ 
			var switcher = $('switcher');
			var els = $$('#switcher .switch');
			var current = 1;

			els.each(function(s, i){										
				s.addEvent('mouseenter', function(){
					cl = parseInt(s.name);
					timerOn = false;

					var set = "url('assets/switcher/fulls/" + s.name + ".gif?') no-repeat";
			    
					switcher.setStyle("background", set);
				});
	
				s.addEvent('mouseleave', function(){
					Site.clearTimer();
					
					timerOn = true;
					Site.timerStart();
				});

				//new Tips(s, {
				//	showDelay: 150,
				//	hideDelay: 100,
				//	fixed: true
				//});

			});	
			
			Site.timerStart();
		}

	};

	window.addEvent('domready', Site.switcher);

