/*
To use this search framework, you need to define the following functions in the host page:
function load_search_results(html, count) {
   //html is the generated results
   //count is a js object {codeset:count,codeset:count}
}
function load_search_error() {
  //What to do when the search encounters an error
}
function before_search(callback) { //OPTIONAL
  //before_search will be executed right before the ajax request. 
  //if you decide to implement a before_search, make SUPER SURE that you (or jQuery) executes the callback
  callback(); 
}
*/
var search_string_cache = {};
var request_id = 0;
function do_search(opts) {
  if (typeof load_search_results == 'undefined' || !$.isFunction(load_search_results))
    return 'Search Error: no function "load_search_results" found.';
  if (typeof opts == 'undefined')
    return 'Search Error: no search options specified';
  if (opts.bit == 0)
    return 'Search Error: minimum one search option required.';

  var current_request_id = ++request_id;
  var id, bits;
  if (opts.id) {
    id = opts.id;
    bits = opts.bit;
  } else if (search_string_cache[opts.str]) {
    id = search_string_cache[opts.str]['id'];
    bits = search_string_cache[opts.str]['bits'];
  }
    
  new_query = true; //defaults to new query, unless...
  if (typeof id != 'undefined') { //it has an id
    if (typeof bits != 'undefined' && (bits | opts.bit) == bits) { //and no new bits
      new_query = false;      //alert('no new bits, use id');
      opts.id = id;
      if (typeof opts.p == 'undefined') //default to page 0
        opts.p = 0;   
    }
  } 
  
  url = (new_query) ? 'ajax-search.php' : 'ajax-search2.php';
  if (new_query && opts.advanced) 
    url = 'ajax_advanced_search2.php';
    
  //Define a before_search function if there isn't one
  if (typeof before_search == 'undefined' || !$.isFunction(before_search)) {
    function before_search(callback) {
      callback();
    }
  }
  
  //Execute the ajax request
  before_search(function() {
    execute_search(url,opts,new_query,current_request_id,false)
  });

}
function execute_search(url, opts, new_query, current_request_id, second_timeout) {
 $.ajax({
    'type':'GET',
    'dataType':'json',
    'async':true,
    'url':url,
    'data':opts,
    'timeout':30000,
    'error':function(xhr, msg) {
      if (msg == 'timeout' && !second_timeout) {
        execute_search(url,opts,new_query,current_request_id, true);
        return;
      }
      $.post('/ajax_log.php',{"type":"ERROR","source":"search/search.js","message":msg+"<br />\n"+url+"?"+$.param(opts)+"<br />\n"+xhr.responseText});
      load_search_error();
    },
    'success':function(data) {
      if (new_query) {
        search_string_cache[opts.str] = {'id':data.id,'bits':opts.bit,'count':data.count};
      }
      if (current_request_id == request_id) //only if the current request is the most recent
        load_search_results(data.html, data.count); //load the search results
    }
  });  
}
function cancel_search() { //doesn't actually cancel the AJAX, but will prevent load_search_results from being called
  request_id++;
}