$(document).ready(function()
{
	var currentIndex = 0;
	var slides = $("#slideshow > img");
	var animating = false;
	
	function next()
	{
		if ( animating ) 
		{
			//console.log( "animating, skipping next()" );
			return;
		}

		animating = true;
		
		var slide = $(slides[currentIndex % slides.length]);
		var fadeTime = 1000;
		
		// first arrange the new slide to be on top
		slides.each( function(i)
		{
			//var zIndex = 1000 - currentIndex % slides.length;
			var zIndex = i == currentIndex % slides.length ? 1000 : 0
			$(this).css( "z-index", zIndex );
			
			//console.log( "i:", i, " currentIndex: ", (currentIndex % slides.length), " z-index: ", zIndex )
		});
		
		// fade in new slide, and at completion, fade out old slides and increment currentIndex
		slide.fadeIn( fadeTime, function()
		{
			slides.each( function(i)
			{
				if ( i != ( currentIndex % slides.length ) )
				{
					$(this).fadeOut( fadeTime );
				}
			});
			
			currentIndex++;		
			animating = false;
		} );
	}
	
	/*
	// for testing
	
	$("#slideshow .pointer").click( function()
	{
		next();
		return false;
	});
	*/
	
	next();
	
	setInterval( next, 5000 );

});	

