// JavaScript Document


Iristo = { /* Iristo Start */
  

  flashFadeIn : function(i) { /* Flash message fadeins start */
    
    i.addClass('invisible')
     .css({
        visibility: "visible",
        opacity: 0
     })
     .animate({opacity: 1.0}, 1000)
     .fadeIn("slow");
     
  }, /* End Flash message fadeins */
  
  reorderList : function(button, url, destination) { /* Drag and Drop sorting start */

    button.click(function(i){
      $(this).after('<img src="images/ajax-loader.gif" id="reorder_spinner" alt="" />');
      $(this).hide();

      /* Serialize order */    
      var new_order = $('ul#position').sortable('serialize', { key:'position[]' });
      /* Set auth token */
      var auth_token = $('form input[name=authenticity_token]').val();
      auth_token = encodeURIComponent(auth_token);
      /* Send post request */
      $.post(url, '_method=post&authenticity_token=' + auth_token + '&' + new_order, function(){
        $("img#reorder_spinner").remove();
        /* $("span.order_changed").show("fast"); */
        window.location = destination;
      });

      i.preventDefault();
    });

  }, /* End Drag and Drop sorting */
  
  
  
  validate : validate = { /* Validate start */
    
    clearErrorMessages : function(form) {
      form.find("span.error_message").remove();
      form.find("input").removeClass("error_bg");
    },

    isNotEqual : function(field, field2) {
      if (field.val() != field2.val()) {
        return true;
      }
    },

    isTooShort : function(field, length) {
      if (field.val().length < length) {
        return true;
      }
    },
    
    isEmpty : function(field) {
      if (field.val() == '' || field.val() == null) {
        return true;
      }
    },
    
    isInvalidWebAddress : function(field) {
      var valid = /^[A-Za-z\-0-9_]*$/;
      if (!valid.test(field.val())) {
        return true;
      }
    },
    
    isInvalidEmail : function(field) {
      /* Check for valid email */
      var valid = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      if (!valid.test(field.val())) {
        return true;
      }
    },

    addErrorMessage : function(field, message) {
      field.focus();
      field.after('<span class="error_message">' + message + ".</span>");
      field.addClass("error_bg");
      $('span.error_message').fadeIn("slow");
    }
    
  } /* End validate */

} /* End Iristo */


$(document).ready(function(){ 

 $('fieldset#login form').submit(function(){
    
    Iristo.validate.clearErrorMessages($(this));
    
    if(Iristo.validate.isEmpty($('input#email'))) {
      Iristo.validate.addErrorMessage($('input#email'), "Email can't be blank");
      return false;
    }
    
    if(Iristo.validate.isEmpty($('input#pass'))) {
      Iristo.validate.addErrorMessage($('input#pass'), "Password can't be blank");
      return false;
    }
    
  });
  
  
  $('fieldset#forgot form').submit(function(){
    
    Iristo.validate.clearErrorMessages($(this));
    
    if(Iristo.validate.isEmpty($('input#email'))) {
      Iristo.validate.addErrorMessage($('input#email'), "Inserisci il tuo indirizzo di email");
      return false;
    }
   
  });
  
     /* Flash notice */
Iristo.flashFadeIn($("p#flash_notice"));
  
  /* Flash error */
Iristo.flashFadeIn($("p#flash_error"));
  
  /* Other errors */
Iristo.flashFadeIn($("div#errorExplanation")); 
  
  
   /* Wizard */
  $('div#now_what ul li a').not('div#now_what ul li#now_what_wizard a, div#now_what ul li#now_what_popup_menu a').each(function(){
    $(this).hide();
  });
  
  $('div#now_what ul li').not('div#now_what ul li#now_what_wizard, div#now_what ul li#now_what_popup_menu').click(function(){
    window.location = $(this).children('a')[0].href;
  });
  
  $('div#now_what ul li').hover(
    function(){
      $(this).children('a').show();
    },
    function(){
      $(this).children('a').hide();
    }
  );
  
  if ($('li#now_what_wizard').length > 0) {
    var obj = $('li#now_what_wizard');

    $('li#now_what_wizard a#hide_wizard').click(function(e){
      setWizardCookie();
      obj.hide();
      e.preventDefault();
    });

    function setWizardCookie() {
      var exp = new Date(2020, 00, 01);
      document.cookie = 'wizard=hidden;expires=' + exp.toGMTString();
    }

    function readWizardCookie() {
      if (document.cookie) {
        var tmp = document.cookie.split(';');
        $.each(tmp, function(i, val) {
          var val = val.split("=");
          if($.trim(val[0]) == 'wizard') {
            obj.addClass(val[1]);
          }
        });
      }
    }

    readWizardCookie();
  }
  
    /* Some basic tooltips */
	$("a.tooltip").click(function(e){
	  
  	xOffset = 20;
  	yOffset = 20;
  	
    t = $(this).attr("rel");
    
	  if($("div#tooltip").length < 1) {
  		$(this).title = "";
      if (typeof document.body.style.maxHeight === "undefined") {
        $('select').addClass('invisible');
      }
    	$("body").append('<div id="tooltip"><p>'+ t +'</p></div>');
  		$("div#tooltip")
  			.css("top",(e.pageY - xOffset) + "px")
  			.css("left",(e.pageX + yOffset) + "px")
  			.fadeIn("fast");
	  }	else {
  		$(this).rel = t;		
      if (typeof document.body.style.maxHeight === "undefined") {
        $('select').removeClass('invisible');
      }
  		$("div#tooltip")
  		.fadeOut("fast", function(){
  		  $(this).remove();
	    });
    }
  });
  
  $("div#tooltip").live("click", function(e){
    $(this).remove();
    if (typeof document.body.style.maxHeight === "undefined") {
      $('select').removeClass('invisible');
    }
    return false;
  })
  
     /* Select all for mailbox checks */
  $('input#messages_box_toggle').click(function(){
    $('tbody#message_form_body input').each(function(i){
      this.checked == true ? this.checked = false : this.checked = true;
    })
  });
  
  
})
