// Written By: Michael Worrall
// Date: 11/04/2007
// Perameters: ClassName of li's you wish to switch between!
var waitfor = null;	
var autoCarosel = new Class({
				
	initialize: function(classname) {
		var arr = $$('.' + classname); //creates array of each element we want to cross fade by returning a list of classes		
		this.arr = arr;
		
		for (var x=0; x<arr.length; x++) {
			arr[x].className = arr[x].className + ' nohover';
			
			arr[x].addEvent('mouseenter', function() { //adds hover event handlers to the tab so that the carosel appears to stop when hovered, when in reality it continues to work, but has a lower zindex than the currently hovered one!
				this.parentNode.className = 'noover';
				this.className = 'over';
			});
			
			arr[x].addEvent('mouseleave', function() {
				this.parentNode.className = '';
				this.className = '';
			});
		}
		
		if (this.waitDuration==null) {
			this.waitDuration = 8000;
		}
		waitfor = this.waitDuration;
	},
	
	start: function() {
		
		var show = new Array(); //creates an array to house an effect object for each object we want to croos fade
		var hide = new Array(); //creates an array to house an effect object for each object we want to croos fade
		var tabs = new Array();
		var arr = this.arr; //retrieves array from initialize function!
		
		var currentSlide = 0;
		
		for (x=0; x<arr.length; x++) {
			var a = arr[x].getChildren()[0];
			var div = arr[x].getChildren()[1];
			tabs[x] = a; //creates array of a tags that we want to swap the class on!
			show[x] = new Fx.Style(div, 'height', {duration: 0, onComplete: continuefade}); //creates and effect object for each element we want to fade!
			hide[x] = new Fx.Style(div, 'height', {duration: 0}); //creates and effect object for each element we want to fade!
		}
				
		//var startfade = (function() { dofade() }).delay(waitfor); //starts the fading on execution of the .start method
		dofade(); //inits the swap imediately
		
		function continuefade() { //continues the fade onComplete of each Fx.Style
			if (waitfor!=null) {
				(function() { dofade() }).delay(waitfor);
			}
		};
		
		function dofade() {
			var previousSlide = null;
			if (currentSlide == 0) {
				show[currentSlide].start(0,234);
				tabs[currentSlide].className = 'tabactive';
				
			} else if (currentSlide == arr.length) {
				previousSlide = currentSlide-1;
				currentSlide = 0;
				show[currentSlide].start(0,234);
				tabs[currentSlide].className = 'tabactive';
				hide[previousSlide].start(234,0);
				tabs[previousSlide].className = 'tab';
				
			} else {
				previousSlide = currentSlide-1;
				show[currentSlide].start(0,234);
				tabs[currentSlide].className = 'tabactive';
				hide[previousSlide].start(234,0);
				tabs[previousSlide].className = 'tab';
				
			} 
			currentSlide++;
		};
		
	}
	
});


autoCarosel.implement({ //adds the optional properties to the class	
	wait: function(waitDuration){
        this.waitDuration = waitDuration;
    }
});

