var galleria = new Object();

var lnkList = new Array();
var imgList = new Array();
var didList = new Array();

	var label_next = "successiva";
	var label_prev = "precedente";
	var label_enlarge = "allarga";
	var label_of = "di";
	var label_click_enlarge = "clicca per allargare";
	var label_click_close = "clicca per chiudere";
	var label_show_info = "mostra informazioni";
	var label_hide_info = "nascondi informazioni";
	var disable_script = "disabilita script";
	var enable_script = "abilita script";
	var enable_or_disable_script = "abilita-disabilita script";

$(document).ready(function(){

	// alert("SCRIPT");
	// findImages();
	
	// $("br.js_hd").addClass("hdn");
	$("br.js_hd").css("display","none");

});

$(window).resize(function() {
	// alert("RESIZE");
	// checkMainSize();
	showImage();
});

$(window).load(function () {

	if ($("body").hasClass("it")) {
	
		label_next = "successiva";
		label_prev = "precedente";
		label_enlarge = "allarga";
		label_of = "di";
		label_click_enlarge = "clicca per allargare";
		label_click_close = "clicca per chiudere";
		label_show_info = "mostra informazioni";
		label_hide_info = "nascondi informazioni";
		disable_script = "disabilita script";
		enable_script = "abilita script";
		enable_or_disable_script = "abilita-disabilita script";
	
	} else {
	
		label_next = "next";
		label_prev = "previuos";
		label_enlarge = "enlarge";
		label_of = "of";
		label_click_enlarge = "click to enlarge";
		label_click_close = "click to close";
		label_show_info = "show info";
		label_hide_info = "hide info";
		disable_script = "disable script";
		enable_script = "enable script";
		disable_script = "disable script";
		enable_or_disable_script = "enable-disable script";
	
	}

	findImages();
	initImages();
	
	$("#js_toggle").mouseover().css("cursor","pointer");
	$("#js_toggle").click(function(){
		// alert("js_toggle");
	
		if ($("#singola_foto").hasClass("nascosta")){

			$("body").removeClass("js_off");
			// alert(" appena rimosso js_off");
			// if ($("body").hasClass("js_off")){ alert(" js_off !"); } else { alert(" NO js_off !"); }
			$("#scripts_session").load("scripts_on.html")
						
			$(".foto_contenitore").addClass("parsed");
	if (getIE6()) {
			$(".foto_contenitore").addClass("foto_contenitore_parsed");
	}
			$(".foto_contenitore img").removeClass("before");
			$(".foto_contenitore img").addClass("parsed");

			$("#singola_foto").show();
			$("#tools").show();
			$("#ddida").show();
			$(".lista_foto").hide();
			$("#singola_foto").removeClass("nascosta");
			sf_w = $("#singola_foto").width();
//			// alert("sf_w = "+sf_w);
//			$("#singola_foto").find("img").width("100%");

			$("#js_toggle").attr("title",disable_script);
			$("#js_toggle").text(disable_script);

		} else {
			$("#scripts_session").load("scripts_off.html")

			$("body").addClass("js_off");
			// alert(" appena assegnato js_off");
			// if ($("body").hasClass("js_off")){ alert(" js_off !"); } else { alert(" NO js_off !"); }
		
			$(".foto_contenitore").removeClass("parsed");
	if (getIE6()) {
			$(".foto_contenitore").removeClass("foto_contenitore_parsed");
	}
			$(".foto_contenitore img").addClass("before");
			$(".foto_contenitore img").removeClass("parsed");

			$("#singola_foto").hide();
			$("#tools").hide();
			$("#ddida").hide();
			$(".lista_foto").show();
			$("#singola_foto").addClass("nascosta");

			$("#js_toggle").attr("title",enable_script);
			$("#js_toggle").text(enable_script);
			

		}
		return false;
	});

//	initImages();

	if (js_status_disabled) $("#js_toggle").click();
});

