//config
var slideContainer = '.slideshow';
var transitionSpeed = 1000;
var switchSpeed = 5000;

// retrieve a list of images from server 	
$(function() {    
	$.getJSON('../slideshowhome.php', startSlideshow); 
	
	function startSlideshow(slides) {
		var startingImageSrc = $(slideContainer + ' > img').attr('src');
		$.each(slides, function(index, value) {
			//ignore the first image
			if (value != startingImageSrc) {
				$('<img src="' + value + '"/>').appendTo(slideContainer);
			}
		});
	};	
});    

// switch the image 
function slideSwitch() {
	var $active = $(slideContainer + ' img.active');

	if ( $active.length == 0 ) $active = $(slideContainer + ' img:last');
		
		var $next =  $active.next('img').length ? $active.next('img') : $(slideContainer + ' img:first');
		
		$active.addClass('last-active');
		
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, transitionSpeed, function() {
				$active.removeClass('active last-active');
			});
}

// timer
$(function() {
	setInterval ("slideSwitch()", switchSpeed);    
});
