var slideshowTimer = null;
var slides = null;
var currentSlideIndex = 0;

var timerInterval = 3500;
var fadeInTime = 1000;
var fadeOutTime = 1000;
var slideInTime = 1250;

$(document).ready(function() {
	//If the slideshow div is present, load it up!
	if($('div#slideshow').size() > 0) {
		//Get all of the slides
		slides = $('ul li', 'div#slideshow');
		//If there are slides present...
		if(slides.length > 0) {
			switch($('ul', 'div#slideshow').attr('class')) {
				case "fade":
					//Fade in the first slide, and set the timer.
					$(slides[0]).css('display', 'block');
					slideshowTimer = setInterval("nextSlideFade()", timerInterval);
					break;
				case "slide":
					//Set all slides to visible, and set the timer.
					$('ul', 'div#slideshow').width(slides.length * $(slides[0]).width());
					$(slides).css('display', 'block');
					slideshowTimer = setInterval("nextSlideSlide()", timerInterval);
					break;
			}
	}
	}
});

function nextSlideFade() {
	//Increase the current slide
	oldSlideIndex = currentSlideIndex;
	currentSlideIndex++;
	//If the current slide ends up larger than the total amount, reset it.
	if(currentSlideIndex >= slides.length) currentSlideIndex = 0;
	//Hide the current slide, show the next one.
	$(slides[oldSlideIndex]).fadeOut(fadeOutTime, function() {
	});
	//The old slide is gone, bring in the new.
	$(slides[currentSlideIndex]).fadeIn(fadeInTime, function() {
		clearInterval(slideshowTimer);
		slideshowTimer = setInterval("nextSlideFade()", timerInterval);
	});	
}

function nextSlideSlide() {
	//Increase the current slide
	currentSlideIndex++;
	//If the current slide ends up larger than the total amount, reset it.
	if(currentSlideIndex >= slides.length) currentSlideIndex = 0;
	//Move the list over
	$('ul', 'div#slideshow').animate({
		left: -1 * ($(slides[currentSlideIndex]).width() * currentSlideIndex)
	}, slideInTime, "linear", function() {
		//It's done sliding, clear the timer and reset it.
		clearInterval(slideshowTimer);
		slideshowTimer = setInterval("nextSlideSlide()", timerInterval);
	});
}