$(function () {
	// fadeIn/fadeOut del body
	$("body").hide().fadeIn(2000);

	// processing menu inferiore
	$("#dynamicmenu ul").detach().addClass("dynamicmenu").hide().appendTo("#nav");
	$("#dynamicmenu").show().animate({top:0,opacity:1}, function () {
		if ($("ul.dynamicmenu li.current").length>0) {
			var p1 = $("ul.dynamicmenu li.current").parents("ul").attr("id");
			if (p1 == "dynamicmenu") return;
			var p2 = p1.substr(0, p1.length-2);
			$("#"+p1).show().animate({top:0,opacity:1});
			$("#"+p2).animate({top:"40px",opacity:0});
			$("#dynamicmenu").animate({top:"40px",opacity:0});
		}
	});
	$("ul.dynamicmenu[id!=dynamicmenu]").prepend("<li><a href='#back'>^</a></li>");

	// processing menu superiore
	$("#header a:not([href*=#]), #footer a:not([href*=#])").click(function(event){
		event.preventDefault();
		var link_location = this.href;
		$("body").fadeOut(1000, function () {window.location = link_location});
	});

	// opacità menù su mouseover
	var $opacity_elements = $("#header a, #footer a, #language a, #anteprimaCollezioni a");
	$opacity_elements.mouseover(function () {
		$opacity_elements.not(this).dequeue().animate({opacity:0.4}, 300);
	}).mouseout(function () {
		$opacity_elements.not(this).dequeue().animate({opacity:1}, 300);
	});

	// fix larghezza menu
	$("ul.dynamicmenu").width($(window).width()-40);

	// gestione link sul footer
	$("ul.dynamicmenu a[href*=#]").click(function () {
		var href = $(this).attr("href");
		if (href.substr(href.length-5) == "#back") {
			var toshow = $(this).parents("ul").attr("id");
			toshow = toshow.substr(0, toshow.length-2);
			$("#"+toshow).show().animate({top:0,opacity:1});
			$(this).parents("ul").animate({top:"-20px",opacity:0}, function () {$(this).hide()});
			var bg_pos = $("#footer").css("background-position");
			if (typeof bg_pos == "undefined") bg_pos = "0 0";
			var footer_top = parseInt(bg_pos.split(" ")[1]) - 66;
			$("#footer").animate({backgroundPosition: "(0 " + footer_top + "px)"});
			return false;
		}

		var bg_pos = $("#footer").css("background-position");
		if (typeof bg_pos == "undefined") bg_pos = "0 0";
		var footer_top = parseInt(bg_pos.split(" ")[1]) + 66;
		$("#footer").animate({backgroundPosition: "(0 " + footer_top + "px)"});
		$(this).parents("ul").animate({top:"40px",opacity:0}, function () {$(this).hide()});
		var toshow = $(this).attr("href");
		$(toshow).show().animate({top:0,opacity:1});
		return false;
	});

	if ($("#bgimg").length>0) {
		$(window).load(function () {
			pignatelli_fullscreen_background("#bgimg");
		});
	}

	if ($("#pignatelli_block_auto_refresh").length == 0) {
		$(window).resize(function () {
			$("body").fadeOut(function (event) {window.location.reload()});
		});
	}

	if ($("#anteprimaCollezioni").length) {
		$("#anteprimaCollezioni ul ul").height($("#anteprimaCollezioni ul ul").height()).hide();
		$("#anteprimaCollezioni>ul>li>a").toggle(function () {
			$("#anteprimaCollezioni ul ul").slideDown();
			$(this).attr("class", "anteprimaClose");
		}, function () {
			$("#anteprimaCollezioni ul ul").slideUp();
			$(this).attr("class", "anteprimaButton");
		});
	}

	$(".openpopup").click(function () {
		var div = $(this).attr("href");
		var lingua = document.location.href.split("/")[3];
		if ($("#copyright").is(":visible") && $("#copyright").attr("class") == div) {
			$("#copyright").fadeOut();
			return false;
		}
		$("#copyright").attr("class", div).fadeOut(function () {
			$("#copyright").load("/"  + lingua + "/" + div.substr(1) + ".php", function () {
				$("#copyright").prepend('<a href="#" class="close" onclick="$(\'#copyright\').fadeOut();"></a>').fadeIn();
			});
		});
		return false;
	});

	$("a.opacityNorm").css("opacity","0.3");
	$("a.opacityNorm").hover(function () {
		$(this).stop().animate({opacity: 1.0}, "normal");
	}, function () {
		$(this).stop().animate({opacity: 0.3}, "normal");
	});
	
	// ho aggiunto questa classe per l'opacity inversa
	
	$("a.opacityRev").css("opacity","1.0");
	$("a.opacityRev").hover(function () {
		$(this).stop().animate({opacity: 0.3}, "normal");
	}, function () {
		$(this).stop().animate({opacity: 1.0}, "normal");
	});
	
});

pignatelli_fullscreen_background = function (theItem) {
	var winWidth=$(window).width();
	var winHeight=$(window).height();
	var imageWidth=$(theItem).width();
	var imageHeight=$(theItem).height();
	var picHeight = imageHeight / imageWidth;
	var picWidth = imageWidth / imageHeight;
	if ((winHeight / winWidth) < picHeight) {
		$(theItem).css("width",winWidth);
		$(theItem).css("height",picHeight*winWidth);
	} else {
		$(theItem).css("height",winHeight);
		$(theItem).css("width",picWidth*winHeight);
	};
	$(theItem).css("margin-left",winWidth / 2 - $(theItem).width() / 2);
	$(theItem).css("margin-top",winHeight / 2 - $(theItem).height() / 2);
}

