// JavaScript Document

function myHover(){
	$("#menu li.open").each(function() {
		$(this).find('li:first').addClass("first");
		$(this).find('li:last').addClass("last");
	});

	$("#menu li.open").hover( function (){
		$(this).find("ul").show();
	},function (){
		$(this).find("ul").hide();
	});
}

/*--------------------------------------------------
Slideshow
--------------------------------------------------*/
function slideshow(){
	increment = 0;
	slideSize = $("#slideshow .slideshow").size();
	var loop;
	function moveOn(parametro){
		$("#slideshow .slideshow_wrap").stop().animate({"left":-(960*parametro) + "px"},1000);
		clearTimeout(loop);
		loop = setTimeout(function(){
			increment = increment + 1;
			if (increment >= slideSize){
				increment = 0;
			}
			moveOn(increment);
		},5000);
	}

	moveOn(increment);

	// frecce
	$(".arrows .arr_next").click(function(){
		increment = increment + 1;
		if (increment >= slideSize){
			increment = 0;
		}
		moveOn(increment);
		return false;
	});

	$(".arrows .arr_prev").click(function(){
		increment = increment - 1;
		if (increment < 0){
			increment = slideSize-1;
		}
		moveOn(increment);
		return false;
	});
}

/*--------------------------------------------------
FADE
--------------------------------------------------*/
function myfade(){
	my_inc = 0;
	newsSize = $(".slidingCont .contenuto").size();
	var my_loop;
	function fadeOn(my_par){
		$(".slidingCont .contenuto:visible").hide()
		$(".slidingCont .contenuto").eq(my_par).fadeIn(500);
		my_loop = setTimeout(function(){
			my_inc = my_inc + 1;
			if (my_inc >= newsSize){
				my_inc = 0;
			}
			fadeOn(my_inc);
		},5000);
	}
	fadeOn(my_inc);
}

/*--------------------------------------------------
TABS
--------------------------------------------------*/

function tabs(){
	var size = $("ul.tabs li").size();
	var url = location.hash;
	if (url) {
		url = url.replace("my","");
		i = $("ul.tabs li a").index($("a[rel=" + url + "]"));
	} else {
		i = 0;
	}
	
	var scr = 0;
	$(".tab_content").hide();
	$("ul.tabs li").mouseover(function(){
		scr = $(window).scrollTop();
		location.hash = "my"+$(this).find("a").attr("rel").replace("#","");
		$("html,body").scrollTop(scr);
		$("ul.tabs li.on").removeClass("on");
		$(this).addClass("on");
		$(".tab_content").hide();

		var index = $("ul.tabs li").index($(this));
		$(".tab_content").eq(index).show();
	});
	$("ul.tabs li:eq("+ i +")").mouseover();
}
/*--------------------------------------------------
TABELLA
--------------------------------------------------*/
function tabella(){
	if($(".contTable table tr:eq(0) th").size() == 5) {
		$(".contTable table td, .contTable table th").addClass("quattro");
	} else if ($(".contTable table tr:eq(0) th").size() == 6) {
		$(".contTable table td, .contTable table th").addClass("cinque");
	} else if ($(".contTable table tr:eq(0) th").size() == 7) {
		$(".contTable table td, .contTable table th").addClass("sei");	
	}
}

/*--------------------------------------------------
NEWS MAGAZINE
--------------------------------------------------*/
function indiceNews()
{
    $(".indice .news:even").addClass("newsLeft");
    $(".indice .news:odd").addClass("newsRight");
}
function textLanding(){
$("#contBoxTop span.title").live("click",function(){
	$("#contBoxTop span.title").removeClass("on");
	$(this).addClass("on");
	var mmm = $(this).parent().index();
	$(".contText div.text").hide();
	$(".contText div.text:eq("+mmm+")").show();		
});
}

/*--------------------------------------------------
INIT
--------------------------------------------------*/

$(document).ready(function ()
{
	myHover();
	tabs();
	myfade();
	slideshow();
	indiceNews();
	tabella();
	textLanding();
	$(".contBoxTop:eq(0) span.title").click();
	$(".fancy").fancybox();
	$("#cont_moduli .box:nth-child(4n)").addClass("col_4");
});
