var isAutoAdvancing = true;
jQuery(function() {
	$('ul.carouselNav li a').click(function() {
		// Unset other thumbs
		$('ul.carouselNav li a').removeClass('sel');

		// Set THIS thumb as selected
		$(this).addClass('sel');

		// Get index of LI
		var linkIndex = $("ul.carouselNav li a").index(this);

		// Scroll to the correct "page"
		$('div.carouselDiv').scrollTo('ul.featureWindow li:eq(' + linkIndex + ')', 200);

		return false;
	});
	
	$('ul.carouselNav li:eq(0)').find('a').addClass("sel");

	function advanceFeature()
	{
		// Get current index
		var currentThumb = $("ul.carouselNav li a.sel");
		var currentIndex = $("ul.carouselNav li a").index(currentThumb);

		// How many thumbnails total?
		var totalThumbs = $("ul.carouselNav li a").length;

		if(currentIndex >= (totalThumbs-1))
		{
			currentIndex = 0;
		} else currentIndex++;
		
		// Unset other thumbs
		$('ul.carouselNav li a').removeClass('sel');
		
		// Set new thumb as selected
		$('ul.carouselNav li a:eq(' + currentIndex + ')').addClass('sel');
		
		// Trigger click event
		$("ul.carouselNav li a.sel").click();

	}
	
	$('div.carouselDiv').everyTime('10s', 'autoAdvance', advanceFeature, 0);

	$('div.carouselDiv').bind('mouseenter', function() {
		if(isAutoAdvancing == true)
		{
			$('div.carouselDiv').stopTime('autoAdvance');
			isAutoAdvancing = false;
		}
	});

	$('div.carouselDiv').bind('mouseleave', function() {
		if(isAutoAdvancing == false)
		{
			isAutoAdvancing = true;
			$('div.carouselDiv').everyTime('10s', 'autoAdvance', advanceFeature, 0);
		}
	});
});