var timerId = 0;

function setslider() {
	var lastvocerullo = $('#slider .boxslide:last');
	var prelastvocerullo = $(lastvocerullo).prev();
	$('#slider').prepend($(lastvocerullo).clone()).prepend($(prelastvocerullo).clone());
	$('#slider').css('left','-2104px');
	
	var firstvocerullothumb = $('#sliderthumb img:first');
	var prefirstvocerullothumb = $(firstvocerullothumb).next();
	//var prelastvocerullothumb = $(lastvocerullothumb).prev();
	//$('#sliderthumb').prepend($(lastvocerullothumb).clone()).prepend($(prelastvocerullothumb).clone());
	$('#sliderthumb').append($(firstvocerullothumb).clone()).append($(prefirstvocerullothumb).clone());
	$('#sliderthumb').css('left','-197px');
	
	//timerId = setInterval ( "rightroll()", 4000 );
}

function rightroll(tot) {
	$('#slider').animate({ left: '-='+(1052*tot)+'px'},
		function() {
			for(i=0;i<tot;i++) {
				var imgtemp = $('#slider .boxslide:first').next().next().clone();
				$('#slider .boxslide:first').remove();
				$('#slider').animate({ left: '+=1052px'},0).append( $(imgtemp) );	
			}
		});
	$('#sliderthumb').animate({ left: '-='+(197*tot)+'px'},
		function() {
			for(i=0;i<tot;i++) {
				var imgthumbtemp = $('#sliderthumb img:first').next().next().clone();
				$('#sliderthumb img:first').remove();
				$('#sliderthumb').animate({ left: '+=197px'},0).append( $(imgthumbtemp) );
			}
		});
	
	for(i=0;i<tot;i++) {
		var nextclaim = $('.claim:visible').next('.claim');
		if( !$(nextclaim).attr('id') ) nextclaim = $('.claim:first');
		$('.claim:visible').hide();
		$(nextclaim).fadeIn();
	}
	
	for(i=0;i<tot;i++) {
		var nexttext = $('.textpanel:visible').next('.textpanel');
		if( !$(nexttext).attr('id') ) nexttext = $('.textpanel:first');
		$('.textpanel:visible').hide();
		$(nexttext).fadeIn();
	}
}

function leftroll() {
	$('#slider').animate({ left: '+=1052px'},
		function() {
			var imgtemp = $('#slider .boxslide:last').prev().prev().clone();
			$('#slider .boxslide:last').remove();
			$('#slider').animate({ left: '-=1052px'},0).prepend( $(imgtemp) );			
		});
	$('#sliderthumb').animate({ left: '+=197px'},
		function() {
			var imgthumbtemp = $('#sliderthumb img:last').prev().prev().clone();
			$('#sliderthumb img:last').remove();
			$('#sliderthumb').animate({ left: '-=197px'},0).prepend( $(imgthumbtemp) );	
		});
	
	var nextclaim = $('.claim:visible').prev('.claim');
	if( !$(nextclaim).attr('id') ) nextclaim = $('.claim:last');
	$('.claim:visible').hide();
	$(nextclaim).fadeIn();
	
	var nexttext = $('.textpanel:visible').prev('.textpanel');
	if( !$(nexttext).attr('id') ) nexttext = $('.textpanel:last');
	$('.textpanel:visible').hide();
	$(nexttext).fadeIn();
}

$().ready(function() {
	setslider();
	
	$('#arrowright').click(function() {
		clearTimeout ( timerId );
		rightroll(1);
	});
	
	$('#arrowleft').click(function() {
		clearTimeout ( timerId );
		leftroll();
	});
	
	$('#sliderthumb img').live('click',function() {
		var position = $('#sliderthumb img').index( $(this) );
		rightroll(position);
	});
});