Ext.ux.autocompleter = Ext.extend(Ext.util.Observable, {
	els : {},
	constructor : function(config) {
		config = config || {};
		Ext.apply(this, config);
		Ext.ux.autocompleter.superclass.constructor.call(this, config);
		this.initEvents();
	},

	initEvents : function() {
		Ext.get(this.observerfield).on("blur", function(ev) {
			var self = this;
			Ext.Ajax.request({
				url : '/domains/autocomplete.json',
				params : Ext.apply(this.params, {
							query : Ext.get(this.observerfield).getValue()
						}),
				success : function(response, opts) {
					var obj = Ext.decode(response.responseText);
					if (obj.results && obj.results[0]) {
						if (Ext.get(self.updatefield)) {
							if (Ext.get(self.updatefield).dom.value == "") {
								Ext.get(self.updatefield).dom.value = obj.results[0][self.params.keyfield];
							}
						} else {
							if (self.updatefield.getValue() == "") {
								self.updatefield
										.setValue((obj.results[0][self.params.keyfield]));
							}
						}
					}
				},
				failure : function(response, opts) {
				}
			}, this);

		}, this);
	}
});

Ext.ux.selectsuggest = Ext.extend(Ext.util.Observable, {
			els : {},
			typeAhead: true,
			forceSelection: false,
			selectOnFocus : false,
			constructor : function(config) {
				config = config || {};
				Ext.apply(this, config);

				var width = Ext.get(this.field).getWidth();
				this.combobox = new Ext.form.ComboBox({
							store : new Ext.data.Store({
										proxy : new Ext.data.HttpProxy({
													url : '/domains/autocomplete.json'
												}),
										baseParams : this.params,
										reader : new Ext.data.JsonReader({
													totalProperty : 'results',
													root : 'results'
												}, [this.readerConfig])
									}),
							displayField : this.displayField,
							valueField : this.valueField,
							typeAhead : this.typeAhead,
							mode : 'remote',
							autoLoad : false,
							emptyText : '',
							
							width : width,
							value : this.defaultValue,
							transform : this.field,
							minChars : 3,
							hideTrigger : true
						});
					Ext.ux.selectsuggest.superclass.constructor.apply(this, arguments);	

			},
			getCombobox : function() {
				return this.combobox;
			}
		});

