var inSlide=false;

//controllo mail valida
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

var stopMove=false;
//nascondi errore
function nascondi_errore(){
	$('.errorcontatti').detach();
}

//sliderhomepage
function scorrigalleryhp(){
	if($('.sdlider_container .sdlider_images img:visible').next().length>0){
		$('.sdlider_container .sdlider_images img:visible').stop(false, true).fadeOut().next().fadeIn();
	} else {
		$('.sdlider_container .sdlider_images img:visible').stop(false, true).fadeOut();
		$('.sdlider_container .sdlider_images img').eq(0).fadeIn();
	}
	setTimeout("scorrigalleryhp()", 5000);
}
//sliderazienda/contatti/links
function scorrigallery2(){
	if($('.cover img:visible').next().length>0){
		$('.cover img:visible').stop(false, true).fadeOut().next().fadeIn();
	} else {
		$('.cover img:visible').stop(false, true).fadeOut();
		$('.cover img').eq(0).fadeIn();
	}
	setTimeout("scorrigallery2()", 5000);
}

//slider sponsor
function movesponsor(){
if(stopMove)
return;
	maxleft=-($('.Sponsor_container ul').width()-930);
	
	if (parseInt($('.Sponsor_container ul').css("left"))>maxleft){
		$('.Sponsor_container ul').stop(false, false).animate({"left": "-=155"}, 500);
	} else {
		$('.Sponsor_container ul').stop(false, false).animate({"left": "0px"}, 500);
	}
	setTimeout("movesponsor()", 3000);
}

//gallery3 
// function makeScrollable($outer, $inner){
	// var extra = 100;
	// var divWidth = $outer.width();
	// var lastElem = $inner.find('img:last');
	// $outer.scrollLeft(0);
	// $outer.unbind('mousemove').bind('mousemove',function(e){
		// var containerWidth = lastElem[0].offsetLeft + lastElem.outerWidth() + 2*extra;
		// var left = (e.pageX - $outer.offset().left) * (containerWidth-divWidth) / divWidth - extra;
		// $outer.scrollLeft(left);
	// });

// }

