//
// Copyright 2008, squawkfox.com
// All rights reserved
//



/*
 * Unit conversion object
 */
var convert = function() {
    return {
        kmToMiles: function(km) { return km / 1.609344; },
        milesToKm: function(miles) { return miles * 1.609344; },
        litresToGallons: function(litres) { return litres / 3.78541178; },
        gallonsToLitres: function(gallons) { return gallons * 3.78541178; },
        mpgToLPer100km: function(mpg) { return 100 * this.gallonsToLitres(1 / this.milesToKm(mpg)); },
        lPer100kmToMpg: function(lphk) { return  this.kmToMiles(1 / this.litresToGallons(lphk / 100)); }
    };
}();


/*
 * Formatting object
 */
var format = function() {
    return {
        formatCurrency: function(value) {
            
            var num = value.toString().replace(/\$|\,/g,'');

            if(isNaN(num)) {
                num = "0";
            }

            sign = (num == (num = Math.abs(num)));
            num = Math.floor(num*100+0.50000000001);
            cents = num%100;
            num = Math.floor(num/100).toString();
            if (cents < 10) {
                cents = "0" + cents;
            }
            for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
                num = num.substring(0,num.length-(4*i+3))+','+
                    num.substring(num.length-(4*i+3));
            }
            return (((sign)?'':'-') + '$' + num + '.' + cents);
        },

        round: function(value, decimals) {
            var exponent = Math.pow(10, decimals);
            return (Math.round(value * exponent) / exponent);
        }
    };
}();


/*
 * Cost Per Volume domain object.
 */
function CostPerVolume(value, volumeUnits) {
    var _cost = value;
    if (volumeUnits == "gallons") {
        _cost = value / convert.gallonsToLitres(1);
    }

    this.getCostPerLitre = function() {
        return _cost;
    }

    this.getTotalCost = function(volume) {
        return volume.getLitres() * _cost;
    }
}


/*
 * Distance domain object.
 */
function Distance(value, units) {
    var _km = value;
    if (units == "miles") {
        _km = convert.milesToKm(value);
    }

    this.getMiles = function() {
        return convert.kmToMiles(_km);
    }

    this.getKm = function() {
        return _km;
    }
}


/*
 * Volume domain object.
 */
function Volume(value, units) {
    var _litres = value;
    if (units == "gallons") {
        _litres = convert.gallonsToLitres(value);
    }

    this.getGallons = function() {
        return convert.litresToGallons(_litres);
    }

    this.getLitres = function() {
        return _litres;
    }

    this.getString = function(units) {
        if (units == "gallons") {
            return "" + format.round(this.getGallons(), 2) + " gal.";
        }
        else {
            return "" + format.round(this.getLitres(), 2) + " litres";
        }
    }
}


/*
 * Efficiency domain object.
 */
function Efficiency(value, units) {
    var _lphk = value;
    if (units == "mpg") {
        _lphk = convert.mpgToLPer100km(value);
    }

    this.getMpg = function() {
        return convert.lPer100kmToMpg(_lphk);
    }

    this.getLPer100km = function() {
        return _lphk;
    }

    this.totalFuelUsed = function(distance) {
        return new Volume(distance.getKm() * _lphk / 100, "l");
    }
}


/*
 * Cost-per-distance domain object.
 */
function CostPerUnitDistance(efficiency, costPerVolume) {
    var _costPerKm = efficiency.getLPer100km() / 100 * costPerVolume.getCostPerLitre();

    this.getCostPerKm = function() {
        return _costPerKm;
    }

    this.getCostPerMile = function() {
        return _costPerKm / convert.kmToMiles(1);
    }
}




