//
// JSON driven area autocomplete (for oceanic's external booking engine)
//

YAHOO.namespace("YAHOO.Ezrez");
		
YAHOO.Ezrez.autocomplete = function(searchURL, config, textFieldId, resultsContainerId, hiddenFieldId, maxResults) {
	this.dataSource = new YAHOO.widget.DS_ScriptNode(searchURL, config.SEARCH_SCHEMA);
	this.dataSource.responseType = config.RESPONSE_TYPE;
	this.dataSource.queryMatchSubset = config.QUERY_MATCH_SUBSET;
	
	this.widget = new YAHOO.widget.AutoComplete(textFieldId, resultsContainerId, this.dataSource);
	this.widget.minQueryLength = config.MIN_QUERY_LENGTH; 
	this.widget.queryDelay = config.QUERY_DELAY;
	this.widget.maxResultsDisplayed = maxResults;
	this.widget.forceSelection = config.FORCE_SELECTION;
	this.widget.useShadow = config.USE_SHADOW;
	this.widget.animHoriz  = config.ANIM_HORIZ;
	this.widget.animSpeed = config.ANIM_SPEED;
	this.widget.useIFrame = config.USE_IFRAME;
	this.widget.itemSelectEvent.subscribe(YAHOO.Ezrez.autocomplete.selectHandler, hiddenFieldId);
	this.widget.textboxFocusEvent.subscribe(YAHOO.Ezrez.autocomplete.textboxFocusHandler, textFieldId);
}

YAHOO.Ezrez.autocomplete.selectHandler = function(sType, aArgs, hiddenFieldId) {
	YAHOO.log(sType); //this is a string representing the event; e.g., "itemSelectEvent"
	var aData = aArgs[2]; //array of the data for the item as returned by the DataSource 
	document.getElementById(hiddenFieldId).value = aData[1];
}

YAHOO.Ezrez.autocomplete.textboxFocusHandler = function(sType, aArgs, textFieldId) {
	YAHOO.log(sType); //this is a string representing the event; e.g., "textboxFocusEvent"
	document.getElementById(textFieldId).value = '';
}

YAHOO.Ezrez.autocomplete.globalConfig =  {
	SEARCH_SCHEMA : ['rs.m', 'd', 's'],
	QUERY_MATCH_SUBSET : true,
	MIN_QUERY_LENGTH : 3,
	QUERY_DELAY : 0.05,
	ANIM_SPEED : 0.15,
	FORCE_SELECTION : false,
	USE_SHADOW : true,
	ANIM_HORIZ : true,
         USE_IFRAME : true,
	RESPONSE_TYPE : YAHOO.widget.DS_XHR.TYPE_JSON
}

