/**
 * Overridden behaviour for autocompleter
 */

Autocompleter.Base.prototype.updateChoices = function(choices) {
    if(!this.changed && this.hasFocus) {
        this.update.innerHTML = choices;
        Element.cleanWhitespace(this.update);
        Element.cleanWhitespace(this.update.down());

        if(this.update.firstChild && this.update.down().childNodes) {
          this.entryCount = 
            this.update.down().childNodes.length;
          for (var i = 0; i < this.entryCount; i++) {
            var entry = this.getEntry(i);
            entry.autocompleteIndex = i;
            this.addObservers(entry);
          }
        } else { 
          this.entryCount = 0;
        }

        this.stopIndicator();
        this.index = -1;
        
        if(this.entryCount==1 && this.options.autoSelect) {
          this.selectEntry();
          this.hide();
        } else {
          this.render();
        }
      }
    }

Autocompleter.Base.prototype.selectEntry = function() {
	this.active = false;
    this.updateElement(this.getCurrentEntry());
    $(this.element).up('form').submit();
  }

Autocompleter.Base.prototype.onKeyPress = function(event) {
	if(this.active)
	      switch(event.keyCode) {
	       case Event.KEY_TAB:
	       case Event.KEY_RETURN:
	         this.selectEntry();
//	         Event.stop(event);
	         return;
	       case Event.KEY_ESC:
	         this.hide();
	         this.active = false;
	         Event.stop(event);
	         return;
	       case Event.KEY_LEFT:
	       case Event.KEY_RIGHT:
	         return;
	       case Event.KEY_UP:
	         this.markPrevious();
	         this.render();
	         Event.stop(event);
	         return;
	       case Event.KEY_DOWN:
	         this.markNext();
	         this.render();
	         Event.stop(event);
	         return;
	      }
	     else 
	       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);
}
Autocompleter.Base.prototype.markPrevious = function() {
    if(this.index > 0) this.index--
      else this.index = this.entryCount-1;
    this.getEntry(this.index).scrollIntoView(false);
}
	
