var currentSlide=0;
var isPlaying=true;
var delay=5000; // in ms
var shortDelay = 4000; // in ms
var timer1, slideCount;

/* Change the currently displayed slide */
function change(slideNum) {
	currentSlide = slideNum;
	$("#slideShow").find(".slide").hide().eq(slideNum).show();
}

/* change slide, wait, repeat */
function doRotation() {
	currentSlide = (currentSlide+1) % slideCount;
	change(currentSlide);
	timer1 = setTimeout('doRotation()', delay);
}

/* executes when user mouses over the slideShow */
function togglePlay() {
	isPlaying = !isPlaying;
	if (isPlaying) {
		doRotation(); /* loop through slides */
	} else {
		clearTimeout(timer1); /* stop the loop */
	}
}

/* initialize */
function initSlideshow() {
	slideCount = $(".slide").length;

	/* add hover functions for user interaction */
	/*$("#slideShow").hover(
		function() {
			togglePlay();
		},
		function() {
			setTimeout('togglePlay()', shortDelay);
		}
	);*/
	
	/* hide features after first */
	$(".slide:gt(0)").each(function(i) {
		$(this).hide();
	});

	/* set up images for slides */
	$(".slide").each(function(i) {
		var imgHref = $(this).find(".slideTitle a").attr("href");
		var imgSrc = $(this).find(".slideImg a").attr("href");
		var imgAlt = $(this).find(".slideTitle a").text();
		$(this).find(".slideImg").html('<a href="' + imgHref + '"><img src="' + imgSrc + '" alt="' + imgAlt + '" /></a>');
	});
	
	/* wait for images to load to start rotation */
	$(".slide:last .slideImg img").load(function(){
		timer1 = setTimeout('doRotation()', delay);
	});
}

function waitForIt() {
	if ($(".slideImg").length) {
		initSlideshow();
	}
	else {
		setTimeout('waitForIt()', 100);
	}
}

waitForIt();
