﻿
// Duration : Represents flight duration

Duration = createClass();
Duration.prototype.init = function(args) {
    /*
        hours	: number of hours
		minutes	: number of minutes
		seconds : number of seconds
    */
	defaults = { hours: 0, minutes: 0, seconds: 0 };
	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 Duration)) {
		if(isDefined(args.s))
		    this.seconds = args.s;
        if(isDefined(args.m))
	        this.minutes = args.m;
        if(isDefined(args.h))	        
	        this.hours = args.h;
    }
	
	// Duration is equal to zero or not
	this.isZero = function() {
	    return this.hours == 0 && this.minutes == 0 && this.seconds == 0;
	};
    
    // Get a property/aggregated value
    this.get = function(pr) {
	    if(/minutes/i.test(pr))
    		return (this.hours*60)+this.minutes;
    };	
    
    this.add = function(d2) {
        var total = this.get('minutes') + d2.get('minutes');
        this.hours = parseInt(total / 60);
        this.minutes = total % 60;
    };
};