// <![CDATA[

hotelCityAuto = {};

hotelCityAuto.$txt = null;
hotelCityAuto.$airports = null;
hotelCityAuto.curIdx = 0;
hotelCityAuto.maxr = 10;
hotelCityAuto.minQueryLength = 3;
hotelCityAuto.aItems = null;

/*var $txt, $txt2, $airports, $airports2;
var to;

// Index of our currently selected/active autocomplete entry
var curIdx = 0;
// 	Max number of rows of suggestions
var maxr = 10;
var minQueryLength = 3;
//	The suggestions array we will be showing
//	Should be setup in our ajax callback, and sorted
// 	then will be used by populateSuggestionList
var aItems;*/

// Our final output variables
// should be set upon a sucessful selection of 
// a suggestion, or if a suggestion is not selected to whatever
// is in the input box upon loss of focus
var CityName = null;
var CountryName = null;
var RegionName = null;
var RegionCode = null;
var AirportCode = null;
// holds the actual typed value in the autosuggest field -- set to null
// when a valid selection has been set and the above city information
// variables contain accurate values
var fieldValue = null;


// move selection up in list
hotelCityAuto.goUp = function () {
	if(hotelCityAuto.curIdx > 0)
		hotelCityAuto.highlight(--hotelCityAuto.curIdx);
}

// move selection down in list
hotelCityAuto.goDown = function () {
	//alert(aItems.length);
	if(hotelCityAuto.curIdx < hotelCityAuto.aItems.length-1)
		hotelCityAuto.highlight(++hotelCityAuto.curIdx);
}

// update DOM to change visible selection, update hotelCityAuto.curIdx to point to the
// correct entry in aItems
hotelCityAuto.highlight = function (idx) {
	jQuery("#airports" + " a").removeClass("hlk");
	
	$lk = jQuery("#airports" + " a:eq(" + idx + ")");
	$lk.addClass("hlk");
	//hotelCityAuto.curIdx = $lk.get(0).id;
	hotelCityAuto.curIdx = idx;
}

// clear suggestion list DOM elements
hotelCityAuto.clearBoxes = function () {
    hotelCityAuto.noshim();

	hotelCityAuto.$airports.html("");
}

// select an airport from our suggestion list at index
hotelCityAuto.selectAirport = function (index){

	if( (hotelCityAuto.aItems != null && hotelCityAuto.aItems.length > 0) ){
	
		var item = hotelCityAuto.aItems[index];

		if(item.co == "United States" || item.co == "Canada"){
			t = item.cn + ", " + item.r + ", " + item.co;
		} else {
			t = item.cn + ", " + item.co;
		}
		hotelCityAuto.$txt.val(t);
		
		hotelCityAuto.resetCityGlobals();
		CityName = item.cn;
		CountryName = item.co;
		RegionName =  item.rn;
		RegionCode = item.r;

		hotelCityAuto.resetSuggestions();
	}
}

// reset globals
hotelCityAuto.resetCityGlobals = function (){
		CityName = null;
		CountryName = null;
		RegionName =  null;
		RegionCode = null;
		fieldValue = null;
}

// reset suggestion script state
hotelCityAuto.resetSuggestions = function (){
	hotelCityAuto.clearBoxes();
	//f = null;
	hotelCityAuto.aItems = null;
	hotelCityAuto.curIdx = null;
}

hotelCityAuto.shimit = function (obj,table) {

var shimobj=document.getElementById('myif').style;
shimobj.height=table.offsetHeight+'px';
shimobj.width=table.offsetWidth+'px';
shimobj.left=obj.offsetLeft+'px';
shimobj.top=obj.offsetTop+'px';
shimobj.zIndex='1';
shimobj.display='block';
//alert(shimobj.height + ' ' + shimobj.width + ' ' + shimobj.left + ' ' + shimobj.top);
}

hotelCityAuto.noshim = function () {
    var shimobj = document.getElementById('myif').style;
    shimobj.display = 'none';
}


