var Price = Class.create(Model, {
    view: ({
        modelView: function() {
            var price = this.object;

            var className = "";
            var sign = "";
            if (price.isBestPrice()) {
                className = "bestPrice";
                sign = "Best Price";
            } else if (price.isBestFare()) {
                className = "bestFare";
                sign = "Best Fare";
            }
            var priceElement = new Element("div").addClassName("price")
                             .addClassName(className)
                             .appendChild(
                                new Element("div").addClassName(price.detailsExists() ? "details" : false)
                                    .appendChild(new Element("span").update(sign).addClassName("sign")).parentNode
                                    .appendChild(new Element("br")).parentNode
                                    .appendChild(
                                        new Element("a", {
                                            target: "_blank",
                                            href: price.getDeepLink()
                                        }).update(price.getValue())
                                  ).parentNode
                              ).parentNode;
            if (price.isBestPrice()) {
                priceElement.addClassName("bestPrice");
            } else if (price.isBestFare()) {
                priceElement.addClassName("bestFare");
            }
            return priceElement;
        }
    }),
    id: false,
    initialize: function() {
    },
    getValue: function(returnFloat, withCurrency) {
        var withCurrency = withCurrency || false;
        var value = parseInt(Math.round(this.price));
        if (returnFloat) {
            return parseFloat(value);
        }
        if (withCurrency) {
            value = this.getCurrency() + " " + value;
        }
        return value;
    },
    getCurrency: function() {
        if (this.details) {
            return this.details['currency'];
        }
        return false;
    },
    getVendor: function() {
        return this.vendor;
    },
    getAirline: function() {
        if (this.details) {
            var airlineCode = this.details.outbound.airline;
            return this._getStorage().getAirlineByCode(airlineCode);
        }
        return new Airline("", "");
    },
    getDeepLink: function() {
        var id = this.getVendor().getId();
        var link = encodeURIComponent(this.deepLink);
        var url = "/ppc.php?supplier_id=" + id
                + "&area=flights&redirect=" + link
                + "&iata=" + Page.getInstance().getForm().getIATA("destinationAirport");
        var storage = Page.getInstance().getFlights().getData();
        var extUrl = $H({priceId: this.getId(),
                         bestPriceId: storage.getBestPrice().getId(),
                         currency: storage.getData('currency'),
                         sessionId: storage.getData('sessionId')}).toQueryString();
        url = url + '&' + extUrl;
        return url;
    },
    setId: function(id) {
        this.id = id;
    },
    getId: function() {
        return this.id;
    },
    detailsExists: function() {
        if (typeof(this._detailsExists) == "undefined") {
            this._detailsExists = false;
            $A(['outbound', 'inbound']).each(function(direction) {
                if (direction == 'inbound' && typeof(this.details[direction]) == "undefined") {
                    return;
                }
                for (key in this.details[direction]) {
                    if (key == "airline") {
                        continue;
                    }
                    var value = this.details[direction][key];
                    if (value && value != "n/a") {
                        this._detailsExists = true;
                        return;
                    }
                }
            }.bind(this));
        }
        return this._detailsExists;
    },
    getDetails: function(direction) {
        var direction = direction || false;
        if (!this.details) {
            return false;
        }
        if (!this.details[direction]) {
            return false;
        }
        for (key in this.details) {
            for (order in this.details[key]) {
                if (this.details[key][order] == null) {
                    this.details[key][order] = 'n/a';
                }
            }
        }
        switch (direction) {
            case "outbound":
                return this._getDirectionDetails('outbound');
                break;
            case "inbound":
                return this._getDirectionDetails('inbound');
                break;
            default:
                return this.details;
                break;
        }
    },
    _getDirectionDetails: function(direction) {
        var data = Page.getInstance().getFlights().getData();
        var details = {};
        for (key in this.details[direction]) {
            details[key] = this.details[direction][key];
        }
        if (details['airline'] != "n/a") {
            var airlineCode = details['airline'];
        } else {
            var airlineCode = this.details['outbound']['airline'];
        }
        var airline = data.getAirlineByCode(airlineCode);
        details['airline'] = airline ? airline.getName() : "";
        try {
            details['airport'] = data.getData((direction == 'outbound') ? 'departure' : 'destination');
            details['date']    = data.getData("datesString").split(" - ")[direction == "outbound" ? 0 : 1];
        } catch (e) {
        }
        return details;
    },
    getVendor: function() {
        return this._getStorage().getVendorByCode(this.vendor);
    },
    isEqual: function(price) {
        if (! price) {
            throw "Price object is required";
        }
        try {
            if (this.getVendor().getCode() == price.getVendor().getCode() &&
                this.getAirline().getCode() == price.getAirline().getCode() && 
                this.getValue() == price.getValue()) {
                return true;
            }
            return false;
        } catch (e) {
            dispatchException(e);
            return false;
        }
    },
    isBestPrice: function() {
        var bestPrice = this._getStorage().getBestPrice();
        return this.isEqual(bestPrice);
    },
    isBestFare: function() {
        var airline  = this.getAirline();
        var bestFare = this._getStorage().getAirlineBestFare(airline); 
        if (this.getValue() == bestFare.getValue() &&
            this.getAirline().getCode() == bestFare.getAirline().getCode()) {
             return true;
        }
        return false;
    },
    getUniqueId: function() {
        return this.getVendor().getCode() + "-" +  this.getAirline().getCode() + "-" + this.getValue()
    }
});