function findImages() {
	$(".lista_foto ul li a").each(function(i){ 
		lnkList[i]=this; 
		$(this).click(function() {
			// alert("foto_thumb_contenitore a "+lnkList[i]+" cliccato ");
			galleria.r = i;
			// $("body").append("<div class=\"big\">QUI<img src=\""+lnkList[i]+"\"><\/div>");
			enlarge();
			return false;
		});
	});
	$(".lista_foto ul li img").each(function(i){ imgList[i]=this; });
	$(".lista_foto ul li .dida").clone().each(function(i){ didList[i]=this; });
/*	$(".lista_foto ul li .dida").each(function(i){ didList[i]=this; }); */
	$(".lista_foto").hide();
}

function initImages() {
	if (imgList.length>0) {
	
		$("#accessibility").append("<a id=\"js_toggle\" href=\"#\" title=\""+enable_or_disable_script+"\">"+disable_script+"<\/a><span id=\"scripts_session\"></span>");	
	
		$(".foto_contenitore").addClass("parsed");
	if (getIE6()) {
		$(".foto_contenitore").addClass("foto_contenitore_parsed");
	}
		$(".foto_contenitore img").removeClass("before");
		$(".foto_contenitore img").addClass("parsed");

		var emptyImg = "<img src=\"\/dev\/spacer.gif\" \/>"

		$(".foto_contenitore").append("<div id=\"singola_foto\"\/>");
//		$(".foto_contenitore").append("<a id=\"singola_foto\"\ href=\"#\" />"); NONNNONONONNNNOO
//		$("#singola_foto").css({"overflow":"hidden", "height":"300px", "position":"relative"});
		$("#singola_foto").append(emptyImg);
		$("#singola_foto img").click(function() {
			enlarge();
			// return false;
		});

		$(".foto_contenitore").append("<div id=\"tools\"\/>");
		
		if (imgList.length>1) {
			$("#tools").append("<a class=\"btn_prev\" href=\"#\" title=\""+label_prev+"\">"+label_prev+"<\/a> <div class=\"counter\"> 1 "+label_of+" "+imgList.length+"<\/div> <a class=\"btn_enlarge\" href=\"#\" title=\""+label_enlarge+"\">"+label_enlarge+"<\/a> <a class=\"btn_next\" href=\"#\" title=\""+label_next+"\">"+label_next+"<\/a>");		
		} else {
			$("#tools").append("<a class=\"btn_enlarge\" href=\"#\" title=\""+label_enlarge+"\">"+label_enlarge+"<\/a>");
		}
		$("#tools").append("<br class=\"clear\"\/>");

		$(".foto_contenitore").append("<div id=\"ddida\"\/>");
			
		$(".btn_next").click(function() {showNextImage();return false;});
		$(".btn_prev").click(function() {showPrevImage();return false;});
		$(".btn_enlarge").click(function() {enlarge(); return false;});

		$("body").prepend("<div id=\"fullscreen\" />"); 
		$("#fullscreen").hide();
		$("body").prepend("<div id=\"copertura\" />"); 
		$("#copertura").css("filter","alpha(opacity=75)"); 
		$("#copertura").css("opacity","0.75"); 
		$("#copertura").css("-moz-opacity","0.75"); 
		$("#copertura").hide(); 
		$("#copertura").fadeOut("fast");

	if (!getIE6()) {
		$("#copertura").css("position","fixed"); 
		$("#fullscreen").css("position","fixed");
	}

		galleria.numImgs 	= imgList.length;
		galleria.r 			= 0;
		
		animateImage();
	} else {
		// alert("no images");
	}
}

function showNextImage() {
	galleria.r++;
	if (galleria.r>= galleria.numImgs) galleria.r=0;
	animateImage();
	return false;
}
function showPrevImage() {
	galleria.r--;
	if (galleria.r<0) galleria.r=galleria.numImgs-1;
	animateImage();
	return false;
}
function showNextImageBig() {
	// alert("showNextImageBig()");
	galleria.r++;
	if (galleria.r>= galleria.numImgs) galleria.r=0;
	animateImageBig();
	// enlarge();
	return false;
}
function showPrevImageBig() {
	// alert("showPrevImageBig()");
	galleria.r--;
	if (galleria.r<0) galleria.r=galleria.numImgs-1;
	animateImageBig();
	// enlarge();
	return false;
}

