//config
var slideContainer = '.slideshow';
var transitionSpeed = 1000;
var switchSpeed = 5000;

// retrieve a list of images from server 	
$(function() {    
	$.getJSON('../json/json_slideshow.php', startSlideshow); 
	
	function startSlideshow(slides) {
		
		var startingImageSrc = '/' + $(slideContainer + ' > img').attr('src');
		var imageArray = new Array();
		var i = 0;
		
		$.each(slides, function(index, value) {
			//ignore the first image		
			if (value != startingImageSrc) {
				imageArray[i] = '<img src="' + value + '"/>';
				i++;
			}
		});
		
		imageArray.sort();
		
		$.each(imageArray, function(index, value) {
			$(value).appendTo(slideContainer);
		});
		
		
	};
	slideTimer();
});    

// switch the image 
function slideSwitch() {
	var $active = $(slideContainer + ' img.active');
	
	if ( $active.length == 0 ) {
		//$active = $(slideContainer + ' img.active');
	}
	
	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 slideTimer() {
	setInterval ("slideSwitch()", switchSpeed);    
};
