/**
 * This overrides the Drupal.behaviors.autocomplete() function in Pressflow
 * Drupal core's misc/autocomplete.js, in order to workaround
 * http://drupal.org/node/836918 and
 * http://www.palprojects.com/projects/27/tickets/498
 */

// Don't set it if it's not already set.  This means that this script file
// must load /after/ misc/autocomplete.js.  Since this is a theme script,
// that should always be the case.
if (Drupal.behaviors.autocomplete) {

  // @note PGA: This is copied directly from misc/autocomplete.js.
  /**
   * Attaches the autocomplete behavior to all required fields
   */
  Drupal.behaviors.autocomplete = function (context) {
    var acdb = [];
    $('input.autocomplete:not(.autocomplete-processed)', context).each(function () {
      var uri = this.value;
      if (!acdb[uri]) {
        acdb[uri] = new Drupal.ACDB(uri);
      }

      // @note PGA: Modifications start here.
      // 13 is "autocomplete".length;
      var input = $('#' + this.id.substr(0, this.id.length - 13), $(this).parent())
        .attr('autocomplete', 'OFF')[0];
      // @note PGA: Modifications end here.

      $(input.form).submit(Drupal.autocompleteSubmit);
      new Drupal.jsAC(input, acdb[uri]);
      $(this).addClass('autocomplete-processed');
    });
  };
}

