// JavaScript Document

//Declare the clock as a global variable. It needs to be outside of the document.ready.
var theClock;
//jQuery document.ready starts here.
$(document).ready(function() {
	//Grab the total number of slides in the slideshow. Add '1' to that number, because the random number generation rounds down. It is 'zero-based', and our DIVs are 'one-based'. 
	var totalSlides = $('.carouselBlock').length + 1;
	slides=new Array()
	for (i=1;i<=totalSlides-1;i++) {
		slides[i]=$('#carouselBlock'+i).html();
		$('#carouselBlock'+i).remove();
	}
	//Take a random starting number between 1 and the total.
	var startingPoint=Math.floor(Math.random()*totalSlides)
	if (startingPoint==0) {
		startingPoint=1;
	}
	if (totalSlides ==1){
		//Actually 0 slides because of the 1 that we added artificially. Abort completely.
		$("#jayCarousel").remove();
	} else if(totalSlides ==2){
		//Actually just 1 slide because of the 1 that we added artificially. Reveal it, but remove the buttons, and do nothing else.
		$('#slide3').html(slides[1]);
		$("#carouselPrev").remove();
		$("#carouselNext").remove();
	} else {
		//This means more than 1 slide exist on the page. Reveal a random starting point, leave the buttons exposed, and set the timer.
		$('#slide3').html(slides[startingPoint]);
		//Remove the extra 1 that we artificially added to the count and store it in a new variable.
		totalSlidesCount = totalSlides - 1;
		//Determine the next slide in the rotation.
		startNumber = startingPoint * 1;
		nextNumber = startNumber + 1;
		//In case we're already on the last one, go to the first one.
		if (nextNumber > totalSlidesCount) {
			nextNumber =1;
		}
		//Set the 'theNextNextSlide' variable so it's available to the buttons if they are clicked before the timer fires the 'slideChanger'. Then set the timer.
		theNextNextSlide = nextNumber;
		theClock = setTimeout('slideChanger("'+nextNumber+'")', 10000);
	}
	
	//Here's the jQuery FUNction to make the changer work on PREVIOUS arrow click
	$("#carouselPrev").click(function() {
		var totalSlides = slides.length-1;
		//This needs to be the slide before the 'theNextNextSlide'. If you're on the first, it needs to be the last.
		currentSlide = theNextNextSlide-1;
		if (currentSlide < 1) {
			currentSlide = totalSlides;
		}
		//This needs to be the slide before the one recently determined to be the 'currentSlide'. If that happens to be the first, 'previousSlide' needs to be the last.
		previousSlide = currentSlide-1;
		if (previousSlide < 1) {
		  previousSlide = totalSlides;
		}
		//theNextNextSlide should still be present and have the correct value. Since we're not advancing the slideshow, it's of no use to us, but we still need to maintain it's value for the next thing that might need it.
		theNextNextSlide = currentSlide;
		$('#slide1').html(slides[currentSlide]);
		$('#slide2').css('position','relative').html(slides[previousSlide]);
		$('#slide3').remove();
		$("#carouselPrev").css('display','none');
		$("#carouselNext").css('display','none');
		$('#slide1').animate( { left:'400px' }, 500, function() {
			//Animation complete callback.
			$('#slide1').empty().css('left','0px');
			$('#slide2').removeAttr('id').attr('id','slide3');
			$('#slide1').after('<div id="slide2" class="slider"></div>');
			$("#carouselPrev").css('display','block');
			$("#carouselNext").css('display','block');
		});
		//Stop the existing timer
		clearTimeout(theClock);
		theClock = setTimeout('slideChanger("'+currentSlide+'")', 10000);
		return false;
	});
	//End of the click function.
	
	//Here's the jQuery FUNction to make the changer work on NEXT arrow click
	$("#carouselNext").click(function() {
		var totalSlides = slides.length-1;
		//theNextNextSlide should still be present and have the correct value (either from the initial set-up, the 'slideChanger' functions's last firing, or the last button click).
		//This needs to be the slide before the 'theNextNextSlide'. If you're on the first, it needs to be the last.
		currentSlide = theNextNextSlide-1;
		if (currentSlide < 1) {
			currentSlide = totalSlides;
		}
		//Determine the 'nextNextSlide' value for the slide that we're currently about to load.
		nextNextSlide = theNextNextSlide;
		//Determine the 'theNextNextSlide' value for the slide that we're going to set for the timer or the next click.
		theNextNextSlide = theNextNextSlide+1;
		if (theNextNextSlide > totalSlides) {
		  theNextNextSlide = 1;
		}
		$('#slide1').html(slides[currentSlide]);
		$('#slide2').css('position','relative').html(slides[nextNextSlide]);
		$('#slide3').remove();
		$("#carouselPrev").css('display','none');
		$("#carouselNext").css('display','none');
		$('#slide1').animate( { left:'-400px' }, 500, function() {
			//Animation complete callback.
			$('#slide1').empty().css('left','0px');
			$('#slide2').removeAttr('id').attr('id','slide3');
			$('#slide1').after('<div id="slide2" class="slider"></div>');
			$("#carouselPrev").css('display','block');
			$("#carouselNext").css('display','block');
		});
		//Stop the existing timer
		clearTimeout(theClock);
		theClock = setTimeout('slideChanger("'+theNextNextSlide+'")', 10000);
		return false;
	});
	//End of the click function.
});
//jQuery document.ready ends here.

//Here's the FUNction to be called by the auto-rotator.
function slideChanger(newSlide){
	var totalSlides = slides.length-1;
	nextNextSlide = newSlide * 1;
	theNextNextSlide = nextNextSlide + 1;
	if (theNextNextSlide > totalSlides) {
		theNextNextSlide = 1;
	}
	currentSlide = nextNextSlide - 1;
	if (currentSlide < 1) {
		currentSlide = totalSlides;
	}
	$('#slide1').html(slides[currentSlide]);
	$('#slide2').css('position','relative').html(slides[nextNextSlide]);
	$('#slide3').remove();
	$('#slide1').animate( { left:'-400px' }, 500, function() {
		//Animation complete callback.
		$('#slide1').empty().css('left','0px');
		$('#slide2').removeAttr('id').attr('id','slide3');
		$('#slide1').after('<div id="slide2" class="slider"></div>');
	});
	theClock = setTimeout('slideChanger("'+theNextNextSlide+'")', 10000);
};



