﻿/*
    - id is the *unique* id of the table DOM object which will be created by this class.
    - anchor is the DOM object (e.g. the button or
    dropdown box) this flight selection is anchored to.
    The parent DOM object of anchor will be used
    as the container for the table object.
    - i18n is an object containing translations with the following properties:
        + date
        + departure
        + arrival
        + flightNo
        + via
*/
var Germanwings = {
  FlightSelection: Class.extend({
      id: null,
      anchor: null,
      inp: null,
      i18n: {},
      table: null,
      data: [],
      rowCount: 0,
      init: function (id, anchor, i18n) {
          this.id = id;
          this.anchor = $j(anchor);
          this.i18n = i18n;
          this.createUi(this.id);
      },
      createUi: function (id) {
          var $table = $j("<table id='" + id + "' class='gwui_flightsel'><thead></thead></table>")
                           .find("thead")
                                .html("<tr><th></th>"+
                                "<th>" + this.i18n.date + "</th>"+
                                "<th>" + this.i18n.departure + "</th>"+
                                "<th>" + this.i18n.arrival + "</th>"+
                                "<th>" + this.i18n.flightNo + "</th>"+
                                "<th>" + this.i18n.via + "</th>"+
                                "</tr>"+
                                "<tr class='gwui_flightsel_flight'>"+
                                "<td colspan='6'>" + this.i18n.noData + "</td>"+
                                "</tr>"
                                )
                           .end()
                           .css("display", "none");
          this.anchor.append($table);
          this.table = $table;
      },
    reset: function () {
      this.table.remove();
      this.createUi(this.id);
    },
    show: function () {
      this.positionTable();
      this.table.show();
    },
    hide: function () {
      this.table.hide();
    },
    toggle: function () {
      this.positionTable();
      this.table.toggle();
      
    },
    visible: function () {
      return (this.table.css("display") !== "none");
        
    },
    appendRow: function (data) {
        var $inp = null,
            $row = null;
        if ( this.rowCount === 0 ) {
            $j(this.table.find("tr").get(1)).remove();
        }

        $inp = $j("<input type='radio' value='' style='border:0' name='" + this.id + "' />")
                .bind("click", {index: this.rowCount, inp: $inp}, $j.proxy(this.select,  this));
        $row = $j("<tr class='gwui_flightsel_flight'></tr>")
               .bind("mouseover", function () {
                    this.className = "gwui_flightsel_flight_hover";
               })
               .bind("mouseout", function () {
                    this.className = "gwui_flightsel_flight";
               })
               .bind("click", {index: this.rowCount, inp: $inp},
               $j.proxy(this.select, this))
                   .append("<td width='20'></td>")
                   .append("<td>" + data.date + "</td>")
                   .append("<td>" + data.departureTime + "</td>")
                   .append("<td>" + data.arrivalTime + "</td>")
                   .append("<td>" + data.flightNo + "</td>")
                   .append("<td>" + (data.via ? data.via : "-" )  + "</td>")
                   .appendTo(this.table);
        $row.find("td").first().get(0).appendChild($inp.get(0));
        this.inp = $inp;
        this.data[ this.rowCount ] = data;
        this.rowCount++;
    },
    positionTable: function () {
        var pos = this.anchor.offset();
        this.table.css({left: pos[0], top: (pos[1] + (this.anchor.height() - 1) ) });
    },
    select: function (event) {
        var index = event.data.index,
            inp = event.data.inp;
        $j.each(this.data, $j.proxy(function (idx, item) {
            $j(item.inp).attr("checked","");
        }, this));

        $j(inp).attr("checked","checked");
        this.hide();
        if (this.callback !== null) {
            this.callback(this.data[index]);
        }
    }
  })
  
};
