
 Object.extend( Ajax.Autocompleter.prototype, { 
        
 	 render: function() {
        if(this.entryCount > 0) {
          for (var i = 0; i < this.entryCount; i++)
            this.index==i && this.active == true ?
              Element.addClassName(this.getEntry(i),"selected") :
              Element.removeClassName(this.getEntry(i),"selected");
          if(this.hasFocus) {
            this.show();
            //this.active = false;
          }
        } else {
          this.active = false;
          this.hide();
        }
      },



      onKeyPress: function(event) {

    	if( ! this.active && event.keyCode==Event.KEY_DOWN && this.getToken().length>=this.options.minChars) {
    		//alert("TOK: "+ this.getToken().length);
    		// this.getToken().length>=this.options.minChars
        	this.active = true;
        	this.index	= -1;
        }
	     
        if(this.active)
          switch(event.keyCode) {
           case Event.KEY_TAB:
           case Event.KEY_RETURN:
             this.selectEntry();
             Event.stop(event);
           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;
           case Event.KEY_BACKSPACE:
        	 if( this.getToken().length>=this.options.minChars ) {
	        	 this.hide();
	             this.active = false;
	             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);
      },

      markPrevious: function() {
        if(this.index > 0) {
        	this.index--;
        	//this.getEntry(this.index).scrollIntoView(true);
        }
        else {
        	this.index	= -1;
        }
        
      },
      
      markNext: function() {
        if(this.index < this.entryCount-1) this.index++
          else this.index = 0;
       // this.getEntry(this.index).scrollIntoView(false);
      },

      selectEntry: function() {
    	if( this.index >= 0 ) {
	        this.active = false;
	        this.updateElement(this.getCurrentEntry());
	        
	        this.element.focus();
	        
            var evt = document.createEvent("KeyboardEvent");
            evt.initKeyEvent("keypress", true, true, null, false, false, false, false, 0, 32);
            this.element.dispatchEvent(evt);

    	}
      }

});