$(document).ready(function()
{
  /**
   * Hide submit button. Users without JS can still use it as fallback, plus some browsers won't submit
   * a form without a submit button.
   */
  $('#gjSearchFilterSubmit').css('display', 'none');

  /**
   *  Submit form when user clicks any checkbox
   */
  $('.gjSearchAjaxTrigger').click(function()
  {
    $('#gjSearchFilterForm').submit();
  });

  /**
   * Clear checkboxes for a search type and submit form
   */
  $('.gjSearchClear').click(function()
  {
    $(this).parent().parent().find('input').each(function()
    {
      $(this).attr('checked', false);
    });

    $('#gjSearchFilterForm').submit();
    return false;
  });

  /**
   * Catch form submit event and turn it into an AJAX request
   */
  $('#gjSearchFilterForm').submit(function()
  {
    $.ajax(
    {
      url: $('#gjSearchFilterForm').attr('action'),
      type: 'POST',
      data: $('.gjSearchAjaxTrigger').serializeArray(),
      async: false,
      success: function(data)
      {
        $('.twoThirds_column').fadeOut(500, function() {
          $('.twoThirds_column').html($(data).find('.twoThirds_column').html());
          $('.twoThirds_column').fadeIn(500);
        });
        $('.search_result_count').fadeOut(500, function() {
          $('.search_result_count').html($(data).find('.search_result_count').html());
          $('.search_result_count').fadeIn(500);
        });
      }
    });

    return false;
  });
});