$(document).ready(function(){

	//External Link
	$('.ExternalLink').click(function(){
		window.open(this.href);
		return false;
	});

	//sliderhomepage
	if ($('.sdlider_container .sdlider_images img').length>1){
		$('.sdlider_container .sdlider_images img:gt(0)').hide();
		setTimeout("scorrigalleryhp()", 5000);
	}
	$('.sdlider_container .sdlider_images img').each(function(){
		if ($(this).attr("alt")!=''){
			$(this).css("cursor", "pointer");
			$(this).click(function(){
				location.href=$(this).attr("alt");
			});
		}
	});

	//sliderazienda/contatti/links
	if ($('.cover img').length>1){
		$('.cover img:gt(0)').hide();
		setTimeout("scorrigallery2()", 5000);
	}

	//slidercollezioni
	maxheight=120;
	$('.slider_Event div').each(function(){
		if ($(this).height()>maxheight){
			maxheight=$(this).height();
		}
	});
	if ($('.slider_Event').length>0){
		$('.slider_Event').css("width", 710*($('.slider_Event div').length/3) +"px").height(maxheight);
		$('.Nav_Event_Image').height(maxheight);
	}
	
	
	$('.Content_Image_absolute:gt(0)').hide();
	$('.Text_style_collezioni ul li').click(function(e){
		alert("ert")
		if (!$(this).hasClass('first_event')){
			
			num=$('.Text_style_collezioni ul li').index($(this));
			
			$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
			$('.Content_Image_absolute').eq(num).stop(true, true).fadeIn();			
			var h2=$('.first_event h2').detach();
			$('.first_event').removeClass("first_event");
			$('.Text_style_collezioni ul li').eq(num).addClass("first_event");
			$('.Text_style_collezioni ul li').eq(num).prepend(h2);
			
			console.log('aa');
			$('.Text_style_collezioni ul li').height(100);
			$(this).height($('.Text_style_collezioni li').height()+30);
			if(!inSlide){
                            inSlide=true;
                            $('.slider_Event').stop(true, true).animate({left: -710*num+"px"},300,function(){
                                inSlide=false;
                            });
                        }
			console.log('zz');
		}
		return false;
	});

	
	
	$('.Text_style ul li').click(function(){
		if (!$(this).hasClass('first_event')){
			
			num=$('.Text_style ul li').index($(this));
			
			$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
			$('.Content_Image_absolute').eq(num).stop(true, true).fadeIn();			
			
			$('.Text_style ul li').height(50);
			$('.testoEvento').hide();
			$(this).find('.testoEvento').show();
			$(this).css("height","auto");
			
			if(!inSlide){
                            inSlide=true;
                            $('.slider_Event').stop(true, true).animate({left: -710*num+"px"},300,function(){
                                inSlide=false;
                            });
                        }
		}
		return false;
	});
	
	
	/*$('.Sport ul li').each(function () {
				if ($(this).attr('rel')=='1'){
					$(this).show();
				}else if($(this).attr('rel')=='0'){
					$(this).show()
				}else{
					$(this).hide()
				}
	});
	$('.marche').click(function(){
			console.log($(this).attr('rel'));
			var copy=this;
			$('.Sport ul li').each(function () {
				if ($(this).attr('rel')==$(copy).attr('rel')){
					$(this).show();
				}else if($(this).attr('rel')=='0'){
					$(this).show()
				}else{
					$(this).hide()
				}
			});
			
	});*/
	
	
	
	
	//eventi
	if ($('.slider_Event2').length>0){
		$('.slider_Event2').css("width", 710*($('.slider_Event2 div').length/3) +"px");
	}
	//XXXXXXXXXXXXXXXXXXXXXXXXx
	
	$('.Nav_Event_menu3 ul li').height(120);
	$('.Nav_Event_menu3 ul li:first').height(150);
	
	
	$('.Nav_Event_menu3 ul li').click(function(){
	
		if (!$(this).hasClass('first_event')){
			
			num=$('.Nav_Event_menu3 ul li').index($(this));
			
			$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
			$('.Content_Image_absolute').eq(num).stop(true, true).fadeIn();
			
			
			totheight=0;
			$('.Nav_Event_menu3 ul li:lt('+num+')').each(function(){
				totheight+=$(this).height();
			});

			$('.Nav_Event_menu3 ul li').height(120);
			$(this).height(150);

			
			$('.Nav_Event_menu3 ul').animate({top:-totheight});
			
			if(!inSlide){
                            inSlide=true;
                            $('.slider_Event2').stop(true, true).animate({left: -710*num+"px"},300,function(){
                                inSlide=false;
                            });
                        }
			
		}
		return false;
	});

	$('.Nav_Event_menu4 ul li').height(100);
	$('.Nav_Event_menu4 ul li').find('.testoEvento').hide();

	$('.Nav_Event_menu4 ul li:first').find('.testoEvento').show();
	$('.Nav_Event_menu4 ul li:first').css("height","auto");
	
	$('.Nav_Event_menu4 ul li').click(function(){
	
		if (!$(this).hasClass('first_event')){
			
			num=$('.Nav_Event_menu4 ul li').index($(this));
			
			$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
			$('.Content_Image_absolute').eq(num).stop(true, true).fadeIn();
			
			
			totheight=0;
			$('.Nav_Event_menu4 ul li:lt('+num+')').each(function(){
				totheight+=$(this).height();
			});

			$('.Nav_Event_menu4 ul li').height(100);
			$(this).css("height","auto");

			
			$('.Nav_Event_menu4 ul').animate({top:-totheight});
			
			if(!inSlide){
                            inSlide=true;
                            $('.slider_Event2').stop(true, true).animate({left: -710*num+"px"},300,function(){
                                inSlide=false;
                            });
                        }
			
		}
		return false;
	});
	
	  if ($('.Nav_Event_menu3 ul li').length>1){
		 $('.arrow_nav2').animate({right:"-35px"}, 500);
		 }
 	 /*	 $('.arrow_nav2 img').click(function(){
			if ($(this).attr("title")=="Su"){
				
				num=$('.Nav_Event_menu3 ul li').index($('.first_event'));
				totheight=0;
				if (num>0){
					$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
					$('.Content_Image_absolute').eq(num-1).stop(true, true).fadeIn();
		
					var h2=$('.first_event h2').detach();
					$('.first_event').removeClass("first_event");
					$('.Nav_Event_menu3 ul li').eq(num-1).addClass("first_event");
					$('.Nav_Event_menu3 ul li').eq(num-1).prepend(h2);
					$('.slider_Event2').stop(true, true).animate({left: -710*(num-1)+"px"});
				
					$('.Nav_Event_menu3 ul li:lt('+(num-1)+')').each(function(){
						totheight+=$(this).height();
					});
					$('.Nav_Event_menu3 ul').stop(true, true).animate({"top": -totheight}, 500);
				} else {
					$('.Nav_Event_menu3 ul').stop(true, true).animate({"top": "0px"}, 500);
				}
				
			} else if ($(this).attr("title")=="Giu"){
				num=$('.Nav_Event_menu3 ul li').index($('.first_event'));
				totheight=0;
	
				if (num<($('.Nav_Event_menu3 ul li').length-1)){
				
				
					$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
					$('.Content_Image_absolute').eq(num+1).stop(true, true).fadeIn();
		
					var h2=$('.first_event h2').detach();
					$('.first_event').removeClass("first_event");
					$('.Nav_Event_menu3 ul li').eq(num+1).addClass("first_event");
					$('.Nav_Event_menu3 ul li').eq(num+1).prepend(h2);
					$('.slider_Event2').stop(true, true).animate({left: -710*(num+1)+"px"});
				
				
					$('.Nav_Event_menu3 ul li:lt('+(num+1)+')').each(function(){
						totheight+=$(this).height();
					});
					$('.Nav_Event_menu3 ul').stop(true, true).animate({"top": -totheight}, 500);
				}
			}
			return false; 
		}); 
	 } */ 
 	// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	$('.Nav_Event_menu2 ul li').click(function(){
		if (!$(this).hasClass('first_event')){
			
			num=$('.Nav_Event_menu2 ul li').index($(this));
			
			$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
			$('.Content_Image_absolute').eq(num).stop(true, true).fadeIn();
			
			var h2=$('.first_event h2').detach();
			$('.first_event').removeClass("first_event");
			$('.Nav_Event_menu2 ul li').eq(num).addClass("first_event");
			$('.Nav_Event_menu2 ul li').eq(num).prepend(h2);
			
			totheight=0;
			$('.Nav_Event_menu2 ul li:lt('+num+')').each(function(){
				totheight+=$(this).height();
			});
			
			$('.Nav_Event_menu2 ul').animate({top:-totheight});
			
			
                        if(!inSlide){
                            inSlide=true;
                            $('.slider_Event2').stop(true, true).animate({left: -710*num+"px"},300,function(){
                                inSlide=false;
                            });
                        }
			
		}
		return false;
	});
	
	if ($('.Nav_Event_menu2 ul li').length>1){
		$('.arrow_nav2').animate({right:"-35px"}, 500);
		$('.arrow_nav2 img').click(function(){
			if ($(this).attr("title")=="Su"){
				
				num=$('.Nav_Event_menu2 ul li').index($('.first_event'));
				totheight=0;
				if (num>0){
					$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
					$('.Content_Image_absolute').eq(num-1).stop(true, true).fadeIn();
		
					var h2=$('.first_event h2').detach();
					$('.first_event').removeClass("first_event");
					$('.Nav_Event_menu2 ul li').eq(num-1).addClass("first_event");
					$('.Nav_Event_menu2 ul li').eq(num-1).prepend(h2);
					$('.slider_Event2').stop(true, true).animate({left: -710*(num-1)+"px"});
				
					$('.Nav_Event_menu2 ul li:lt('+(num-1)+')').each(function(){
						totheight+=$(this).height();
					});
					$('.Nav_Event_menu2 ul').stop(true, true).animate({"top": -totheight}, 500);
				} else {
					$('.Nav_Event_menu2 ul').stop(true, true).animate({"top": "0px"}, 500);
				}
				
			} else if ($(this).attr("title")=="Giu"){
				num=$('.Nav_Event_menu2 ul li').index($('.first_event'));
				totheight=0;
	
				if (num<($('.Nav_Event_menu2 ul li').length-1)){
				
				
					$('.Content_Image_absolute:visible').stop(true, true).fadeOut();
					$('.Content_Image_absolute').eq(num+1).stop(true, true).fadeIn();
		
					var h2=$('.first_event h2').detach();
					$('.first_event').removeClass("first_event");
					$('.Nav_Event_menu2 ul li').eq(num+1).addClass("first_event");
					$('.Nav_Event_menu2 ul li').eq(num+1).prepend(h2);
					$('.slider_Event2').stop(true, true).animate({left: -710*(num+1)+"px"});
				
				
					$('.Nav_Event_menu2 ul li:lt('+(num+1)+')').each(function(){
						totheight+=$(this).height();
					});
					$('.Nav_Event_menu2 ul').stop(true, true).animate({"top": -totheight}, 500);
				}
			}
			return false; 
		});
	}
	
	$('.Text_Event2').each(function(){
		if ($(this).children().length<5){
			$(this).next().css("visibility", "hidden");
			$(this).prev().css("visibility", "hidden");
		}
	});
	var indexMaxHeight=0;
	$('.Nav_arrow2').click(function(){
                num=Math.floor($(".Nav_arrow2").index($(this))/2);
                maxheight=Math.floor((($(".Text_Event2:eq("+num+") img").length)-1)/4);
		if ($(this).css("visibility")!="hidden"){
                    if($(".Nav_arrow2").index($(this))%2==0){
                        // Slide verso sx
                        if(indexMaxHeight==0){
                            return;
                        }
                        indexMaxHeight--;
                        
                        var t=$(this).next().find("img")
                        var primaL=parseInt(t.css('left'))
                        //alert(primaL)
                        t.stop(false,true).fadeOut(300,function(){
                            t.hide();
                            t.css('top',(-114*indexMaxHeight)+"px")
                            //t.css('left',primaL)
                           t.fadeIn(300);
                        });
                    }else{
                        // Slide verso dx
                        if(indexMaxHeight==maxheight){
                            return;
                        }
                        indexMaxHeight++;
                        
                        var t=$(this).prev().find("img")
                        var primaL=parseInt(t.css('left'))
                        t.stop(false,true).fadeOut(300,function(){
                            t.hide();
                            t.css('top',(-114*indexMaxHeight)+"px")
                            //t.css('left',primaL)
                            t.fadeIn(300);
                        });
                    }         
		}
		return false;
	});
	
	
	$('.Text_Event2 img').click(function(){
		$('.Content_Image_absolute:visible').attr("src", $(this).attr("src").replace("150x100", "710x570"));
	});
	
	
	
	//sponsor
	$('.Sponsor_container ul').width(($('.Sponsor_container ul li').length/2)*155);
	setTimeout("movesponsor()", 3000);
	$('.Sponsor_container ul li a').click(function(){
		if (this.href!="" && this.href!=$("base").attr("href")){
			window.open(this.href);
		}
		return false;
	});
	
	
	// makeScrollable($('.Sponsor_container'), $('.sponsor_slider'));
	// $('.footer_arrow_right').click(function(){
		// maxleft=-($('.Sponsor_container ul').width()-930);
		// if (parseInt($('.Sponsor_container ul').css("left"))>maxleft){
			// $('.Sponsor_container ul').animate({"left": "-=155"}, 500);
		// } else {
			// $('.Sponsor_container ul').animate({"left": "0px"}, 500);
		// }
	// });
	// $('.footer_arrow_left').click(function(){
		// if (parseInt($('.Sponsor_container ul').css("left"))<155){
			// maxleft=-($('.Sponsor_container ul').width()-930);
			// $('.Sponsor_container ul').animate({"left": maxleft+"px"}, 500);
		// } else {
			// $('.Sponsor_container ul').animate({"left": "+=155"}, 500);
		// }
	// });
	
	//slider footer punti vendita
	$('.slider_punti_vendita').each(function(){
		$(this).width(145*$(this).children().length)
	});
	
});
