
//= depends: prototype, controls
//= depends: zapi/zapi

ZAPI.namespace(window, 'APP.Widget').AjaxAutocompleter = Class.create(Ajax.Autocompleter, {
	onKeyPress: function($super, event) {
		if (this.active) {
			switch (event.keyCode) {
				case Event.KEY_TAB:
				case Event.KEY_RETURN:
					if (this.index < 0) {
						return;
					}
			}
		}
		return $super(event);
	},

	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();
			}
		}
	}
});
