/** Map.Search
* A Singleton class that manages the auto-complete "search" text input, so that markers can be displayed by partial name match.
* 
* @class Map.Search
* @singleton
* @returns {void}
*/
Map.Search = new function() {

	this.init = function() {
		this.autoCompleteInput = document.getElementById( 'autoCompleteInput' );
		this.autoCompleteInput.value = "";
		this.autoCompleteInputAsWatermark = new WatermarkField( this.autoCompleteInput, "Search for location..." );
				
		// Instantiate the datasource.  Use an XHRDataSource (XMLHttpRequest, i.e. AJAX)
		this.datasource = new YAHOO.util.XHRDataSource( "data/getSearchData.php" );
		this.datasource.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT;		// Set the responseType
		this.datasource.responseSchema = {		// Define the schema of the delimited results
			recordDelim: "\n",
			fieldDelim: "\t"
		};
		this.datasource.maxCacheEntries = 5;		// Enable caching
		
		
		// Instantiate the AutoComplete
		this.autoComplete = new YAHOO.widget.AutoComplete( this.autoCompleteInput, "myContainer", this.datasource );
		this.autoComplete.useIFrame = true;
		this.autoComplete.minQueryLength = 1; 
		this.autoComplete.autoHighlight = true;  // Automatically highlight the first item in the results
		this.autoComplete.generateRequest = function( sQuery ) {
			return "?q=" + sQuery ;
		};
		this.autoComplete.resultTypeList = false;
		this.autoComplete.formatResult = function( oResultData, sQuery, sResultMatch ) {
			return '<div class="result"><span class="name"><img src="' + oResultData[0] + '" />' + oResultData[ 1 ] + '</span></div>';
		}
		// Function to handle the selection of an item
		var itemSelectHandler = function( sType, aArgs ) {
			var mapItemID = aArgs[ 2 ][ 2 ];
			Map.showMapItemInfoWindow( null, mapItemID );
			this.autoCompleteInputAsWatermark.clear();		// Clear the autocomplete text field
			
			// Blur the textbox after 100 milliseconds.  For some reason, blurring it immediately causes the autocomplete to not work
			YAHOO.lang.later( 100, this.autoCompleteInputAsWatermark, this.autoCompleteInputAsWatermark.blur, null, false );
		}
		this.autoComplete.itemSelectEvent.subscribe( itemSelectHandler, this, true ); 		// Subscribe to the autocomplete's itemSelectEvent
	}

}