// <![CDATA[

// autocomplete code for hotelsearch.php

hotelAirportAuto = {};

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

/*var $txt, $airports;
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;
//var rs;
// 	Select "From" airport active, false implies airport 2
//var f = null;
//	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 fromCode = "";
// toCode is unused for the hotel application
//var toCode = "";

// tracks if our codes have been set using autocomplete since the last
// autocomplete request for that field -- if this is false when the 
// respective input is deselected we need to use the input value
// as the code
var fromSet = false;
// toSet is unused for the hotel application
//var toSet = false;

// move selection up in list
hotelAirportAuto.goUp = function () {
// unnecessary for hotels -- hotels only has one destination field, not two
//	var s = f ? "" : "2";

	if(hotelAirportAuto.curIdx > 0)
		hotelAirportAuto.highlight(--hotelAirportAuto.curIdx);
}
// move selection down in list
hotelAirportAuto.goDown = function () {
//	var s = f ? "" : "2";

	if(hotelAirportAuto.curIdx < hotelAirportAuto.maxr)
		hotelAirportAuto.highlight(++hotelAirportAuto.curIdx);
}

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

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

// clear suggestion list DOM elements
hotelAirportAuto.clearBoxes = function () {

    hotelAirportAuto.noshim();

	hotelAirportAuto.$airports.html("");
//	$airports2.html("");
}

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

	if( (hotelAirportAuto.aItems != null && hotelAirportAuto.aItems.length > 0) ){
		var code = hotelAirportAuto.aItems[index].c;

		hotelAirportAuto.$txt.val(hotelAirportAuto.aItems[index].n + " - " + hotelAirportAuto.aItems[index].an + " (" + hotelAirportAuto.aItems[index].c + ")");
		fromCode = code;
		fromSet = true;

		hotelAirportAuto.resetSuggestions();
	} else {
	/*
		if(f){
			if(!fromSet) fromCode = $txt.val();
		} else {
			if(!toSet) toCode = $txt2.val();
		}
	*/
		hotelAirportAuto.resetSuggestions();
	}
}

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

hotelAirportAuto.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);
}

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

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

	var expr = hotelAirportAuto.$txt.val();
	h = "" ;
	
	if(aItems.length) {
		for(var i=0; i< aItems.length; i++) {
			var item = aItems[i];
			
			var c = item.c;
			var n = item.n;
			var an = item.an;
			
		  var matchStr = new RegExp("^(" + expr + ")", "i");		
		  
			var hc = c.replace(matchStr, "<span class='hc'>$1</span>");
			var hn = n.replace(matchStr, "<span class='hn'>$1</span>");
			var han = an.replace(matchStr, "<span class='hn'>$1</span>");;

			t = hn + " - " + han + " (" + hc + ")";

			lk = "<a class='lk' id='" + i + "' href='javascript:void(0)' onclick='javascript:hotelAirportAuto.selectAirport(" + i + ")' onmouseover='hotelAirportAuto.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>";

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

		hotelAirportAuto.highlight(0);
		
	}
	
};

// request matches from the server, if matches are found
// setup aItems and process our suggestionList
hotelAirportAuto.startSearch = function (query){
	if(query.length >= hotelAirportAuto.minQueryLength){
		query = query.replace(/ /, "%20");
		var queryStr = "auto/gz.php?q="+query;
		jQuery.get("auto/gz.php?q="+query, function(data){
			// first verify that the result we're processing still
			// applies to what has been input

			queriedVal = data.documentElement.getAttribute('q');
			if(hotelAirportAuto.$txt.val() != queriedVal){
				return;
			}

			// now map all our "a" elements to JS objects
			if(data.documentElement.childNodes.length > 0){
				hotelAirportAuto.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 = {
						'c' :  a.getAttribute('c'),
						'n' :  a.getAttribute('n'),
						'an' : a.getAttribute('an'),
						'p' :  a.getAttribute('p')
					};
					hotelAirportAuto.aItems.push(thisItem);
				}
				hotelAirportAuto.aItems.sort(function (a, b) {
						var pLess = b.p - a.p;
						var anLess = a.an < b.an;
						if(pLess != 0){
							return pLess;
						} else {
							return (anLess) ?  -1 : 1;
						}
					});
					var previousItem = null;
					var i=0;
					while(i < hotelAirportAuto.aItems.length){
						if(previousItem != null && previousItem.c == hotelAirportAuto.aItems[i].c){
							hotelAirportAuto.aItems.splice(i, 1);	
						} else {
							previousItem = hotelAirportAuto.aItems[i];
							i++;
						}
					}
				if(hotelAirportAuto.aItems.length > hotelAirportAuto.maxr){
					hotelAirportAuto.aItems = hotelAirportAuto.aItems.slice(0, hotelAirportAuto.maxr);
				}
				hotelAirportAuto.populateSuggestionList(hotelAirportAuto.aItems);
				
				fromSet = true;
				//toSet = !f ? true : toSet;
			} else {
				fromSet = false;
				//toSet = !f ? false : toSet;
				hotelAirportAuto.clearBoxes();
			}
		});
	} else {
		hotelAirportAuto.resetSuggestions();
	}
}

// function is inherently unused -- for debugging purposes
/*function showMessage(isfrom) { // parameter is useless for hotel application
	var s, nm, pos;

	s = "origin";
	nm = $txt.val();
	pos = 1;
	$ap = $airports;

	nm = " '" + nm + "'";
	var msg = "Unrecognized " + s + " airport. Please check and reenter.";
	$ap.html("<table><tr><td class='error'>" + msg + "</td></tr></table>");
}*/


function enableAirportAC() {

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

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

	hotelAirportAuto.$txt.keyup(function(e) {

        // supposed to be using city autosuggest, not airport
        if (!document.searchForm.airport.checked)
            return;

		var k = e.keyCode;

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

			case 38:
				hotelAirportAuto.goUp();
				break;

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

			default:
				fromCode = hotelAirportAuto.$txt.val();
				hotelAirportAuto.curIdx = null;
				hotelAirportAuto.startSearch(hotelAirportAuto.$txt.val());
		}
		e.stopPropagation();
	});
	
	hotelAirportAuto.$txt.blur(function(e) {
		hotelAirportAuto.selectAirport(hotelAirportAuto.curIdx);
	});
}






// ]]>
