$(document).ready(function(){
	
		
	//BACKGROUND RESIZE
	var theWindow = $(window);
	var $bg = $("#bg");
	var aspectRatio = $bg.width() / $bg.height();
	var wHeight = "";
	var wWidth = "";
	
	resizeBg();

	function resizeBg() {
	
		wHeight = theWindow.height();
		wWidth = theWindow.width();
	
		if((theWindow.width() / theWindow.height()) < aspectRatio) {

			$bg.removeClass().addClass('bgheight');
			
		} else {

			$bg.removeClass().addClass('bgwidth');
			
		}		
	}


	theWindow.resize(function() {
		resizeBg();
	}).trigger("resize");
	
	
	
	
	//FOOTER HEIGHT
	var docH = $(document).height();
	var contH = $("#container").height();
	
	var H = docH - contH - 20;
	
	//alert(H);
	
	$("#footer").css({
		height : H + "px"
	})
	
	
	//BLOCKS
	$(".block").mouseover(function(){
		
		//alert($(this).html());
		
		$(this).children(".filtro, .filtro1, .filtro2").css({
			display : "block"
		});
		
		$(this).children(".filtro, .filtro1, .filtro2").stop().animate({
			opacity : 0.80
		}, 500)
		
		
	});
	
	$(".block").mouseout(function(){
		
		$(this).children(".filtro, .filtro1, .filtro2").stop().animate({
			opacity : 0
		}, 500 , function(){
			$(this).css({ 
				display : "none"	
			});
		});		
	});
	
	
	//GESTIONE MENU CATEGORIE
	if(cat != ""){
		
		$("#" + cat).css({
			height: "auto",
			overflow: "hidden"
		}).addClass("selected");
	}
	

	
	if(subcat != ""){
		
		$("#" + subcat).css({
			height: "auto",
			overflow: "hidden"
		}).addClass("selected");
	}
	
	if(thrdLevel != ""){
		
		$("#" + thrdLevel).css({
			height: "auto",
			overflow: "hidden"
		}).addClass("selected");
	}
	
	
	//PRODOTTI
	//IMAGE CENTRATA
	var imgContW = $(".imgProd").width() / 2;
	var imgW = $(".imgProd img").width() / 2;
	
	//alert($(".imgProd img").width() + " - " + $(".imgProd").width())
	
	if($(".imgProd img").width() > $(".imgProd").width())
	{
		var pos = imgContW - imgW;
			
		$(".imgProd img").css({
			//marginLeft : pos + "px"
		});
	} 
	
	
	//SLIDER	
	var step = 346;
	var stepDesc = 150;
	var stepGall = 650;
	var stepEvt = 288;
	var pag = 1;
	
	if(cat != ""){
	
	var wrapW = parseInt($("#prodotti").children(".prodotto").length * step);
	var pos = $("#prodotti").css("left").replace("px", "");
	
	var wrapDescW = parseInt($("#descrizioni").children(".descProd").length * stepDesc);
	var posD = $("#descrizioni").css("left").replace("px", "");
	
	
	}
	
	if($("#events").length){
	
		var wrapEvt = parseInt($("#events").children(".evento").length * stepEvt);
		var posEvt = $("#events").css("left").replace("px", "");
	
	}
	
	if(cat == "" && $("#galleria").css("left")){
	
	//alert($("#galleria").children(".gall").length);
	
	var wrapGall = parseInt($("#galleria").children(".gall").length * stepGall);
	var posGall = $("#galleria").css("left").replace("px", "");
	
	}
	
	//wrapGall = wrapGall / 4;
	
	//alert(wrapEvt);
	
	$("#prodotti").css({
		width : wrapW + "px",
		height : "490px"
	});
	
	$("#descrizioni").css({
		width : wrapDescW + "px",
		height : "200px"
	});
	
	$("#galleria").css({
		width : wrapGall + "px",
		heigth : "376px"
	});
	
	
	if($("#events").length){
		
		$("#events").css({
			width : wrapEvt + "px",
			heigth : "298px"
		});
		
	}
	
	$("#paginazione").val(pag);	
	
	$("#pulsanti a").click(function(){
		
				
		if($(this).attr("rel") == -1){
			
			pos = pos - step;
			posD = posD - stepDesc;
			posGall = posGall - stepGall;
			posEvt = posEvt - stepEvt;
			pag = parseInt(pag) + 1;
			
			if(pag > $("select option:last-child").val()){
				pag = $("select option:last-child").val();
			}
			
			
			if(pos < (-step  * parseInt($("#prodotti").children(".prodotto").length - 1)) && posD < (-stepDesc  * parseInt($("#descrizioni").children(".descProd").length - 1)))
			{		
				pos = (-step  * parseInt($("#prodotti").children(".prodotto").length - 1));
				posD = (-stepDesc  * parseInt($("#descrizioni").children(".descProd").length - 1));
										
				return true;
													
			}
			
			if(posGall < (-stepGall  * parseInt($("#galleria").children(".gall").length - 1)))
			{		
				
				posGall = (-stepGall  * parseInt($("#galleria").children(".gall").length - 1));
				return true;
													
			}
			
			
			if(posEvt < (-stepEvt  * parseInt($("#events").children(".evento").length - 1)))
			{	
				//pag = $("#paginazione").val();
				
				posEvt = (-stepEvt  * parseInt($("#events").children(".evento").length - 1));
				return true;
													
			}
					
			$("#prodotti").stop().animate({
				left : pos + "px"
			});
			
			$("#descrizioni").stop().animate({
				left : posD + "px"
			});
			
			$("#galleria").stop().animate({
				left : posGall + "px"
			});
			
			
			$("#events").stop().animate({
				left : posEvt + "px"
			});
			
			$("#paginazione").val(pag);
		
			
		} else {
			
			if(pos == 0 && posD == 0 || posGall == 0 || posEvt == 0)
			{
				
				//pag = $("#paginazione").val();
				
				return true;	
			}
	
			
			pos = pos + step;
			posD = posD + stepDesc;
			posGall = posGall + stepGall;
			posEvt = posEvt + stepEvt;
			pag = parseInt(pag) - 1;
			
			
			$("#prodotti").stop().animate({
				left : pos + "px"
			});
			
			$("#descrizioni").stop().animate({
				left : posD + "px"
			});
			
			$("#galleria").stop().animate({
				left : posGall + "px"
			});
			
			$("#events").stop().animate({
				left : posEvt + "px"
			});
		
			$("#paginazione").val(pag);
		
		}
		
	});
	
	$("#paginazione").change(function(){
		//alert($(this).val());
		
		var pagina = ($(this).val()-1) * -stepEvt;
		var eventLeft = $("#events").css("left").replace("px", "");
		var tots = pag * stepEvt;
		
		//alert("pagina: " + pagina + " - tots: " + tots);
		
		
		
		if(pagina != tots){
				
			$("#events").stop().animate({
				left : pagina + "px"
			});
			
			posEvt = pagina;
			
			pag = $(this).val();
			
		} else {
			pag = $(this).val();
			return true;
		}
				
	});
	
	//CENTRATURA VERTICALE PRODOTTO
	/*var top = "";
	
	if($("#prodotti").length > 0){
		
		
		
		$(".prodotto").each(function(){
			
			var prodContH = $(this).children().height(); 
			
			top = ($(this).parent().height() / 2) - ( prodContH / 2);
			
			alert($(this).children().height());
			
			top = Math.round(top);
			
			//alert($(this).html());
			
			//alert(top);
			
			$(this).children().css({
				marginTop: top + "px"
			});
			
		})
		
		
	}*/
		
});

