var airport_code_to_full_name = new Object();  // a map of airport code to airport city name

var o2d_map = new Object();    // a map of the form { origin_1 -> [destination_1, dest_2, .. ], origin_2 -> [dest_N, ...] } 
var d2o_map = new Object();   // a map of the form { dest_1 -> [origin_1, origin_2, .. ], dest_N -> [origin_2, ...] }

var all_destinations = new Array();
var all_origins = new Array();

// JSON objects for the above
var o2d_map_json_objs = new Object();
var d2o_map_json_objs = new Object();

var all_destinations_json_objs = new Array();
var all_origins_json_objs = new Array();

// IE missing indexOf fix
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

function airport(c,n,d,cty,p,cn) {
	this.code = c;
	this.name = n;
	this.dests = d;
	this.city = cty;
	this.province = p;
	this.country = cn;
	
	this.display	= function ()
	{
		var msg		=  "Airport: "
					+ "\n   code: " + this.code
					+ "\n   name: " + this.name
					+ "\n   country: " + this.country
					+ "\n   dests: ";
		for (var i=0; i < this.dests.length; i++)
		{
			msg		+= "'" + this.dests[i] + "', ";
		}
		alert( msg );
	};
}

function airportsJSGetAirport(code) {
	return Airports[code];
}

function sortByAirportName(a, b) {
	var x = a.name.toLowerCase();
	var y = b.name.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByDisplayListName(a, b) {
	var x = a.text.toLowerCase();
	var y = b.text.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

// for a list of typeahead airport options, set the '.index' member to
// match position in the list.  
function reindexTypeAheadAirportOptions(tao_list) {
	for(var i=0;i<tao_list.length;i++) {
		tao_list[i].index = i;
	}
}

function captureSearchPrefs(searchPrefs) {
	// config settings here
}

function captureDatePrefs(datePrefs)
{
}

function captureDateText(dateText) {
	dateText.datesOverlap = "Please make sure that your outbound date is not later than your return date.\nThe date you modified has been reset.\nPlease re-select your date.";
}

function newAirportJSONObj(code) {
    var o = new Object();
    o.code = code;
    o.name = airport_code_to_full_name[code];

    return o;
}

function ensureAjaxDataLoad() {
    var locale = getPathParam(1);
    var apts = new Array();
        
    $.ajax({
        type: 'GET',
        url: "/data/jsp/airports_list.jsp?locale="+locale,
        dataType: 'json',
        success: function(data){
          for( var i=0; i<data.length; i++ ) {
		airport_code_to_full_name[data[i].code] = data[i].name;
		apts.push( new airport( data[i].code, 
			data[i].name,
			null,	           // destinations from seasonality 
			"#CITY", "#STATE", "#COUNTRY"));
          }
          apts.sort
        },
        data: {},
        async: false
    });

    $.ajax({
        type: 'GET',
        url: "/data/jsp/seasonality.jsp",
        dataType: 'json',
        data: {},
        async: false,
        success: function(data){
            seasonalityData=data.seasonality; 
            
            // assemble the lists of origin to destinations
            o2d_map = new Object();
            d2o_map = new Object();
                        
            var tmp_airport=null;
            for( var i=0; i < seasonalityData.airports.length; i++ ) {
                tmp_airport = seasonalityData.airports[i];
				
				if(tmp_airport.valid_orig)
				{
                	all_origins.push(tmp_airport.code);
                }

                // pull origin directly from seasonality
                o2d_map[tmp_airport.code] = tmp_airport.routes;
                
                // create destination map by flipping the origin one
                for( var j=0; j < tmp_airport.routes.length; j++ ) {
                    var tmp_airport_codes_list = null;
                    
                    // get the array of destinations (possibly new)
                    if(d2o_map[tmp_airport.routes[j]] != null) {
                        tmp_airport_codes_list = d2o_map[tmp_airport.routes[j]];                             
                    } else {
                        tmp_airport_codes_list = new Array();
                    }
    
                    // do not insert duplicates                
                    if(tmp_airport_codes_list.indexOf(tmp_airport.code) == -1)
                        tmp_airport_codes_list.push(tmp_airport.code);

                    // store the resulting map
                    d2o_map[tmp_airport.routes[j]] = tmp_airport_codes_list;
                    
                    // add to the list of all destinations
                    if(all_destinations.indexOf(tmp_airport.routes[j]) == -1)
                        all_destinations.push(tmp_airport.routes[j]);    
                }
            }            
        }
    });
    
    return apts.sort(sortByAirportName);
}

var Airports = ensureAjaxDataLoad();
