$(document).ready(function(){
	var totWidth=0;
	var positions = new Array();
	
	$('#slideshowSlides .slideshowSlide').each(function(i) {
		positions[i]= totWidth;
		totWidth += $(this).width();
		
		/* The positions array contains each slide's commulutative offset from the left part of the container */
		
		if(!$(this).width()) {
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});
	
	$('#slideshowSlides').width(totWidth);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#slideshowMenu ul li a').click(function(e,keepScroll){
		$('li.slideshowMenuItem').removeClass('act').addClass('inact');
		$(this).parent().addClass('act');
		
		var pos = $(this).parent().prevAll('.slideshowMenuItem').length;
		
		$('#slideshowSlides').stop().animate({marginLeft:-positions[pos]+'px'},450);
		
		e.preventDefault();
		
		// Stopping the auto-advance if an icon has been clicked:
		if(!keepScroll) clearInterval(itvl);
	});
	
	$('#slideshowMenu ul li.slideshowMenuItem:first').addClass('act').siblings().addClass('inact');
	
	var current=1;
	function autoAdvance() {
		if(current==-1) return false;

		$('#slideshowMenu ul li a').eq(current%$('#slideshowMenu ul li a').length).trigger('click',[true]);
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 8;
	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
});