// Setup our suggestion list given the array of aItems
hotelCityAuto.populateSuggestionList = function (aItems) {
	hotelCityAuto.clearBoxes();

	var expr = hotelCityAuto.$txt.val();
	h = "" ;
	
	if(aItems.length) {
		for(var i=0; i< aItems.length; i++) {
			
			var item = aItems[i];
			
			var cn = item.cn;
			var r = item.r;
			var rn = item.rn;
			var co = item.co;
			
		  var matchStr = new RegExp("^(" + expr + ")", "i");		
		  
		  /*
			var hcn = cn.replace(matchStr, "<span class='hc'>$1</span>");
			var hr = r.replace(matchStr, "<span class='hc'>$1</span>");
			var hrn = rn.replace(matchStr, "<span class='hc'>$1</span>");
			var hco = co.replace(matchStr, "<span class='hc'>$1</span>");
			*/
			var hcn = cn;
			var hr = r;
			var hrn = rn;
			var hco = co;
			
			if(co == "United States" || co == "Canada"){
				t = hcn + ", " + hr + ", " + hco;
			} else {
				t = hcn + ", " + hco;
			}
		  
			var t = t.replace(matchStr, "<span class='hc'>$1</span>");

			lk = "<a class='lk' id='" + i + "' href='javascript:void(0)' onclick='javascript:hotelCityAuto.selectAirport(" + i + ")' onmouseover='hotelCityAuto.highlight(" + i + ",true)'>" + t + "</a>";

			h += "<tr><td nowrap='nowrap'>" + lk + "</td></tr>";
		}

        var x = getScreenX(document.searchForm.where, document.getElementById("airports").offsetParent)+'px';
        var y = getScreenY(document.searchForm.where, document.getElementById("airports").offsetParent)+document.getElementById("where").offsetHeight+'px';

        document.getElementById('airports').style.left = x;
        document.getElementById('airports').style.top = y;

	    h = "<table id='airportsTable'>" + h + "</table>";

        hotelCityAuto.$airports.html(h);
		
        hotelCityAuto.shimit(document.getElementById('airports'),
               document.getElementById('airportsTable'));
               

		hotelCityAuto.highlight(0);
		
	}
	
};

// request matches from the server, if matches are found
// setup aItems and process our suggestionList
hotelCityAuto.startSearch = function (query){
	if(query.length >= hotelCityAuto.minQueryLength){
		query = query.replace(/ /, "%20");
		var rand = Math.random();
		var queryStr = "auto/citygz.php?q="+query+"&rand="+rand;
		jQuery.get(queryStr, function(data){
			// first verify that the result we're processing still
			// applies to what has been input
			queriedVal = data.documentElement.getAttribute('q');
			if(hotelCityAuto.$txt.val() != queriedVal){
				return;
			}
			
			// now map all our "a" elements to JS objects
			if(data.documentElement.childNodes.length > 0){
				hotelCityAuto.aItems = new Array();
				var z = 0;
				for(i=0; i<data.documentElement.childNodes.length; i++){
					var a = data.documentElement.childNodes.item(i);
					z++;
					var thisItem = {
						'cn' :  a.getAttribute('cn'),
						'r' :  a.getAttribute('r'),
						'rn' : a.getAttribute('rn'),
						'co' :  a.getAttribute('co'),
						'pop' : a.getAttribute('pop')
					};
					hotelCityAuto.aItems.push(thisItem);
				}

				hotelCityAuto.populateSuggestionList(hotelCityAuto.aItems);
				
			} else {
				hotelCityAuto.clearBoxes();
			}
		});
	} else {
		hotelCityAuto.resetSuggestions();
	}
}


function enableCityAC() {

	hotelCityAuto.$txt = jQuery("#where");
	hotelCityAuto.$airports = jQuery("#airports");

	hotelCityAuto.$airports.html("");
	hotelCityAuto.$txt.val("");
	hotelCityAuto.$txt.get(0).disabled = false;

	hotelCityAuto.$txt.keyup(function(e) {
        if (!document.searchForm.city.checked)
        {	
            return;
        }

		f = true;

		var k = e.keyCode;

		switch(k) {
			case 40:
				hotelCityAuto.goDown();
				break;

			case 38:
				hotelCityAuto.goUp();
				break;

			case 13:
				hotelCityAuto.selectAirport(hotelCityAuto.curIdx);
				break;

			default:
				hotelCityAuto.resetCityGlobals();
				fieldValue = hotelCityAuto.$txt.val();
				
				hotelCityAuto.curIdx = null;
				hotelCityAuto.startSearch(hotelCityAuto.$txt.val());
		}
		e.stopPropagation();
	});
	
	hotelCityAuto.$txt.focus(function(e) {
		f = true;
	});
	
	hotelCityAuto.$txt.blur(function(e) {
		hotelCityAuto.selectAirport(hotelCityAuto.curIdx);
	});
}






// ]]>

