
YAHOO.namespace("gckAutocomplete");

YAHOO.gckAutocomplete.autocomplete = function(inputElem,containerElem,noResultsElem,workingGifElem,emptyBackgroundImage,filledBackgroundImage) {
	var dataSource = new YAHOO.widget.DS_XHR("/data/region-search.xml", ["Result", "Name", "Alias"]);
	var theInputElem = document.getElementById(inputElem);
	var theContainerElem = document.getElementById(containerElem);
	var noResultsMessage = document.getElementById(noResultsElem);
	var workingGif = document.getElementById(workingGifElem);
	var toggleBackground = function(){
		if(theInputElem.value === ''){
			if(emptyBackgroundImage){
				theInputElem.style.backgroundImage = "url(" + emptyBackgroundImage + ")";
			}
		}else{
			if(filledBackgroundImage){
				theInputElem.style.backgroundImage = "url(" + filledBackgroundImage + ")";
			}else{
				theInputElem.style.backgroundImage = 'none';
			}
		}
	}
	toggleBackground();
	YAHOO.util.Event.addListener(theInputElem, 'keyup', function(e){
		toggleBackground();
	});
	YAHOO.util.Event.addListener(theInputElem, 'click', function(e){
		if(filledBackgroundImage){
			theInputElem.style.backgroundImage = "url(" + filledBackgroundImage + ")";
		}else{
			theInputElem.style.backgroundImage = 'none';
		}
	});
	dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
	var chooseLocAutoComplete = new YAHOO.widget.AutoComplete(inputElem,containerElem,dataSource);
	chooseLocAutoComplete.animHoriz = false;
	chooseLocAutoComplete.animVert = false;	
	chooseLocAutoComplete.alwaysShowContainer = false;
	chooseLocAutoComplete.maxResultsDisplayed = 20;
	chooseLocAutoComplete.minQueryLength = 3;
	chooseLocAutoComplete.queryDelay = 0.2;//default is 0.2
	chooseLocAutoComplete.formatResult = function(aResultItem, sQuery) {
		return '<div style="padding:0px 5px 0px 5px; font-size: 11px; line-height: 15px;">' + aResultItem[0] + '</div>';
	}
	var isWaitingForResponse = 0;
	var hasValidResults = true;
	chooseLocAutoComplete.dataRequestEvent.subscribe(function(autoComplete){
		noResultsMessage.style.display = 'none';
		workingGif.style.display = 'block';
	});
	chooseLocAutoComplete.dataReturnEvent.subscribe(function(foo, aResults){
		workingGif.style.display = 'none';
		theContainerElem.style.visibility = 'visible';
		if(aResults[2].length == 0){
			hasValidResults = false;
			noResultsMessage.style.display = 'block';
			theContainerElem.style.visibility = 'hidden';
		}else{
			hasValidResults = true;
			noResultsMessage.style.display = 'none';
		}
	});
	chooseLocAutoComplete.containerCollapseEvent.subscribe(function(autoComplete){
		toggleWorkingGif();
		theContainerElem.style.visibility = 'hidden';
	});
	chooseLocAutoComplete.containerExpandEvent.subscribe(function(autoComplete){
		toggleWorkingGif();
		theContainerElem.style.visibility = 'visible';
	});
	chooseLocAutoComplete.itemSelectEvent.subscribe(function(autoComplete, selectedItem, selectedData){
		document.location = '/region/' + selectedItem[2][1];
	});

	var displaySearchByError = function () {
	  var panel =  new YAHOO.widget.Panel("error-explanation",
	                                      { width: "280px",
	                                      height: "125px", 
	                                      fixedcenter:true, 
							              close:true,  
							              draggable:false,  
							              modal:true, 
							              visible:false,
							              zindex:4
	                                      } );                         
	  panel.setBody("<div style='position:absolute;top:25px;left:30px;width:220px;height:75px;vertical-align:middle;'>" +
	  					"We are unable to process your request at this time. Please make sure that you have entered in a proper city, state or zip and try again." +
	  				"</div>"
	  				);
	  panel.render(document.body);
	  panel.show(); 
	}
	var zipCheckCallback = {
		success: function(o,p) {
			var data = YAHOO.lang.JSON.parse(o.responseText);
			if(data.alias === ""){
				displaySearchByError();
			}else{
				window.location = '/whats-nearby/' + data.zipcode;
			}
		},
		failure: function(o){
		}
	}
	var tryZipCode = function(){
		if(!hasValidResults){
			if(theInputElem.value.match(/[0-9][0-9][0-9][0-9][0-9]/)){
				YAHOO.util.Connect.asyncRequest('GET',"/data/location-finder.json?query=" + theInputElem.value,zipCheckCallback);
			}
		}
	}
	var kl = new YAHOO.util.KeyListener(theInputElem,{keys: 13},{fn:tryZipCode});
	kl.enable();
}
