$(function(){
	$("ul.lingua>li[lang='"+$("html").attr("lang")+"']").addClass("attivo");
	$("ul.lingua>li").css({cursor:"pointer"}).click(function(){
		var vai=/^(.+)\/sito-..\/(.*)$/.exec(window.location);
		window.location=vai[1]+"/sito-"+$(this).attr("lang")+"/"+vai[2];
	});

	$("ul.menu>li:not(.attivo)").each(function(){
	}).hover(function(){
	}, function(){
	});

	var indem = ["it","farmcompany.","@","info"].reverse().join("");
	var re_mail=/^(.*)root@localhost(.*)$/;

	$("a.email").each(function(){
		if (s=re_mail.exec($(this).attr("href")))
			$(this).attr({"href":s[1]+indem+s[2]});
		if (s=re_mail.exec($(this).text()))
			$(this).text(s[1]+indem+s[2]);
	});
	$("a.esterno").attr({"target":"_new"});

	(function(){
		var credits=false;
		var datisocietari=false;
		$(".credits").css({cursor:"pointer"}).click(function(){
			if (datisocietari) {
				$("div.datisocietari").hide(600);
				datisocietari = false;
			}
			if (credits=!credits) {
				caboa.tracking.trackEvent("info", "credits");
				$("div.credits").show(600);
			} else
				$("div.credits").hide(600);
		});
		
		$(".datisocietari").css({cursor:"pointer"}).click(function(){
			if (credits) {
				$("div.credits").hide(600);
				credits = false;
			}
			if (datisocietari=!datisocietari) {
				caboa.tracking.trackEvent("info", "datisocietari");
				$("div.datisocietari").show(600);
			} else
				$("div.datisocietari").hide(600);
		});
	})();
	
	(function(){
		var $w = $(window);
		var $foto = $("div.wallpaper>img");
		var fw;
		var fh;
		var centra = parseInt($foto.css("top")) != 0;
		$foto.hide();
		
		function aggiusta_foto() {
			var w = $w.width();
			var h = $w.height();
			var iw,ml;
			var ih,mt;
			
			if (w/h > fw/fh) {
				ih=parseInt(w*fh/fw);
				iw=w;
			} else {
				iw=parseInt(h*fw/fh);
				ih=h;
			}
			ml = parseInt(-iw/2);
			
			if (centra)
				mt = parseInt(-ih/2);
			else
				mt = 0;
			
			$foto.css({
				width: iw,
				height: ih,
				marginTop: mt,
				marginLeft: ml
			});
		}

		$foto.waitload(function(){
			$foto.show();
			fw = $foto[0].width;
			fh = $foto[0].height;
			$w.resize(aggiusta_foto);
			aggiusta_foto();
		});
	})();
	
	(function(){
		var $w = $(window);
		var $sito=$("div.sito");
		var sw=$sito.width();
		var ml=parseInt($sito.css("marginLeft"));
		
		function aggiusta_sito() {
			var w = $w.width();
			var nml=ml;
			if (w<sw+ml) {
				nml = w-sw;
				if (nml<0)
					nml = 0;
			}
			$sito.css({
				marginLeft: nml
			});
		}
		
		$w.resize(aggiusta_sito);
		aggiusta_sito();
		
	})();
	
	$("div.area,div.titolo").fadeTo(0.8).hover(function(){
		$("div.area,div.titolo").fadeTo(1.0,150)
	},function(){
		$("div.area,div.titolo").fadeTo(0.8,150)
	});
	
	$("ul.menu1>li:not(.attivo)").hover(function(){
		$("a",$(this)).stop().animate({"paddingRight":0}, 250);
	},function(){
		$("a",$(this)).stop().animate({"paddingRight":20}, 250);
	});
	
	(function(){
		var $slides=$("div.area>div.slideshow img.slide");
		var $dots=$("div.area>div.slideshow>ul.dots>li");
		
		if ($slides.length<2)
			return;
		
		var m=$slides.length;
		var n=0;
		var ora=null;
		
		$slides.css({"display": "none"}).eq(n).css({"display": "block"});
		$dots.removeClass("attivo").eq(n).addClass("attivo");
		
		$dots.css({cursor:"pointer"}).click(function(){
			$slides.eq(n).fadeOut(500);
			$dots.eq(n).removeClass("attivo");
			n = parseInt((/\bdot(\d+)\b/.exec($(this).attr("class")))[1]);
			$slides.eq(n).fadeIn(500);
			$dots.eq(n).addClass("attivo");
		});
		
		setInterval(function(){
			$slides.eq(n).fadeOut(500);
			$dots.eq(n).removeClass("attivo");
			n = (n+1)%m;
			$slides.eq(n).fadeIn(500);
			$dots.eq(n).addClass("attivo");
		}, 4500);
	})();

	$(window).focus(function(){
		$.fx.off = false;
	});
	$(window).blur(function(){
		$.fx.off = true;
	});

});

function riduci_uri(u) {
	while (u.slice(-2)=="/-")
		u=u.slice(0,-2);
	return u;
}
