var browserDim;
var browserScrollTop = 0;
var photoTimer;

$(document).ready(function() {
	$("a[class=gallery]").click(function() {
		browserDim = getBrowserDim();
		link = $(this).attr("href");
		loadingGallery();
		$.ajax({
			type: "GET",
			url: link,
			cache: true,
			success: function(html){
				initGallery(html);
			}
		});
		return false;
	});
});

// Crea e setta i div di override e di loading (se IE6 anche l'eventuale iframe per nascondere i select)
function loadingGallery() {
	$("body").append('<div id="GY_override"></div>');
	$("body").append('<div id="GY_loader"></div>');
	loaderTop = parseInt((browserDim.height/2) - 50);
	loaderLeft = parseInt((browserDim.width/2) - 50);
	if ($.browser.msie && $.browser.version == 6) {
		browserScrollTop = document.documentElement.scrollTop;
		$("html").css("overflow","hidden");
		$("body").append('<iframe id="GY_hideselect"></iframe>');
		$("#GY_hideselect").css({height: document.body.offsetHeight});
		$("#GY_override").css({position: "absolute", height: document.body.offsetHeight});
		$("#GY_loader").css({position: "absolute"});
		loaderTop += browserScrollTop
	}
	$("#GY_loader").css({top: loaderTop, left: loaderLeft});
}

// Inserisce nel DOM la gallery e attende il caricamento dell'immagine
function initGallery(html) {
	$("body").append(html);
	if ($.browser.msie && $.browser.version == 6)
		$("#GY_container").css({position: "absolute"});
	$("#GY_container td").ifixpng();
	$("#GY_container div").ifixpng();
	photoTimer = setInterval("waitingPhoto()", 200);
}

// Funzione che attende il caricamente nel browser della foto
function waitingPhoto() {
	if ($("#GY_content img").width() && $("#GY_content img").height()) {
		clearInterval(photoTimer);
		$("#GY_loader").remove();
		showGallery();
	}
}

// Mostra la gallery
function showGallery() {
	// Setto le ombre dei lati lunghi
	contentWidth = $("#GY_content").width();
	contentHeight = $("#GY_content").height();
	$("#GY_container .t3").width(contentWidth-28);
	$("#GY_container .s2").height(contentHeight-28);
	$("#GY_container .d2").height(contentHeight-28);
	$("#GY_container .b3").width(contentWidth-28);
	
	// Centro la gallery nello schermo
	galleryWidth = $("#GY_container").width();
	galleryHeight = $("#GY_container").height();
	galleryTop = parseInt((browserDim.height/2) - (galleryHeight/2)) + browserScrollTop;
	galleryLeft = parseInt((browserDim.width/2) - (galleryWidth/2));
	$("#GY_container").css({top: galleryTop, left: galleryLeft});
	
	// Setto l'evento di chiusura
	$("#GY_close").click(function() {
		galleryClose();
	});
}

// Funzione di chiusura della gallery
function galleryClose() {
	$("#GY_override").remove();
	$("#GY_container").remove();
	if ($.browser.msie && $.browser.version == 6) {
		$("html").css("overflow", "");
		$("#GY_hideselect").remove();
	}
}

// Ritorna le dimensioni del browser
function getBrowserDim() {
	var dim = {height: 0, width: 0};
	if (self.innerHeight) {
		dim.width = self.innerWidth;
		dim.height = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		dim.width = document.documentElement.clientWidth;
		dim.height = document.documentElement.clientHeight;
	} else if (document.body) {
		dim.width = document.body.clientWidth;
		dim.height = document.body.clientHeight;
	}
	return dim;
}
