// <![CDATA[

carCityAuto = {};

carCityAuto.$txt = null;
carCityAuto.$txt2 = null;
carCityAuto.$airports = null;
carCityAuto.$airports2 = null;
carCityAuto.curIdx = 0;
carCityAuto.maxr = 10;
carCityAuto.minQueryLength = 3;
carCityAuto.aItems = null;
carCityAuto.f = 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
carCityAuto.goUp = function () {
	if(carCityAuto.curIdx > 0)
		carCityAuto.highlight(--carCityAuto.curIdx);
}

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

// update DOM to change visible selection, update carCityAuto.curIdx to point to the
// correct entry in aItems
carCityAuto.highlight = function (idx) {
	var s = carCityAuto.f ? "" : "2";

	jQuery("#airports" + s + " a").removeClass("hlk");
	
	$lk = jQuery("#airports" + s + " a:eq(" + idx + ")");
	$lk.addClass("hlk");
	//carCityAuto.curIdx = $lk.get(0).id;
	carCityAuto.curIdx = idx;
}

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

	carCityAuto.$airports.html("");

    if (carCityAuto.$airports2)
        carCityAuto.$airports2.html("");
}

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

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

		if(item.co == "United States" || item.co == "Canada"){
			t = item.cn + ", " + item.r + ", " + item.co;
		} else {
			t = item.cn + ", " + item.co;
		}

        if (carCityAuto.f) {
		    carCityAuto.$txt.val(t);
        } else {
            carCityAuto.$txt2.val(t);
        }
		
		carCityAuto.resetCityGlobals();
		CityName = item.cn;
		CountryName = item.co;
		RegionName =  item.rn;
		RegionCode = item.r;

		carCityAuto.resetSuggestions();
	}
}

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

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

carCityAuto.shimit = function (obj,table,theIf) {

var shimobj=theIf.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);
}

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

    var shimobj = document.getElementById('myif2');
    if (shimobj)    
        shimobj.style.display = 'none';
}


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

	var expr = carCityAuto.f ? carCityAuto.$txt.val() : carCityAuto.$txt2.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:carCityAuto.selectAirport(" + i + ")' onmouseover='carCityAuto.highlight(" + i + ",true)'>" + t + "</a>";

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

		if(carCityAuto.f) {
            var x = getScreenX(document.searchForm.pickup, document.getElementById("airports").offsetParent)+'px';
            var y = getScreenY(document.searchForm.pickup, document.getElementById("airports").offsetParent)+document.getElementById("pickup").offsetHeight+'px';

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

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

			carCityAuto.$airports.html(h);

            carCityAuto.shimit(document.getElementById('airports'), 
                   document.getElementById('airportsTable'),
                   document.getElementById('myif'));
        }
		else {
            var x = getScreenX(document.searchForm.dropoff, document.getElementById("airports2").offsetParent)+'px';
            var y = getScreenY(document.searchForm.dropoff, document.getElementById("airports2").offsetParent)+document.getElementById("dropoff").offsetHeight+'px';

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

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

			carCityAuto.$airports2.html(h);

            carCityAuto.shimit(document.getElementById('airports2'), 
                   document.getElementById('airports2Table'),
                   document.getElementById('myif2'));
		}

		carCityAuto.highlight(0);
		
	}
	
};

// request matches from the server, if matches are found
// setup aItems and process our suggestionList
carCityAuto.startSearch = function (query){
	if(query.length >= carCityAuto.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((carCityAuto.f && carCityAuto.$txt.val() != queriedVal) ||
               (!carCityAuto.f && carCityAuto.$txt2.val() != queriedVal)){
				return;
			}
			
			// now map all our "a" elements to JS objects
			if(data.documentElement.childNodes.length > 0){
				carCityAuto.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')
					};
					carCityAuto.aItems.push(thisItem);
				}

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

carCityAuto.setupTxt1 = function () {

	carCityAuto.$txt = jQuery("#pickup");
	carCityAuto.$airports = jQuery("#airports");
	carCityAuto.$airports.html("");
	carCityAuto.$txt.val("");
	carCityAuto.$txt.get(0).disabled = false;

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

		carCityAuto.f = true;

		var k = e.keyCode;

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

			case 38:
				carCityAuto.goUp();
				break;

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

			default:
				carCityAuto.resetCityGlobals();
				fieldValue = carCityAuto.$txt.val();
				
				carCityAuto.curIdx = null;
				carCityAuto.startSearch(carCityAuto.$txt.val());
		}
		e.stopPropagation();
	});

	carCityAuto.$txt.focus(function(e) {
		carCityAuto.f = true;
	});
	
	carCityAuto.$txt.blur(function(e) {
		carCityAuto.selectAirport(carCityAuto.curIdx);
	});
}

carCityAuto.setupTxt2 = function () {

    carCityAuto.$txt2 = jQuery("#dropoff");
    carCityAuto.$airports2 = jQuery("#airports2");

    carCityAuto.$airports2.html("");
    carCityAuto.$txt2.val("");
    carCityAuto.$txt2.get(0).disabled = false;
	
	carCityAuto.$txt2.keyup(function(e) {
        if (!document.searchForm.city.checked)
            return;

		carCityAuto.f = false;

		var k = e.keyCode;

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

			case 38:
				carCityAuto.goUp();
				break;

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

			default:
				carCityAuto.resetCityGlobals();
				fieldValue = carCityAuto.$txt2.val();
				
				carCityAuto.curIdx = null;
				carCityAuto.startSearch(carCityAuto.$txt2.val());
		}
		e.stopPropagation();
	});


    carCityAuto.$txt2.focus(function(e) {
        carCityAuto.f = false;
    });

    carCityAuto.$txt2.blur(function(e) {
        carCityAuto.selectAirport(carCityAuto.curIdx);
    });
}






// ]]>