function animateImage() {
	$("#singola_foto img").fadeOut("slow",function () {
		showImage();
	});
	return false;
}

function showImage() {
	if (imgList.length>0) {

		//$("#singola_foto img").eq(0).src=imgList[galleria.r].src;
		
		$("#singola_foto img")[0].src=imgList[galleria.r].src;
		$("#singola_foto img").css("position","absolute");
		$("#singola_foto img").css("top","50%");
				//$("#singola_foto img").css("left","50%"); // l'immagine si adatta alla larghezza quindi non serve il posizionamento orizzontale
				// var img_mid_w = $("#singola_foto img").width()/2;
			var img_mid_h = $("#singola_foto img").height()/2;
		$("#singola_foto img").css("marginTop",-img_mid_h);
				// $("#singola_foto img").css("marginLeft",-img_mid_w);
	
		$("#singola_foto img").attr("title",label_click_enlarge); 	
	
		$("#tools .counter").html((galleria.r+1)+" "+label_of+" "+imgList.length);
	
		$("#ddida").html(didList[galleria.r]);
		$("#singola_foto img").fadeIn("slow");

	}
}

function animateImageBig() {
	// alert("animateImageBig()");
	var w_h = getWh();

	$("#fullscreen .opnd_gall_btns").fadeOut("fast",function () {
		$("#img_big").fadeOut("fast",function () {
			showImageBig();
		});
	});
}

function getIE6() {
	if (jQuery.browser.msie && jQuery.browser.version.split('.')[0]=='6' ) { 
		return true;
	} else {
		return false;
	}
}

function getWw() {
	if (jQuery.browser.msie && jQuery.browser.version.split('.')[0]=='6' ) { 
		var w_w = $(window).innerWidth();
	} else {
		var w_w = $(window).width();
	}
	// alert("w_w = "+w_w);
	return w_w;
}
function getWh() {
	if (jQuery.browser.msie && jQuery.browser.version.split('.')[0]=='6' ) { 
		var w_h = $(window).innerHeight();
	} else {
		var w_h = $(window).height();
	}
	// alert("w_h = "+w_h);
	return w_h;
}

function showImageBig() {
// function enlarge() {
	
	$("#big_container").html("<img id=\"img_big\" src=\""+
							lnkList[galleria.r]+
							"\" />"); // <img src=\"ajax-loader.gif\" \/>

	// $("#big_preloader").show();

	$('img#img_big').hide();
	$('img#img_big').load(function(){
		// alert(" $(this).height() "+$(this).height() );

		var img_full_w = $(this).outerWidth();
		var img_full_h = $(this).outerHeight();
		var img_mid_w = img_full_w/2;
		var img_mid_h = img_full_h/2;
	
	//	$("#fullscreen img").hide();
		$("#fullscreen #big_container").hide();
	
		var w_h = getWh();
		
		var frst_mt_calc = calcola_frst_mt_calc(w_h);
		var mt_calc = calcola_mt_calc(w_h,img_full_h);
		
		animaImmagine(frst_mt_calc,img_full_w,img_full_h,mt_calc);

	});

}	

