﻿
// Airport : Represents an airport

Airport = createClass();
Airport.prototype.init = function(args) {
    /*
        code	: airport code
		name	: airport name
		alt     : alternate airport name
    */
	defaults = { code: "", name: "", short: "" };
	for(var pr in defaults)
	    this[pr] = isDefined(args[pr]) ?args[pr] :defaults[pr];

	// Load details from json (usually downloaded from server)
	if(!(args instanceof Airport)) {
	    this.code = args.c;
	    this.name = args.n;
	    if(isDefined(args.s))
		    this.short = args.s;
        else 
            this.short = this.name;
    }
};

// Airports : Represents a collection of airports 

Airports = createSingleton(Airports = {});
Airports.prototype.init = function(args) {
	this.list = {};
    
    // Add an airport to list
    this.add = function(a) {
        this.list["_" + a.code] = a;
    };
    
	// Load details from json (usually downloaded from server)
	var arr = isArray(args) ?args :args.airports;
	for(var i=0; i<arr.length; i++)
		this.add(new Airport(arr[i]));
};

// Static function to get airport obj from code
Airports.get = function(code, pr) {
	var list = Airports.getInstance().list;
	var obj = list["_" + code];
	if(isDefined(obj)) {
		if(isDefined(pr))
			obj = obj[pr];
	}
	else
	    obj = code;
	return obj;
};
