i = 0;

$(function(){
	$.getScript("/smtvs/gallery/bari/galleryList.php",function(){
		$("#frecciaGalSx").click(function(){
			frecciaClick(-1,gallery.length);	
			return false;
		});
		$("#frecciaGalDx").click(function(){
			frecciaClick(1,gallery.length);	
			return false;
		});
        $('#switch a').lightBox();

	});	
});

loadFoto = function(gallery){
	$("#switch").fadeOut("fast",function(){
		var urlGal = "/smtvs/gallery/bari/thumb/" + gallery[i];
		var img = new Image();
		img.src = urlGal;
		img.onload = function(){		
			$("#fotoGal").attr("src",urlGal);
			$("#zoom").attr("href",(urlGal.replace("thumb","zoom")));
			$("#switch").fadeIn("slow");		
		}
	});
}

frecciaClick = function(x,max){
	if((i+x)<=0){
		i = 0;
		$("#frecciaGalSx").fadeOut("fast");
	} else if((i+x)>=max) {
		i = max - 1;	
		$("#frecciaGalDx").fadeOut("fast");
	} else {
		i = i + x;	
		$("#frecciaGalSx").show();
		$("#frecciaGalDx").show();
	}
	loadFoto(gallery);
}
