function newsTicker()
{
	var tick; 
	var n = 4;
	if ($("#col-news .news-item").length > n) // se ci sono più di 4 news...
	{
		tick = setInterval("newsTickerScroll("+n+")", 3000); // fai partire il timer
		
		// calcola quanto sono alte le prime 4 notizie, tutte insieme
		var hh1 = 0; for (i=0; i<n; i++) { hh1 = hh1 + $("#col-news .news-item").eq(i).outerHeight(true) }
		
		// setta l'altezza del contenitore su misura (nascondendo le altre)
		$("#col-news .box").css({overflow:'hidden',height:hh1 + 'px'});
	}

}

function newsTickerScroll(n)
{

	var h = $("#col-news .news-item:first").outerHeight(true); // altezza del primo elemento, quello che verrà nascosto scrollando verso l'alto
	var hh1 = hh2 = 0; // hh1 = altezza totale delle prime quattro; hh2 = altezza totale delle news successive (altezza successiva del contenitore)
	
	// calcola quanto sono alte le prime 4 notizie, tutte insieme, e delle successive 4 (dalla 1° alla 5°)
	for (i=0; i<n; i++) { hh1 = hh1 + $("#col-news .news-item").eq(i).outerHeight(true) } //hh1
	for (i=1; i<(n+1); i++) { hh2 = hh2 + $("#col-news .news-item").eq(i).outerHeight(true) } //hh2

	// setta l'altezza del contenitore su misura (nascondendo le altre)
	$("#col-news .box").css({overflow:'hidden',height:hh1 + 'px'});
	
	// duplica la prima notizia in fondo
	$("#col-news .news-item:first").clone().appendTo("#col-news .box");

	// adeguo l'altezza del contenitore alle news successive durante l'animazione
	$("#col-news .box").delay(500).animate({height: hh2}, 500);
	
	// fa scorrere la prima verso l'alto
	$("#col-news .news-item:first").animate({marginTop: -h}, 1000, function() 
	{
		// al termine dell'animazione, rimuove la prima (rimane la copia in fondo)
		$("#col-news .news-item:first").remove();
	});
	
}


function newsGallery(id)
{

	$("#"+id+" .big-photo").html('<img alt="photo" />');
	$("#"+id+" .big-photo img").attr("src",$("#"+id+" .list img:first").attr("src"));

	$("#"+id+" .thumbs a").click(function(event)
	{
		event.preventDefault();
		var src = $(this).find("img").attr("src"); 
		$("#"+id+" .big-photo img").attr("src",src);
	});
	
	if ( $("#"+id+" .thumbs a").length > 3)
	{
			
		$("#"+id+" .next").click(function()
		{
			if ( $("#"+id+" .list").is(":animated") ) {return false; }
			var thw = 92; // thumb width + border + margin;
			var nth = $("#"+id+" .list img").length;
			var visible = 3;
			var maxOffset =  thw*(nth-visible);
			var currentOffset = $("#"+id+" .list").css("marginLeft").replace("px","");
			if (currentOffset > -maxOffset)
			{
				$("#"+id+" .list").animate({"marginLeft":"-="+thw+"px"}, 300);
			}
			else
			{
				$("#"+id+" .list").animate({"marginLeft":"0"}, 600);
			}
		});
		
		$("#"+id+" .prev").click(function()
		{
			if ( $("#"+id+" .list").is(":animated")) {return false; }
			var thw = 92; // thumb width + border + margin;
			var nth = $("#"+id+" .list img").length;
			var visible = 3;
			var maxOffset =  thw*(nth-visible);
			var currentOffset = $("#"+id+" .list").css("marginLeft").replace("px","");
			if (currentOffset != 0)
			{
				$("#"+id+" .list").animate({"marginLeft":"+="+thw+"px"}, 300);
			}
			
		});
	
	} 
	else
	{
		$(".prev, .next","#"+id).remove();
	}
	
	
} 


function headerSlider()
{
	$('#slider').nivoSlider({
		effect:'random', // Specify sets like: 'fold,fade,sliceDown'
		slices:15, // For slice animations
		boxCols: 8, // For box animations
		boxRows: 4, // For box animations
		animSpeed:300, // Slide transition speed
		pauseTime:10000, // How long each slide will show
		startSlide:0, // Set starting Slide (0 index)
		directionNav:false, // Next & Prev navigation
		directionNavHide:true, // Only show on hover
		controlNav:true, // 1,2,3... navigation
		controlNavThumbs:false, // Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, // Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', // Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
		keyboardNav:false, // Use left & right arrows
		pauseOnHover:false, // Stop animation while hovering
		manualAdvance:false, // Force manual transitions
		captionOpacity:0.8, // Universal caption opacity
		prevText: 'Successivo', // Prev directionNav text
		nextText: 'Precedente', // Next directionNav text
		beforeChange: function(){}, // Triggers before a slide transition
		afterChange: function(){}, // Triggers after a slide transition
		slideshowEnd: function(){}, // Triggers after all slides have been shown
		lastSlide: function(){}, // Triggers when last slide is shown
		afterLoad: function(){} // Triggers when slider has loaded
	});
}


