// JavaScript Document


function _set_image_and_effect(indirizzo,Did_Selected,Link_Selected) {
	
	$(".foto_grande").attr({ src: indirizzo }).hide();
	
	
	$(".immagine_grande").append('<div class="loading"><img src="img/lightbox-ico-loading.gif" /></div>').show();
	
	var objImagePreloader = new Image();
	objImagePreloader.onload = function() {
		
		
		//height_img_orig = $('.foto_grande').height();
		$(".loading").remove();
		var H_slide = -(($(".foto_grande").height())/2);
		$(".foto_grande").css({'margin-top':H_slide}).hide().fadeIn(400);
		$(".box_menu_foto ul li a").removeClass("selected");
		$(".box_menu_foto ul li a[count="+Img_Active+"]").addClass("selected");
		
				
				if(Did_Selected == 0 || Did_Selected === undefined ) { //se non c'è didascalia nasconde il div altrimenti crea effetto
					
					$(".Title").hide();
					
				} else {
				
					$(".Title p").empty().hide().append(Did_Selected).fadeIn(400);		
					
				}	
				
				
				if(Link_Selected == 0 || Link_Selected === undefined ) { //se non c'è link mette cancelletto altrimenti mette link, disattiva click e toglie manina
					
					$(".img_link").attr("href","#");
					$(".img_link").css("cursor","default");					
					$(".img_link").click(function() {return false});
					
				} else {
				
					$(".img_link").attr("href",Link_Selected);		
					
				}

				
		objImagePreloader.onload=function(){};
	};

		
	objImagePreloader.src = indirizzo;

	
}


function StopCycle() {

		$(".play").show();
		$(".stop").hide();
		clearInterval(GoCycle);

}


function StartCycle(Img_Total,Titles,Links,GallImg,Speed) {
	
	
	$("a.stop").click(function() {
		StopCycle();					   
		return false;
	});
	
	GoCycle = setInterval(function() {			
   			SetCycle(Img_Total,Titles,Links,GallImg);
	}, Speed);

}



function SetCycle(Larg_Gallery,Titles,Links,Gallery,Direction) {
	
				
	if(Direction === undefined || Direction  == 1)  {
			
			
			if (Img_Active==Larg_Gallery-1) {
				
				_set_image_and_effect(Gallery[0],Titles[0],Links[0]);
				Img_Active = 0;	
					
			} else {
				
				Img_Active++;
				
				_set_image_and_effect(Gallery[Img_Active],Titles[Img_Active],Links[Img_Active]);
				
			}
			
			
			
	} else if (Direction  == -1)  {
				
			
			if (Img_Active == 0) {
			
				Img_Active = Larg_Gallery-1;
				_set_image_and_effect(Gallery[Img_Active],Titles[Img_Active],Links[Img_Active]);
				
			} else {
			  
				Img_Active--;
			  
			  _set_image_and_effect(Gallery[Img_Active],Titles[Img_Active],Links[Img_Active]);
			  
			}
			
			
				
	}
		

}






function Image_Viewer_Blue(GallImg,Titles,Links,Speed,Keyboard,ControllerManual,AutoPlay) {
	
	
	
	Img_Active = 0;
	
	$(".stop").hide();
	

	
	var Img_Total = GallImg.length;
	
	
	if(Speed==0) {	
		$(".play").hide();
		$(".stop").hide();		
	} else if (Speed === undefined) {	
		Speed = 2000;		
	}
	
	
	if(Titles === undefined || Titles === null || Titles === 0  ) { //se l'array è vuoto o è uguale a zero o è settato a NoTitle nasconde div Title
		
	
	Titles = new Array();
	
	} 
	
	
	if(Keyboard=="YesKey") { $(".box_menu_foto").show();} else {$(".box_menu_foto").hide();}
	
	if(ControllerManual=="YesContr") {$(".Controller_Manuale").show();} else {$(".Controller_Manuale").hide();}
	
	if(AutoPlay=="YesAutoplay") {
		$(".play").hide();
		$(".stop").show();
		StartCycle(Img_Total,Titles,Links,GallImg,Speed);	
	}
	
	if(Img_Total==1 && AutoPlay=="YesAutoplay") { //se l'img è una sola e se la galleria è impostata con autoplay lo disabilita e nasconde play e stop
		StopCycle();
		$(".play").hide();
		$(".stop").hide();	
	}
	
	
	
	
	
	$(".box_menu_foto ul").append("<li><p>Foto</p></li>");
	
	
	
	for (i=0;i<Img_Total;i++) {
		
		$(".box_menu_foto ul").append("<li><a href=\"#\" count=\""+i+"\">"+(i+1)+"</a></li>");
		
	}
	


	_set_image_and_effect(GallImg[0],Titles[0],Links[0]);

    
	$(".box_menu_foto a").click(function() {			
		
		var CountImg = $(this).attr("count");
		
		Img_Active = CountImg;
		
		_set_image_and_effect(GallImg[Img_Active],Titles[Img_Active],Links[Img_Active]);
		StopCycle();
		

		return false;
	});
	

	
	$("a.play").click(function() {								   
		$(".play").hide();
		$(".stop").show();		
		StartCycle(Img_Total,Titles,Links,GallImg,Speed);

		return false;
	});
	
	
	$("a.next").click(function() {								   	
		SetCycle(Img_Total,Titles,Links,GallImg,1);
		StopCycle();
		return false;
	});
	
	
	$("a.prev").click(function() {								   		
		SetCycle(Img_Total,Titles,Links,GallImg,-1);
		StopCycle();
		return false;
	});
	
	
	//StartCycle(Img_Total,GallImg);

	
}
