$(document).ready(function() {
	
 
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
 
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
 

 
});

function email(name, stile)
{
   var address = name + "\u0040" + "gestioneqsa" + "." + "it";
   var url = "mailto:" + address;

   
      text = address;
   
   document.write("<a href=\"" + url + "\" class="+stile+">" + text + "</a>");
}

function emailImg(name, domain, suffix, text)
{
   var address = name + "\u0040" + "studiocaratti" + "." + "it";
   var url = "mailto:" + address;

   if( ! text )
   {
      text = address;
   }

   document.write("<a href=\"" + url + "\"><img src=/img/structure/info_at_studiocaratti_dot_it.png  /></a>");
}


function validate() {
  validity = true; // assume valid
  var colore = "#ffcc00"
  
    if (!check_empty(document.formcontatti.nome.value))
        { 	validity = false;	
			check_nome.style.display = "block";
			document.formcontatti.elements["nome"].style.backgroundColor = colore; 
			document.formcontatti.elements["nome"].focus(); return false;}

    if (!check_empty(document.formcontatti.cognome.value))
        { 	validity = false;	
			check_cognome.style.display = "block";
			document.formcontatti.elements["cognome"].style.backgroundColor = colore; 
			document.formcontatti.elements["cognome"].focus(); return false;}

    if (!check_empty(document.formcontatti.citta.value))
        { 	validity = false;	
			check_citta.style.display = "block";
			document.formcontatti.elements["citta"].style.backgroundColor = colore; 
			document.formcontatti.elements["citta"].focus(); return false;}			

	if (!check_email(document.formcontatti.mail.value))
        { 	
			validity = false;	
			check_mail.style.display = "block";
			document.formcontatti.elements["mail"].style.backgroundColor = colore; 
			document.formcontatti.elements["mail"].focus(); return false;}					

  if (validity)
		return validity;
}

function check_empty(text) {
  check_nome.style.display = "none";
  check_cognome.style.display = "none";
  check_citta.style.display = "none";  
  check_mail.style.display = "none";
  return (text.length > 0); // returns false if empty 
	
}

function check_email(address) {
  if ((address == "")
    || (address.indexOf ('@') == -1)
    || (address.indexOf ('.') == -1))
      return false;
  return true;
}


