var imgNum = 0;
var stdW = 0;
var slideshowW = 0;
var slideTime = 0;

var change_img = function( stdW ){
	var newLeft = '-' + stdW + 'px';
	$('#slideshow_all_images').animate({'left' : newLeft}, 500, 'easeInExpo', function(){
		var firstChild = $('#slideshow_all_images img:first-child');
		var theSrc = firstChild.attr('src');
		var theTitle = firstChild.attr('title');

		$('<img />')
			.attr('src', theSrc)
			.attr('title', theTitle)
			.attr('alt', '')
			.appendTo('#slideshow_all_images');
		firstChild.remove();
		$('#slideshow_all_images').css('left', '0px');

		/* Preload della prossima immagine */
		var imgToPreload = $('#slideshow_all_images img:nth-child(2)').attr('src');
		$('<img />')
			.attr('src', imgToPreload)
			.load(function(){
				$(this).remove();
			})
	});
}

$(document).ready(function(){
	/* Sistemo la larghezza del contenitore di immagini in modo da farle stare tutte in riga */
	imgNum = $('#slideshow_all_images img').length;
	stdW = $('#top_slideshow').width();
	slideshowW = imgNum * stdW;
	$('#slideshow_all_images').width(slideshowW);
	
	$('#slideshow_all_images img').each(function(){
		if( $(this).attr('title') != '' ) {
			$(this).css('cursor', 'pointer');
		}
	})
})

$(window).load(function(){
	if( imgNum > 1 ) {
		slideTime = setInterval(change_img, 5500, stdW);
	}
	
	$('#slideshow_all_images img').mouseup(function(){
		if( $(this).attr('title') != '' ) {
			window.open($(this).attr('title'));
		}
	})
})
