var refine_search = function() {
  
  var that = this,
      form = $('form.refine_results'),
      content = $('div.main_content'),
      timer = {timeout: 2000};
      
  timer.start = function() {
    timer.time = setTimeout("formHandler()", this.timeout);
  };
  
  timer.stop = function() {
    clearTimeout(timer.time);
  };
  
  timer.reset = function() {
    timer.stop();
    timer.start();
  };
      
  var formHandler = function() {
    
    content.load(form.attr('action') + ' .main_content', form.serialize(), function() {
      form.triggerHandler('submit');
      $(this).css({
        'background-image': 'none'
      });
    });
  };


  form.find('input[type=submit]').hide();
  
  form.find('input[type=checkbox]').change(function() {
    $('ul.results, div.pagination, h3.no_results').remove();
    content.css({
      background:'url(/images/ajax-loader.gif) no-repeat center',
      'min-height': '450px'
    });
    
    timer.reset();
  });
  

  that.formHandler = formHandler;

  return that;
  
};

$(function() {
  refine_search();
});

