/**
 * @author Fabrice fabrice@resto.be 10/06/2008
 */

function getAutoSuggest(inputid, containerid, serverscript, querystring, querydelay, accessor, customformatfunction)
{
	//first set the datasource
	var ds = new YAHOO.widget.DS_XHR(serverscript, accessor);
	ds.scriptQueryAppend = querystring;
	//the serverscript will return a flat file
	ds.responseType = YAHOO.widget.DS_XHR.TYPE_JSON;			
	var result = new YAHOO.widget.AutoComplete(inputid, containerid, ds);
	result.queryDelay = querydelay;
	result.useShadow = false;
	result.minQueryLength = 0;
	if(!customformatfunction)
	{
		result.formatResult = function(oResultItem, sQuery){return oResultItem[0]};			
	}
	else
	{
		result.formatResult = customformatfunction;		
	}			
	result.doBeforeExpandContainer = function(oTextbox, oContainer, sQuery, aResults) 
	{
 		var pos = YAHOO.util.Dom.getXY(oTextbox); 
	   	pos[1] += YAHOO.util.Dom.get(oTextbox).offsetHeight + 2; 
	    YAHOO.util.Dom.setXY(oContainer,pos);
		return true; 
	};
	
	//special for IE6
	result.containerExpandEvent.subscribe(onAutoCompleteContainerExpand);
	result.containerCollapseEvent.subscribe(onAutoCompleteContainerCollapse);
	
	
	return result;
}   	 

function bindValueTo(autosuggest, inputid)
{
	autosuggest.itemSelectEvent.subscribe(function(e, args){YAHOO.util.Dom.get(inputid).value = args[2][1];});		
}
