$(document).ready(function(){
  $('.cont').click(function(){
    if ($(this).text()=='...[espandi]') text="[chiudi]"; else text="...[espandi]";
    $(this).text(text).prev().slideToggle('medium');
    return false;
  });
  
  //controlli
  
  regexps= {
    username : /^[a-zA-Z0-9_]{4,15}$/i,
    password : /^[a-zA-Z0-9_]{4,15}$/i,
    cap : /^[0-9]{5}$/i,
    email : /^[^@]+@[^@]+.[a-z]{2,}$/i,
    telefono : /^[0-9]{2,6}-? ?[0-9]{6,7}$/i,
  };
  
  $("form").submit(function() {
    if ($(this).attr("name")=="right-login") return true;
    var ok=true;
    $(this).find(":password, :text").each(function() {
      $(this).removeClass("err-input").parent().next().text("").css("visibility","hidden");
      txt="";
      
      if ($(this).attr("name")=="user" && $(this).attr("nondispo")=="true") {
        ok=false;
        txt="username non disponibile";
      }      
      
      if ($(this).attr("name")=="cap" && !$(this).attr("value").match(regexps['cap'])) {
        ok=false;
        txt="il CAP deve essere di 5 caratteri numerici";
      }
      
      if ($(this).attr("name")=="telefono" && !$(this).attr("value").match(regexps['telefono'])) {
        ok=false;
        txt="il numero di telefono deve essere nel formato<br>0123-456789 (fissi) o<br>333-1234567 (cellulari)";
      }
      if ($(this).attr("name")=="mail" && !$(this).attr("value").match(regexps['email'])) {
        ok=false;
        txt="inserire un indirizzo e-mail corretto";
      }
      
      if ($(this).attr("name")=="user" && !$(this).attr("value").match(regexps['username'])) {
        ok=false;
        txt="l'username deve essere di 4-15 caratteri alfanumerici";
      }
      if ($(this).attr("name")=="pwd" && !$(this).attr("value").match(regexps['password'])) {
        ok=false;
        txt="la password deve essere di 4-15 caratteri alfanumerici";
      }
      if ($(this).attr("name")=="pwd2" && $(this).attr("value")!=$("#pwd1").attr("value")) {
        ok=false;
        txt="le password devono coincidere";
      }
      if ($(this).attr("value")=="") {
        ok=false;
        txt="completare il campo";
      }
      if (txt!="") $(this).addClass("err-input").focus(function() {
        $(this).removeClass("err-input").parent().next().text("").css("visibility","hidden");
      }).parent().next("td").html(txt).css("visibility","visible");
    });
    if (!ok) {
      alert("ATTENZIONE: i campi contrassegnati in rosso presentano errori.");
      return false;
    }
  });
  
  $("#usern").keyup(function(){
    if (!$(this).attr("value").match(regexps['username'])) {
      $(this).addClass("err-input").focus(function() {
        $(this).removeClass("err-input").parent().next().text("").css("visibility","hidden");
      }).parent().next("td").html("l'username deve essere di 4-15 caratteri alfanumerici").css("visibility","visible");
      return;
    }
    $.post("/cart/checkusername", {username : $(this).attr("value")} ,
    function(data){
      if (data=="no")
        $("#usern").addClass("err-input").focus(function() {
          $(this).removeClass("err-input").parent().next().text("").css("visibility","hidden");
        }).attr('nondispo','true').parent().next("td").html("username non disponibile").css("visibility","visible");
      else
        $("#usern").removeClass("err-input").attr('nondispo','false').parent().next().html("<font style=\"color:green;\">userame disponibile</font>");
    });
  });
  
  //notizie precedenti
  $('#loadnews').click(function() {
    if ($(this).attr('href').indexOf("eventi-notizie")!==-1) get='/eventi-notizie/l/';
      else get='/rubriche/l/';
    $.get(
      get+$(this).attr('l'),
      function(data) {
        $('#loadnews').parent().before(data);
        if (data!="") {
          l=parseInt($('#loadnews').attr('l'));
          l++;
          $("#loadnews").attr('l',l);
        }
        $("#newsloading").hide();
      }
    );
    $("#newsloading").show();
    return false;
  });
  
  //form libro
  $('#viewform').click(function() {
    $('#show-form').toggle('fast');
    return false;
  });
  
  //galleria museo
  $('#m-gallery').gallery({
    interval: 5500,
    height: '402px',
    width: '510px',
    ratio: 0.27,
    thumbHeight: 100,
    thumbWidth: 130,
    showOverlay:false
  });
  
});