var CompareView = Class.create();

Object.extend(CompareView.prototype, {
    initialize: function() {
        this.container   = $('comparison_container');
        this.departure   = getQueryVariable('departure').replace(/-/g, " ");
        this.destination = getQueryVariable('destination').replace(/-/g, " ");
    },
    prepare: function(prices) {
        $('bl_title').setStyle({background: $('bl_title').getAttribute('bg')});

        var tableElement = elem("table", {border: 0, cellspacing: 0, cellpadding: 0});
        tableElement.cellSpacing = 0;
        if (Browser.isIE) {
            tableElement.style.marginBottom = "20px";
        }

        var theadElement   = elem("thead");
        var trTheadElement = elem("tr");
        var firstHeadTrElement = elem("tr");
        var orangeCellElement  = elem("td", false, {border: "none"});
        orangeCellElement.colSpan = 100;
        var imageElement = elem("img", {src: "/common/i/spacer.gif", width: 60, height: 1});
        orangeCellElement.appendChild(imageElement);

        firstHeadTrElement.appendChild(orangeCellElement);

        theadElement.appendChild(firstHeadTrElement);

        var tdElement = elem("td", false, false, "Airline");

        var imageElement = elem("img", {src: "/common/i/spacer.gif", width: 60, height: 1});
        if (! Browser.isIE) {
            imageElement.width = 90;
        }

        tdElement.appendChild(imageElement);

        tdElement.className = "empty_head_cell";

        tableElement.appendChild(theadElement);
        theadElement.appendChild(trTheadElement);
        trTheadElement.appendChild(tdElement);

        prices.each(function(price) {
            var tdElement = elem('td');
            tdElement.className = 'head_cell';

            var divElement = elem('div');
            divElement.className = 'btn_container';
            tdElement.appendChild(divElement);

            var deleteButtonElement = elem('img', {src: '/common/i/flights/bt.remove.gif'});
            deleteButtonElement.className = 'btn_remove';
            deleteButtonElement.price = price;
            divElement.appendChild(deleteButtonElement);
            Event.observe(deleteButtonElement, 'click', compare.deletePrice.bindAsEventListener(compare));

            var linkElement = elem('a', {href: price.deepLink, target: '_blank'});
            var selectButtonElement = elem('img', {src: '/common/i/flights/bt.select.gif'});
            selectButtonElement.className = 'btn_select';
            linkElement.appendChild(selectButtonElement);

            divElement.appendChild(linkElement);

            var airline = price.details.outbound.airline;

            var innerTableElement = elem('table');
            var innerTbodyElement = elem('tbody');
            var innerTrElement = elem('tr');
            var innerTdElement = elem('td');
            var imageElement = elem('img', {
                src: '/common/i/airlines/' +  airline.code + '.gif',
                title: airline.name,
                alt: airline.name
            });
            imageElement.className = 'ico_airline';

            innerTableElement.appendChild(innerTbodyElement);
            innerTbodyElement.appendChild(innerTrElement);
            innerTrElement.appendChild(innerTdElement);
            innerTdElement.appendChild(imageElement);

            tdElement.appendChild(innerTableElement);
            trTheadElement.appendChild(tdElement);
        }.bind(this));

        this.tbodyElement = elem("tbody");
        tableElement.appendChild(this.tbodyElement);

        var priceValues  = [];
        var priceVendors = [];
        var outboundDepartures  = [];
        var outboundArrival  = [];
        var outboundDuration = [];
        var outboundStops    = [];
        var inboundDepartures  = [];
        var inboundArrival  = [];
        var inboundDuration = [];
        var inboundStops    = [];
        var airlineDetails  = [];

        airlineDetails['website'] = [];
        airlineDetails['av_channels'] = [];
        airlineDetails['baggage']     = [];
        airlineDetails['check_time']  = [];
        airlineDetails['code']        = [];
        airlineDetails['power_access']   = [];
        airlineDetails['seat_recline']   = [];
        airlineDetails['seats_distance'] = [];
        airlineDetails['on_time_departure'] = [];
        airlineDetails['alliance'] = [];
        airlineDetails['programmes'] = [];
        airlineDetails['isinetaccess'] = [];

        var supporter = [];

        var inboundExists = false;
        prices.each(function(price) {
            try {
                supporter.push(elem('img', {src: '/common/i/spacer.gif', width: 160, height: 1}));

                priceValues.push(price.getValue(false, true));

                var imageElement = elem('img', {src: "/common/i/suppliers/" + price.vendorImage + ".gif"});
                priceVendors.push(imageElement);

                outboundDepartures.push(price.details.outbound.departingTime);
                outboundArrival.push(price.details.outbound.arrivingTime);
                outboundDuration.push(price.details.outbound.duration);

                var oStops = price.details.outbound.stops;
                if (oStops == 0 || oStops == null) {
                    oStops = "Non-stop";
                } else if (oStops == 'n/a') {
                } else if (oStops == 1) {
                    oStops += " Stop";
                } else {
                    oStops += " Stops";
                }

                outboundStops.push(oStops);

                if (typeof(price.details.inbound.stops) != "undefined") {
                    inboundExists = true;
                    inboundDepartures.push(price.details.inbound.departingTime);
                    inboundArrival.push(price.details.inbound.arrivingTime);
                    inboundDuration.push(price.details.inbound.duration);


                    var iStops = price.details.inbound.stops;
                    if (iStops == 0 || iStops == null) {
                        iStops = 'Non-stop'
                    } else if (iStops == 'n/a') {
                    } else if (iStops == 1) {
                        iStops += ' Stop'
                    } else {
                        iStops += ' Stops'
                    }

                    inboundStops.push(iStops);
                }
                
                var currentAirline = price.details.outbound.airline;
                
                ['av_channels', 'baggage', 'check_time', 'on_time_departure',
                 'power_access', 'seat_recline', 'seats_distance', 'website',
                 'alliance', 'programmes', 'isinetaccess'].each(function (row) {
                    switch (row) {
                        case 'website':
                            airlineDetails['website'].push(elem('a', {
                                target: '_blank',
                                href: 'http://' + (currentAirline[row] ? currentAirline['website'] : ' ')
                            }, false, 'http://' + currentAirline[row] ? currentAirline['website'] : ' '));
                        break;
                        case 'baggage':
                            if (currentAirline[row] && parseInt(currentAirline[row]) == 0) {
                                airlineDetails[row].push('n/a');
                            } else {
                                airlineDetails[row].push(currentAirline[row]);
                            }


                        break;
                        case 'on_time_departure':
                            if (currentAirline[row] && currentAirline[row] == '0') {
                                airlineDetails[row].push('n/a');
                                break;
                            }
                        case 'power_access':
                             if (currentAirline[row] && currentAirline[row] == '0') {
                                airlineDetails[row].push('no');                                
                             } else {
                                airlineDetails[row].push('yes');  
                             }  
                             break; 
                        case 'alliance':
                            if (currentAirline[row] && parseInt(currentAirline[row]) == '0') {
                                airlineDetails[row].push('n/a');
                            } else {                               
                                airlineDetails[row].push(currentAirline[row]);
                            }
                        break;
                        case 'programmes':
                            if (currentAirline[row] && parseInt(currentAirline[row]) == '0') {                                
                                airlineDetails[row].push('n/a');
                            } else {
                                airlineDetails[row].push(currentAirline[row]);
                            }
                        break;
                        case 'isinetaccess':
                            if (currentAirline[row] && parseInt(currentAirline[row]) == '0') {
                                airlineDetails[row].push('no');
                            } else {
                                airlineDetails[row].push('yes');
                            }
                        break;
                        default:
                            airlineDetails[row].push(currentAirline[row] 
                          ? currentAirline[row].unescapeHTML() : "&nbsp;");
                        break;
                    }
                }.bind(this));

            } catch (e) {
                console.log(e);
            }
        }.bind(this));

        this.createRow('Price', priceValues);

        this.createRow('Vendor',    priceVendors, false, true);

        var string = 'Outbound : ' + this.departure + ' - ' + this.destination;

        this.createRow(string,  false, true, true);


        this.createRow('Departure', outboundDepartures);
        this.createRow('Arrival',   outboundArrival);
        this.createRow('Duration',  outboundDuration);
        this.createRow('Stops',     outboundStops);

        if (inboundExists) {
            var string = 'Inbound : ' + this.destination + ' - ' + this.departure;
            this.createRow(string,  false, true, true);
            this.createRow('Departure', inboundDepartures);
            this.createRow('Arrival',   inboundArrival);
            this.createRow('Duration',  inboundDuration);
            this.createRow('Stops',     inboundStops);
        }

        this.createRow('Airline Profiles', false, true, true);

        this.createRow('AV Channels',       airlineDetails['av_channels']);
        this.createRow('Baggage',           airlineDetails['baggage']);
        this.createRow('Check in time',     airlineDetails['check_time']);
       // this.createRow('On time departure', airlineDetails['on_time_departure']);
        this.createRow('Power access',      airlineDetails['power_access']);
        this.createRow('Seat recline',      airlineDetails['seat_recline']);
        this.createRow('Seat pitch',        airlineDetails['seats_distance']);
        this.createRow('Alliances',         airlineDetails['alliance']);
        this.createRow('Frequent Flyer programmes',  airlineDetails['programmes']);
        this.createRow('Internet Access',    airlineDetails['isinetaccess']);
        //this.createRow('website',           airlineDetails['website'], false, true);
        this.createRow(elem("img", {src: "/common/i/spacer.gif", width: 50, height: 1}), supporter, false, true);

        this.container.appendChild(tableElement);
    },
    createRow: function(title, values, separator, isElements) {
        var separator = separator || false;
        var isElements = isElements || false;

        var trElement = elem("tr");
        if (separator) {
            trElement.className = 'separator';
        }
        var tdElement = elem('td', false, false, (typeof (title) == 'string') ? title : false);
        if (typeof (title) == 'string') {
            if (! title) {
                title = ' ';
            }
            if (title == ' ') {
                tdElement.className = 'empty_cell';
            } else {
                tdElement.className = 'cell' + (separator ? ' first_cell' : '');
            }
        } else {
            tdElement.className = 'empty_cell';
            tdElement.appendChild(elem('img', {src: '/common/i/spacer.gif', width: 50, height: 1}));
        }

        this.tbodyElement.appendChild(trElement);
        trElement.appendChild(tdElement);

        if (! values) {
            tdElement.colSpan = 100;
        } else {
            this.appendCellsToRow(trElement, values, isElements, title);
        }
    },
    appendCellsToRow: function(rowElement, values, isElements, title) {
        this.createTableCells(values, isElements, title).each(function(element) {
            rowElement.appendChild(element);
        }.bind(this));
    },
    createTableCells: function(values, isElements, title) {
        var elements = [];
        var innerValues = values.clone();
        $A(innerValues).each(function(value) {
            if (! isElements) {
                var tdElement = elem('td', false, false, value);
                tdElement.className = 'cell';
            } else {
                var tdElement = elem('td');
                tdElement.className = 'cell';
                if (typeof(title) != 'string') {
                    tdElement.className = 'empty_cell';
                }
                tdElement.appendChild(value);
            }
            elements.push(tdElement);
        }.bind(this));
        return elements;
    },
    show: function(prices) {
        this.clearContainer();
        this.prepare(prices);
    },
    clearContainer: function() {
        this.container.update();
    },
    highlight: function(show) {
    }
});