function replay() {
	clearTimeout($timeoutg);
	$pause.hide();   
	$title.animate( { bottom:'0px' });
	$thumbnails.animate( { top:'0px' });
	$("#menu").fadeIn();
	$.vegas( 'slideshow' );
	return false;
}
$(function() {
								//inizializza le variabili
	var pictures = [];
	$flash = $( '#flash' );
	$thumbnails = $("#thumbnails");
	$img = $("#scroll a img");
	$pause = $( '#pause' );
	$title = $( '#title' );
	$idx = 0;
								//imposta le misure delle immagini
	$img.width(112).css("padding-left","5px");
	$img.fadeTo("fast", 0.5)
								//imposta la lunghezza del contenitore delle immagini
	$('#scroll' ).width( function() {
		$totalWidth = 0;
		$( this ).find( 'a img' ).each( function() {
			$totalWidth += $( this ).outerWidth( true );
		});
        return $totalWidth+(3.1*$('#scroll').find( 'a img' ).length);
	});
								//rendi il contenitore flottante
	$thumbnails.mousemove(function(e){
		$goto = (e.pageX/$(window).width())*($("#scroll").width()-$(window).width());
		$(this).scrollLeft($goto);
	});
								//genera una lista di tutte le immagini
	$("#scroll").find( 'a' ).each( function() {
	      	pictures.push({ src: $( this ).attr( 'href' )});
      })
								//avvia vegas con la lista immagini
	$.vegas( 'slideshow', { 
	        backgrounds: pictures,
	        delay: 4000
	})( 'overlay' );
								//funzione ogni volta che vegas cambia immagine
	$( 'body' ).bind( 'vegasload', function( e, img ) {
		var src = $( img ).attr( 'src' );
		$img.eq($idx).fadeTo("slow", 0.5);
		$idx = $( 'a[href="' + src + '"]' ).index();
		$title.text();
		$img.eq($idx).fadeTo("slow", 1);
		
		$flash.show().fadeOut( 1000 );
	});
								//cliccando su una immagine  
	$thumbnails.find( 'a' ).click( function() {
		$pause.show();
	        $thumbnails.animate( { top: '-85px' });
	        $title.animate( { bottom: '-130px' });
		$("#menu").fadeOut();
		$img.eq($idx).fadeTo("slow", 0.5);
	        $idx = $( this ).index();
	        $.vegas( 'slideshow', { step: $idx } )( 'pause' );
		$timeoutg = setTimeout(replay,5000);
		return false;
	});
								//cliccando sul tasto pausa
	$pause.click(replay);
//$title.text($("#scroll").width());
});
