var curSlide = 1;
var carouselItems = 0;
var itemWidth = 910;
var carouselTimer = null;
var circlingNext = false;
var circlingPrev = false;


$(document).ready(function() {
	
	// only load the carousel on the home page
	if ($("body").hasClass("home")) {
	
		$(".slide").each(function(e,i) {
			window.carouselItems++;
		});
	
		var itemsWidth = (window.carouselItems * itemWidth) + itemWidth;
		$(".carousel_viewport").css("width", itemsWidth + "px");
		$(".carousel_viewport").css("left", "0px");
		
		$(".carousel").hover(carouselHoverIn, carouselHoverOut);
	
		$("#carousel-prev").click(prevClick);
		$("#carousel-next").click(nextClick);
		
		$("#carousel-prev").hide();
		$("#carousel-next").hide();
		
		carouselAutoRotate();
	}


});

function carouselAutoRotate() {
	window.clearInterval(carouselTimer);
	carouselTimer = window.setInterval("nextSlide();", 15000);
}


function nextSlide() {
	
	// Are we at the last slide?
	if (window.curSlide == window.carouselItems) {
		// Duplicate the first slide for the animation
		// and append at the end
		firstSlide = $(".slide:first").clone();
		$(".carousel_viewport").append(firstSlide);
		
		// Flag that we're circling around (forwards)
		window.circlingNext = true;
	}

	// Go to the next slide
	window.curSlide++;
	animateSlides();

}



function prevSlide() {
	
	// Are we at the first slide?
	if (window.curSlide == 1) {
		// Duplicate the last slide for animation
		// And append it before this one
		lastSlide = $(".slide:last").clone();
		$(".carousel_viewport").prepend(lastSlide);
		
		// Move over to see our original slide
		$(".carousel_viewport").css("left", (itemWidth * -1)+"px");
		window.curSlide = 2;
		
		// Flag that we're circling around (backwards)
		window.circlingPrev = true;
	} 
	
	window.curSlide--;
	animateSlides();

}

function animateSlides() {
	
	var targetLeft = ((itemWidth * curSlide) - itemWidth);
	var curLeft = $(".carousel_viewport").css("left");
	curLeft = curLeft.substring(0, curLeft.length - 2);
	
	if (targetLeft < curLeft.left) {
		console.log("minus one!");

	}
	targetLeft *= -1;	
	
	$(".carousel_viewport").animate({ left: targetLeft + "px"}, 400, slideAnimationComplete);
}

function slideAnimationComplete() {
// The slide animation is done
// Handle all post-animation duties

	// If the user is circling around from moving forwards
	if (window.circlingNext) {
		// Set current slide number and position properly
		window.curSlide = 1; 	
		$(".carousel_viewport").css("left", "0px");
		
		// Get rid of the animation slide
		$(".slide:last").remove();
		window.circlingNext=false;
	}
	
	
	// If the user is circling around from moving backwards
	if (window.circlingPrev) {
	
		// Set current slide number and position properly
		window.curSlide = window.carouselItems;
		
		targetLeft = ((window.carouselItems * window.itemWidth) - window.itemWidth) * -1;
		$(".carousel_viewport").css("left", targetLeft + "px");
		
		$(".slide:first").remove();
		
		window.circlingPrev = false;
	}
}

function gotoSlide(slide) {

	window.curSlide = slide;

	animateSlides();
	
}


function carouselHoverIn() {
	$("#carousel-prev").fadeIn(400);
	$("#carousel-next").fadeIn(400);
}

function carouselHoverOut() {
	$("#carousel-prev").delay(200).fadeOut(400);
	$("#carousel-next").delay(200).fadeOut(400);	
}



function prevClick() {
	window.clearInterval(carouselTimer);
	setTimeout("carouselAutoRotate()", 20000);
	prevSlide();
	return false;
}

function nextClick() {
	window.clearInterval(carouselTimer);
	setTimeout("carouselAutoRotate()", 20000);
	nextSlide();
	return false;
}
