nmdgf.widgets.Autocomplete = function(input, container, url, fields, options) {
	this.url = url;
	this.fields = fields;
	this.input = input;
	this.container = container;
	this.options = options || {};
	nmdgf.registerWidget(this);
};
nmdgf.widgets.Autocomplete.prototype = {
		
	fields : null,
	
	input : null,
	
	container : null,
	
	autocomplete : null,
	
	datasource : null,
	
	handlers : null,
	
	options : null,
	
	initialize : function() {
	    this.datasource = new YAHOO.util.XHRDataSource(this.url);
	    this.datasource.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
	    
	    this.datasource.responseSchema = {
	        resultsList : "results",
	        fields : this.fields
	    };
	    
	    this.datasource.maxCacheEntries = this.options.maxCacheEntries || 10;

	    this.autocomplete = new YAHOO.widget.AutoComplete(this.input, this.container, this.datasource);
	    this.autocomplete.forceSelection = this.options.forceSelection !== undefined ? this.options.forceSelection : true;
	    this.autocomplete.minQueryLength = 1;
	    this.autocomplete.queryDelay = .5;
	    this.autocomplete.animVert = false;
	    this.autocomplete.useShadow = true;
	    this.autocomplete.itemSelectEvent.subscribe(this.itemSelected, this, true);
	    if(nmdgf.byId(this.input).value != null) {
	    	this.autocomplete._bItemSelected = true;
	    }
	},
	
	itemSelected : function(event, data) {
		var label = data[2][0];
		var value = data[2][1];
		if(this.options.hidden !== undefined) {
			nmdgf.byId(this.options.hidden).value = value;
		}
	}
	
};
