$(document).ready(function() {
   
  $("#photo").hide();
  $(".hid").hide();
  
  $("#li3").hover(function() {
      $("#pr3").show("slow",function () {$(this).attr("style","z-index:300; padding-top:11px;");});
    }, function() {
      $("#pr3").hide("slow");
    });
	 $("#nav li:not(#li3) a, #btnC").hover(function() {
		var curr = $(this).attr("href");
		curr = curr.replace("#","");
		next = "#" + curr.replace(".html","");
		$(this).attr("href",next);
	 });
  $("#nav li:not(#li3) a, #btnC").click(function() {
	 if (($(this).attr("href") == "#home") || ($(this).attr("href") == "#contatti") || ($(this).attr("href") == "#dove_siamo") || ($(this).attr("href") == "#chi_siamo")) {
		$(".hidden").load('html/blank.html');
	 }
	 else {
		var tohid = 'html/' + $(this).attr('href') + '_hidden.html'
		tohid = tohid.replace('#','');
		$(".hidden").load(tohid);
	 }
	 var toload = 'html/' + $(this).attr("href") + '.html';
	 toload = toload.replace("#","");
	 if($(this).attr("href") == "#contatti") {
	    if(isIe7()) {
	      toload = "html/contatti_ie.html"
	    }
	 }
	 function loadContent() {
		$('#body-left').load(toload,'',showNewContent);
	}
	 $('#body-left').fadeOut(1000,loadContent);
	 
  });
	
	$('.slideshow').cycle({
		fx: 'fade',
		speed:  2500,
		timeout:  6000,
		pause: 1
	});
	$("#photo").attr("style","visibility: visible;");	
});

function showNewContent() {
	$('#body-left').fadeIn(1000);
}

function isIe7() {
    var check = false;
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
      var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
    if (ieversion < 8)
      check = true;
    }
    return check;
}

function check_form() {
  var check = true;
  if($("#nome").attr("value") == "") {
    $("#lnome").attr("style","color: red; font-weight: bold;");
    check = false;
  }
  if($("#cognome").attr("value") == "") {
    $("#lcognome").attr("style","color: red; font-weight: bold;");
    check = false;
  }
  if($("#citta").attr("value") == "") {
    $("#lcitta").attr("style","color: red; font-weight: bold;");
    check = false;
  }
  if(($("#email").attr("value") == "") && ($("#telefono").attr("value") == "")) {
    if($("#email").attr("value") == "") {
      $("#lemail").attr("style","color: red; font-weight: bold;");
    }
    if($("#telefono").attr("value") == "") {
      $("#ltelefono").attr("style","color: red; font-weight: bold;");
    }
    check = false;
  }
  return check;
  
}

function send_msg() {
  if(check_form()) {
    var nome = encodeURIComponent($("#nome").attr("value"));
    var cognome = encodeURIComponent($("#cognome").attr("value"));
    var citta =  encodeURIComponent($("#citta").attr("value"));
    var provincia = encodeURIComponent($("#provincia").attr("value"));
    var indirizzo = encodeURIComponent($("#indirizzo").attr("value"));
    var telefono = encodeURIComponent($("#telefono").attr("value"));
    var email = encodeURIComponent($("#email").attr("value"));
    var consenso = encodeURIComponent($("#consenso").attr("value"));

    var messaggio = encodeURIComponent($("#messaggio").attr("value"));
    var query_str = "nome=" + nome + "&cognome=" + cognome + "&citta=" + citta + "&provincia=" + provincia + "&indirizzo=" + indirizzo + "&telefono=" + telefono + "&email=" + email + "&consenso=" + consenso + "&messaggio=" + messaggio;
    $("#form_invio").load("php/invio_mail.php?"+query_str);
  }
  else {
    $("#errore").removeClass("error");
  }
}

function clear_err(id) {
  var nid = "#l" + id;
  if((nid == "#ltelefono") || (nid == "#lemail")) {
    $("#ltelefono, #lemail").attr("style","color: #5C5C5C;");
  }
  $(nid).attr("style","color: #5C5C5C;");
  if(!$("#errore").hasClass("error")) {
    if(check_form()) {
      $("#errore").addClass("error");
    }
  }
}
