// Carousel
var firstRun = true;
var carOpenIndex = 1; 
var carIndex = 2;
var carAutoMode = true;
var carButtonOn = false;
var carButtonColor = false;

$(document).ready(function(){
			 
	// 1st item setup
	carouselAutomate();
	
	carButtonColor = $("#carouselButtons li").css('color');
	
	// Submenus
	$("#carouselButtons li").click(function() {
		carAutoMode = false;
		carClickedIndex = $(this).closest('li').index() +1;
		carouselJump(carClickedIndex)
	});
	
	
	$("#carouselButtons li").hover(function() {
 		$(this).css('cursor','pointer');
 		}, function() {
 		$(this).css('cursor','auto');
	})
	
	$('#carouselButtons li:nth-child(1)').css('borderColor', '#a08009');
	$('#carouselButtons li:nth-child(1)').css('color', '#a08009');
	
});

function carouselJump(clickedIndex){
	$('#carousel li:nth-child('+carOpenIndex+')').fadeOut(300);
	$('#carousel li:nth-child('+clickedIndex+')').fadeIn(300);
	carButtonOnSwitch(clickedIndex);
}

function carButtonOnSwitch(clickedIndex){

	if(carButtonOn){
		$('#carouselButtons li:nth-child('+carButtonOn+')').css('borderColor', carButtonColor);
		$('#carouselButtons li:nth-child('+carButtonOn+')').css('color', carButtonColor);
	}
	
	$('#carouselButtons li:nth-child('+clickedIndex+')').css('borderColor', '#a08009');
	$('#carouselButtons li:nth-child('+clickedIndex+')').css('color', '#a08009');
	carButtonOn = clickedIndex
	carOpenIndex = clickedIndex;
}

// Looper
function carouselAutomate(){
	
	if(!carAutoMode){return false;}
	
	var carItems = $("#carousel li").size();
	
	if(firstRun){
		$('#carousel li').first().css('display', 'block');
		firstRun = false;
		carButtonOn = 1;
		//carButtonOnSwitch(1);
		
		setTimeout("carouselAutomate()",7000);
		return false;
	}

	$('#carousel li:nth-child('+carOpenIndex+')').fadeOut(300);
	$('#carousel li:nth-child('+carIndex+')').fadeIn(300);
	carButtonOnSwitch(carIndex);
	carOpenIndex = carIndex;
	carIndex++;
	
	if(carIndex > carItems){
		carIndex = 1;	
	}
	
	setTimeout("carouselAutomate()",7000);
}