function enlarge() {
	// alert("enlarge()");

	if ($("body").hasClass("js_off")){ 
		// alert(" js_off !");
		location.href = lnkList[galleria.r];
		return true;
	} else { 
		// alert(" NO js_off !");
	}
	
	galleria.old_r = galleria.r; // memorizzo lo stato della galleria piccola
	
	if (jQuery.browser.msie && jQuery.browser.version.split('.')[0]=='6' ) $("#copertura").css("height",$(window).innerHeight());
	$("#copertura").fadeIn('slow'); 
	
	if (jQuery.browser.msie && jQuery.browser.version.split('.')[0]=='6' ) $("#fullscreen").css("height",$(window).innerHeight());
	$("#fullscreen").fadeIn('slow'); 

	$("#fullscreen").html("<div id=\"big_container\"><img id=\"img_big\" src=\""+
							lnkList[galleria.r]+
							"\" /><\/div>"); // <img src=\"ajax-loader.gif\" \/>

	var w_h = getWh();
	var w_w = getWw();
	var p_mid_w = Math.floor( w_w/2 )-40; // 40 = 6em/2+1em padding
	var p_mid_h = Math.floor( w_h/2 )-40; // 
	
	$("#fullscreen #big_container").css({
		'position':'relative',
		'background-color':'#000',
		'width':'6em',
		'height':'6em',
		'padding':'1em',
		'marginLeft':p_mid_w,
		'marginTop':p_mid_h
	});

	// alert("QUI");

	// $("#big_preloader").show();
	$('img#img_big').hide();
	$('img#img_big').load(function(){
		// alert(" $(this).height() "+$(this).height() );
		var img_full_w = $(this).outerWidth();
		var img_full_h = $(this).outerHeight();
		var img_mid_w = img_full_w/2;
		var img_mid_h = img_full_h/2;
	
	//	$("#fullscreen img").hide();
		$("#fullscreen #big_container").hide();
		$("#fullscreen #big_container").css({
			'position':'relative',
			'background-color':'#000',
			'width':'6em',
			'height':'6em',
			'padding':'3em 0 0 0' // 'padding':'1em'
		});
	
		var frst_mt_calc = calcola_frst_mt_calc(w_h);
		var mt_calc = calcola_mt_calc(w_h,img_full_h);
		
		animaImmagine(frst_mt_calc,img_full_w,img_full_h,mt_calc);

	});

}	

function calcola_frst_mt_calc(w_h) {
	var frst_mt_calc = Math.floor( (w_h - $("#fullscreen #big_container").height())/2 );
	if(frst_mt_calc<0) { frst_mt_calc = 0; }
	// alert("frst_mt_calc = "+frst_mt_calc);
	return frst_mt_calc;
}
function calcola_mt_calc(w_h,img_full_h) {

	// alert(" calcola_mt_calc(w_h="+w_h+",img_full_h="+img_full_h+") ");
	var mt_calc = Math.floor( (w_h - img_full_h)/2 );
	if(mt_calc<0) { mt_calc = 0; } // il margine calcolato deve essere almeno 0 per non nascondere il bottone di chiusura
	// alert("mt_calc = "+mt_calc);
	return mt_calc;
}

function animaImmagine(frst_mt_calc,img_full_w,img_full_h,mt_calc) {
	// alert("animaImmagine");	
	// alert(" frst_mt_calc = "+frst_mt_calc);
	// alert(" mt_calc = "+mt_calc);
	$("#fullscreen #big_container").css({
		'margin-top':frst_mt_calc,
		'margin-left':'auto',
		'margin-right':'auto'		
	}).show().animate({
		width:img_full_w, height:img_full_h, marginTop:mt_calc
	}, "slow", function() {
		$("#big_preloader").hide(); // nascondo preloader
		$("#img_big").fadeIn("slow",function(){ 
			mostraBottoniBIg(frst_mt_calc);
		});
	});
}

