jQuery(function(){
	var item = 1;
	var fadeTimeOut = 1000; // Time it takes to fade the images in/out
	var switchTimeOut = 7000; // Time it takes to switch from one to the other
	var carouselTimeOut = 0; // Initialize the setTimeout
	var numOfItems = jQuery('#banner li').size(); // Get the number of items in the carousel
	var mouseOver = false;
	
	jQuery('#banner li').each(function(i){
		/*jQuery(this).click(function(){
			if( jQuery('a', this).attr('href') ) {
				location.href = jQuery('a', this).attr('href');
			}
		});*/
	
		jQuery(this).mouseover(function(){
			mouseOver = true;
			clearTimeout(carouselTimeOut);
		});
		
		jQuery(this).mouseout(function(){
			mouseOver = false;
			go();
		});
	});
	
	// Start the carousel after it is loaded in the DOM
	jQuery('#banner').ready(function(){
		go();
	});
	
	jQuery('#banner .controls a').each(function(i){
		jQuery(this).click(function(){
			mouseOver = false;
			clearTimeout(carouselTimeOut);
			item = i;
			loopCarousel();
		});
	});
	
	function go(){
		carouselTimeOut = setTimeout(function(){
			loopCarousel();
		}, switchTimeOut);
	}
	
	function loopCarousel(){
		if(!mouseOver){
			jQuery("#banner li.current").fadeOut(fadeTimeOut).removeClass("current");
			jQuery("#banner li:eq(" + item + ")").fadeIn(fadeTimeOut).addClass("current");
			
			jQuery("#banner .controls a.current").removeClass("current");
			jQuery("#banner .controls a:eq(" + item + ")").addClass("current");
			
			// Increment item or reset to 0
			item = (item == numOfItems - 1) ? 0 : item + 1;
			
			go();
		}
	}
});
