﻿
// Flight : Represents one flight

Flight = createClass();
Flight.prototype.init = function(args) {
    /*
        airlineCode		: Carrier airline
        flightNumber	: Flight number
        originCode		: Origin airport code
        destinationCode	: Destination airport code
        fareBasis       : Fare-basis for fare
        stops           : Number of stops 
		deptTime		: Departure time
		arrvTime		: Arrival time
		duration		: Flight duration
    */
	defaults = { airlineCode: "", flightNumber: "", originCode: "", destinationCode: "", 
	             fareBasis: "", stops: 0, deptTime: null, arrvTime: null, duration: null };
	for(var pr in defaults)
		this[pr] = isDefined(args[pr]) ?args[pr] :defaults[pr];

	// Load details from fare json (usually downloaded from server)
	if(!(args instanceof Flight)) {
	    
	    var arr = args.f.split("-");
	    this.airlineCode = arr[0];
	    this.flightNumber = arr[1];
	    this.originCode = args.o;
	    this.destinationCode = args.d;
	    if(isDefined(args.fb))
	        this.fareBasis = args.fb;
	    this.stops = args.s;
	    this.deptTime = Util.getDate(args.dt);
	    this.arrvTime = Util.getDate(args.at);
	    this.duration = new Duration(args.du);
    }

    // Get property/aggregated value
    this.get = function(pr) {
	    var value = null; 
	    if(/flightNumber/i.test(pr))
		    value = this.airlineCode + "-" + this.flightNumber;
	    else if(/origin/i.test(pr))
		    value = this.originCode;
	    else if(/destination/i.test(pr))
		    value = this.destinationCode;
	    else if(/dept/i.test(pr))
		    value = this.deptTime;
	    else if(/arrv/i.test(pr))
		    value = this.arrvTime;
	    else if(isDefined(this[pr]))
		    value = this[pr];
	    return value;
    };
};