function check_form_contatti()
{
	 $("#contatti_invia").click(function(event){
	
			event.preventDefault(); 
			if ($("#form_contatti .required.email input").val().indexOf("@") == -1
				||
				$("#form_contatti .required.email input").val().indexOf(".") == -1 
			){ 
				alert("L'indirizzo e-mail inserito non è valido");
				$("#form_contatti .required.email input").focus();
				return false;
			}
			
			if (
				$("#form_contatti .required textarea").val() == ""
			){
				alert("È necessario compilare tutti i campi con l'asterisco"); 
				return false;
			}; 
			
			if (
				$("#accetto_privacy").is(":checked")
			){
				$("#form_contatti").submit();
			} else {
				
				alert("È necessario accettare quanto dichiarato nell'informativa sulla privacy"); 
				return false;
			}; 

		});
		
}

function check_form_login_itchef()
{
 	$("#login-chef").click(function(event){
		event.preventDefault(); 
		
		if ($("#user-chef").val() == "" || $("#pass-chef").val() == "") 
		{
			alert("Username o password non inseriti");
			return false;
		}
		
		$("#login-it-chef").submit();
	}); 
}

function check_form_login_schede()
{
 	$("#login-schede").click(function(event)
	{
		event.preventDefault(); 
													
		if ($("#user-schede").val() == "" || $("#pass-schede").val() == "") 
		{
			alert("Username o password non inseriti");
			return false;
		}
		
		$("#login-schede-tecniche").submit();
	}); 
}


//Inizio form registrazione 
$(document).ready(function(){
	var abilitedOnblur =false;

	$('#registerMember').submit(function(e) {
		
		var stepname = $('#stepName').val();
		var b = false;
		if(stepname== '2' ){
			b = controlStep2()			
		}
		if(stepname== '3' ){
			b = controlStep3();
		}
		return b;	
	});
});

function controlStep2(){
	var valid = true;
	$("#email").css('background', '#fff');
	var email = $("#email").val();
	var regExMail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
    if (regExMail.test(email) == false) {
        valid = false;
        $("#email").css('background', '#ff7f7e');
    }
	return valid;
}

function controlStep3(){
	
	var valid = true;
	
	
	$("#nome").css('background', '#fff');
	$("#cognome").css('background', '#fff');
	$("#codfis-piva").css('background', '#fff');
	$("#address1").css('background', '#fff');
	$("#citta").css('background', '#fff');
	$("#regione").css('background', '#fff');
	$("#telefono").css('background', '#fff');
	$("#cell").css('background', '#fff');
	$("#fax").css('background', '#fff');
	
	
	var nome = $("#nome").val();
	var cognome = $("#cognome").val();   
	var codfispiva = $("#codfis-piva").val(); 
	var telefono = $("#telefono").val();
	var cell = $("#cell").val();
	var fax = $("#fax").val();
	var regione = $("#regione").val();
	var citta = $("#citta").val();
	
	var regNome=  /[a-zA-Zàòèéùì]*/
	if ((nome == '')||(regNome.test(nome) == false)) {
		valid = false;
		$("#nome").css('background', '#ff7f7e');
	}
	var regCogn = /[a-zA-Zàòèéùì' ]+/
	if ((cognome == '')||(regCogn.test(cognome) == false)) {
		valid = false;
		$("#cognome").css('background', '#ff7f7e');
	}
	var regCFPIVA = /^[A-Z]{6}[0-9]{2}[A-Z][0-9]{2}[A-Z][0-9]{3}[A-Z]$/
		/*if ((codfispiva == '')||(regCFPIVA.test(codfispiva) == false)) {*/
		if (codfispiva == '') {
			valid = false;
			$("#codfis-piva").css('background', '#ff7f7e');
		}
   
   
	var regTel = /[0-9]/
	if ((telefono != '')&&(regTel.test(telefono) == false)) {    
		valid = false;
		$("#telefono").css('background', '#ff7f7e');
	}
	if ((cell != '')&&(regTel.test(cell) == false)) {
			valid = false;			
			$("#cell").css('background', '#ff7f7e');
		}
	if ((fax != '')&&(regTel.test(fax) == false)) {
		valid = false;			
		$("#fax").css('background', '#ff7f7e');
	}
	var regCitta = /[a-zA-Zàòèéùì' ]+/
	if ((citta == '')||(regCitta.test(citta) == false)){
		valid = false;
		$("#citta").css('background', '#ff7f7e');
	}
	if ((regione != '')&&(regCitta.test(regione) == false)){
		valid = false;
		$("#regione").css('background', '#ff7f7e');
	}
	abilitedOnblur =true;
	return valid;
}
function onBlurControlStep2(){
		
	controlStep2();
	
}
function onBlurControlStep3(){
	
	if(abilitedOnblur==true){
		controlStep3();
	}

}

// Fine form registrazione 

