var Compare = Class.create();

Object.extend(Compare.prototype, {
    initialize: function() {
        try {
            /*if (Cookie.get("compare_prices") == null || Cookie.get("compare_prices").length == 0) {
                window.history.back();
            }*/
            try {
                this.data = new Data();
            } catch (e) {
                dispatchException(e);
            }
            this.prices = $A();
            this.airlineCodes = $A();
            this.airlines = $A();
            this.priceIds = getQueryVariable("prices").split(",");
            this.view = new CompareView();
        } catch (e) {
            window.history.back();
        }
    },

    fetchPricesData: function() {
        this.ids = this.getPriceIds();
    },

    fetchPriceData: function() {
        var url = "/flights/?action=data";

        var options = {
            method: "post",
            parameters: {
                ids: this.getPriceIds().join(",")
            },
            onSuccess: this.onPriceRequestSuccess.bind(this),
            onFailure: this.onPriceRequestFailure.bind(this)
        };

        this.view.highlight(true);

        new Ajax.Request(url, options);
    },

    getPriceIds: function() {
        return this.priceIds;
    },

    onPriceRequestSuccess: function(transport, params) {
        var searchResponse = transport.responseJSON;
        $A(searchResponse.prices).each(function(priceData) {
            var price = Object.extend((new Price()), priceData);
            this.prices.push(price);
            this.airlineCodes.push(price.details.outbound.airline);
        }.bind(this));
        //this.getAirlines();
        this.view.show(this.prices);
        this.view.highlight();
    },
    getAirlines: function() {
        var parameters = this.airlineCodes.join(",");
        new Ajax.Request("/flights/?action=airlines", {
            method: "post",
            postBody: "airlines=" + parameters,
            onSuccess: function(transport) {
                var airlines = transport.responseJSON;
                $A(airlines).each(function(airlineData) {
                    try {
                        var airline = Object.extend((new Airline()), airlineData);
                        this.airlines.push(airline);
                    } catch (e) {
                        dispatchException(e);
                    }
                }.bind(this));
            },
            onFailure: function() {
                console.log('onFailure');
            }
        });
    },
    convertNodeToHash: function(node) {
        var nodeData = {};
        if (node.hasChildNodes()) {
            var childNode, i = 0;
            while (childNode = node.childNodes[i]) {
                i++;
                if (childNode.nodeType == 3) {
                    continue;
                }

                var emptyValue = "n/a";
                nodeData[childNode.nodeName] = childNode.hasChildNodes()
                                             ? (childNode.firstChild.nodeValue == "null" || childNode.firstChild.nodeValue == "")
                                               ? emptyValue
                                               : decodeURIComponent(childNode.firstChild.nodeValue)
                                             : emptyValue;
            }
        }
        return nodeData;
    },
    onPriceRequestFailure: function(transport, exception) {
        this.view.highlight();
    },
    deletePrice: function(event) {
        var element = Event.element(event);
        var price = element.price;

        this.prices.each(function(item, index) {
            if (item.getValue() == price.getValue() &&
                item.getAirline().getCode() == price.getAirline().getCode() &&
                item.getVendor() == price.getVendor()) {
                    this.priceIds[index] = null;
                    this.prices[index]   = null;
                    this.priceIds    = this.priceIds.compact();
                    this.prices      = this.prices.compact();
                    return;
            }
        }.bind(this));

        this.view.show(this.prices);
    }
});