$(document).ready(function(){

  // AUTOCLEAR
  jQuery('.autoclear').each(function(){
    this.defaultValue = this.value;
  });

  jQuery('.autoclear').focus(function(){
    if(this.value == this.defaultValue)
    {
      this.value = "";
    };
  });

  jQuery('.autoclear').blur(function(){
    //lose focus
    if(this.value == "")
    {
      this.value = this.defaultValue;
    };
  });

  // hide all faq answers after page load
  $('#faq_cont p').toggle();

  $('#faq_cont h4').click(function(){
     //toggle answer
     var itemToChange = (this.id.split("_"));
     //alert($(this).parent('div').attr('id'));
     $(this).next('p').toggle();
  });

  $('.search_count_option').change(function()
  {
    // Set the cookie
    $.cookie('search_count', $('.search_count_option option:selected').val());

    // Reload the page!
    window.location.href=window.location.href;
  });

  //levelling drop down changed
  $('#primary_category').change(function()
  {
    $('#sub_category').html('<option val="">Loading...</option>');
    $.get('/json/subcat/id/'+$('#primary_category option:selected').val(), function(data) {
      $('#sub_category').html(data);
    });
  });

  // Filter the contact list by category
  $('#contact_categorys').change(function()
  {
    $('.affiliate_contact').each(function(){
      if($(this).hasClass($('#contact_categorys option:selected').val()))
      {
        $(this).show();
      }
      else
      {
        $(this).hide();
      }
    });
  });

  // Filter the contact list by surname
  $('#contact_surnames').change(function()
  {
    $('.affiliate_contact').each(function(){
      if($(this).hasClass($('#contact_surnames option:selected').val()))
      {
        $(this).show();
      }
      else
      {
        $(this).hide();
      }
    });
  });

  // Facebox
  $('a[rel*=facebox]').facebox();

  //navigation stuff
  //Code goes here
  //On Hover Over
  function megaHoverOver(){
      $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
      (function($) {
          //Function to calculate total width of all ul's
          jQuery.fn.calcSubWidth = function() {
              rowWidth = 0;
              //Calculate row
              $(this).find("ul").each(function() { //for each ul...
                  rowWidth += $(this).width(); //Add each ul's width together
              });
          };
      })(jQuery);

      if ( $(this).find(".row").length > 0 ) { //If row exists...

          var biggestRow = 0;

          $(this).find(".row").each(function() {  //for each row...
              $(this).calcSubWidth(); //Call function to calculate width of all ul's
              //Find biggest row
              if(rowWidth > biggestRow) {
                  biggestRow = rowWidth;
              }
          });

          $(this).find(".sub").css({'width' :biggestRow}); //Set width
          $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

      } else { //If row does not exist...

          $(this).calcSubWidth();  //Call function to calculate width of all ul's
          $(this).find(".sub").css({'width' : rowWidth}); //Set Width

      }
  }
  //On Hover Out
  function megaHoverOut(){
    $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
        $(this).hide();  //after fading, hide it
    });
  }

  //Set custom configurations
  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'}); //Fade sub nav to 0 opacity on default
  $("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

});