function mostraBottoniBIg(frst_mt_calc) {
	// ------------------------------------------------------ bottoniera big ------------------------------------------------------
	
	$("#fullscreen #big_container").append("<div class=\"opnd_gall_btns\" ><\/div>");
	$("#fullscreen .opnd_gall_btns").hide();
	$("#fullscreen .opnd_gall_btns").append("<div class=\"close_panel\" ><a href=\"\" id=\"big_close\" title=\""+label_click_close+"\" >&nbsp;<\/a><\/div>");
//	$("#fullscreen .opnd_gall_btns .close_panel").hide();

	var cp_h = $("#fullscreen .opnd_gall_btns .close_panel").height();
	var big_w = $("#img_big").width();
	var big_h = $("#img_big").height();

	$("#fullscreen .opnd_gall_btns").css("width",big_w);
	$("#fullscreen .opnd_gall_btns").css("height",big_h);
	$("#fullscreen .opnd_gall_btns").css({
		'position':'absolute',
		'zIndex':'1000',
		'top':'0',
		'left':'0',
		'width':'100%'
		/* 'backgroundColor':'yellow' */
		/* 'borderWidth':'1em', */
		/* 'borderColor':'#000', */
		/* 'borderStyle':'solid' */
	});
	$("#fullscreen .opnd_gall_btns").fadeIn("fast");
/*	,function(){
			$("#fullscreen .opnd_gall_btns .close_panel").slideDown();
	});
*/
	if (imgList.length>1) {
	
		$("#fullscreen .opnd_gall_btns").append("<a href=\"\" id=\"big_prev\" title=\"precedente\" >&nbsp;<\/a>");
		$("#fullscreen .opnd_gall_btns").append("<a href=\"\" id=\"big_next\" title=\"successiva\" >&nbsp;<\/a>");
	
		$("#big_prev").css("height",big_h-cp_h);
		$("#big_next").css("height",big_h-cp_h);
	
		$("#big_next").focus(function() { $(this).css("backgroundImage","url(http://sms.siena.it/dev/20090203/img/btn_next.gif)") });
		$("#big_next").blur(function() { $(this).css("backgroundImage","url(http://sms.siena.it/dev/20090203/img/spacer.gif)") });
	
		$("#big_prev").focus(function() { $(this).css("backgroundImage","url(http://sms.siena.it/dev/20090203/img/btn_prev.gif)") });
		$("#big_prev").blur(function() { $(this).css("backgroundImage","url(http://sms.siena.it/dev/20090203/img/spacer.gif)") });
	
		$("#big_next").click(function() {showNextImageBig(); return false;});
		$("#big_prev").click(function() {showPrevImageBig(); return false;});
	}

	$("#fullscreen .opnd_gall_btns>a").mouseover(function() {
		$(this).css("cursor","pointer");	
	}).mouseout(function() {}); 

	$("#big_close").click(function() {
		// $("#fullscreen .opnd_gall_btns .close_panel").slideUp("fast",function() {

			galleria.r = galleria.old_r; // riporto la galleria piccola allo stato iniziale

			$("#fullscreen .opnd_gall_btns").fadeOut("fast",function () {
				$("#img_big").fadeOut("slow",function () {
					// alert("frst_mt_calc = "+frst_mt_calc);
				//	$("#big_container").fadeOut('slow', function() { 	
					// alert("frst_mt_calc = "+frst_mt_calc);
					$("#big_container").animate({
						width:0, height:0, marginTop:frst_mt_calc, "opacity":"toggle"
					}, "slow", function () {
						$("#big_container").fadeOut("fast");
						$("#fullscreen").fadeOut('slow'); 	
						$("#copertura").fadeOut('slow');
					});
				});
			});

		// });
		return false;
	});
}
/*
	function checkMainSize() { // per il max-width su IE6	
	
		userAgent = self.navigator.userAgent;
		appName = self.navigator.appName;
		if(appName.indexOf('Microsoft Internet Explorer') >= 0){
			var ind = (userAgent.indexOf('MSIE')+5);
			if(((ind+1) < userAgent.length)&&(ind >= 5)){
				isIE = true;
				var bsVersion = parseInt(userAgent.substring(ind, ind+1));
				if(!isNaN(bsVersion)){
					ieVersion = bsVersion;
					if(ieVersion >= 7){
						vers = 7;
						isIE7 = true;
					}else if(ieVersion >= 6){
						vers = 6;
						isIE6 = true;
	
		var maxwidth = $("#main").css('max-width');
		var currwidth =	$("#main").width();
		// alert (" maxwidth "+maxwidth+" e currwidth "+currwidth);
		da_width = "98%";
		if (parseInt(currwidth)>parseInt(maxwidth)) {
			// alert (" MAGGIORE ");
			da_width = "90em";
		}
		$("#main").css("width",da_width);
		
					}else if(ieVersion >= 5){
						vers = 5;
						isIE5 = true;
					}else if(ieVersion >= 4){
						vers = 4;
						isIE4 = true;
					}
				}
			}
		}
	}
*/
