var speed=450;
var current=0;
var auto=true;

function nextSlide() {
	if(!auto) return;
	
	current = (current+1)%$('#slideshowmenu li').length;
	if(current == 0) {
		speed = 0;
	}
	
	$($('#slideshowmenu li')[current]).click();
	auto = true;
}
	
$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	var totWidth=0;
	var positions = new Array();
	var autoSlide;

	$('#slides IMG').each(function(i){
		/* Loop through all the slides and store their accumulative widths in totWidth */
		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;
		}
	});

	$('#slides').width(totWidth);
	/* Change the container div's width to the exact width of all the slides combined */
	
	// Resizing the last update panel
	var totUpdatesWidth = 0;
	$(".last_updates_frame #last_updates DIV").each(function(){
		totUpdatesWidth += $(this).outerWidth(true);
	});
	
	$(".last_updates_frame #last_updates").width(totUpdatesWidth);

	$('#slideshowmenu li').click(function(e){

		/* On a thumbnail click */
		$('#slideshowmenu li').removeClass('sel');
		$(this).addClass('sel');
		
		var pos = $(this).prevAll('li').length;
		current = pos;
		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},speed);
		/* Start the sliding animation */

		if(!auto) clearInterval(autoSlide);
		auto = false;
		speed = 450;
		//e.preventDefault();
		/* Prevent the default action of the link */
	});
	
	$($('#slideshowmenu li')[0]).addClass('sel');
	var autoSlide = setInterval("nextSlide()", 5000);

	//$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
});
