var carouselDelay = 6000; //ms until slide start, and between transitions
var carouselDuration = 500; //ms for transitions
var carouselIndex = 0; //Current Active Index (Zero-Based)

$(function(){
	var topImages = $('.top-image');
	var contentImages = $('.content-image');
	var carouselCaptions = $('.carousel-caption');
	var carouselDescriptions = $('.carousel-description');
	var sideNav = $('a', '#index-side-nav');
	var carouselNav = $('a', '#carousel-nav');
	
	function carouselGoTo( nextIndex ){
		// Top Images
		$(topImages[carouselIndex]).animate({opacity:"0"}, {queue:false, duration:carouselDuration});
		$(topImages[nextIndex]).animate({opacity:"1"}, {queue:false, duration:carouselDuration});
		// Content Images		
		$(contentImages[carouselIndex]).animate({opacity:"0"}, {queue:false, duration:carouselDuration});
		$(contentImages[nextIndex]).animate({opacity:"1"}, {queue:false, duration:carouselDuration});
		// Side Nav
		// IE7 Specific, prevent weird flickering
		if($.browser.msie && $.browser.version.substr(0,1)=="7"){
			$(sideNav[carouselIndex]).css('color','#B2B2B2').mouseenter(function(){
				$(this).css('color','#8D0E3A');
			}).mouseleave(function(){
				$(this).css('color','#B2B2B2');
			});
			$(sideNav[nextIndex]).css('color','#8D0E3A').unbind('mouseenter').unbind('mouseleave');			
		}else{				
			$(sideNav[carouselIndex]).removeClass('current');
			$(sideNav[nextIndex]).addClass('current');
		}
		// Carousel Nav
		$(carouselNav[carouselIndex]).removeClass('active');
		$(carouselNav[nextIndex]).addClass('active');
		// Carousel Captions
		$(carouselCaptions[carouselIndex]).animate({opacity:"0"}, {queue:false, duration:carouselDuration});
		$(carouselCaptions[nextIndex]).animate({opacity:"1"}, {queue:false, duration:carouselDuration});
		// Carousel Descriptions
		$(carouselDescriptions[nextIndex]).appendTo('#secondary-content'); // Make sure active content is top-most (and therefore clickable)
		$(carouselDescriptions[carouselIndex]).animate({opacity:"0"}, {queue:false, duration:carouselDuration});
		$(carouselDescriptions[nextIndex]).animate({opacity:"1"}, {queue:false, duration:carouselDuration});
		// Update Index
		carouselIndex = nextIndex;
	}
	// Make sure starting content is top-most (and therefore clickable);
  	$(carouselDescriptions[0]).appendTo('#secondary-content');
  	carouselGoTo(0);
	// Queue up carousel
	$('#wrapper').everyTime(carouselDelay, 'homecarousel', function(){
		// Get Next Index
		var nextIndex = carouselIndex + 1;
		if(nextIndex >= topImages.length){
			nextIndex = 0;
		}
		carouselGoTo(nextIndex);	
    });
    sideNav.each(function(i){
    	$(this).click(function(event){
    		$('#wrapper').stopTime('homecarousel');
    		carouselGoTo(i);
    		return false;
    	});
    });
    carouselNav.each(function(i){
    	$(this).click(function(event){
    		$('#wrapper').stopTime('homecarousel');
    		carouselGoTo(i);
    		return false;
    	});
    });
});