var stop = false;
var timingfade = 600;
var timingTimeOutRotation = 8000;
var next=1;
var current=0;
var totLength;
var images;
var lock=false;

$(document).ready(function() {		
	//Load the slideshow
	//alert('externalJs caricato');
	images = $('#boxProgetto').children('img');
	//alert('Trovate ' + images.length + ' immagini');
	totLength=images.length
	setTimeout('startRotator()', 7000);
});



function startRotator(){
	
	if(stop!=true){
		
		$(images[current]).fadeOut(timingfade, function() {
			$(images[next]).fadeIn(timingfade, function() {
				if(stop!=true){
					current = (current + 1)%totLength;
					next = (next + 1)%totLength;
					setTimeout('startRotator()', timingTimeOutRotation);
				}
			});
		});
	}
}

function nextPush(){
	if(lock==false){
		stop = true;
		lock=true;
		if(current==next){
			next++;
		}
		$(images[current]).fadeOut(timingfade, function() {
			$(images[next]).fadeIn(timingfade, function(){
				current = (current + 1)%totLength;
				next = (next + 1)%totLength;
				lock=false;
			});
		});
	}
}

function prevPush(){
	if(lock==false){
		stop = true;
		lock=true;
		next=(current+totLength-1)%totLength;
		
		$(images[current]).fadeOut(timingfade, function() {
			$(images[next]).fadeIn(timingfade, function(){
				current=next;
				lock=false;
			});
		});
	}
}

