function menupos(){
    pagpos=$("#"+menu).position().left-11+$("#"+menu).width()/2;
}

function onSlide(dir){
	if(wait==0){
		wait=1;
		posSlider=$("#contentslider").position().left;
		$(".sliderText[rel=t"+textRel+"]").stop().animate({top:90},250);
		switch(dir){
			case "avanti":
			if(posSlider-885<-885*2) {posSlider=0; textRel=1}
			else {posSlider=posSlider-885; textRel++}
			break;
			case "indietro":
			if(posSlider+885>0) {posSlider=-885*2; textRel=3}
			else {posSlider=posSlider+885; textRel--}
			break;
		}
		$(".sliderText[rel=t"+textRel+"]").stop().animate({top:0},250);
		$("#contentslider").animate({left:posSlider},1500,'easeOutExpo',function(){wait=0});
	}
}

function timerSlide(time){
	setTimeout(function(){
		$("#frecciaDx").trigger('click');
		timerSlide(time);
	},time);
}
function dimensioni(){
	if(typeof(window.innerWidth)=='number'){//Non-IE
		ie=0;
		larghezza = window.innerWidth;
		altezza = window.innerHeight;
	}
	else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)){//IE 6+ in 'standards compliant mode'
		ie=1;
		larghezza = document.documentElement.clientWidth;
		altezza = document.documentElement.clientHeight;
	}
	else if(document.body&&(document.body.clientWidth||document.body.clientHeight)){//IE 4 compatible
		ie=1;
		larghezza = document.body.clientWidth;
		altezza = document.body.clientHeight;
	}	
}
$(document).ready(function(){
	/*segnalino menu*/
	menupos();
	$("#segnalino").animate({left:pagpos},0);
	/*$("#menu a").hover(function(){
		hoverpos=$(this).position().left-11+$(this).width()/2;
		$("#segnalino").stop().animate({left:hoverpos},1500,'easeOutExpo');
	}, function(){
		$("#segnalino").stop().animate({left:pagpos},1500,'easeOutExpo');
	});*/
	
	/*INDEX*/
	/*slider*/
	timerSlide(20000);
		/*frecce*/
		textRel=1;
		wait=0;
		$("#frecciaDx").hover(function(){
			$(this).stop().animate({right:0},250);
		}, function(){
			$(this).stop().animate({right:-10},250);
		});
		$("#frecciaSx").hover(function(){
			$(this).stop().animate({left:0},250);
		}, function(){
			$(this).stop().animate({left:-10},250);
		});
		$("#frecciaDx").click(function(event){
			onSlide("avanti");
			event.preventDefault();
		});
		$("#frecciaSx").click(function(event){
			onSlide("indietro");
			event.preventDefault();
		});
		/*box Testo*/
		if(pagina=='home') {$("#boxTxt").animate({left:$("#mascheraslider").position().left-30},0).children("div[rel!=t1]").animate({top:90},0);}
	/*Ultime News*/
	$(".blocconews[rel=n1]").animate({top:0},0);
	$("#ultimeNav a[rel=n1]").addClass("active");
	$("#ultimeNav a").click(function(){
		var nRel=$(this).attr("rel");
		$("#ultimeNav a").removeClass("active");
		$("#ultimeNav a[rel="+nRel+"]").addClass("active");
		$(".blocconews").animate({top:200},125);
		$(".blocconews[rel="+nRel+"]").animate({top:0},125);
	});
		
	/*NEWS*/
	pagwidth=0;
	pagnewswidth=0;
	$("#dettaglionews img").click(function(){
		dimensioni();
		var source=$(this).attr("src");
		$("#sfondo").html('<img src="'+source+'" />')
			.fadeIn(250)
			.children("img").css({"margin-top":(altezza-$("#sfondo img").height())/2+'px'});
	});
	$("#sfondo").click(function(){
		$(this).fadeOut(250);
	});
	$("#paginazione a").each(function(){
		pagwidth+=16;
	});
	$("#paginazione").width(pagwidth);
	if(pagina=='news' || pagina=='catalogo'){
		$(".pagnews").each(function(){
			pagnewswidth+=885;
		});
		$("#contentPagnews").width(pagnewswidth);
	}
	$("#paginazione a[rel=0]").addClass("active");
	$("#paginazione a").click(function(event){
		var thisRel=$(this).attr("rel");
		$("#paginazione a").removeClass("active");
		$("#paginazione a[rel="+thisRel+"]").addClass("active");
		$("#contentPagnews").animate({left:-885*thisRel},250);
		event.preventDefault();
	});
	
	/*CONTATTI*/
	$("#boxinputlibri ul").slideUp(0);
	$("#selectLibro").addClass("down");
	$("#selectLibro").click(function(){
	var classe=$(this).attr("class");
	if(classe=="down")
	{
		$("#boxinputlibri ul").slideDown(500);
		$(this).addClass("up") 
			.removeClass("down")
	}
	else
	{
		$("#boxinputlibri ul").slideUp(250);
		$(this).addClass("down")
			.removeClass("up")
	}
	});
	$("#boxinputlibri li").click(function(){
		var hiddenvalue=$(this).html();
		$("#libro").val(hiddenvalue);
		var testo=$(this).html();
		$("#boxinputlibri ul").slideUp(250);
		$("#selectLibro").html(testo+'<span></span>')
			 .addClass("down")
			 .removeClass("up");
	});
});
