
/**
 * Slideshow
 */
(function($){
	var slideshow_dir = 'upload/slideshow/',
		delay = 9000,
		num_slides = 0,
		current_slide_num = 0,
		current = null,
		next = null;
	
	$(function(){
		$('.slideshow').each(enable_slideshow);
	});
	
	function enable_slideshow() {
		var $this = $(this);
		
		//count slides
		num_slides = slideshow_pictures.length;
		
		//add style
		$this.css('position', 'relative');
		
		//add the first image if it's not there
		current = $('<img/>').attr('src', nextSlide());
		next = $('<img/>').attr('src', nextSlide()).hide();
		
		$this.html('');
		$this.append(current).append(next);
		
		//add position absolute to img
		$('img', $this).css({
			position: 'absolute',
			left: 0,
			top: 0
		});
		
		//start the cycline
		setInterval(function(){
			showNext();
		}, delay);
	}
	
	function showNext() {
		next.hide().css({left:0});

		next.fadeIn(1500, function(){
			current.attr('src', next.attr('src'));
			next.css({left:'-1000px'}).attr('src', nextSlide());
		});
	}
	
	function nextSlide() {
		current_slide_num = (current_slide_num + 1) % num_slides;
		var current_slide = slideshow_pictures[current_slide_num];
		return slideshow_dir + current_slide['id'] + '/' + current_slide['file'];
	}
	
})(jQuery);
