// Modification of the Scriptaculous Autompleter.Base class
var CFAutocompleter = Class.create(Autocompleter.Base, {
  initialize: function(element, update, url, options) {
    this.baseInitialize(element, update, options);
    this.options.asynchronous  = true;
    this.options.onComplete    = this.onComplete.bind(this);
    this.options.defaultParams = this.options.parameters || null;
    this.url                   = url;
  },

  getUpdatedChoices: function() {
	this.hide();
	//this.startIndicator();
	var thisObj = this;
    window.setTimeout( function(){ thisObj.startIndicator() }, 150);
    
    var entry = encodeURIComponent(this.options.paramName) + '=' + 
      encodeURIComponent(this.getToken());

    this.options.parameters = this.options.callback ?
      this.options.callback(this.element, entry) : entry;

    if(this.options.defaultParams) 
      this.options.parameters += '&' + this.options.defaultParams;
    
    new Ajax.Request(this.url, this.options);
  },

  onComplete: function(request) {
    this.updateChoices(request.responseText);
  },
  
  onClick: function(event) {
  },
  
  onBlur: function(event) {
    this.hasFocus = false;
    this.active = false;     
  },
  
  onKeyPress: function(event) {
    if(event.keyCode==Event.KEY_TAB || event.keyCode==Event.KEY_RETURN || 
    	(Prototype.Browser.WebKit > 0 && event.keyCode == 0)) return;
	
	this.changed = true;
    this.hasFocus = true;

    if(this.observer) clearTimeout(this.observer);
      this.observer = 
        setTimeout(this.onObserverEvent.bind(this), this.options.frequency*1000);
  }
});