﻿// $Id: livesearch.js,v 1.9 2007/04/04 07:24:34 kourge Exp $
if (Drupal.jsEnabled) { // global killswitch
(function($) { // Dollar namespace scoping
Drupal.LiveSearch = {
  timer: null, 
  localizedTerm: null, 
  id: 'live-search-results', // Change this if conflicts happen
  
  eventListener: function(event) {
    if (event.keyCode == 27) { // KEY_ESC = 27
      $(this).val('');
      // I'm fed up with debugging animations. ITFC if you like.
      $('#' + Drupal.LiveSearch.id).empty().hide();
    }
    else {
      try {window.clearTimeout(Drupal.LiveSearch.timer);} catch (e) {}
      // Throttle and delay
      Drupal.LiveSearch.timer = window.setTimeout(
        'Drupal.LiveSearch.performSearch()', 
        parseInt(Drupal.settings.liveSearch.delayDuration) || 1250
      );
    }
  },
  
  performSearch: function() {
    var id = this.id;
    // Typing this over and over drives me over the edge.
    var settings = Drupal.settings.liveSearch;
    var type = settings.targetSearchBox || 'theme';
    var searchBox = $('input#edit-search-' + type + '-form-keys');
    var keyword = Drupal.encodeURIComponent(searchBox.val());
    
    if (!keyword ||
        keyword.length < settings.minimumWordSize ||
        keyword == this.localizedTerm) {
      $('#' + id).empty().hide();
      return;
    } else {
      searchBox.addClass('throbbing');
      
      $.getJSON(settings.queryURL + '/' + keyword, function(data) {
        $('#' + id).html(data.found ? data.results : data.message).show();
        $('input#edit-search-' + type + '-form-keys').removeClass('throbbing');
      });
    }
  }
};

// This should be faster than 'document'.
$('input').ready(function() {
  var settings = Drupal.settings.liveSearch;
  // User permission killswitch
  if (!settings.searchAllowed) {return;}
  var type = settings.targetSearchBox || 'theme';
  var searchForm = $('form#search-' + type + '-form');
  var searchBox = $('#edit-search-' + type + '-form-keys');
  var localizedTerm;
  
  Drupal.LiveSearch.localizedTerm = localizedTerm = 
    searchForm.find('input[@type=submit]').val();
  
  searchForm.end().
    addClass('live-search').
    append('<div id="' + Drupal.LiveSearch.id + '"></div>');
  $('#' + Drupal.LiveSearch.id).hide();
  
  if (settings.hideSnippets) searchForm.addClass('hide-snippet');
  (settings.showItemInfo) ?
    searchForm.addClass('show-item-info') :
    searchForm.addClass('hide-item-info');
  
  // Apple HIG fans should appreciate this option.
  if (settings.compactSearchBox) {
    searchForm.addClass('compact');
    searchBox.val(
      localizedTerm
    ).focus(function() {
      if ($(this).val() == localizedTerm) {
        $(this).val('');
      }
    }).blur(function() {
      if (!$(this).val()) {
        $(this).val(localizedTerm);
      }
    });
  }

  searchBox.
    addClass('form-autocomplete').
    keyup(Drupal.LiveSearch.eventListener).
    attr('autocomplete', 'off');
});
})(jQuery); // End dollar namespace scoping
} // End global killswitch