﻿var Germanwings = {
  /* - 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
    */
  FlightSelection : function( id, anchor, i18n )
  {
    this._id     = id;
    this._anchor = Element.extend( anchor );
    this._i18n   = i18n;

    this._init = function()
    {
      this._data     = [];
      this._rowCount = 0;

      var table           = Element.extend( document.createElement( 'table' ) );
      table.className     = 'gwui_flightsel';
      table.style.display = 'none';
      table.id            = this._id;
      var thead = table.createTHead();
      var tr    = thead.insertRow( -1 );
      var thInp = document.createElement( 'th' );
      tr.appendChild( thInp );
      var thDate = document.createElement( 'th' );
      thDate.appendChild( document.createTextNode( this._i18n.date ) );
      tr.appendChild( thDate );
      var thDepart = document.createElement( 'th' );
      thDepart.appendChild( document.createTextNode( this._i18n.departure ) );
      tr.appendChild( thDepart );
      var thArrive = document.createElement( 'th' );
      thArrive.appendChild( document.createTextNode( this._i18n.arrival ) );
      tr.appendChild( thArrive );
      var thFlightNo = document.createElement( 'th' );
      thFlightNo.appendChild( document.createTextNode( this._i18n.flightNo ) );
      tr.appendChild( thFlightNo );
      
      var thVia = document.createElement( 'th' );
      thVia.appendChild( document.createTextNode( this._i18n.via ) );
      tr.appendChild( thVia );
      // add default no data text
      var trNoData = table.insertRow( -1 );
      trNoData.className = 'gwui_flightsel_flight';
      var tdNoData = trNoData.insertCell( -1 );
      tdNoData.colSpan = 6;
      tdNoData.appendChild( document.createTextNode( this._i18n.noData ) );

      this._anchor.parentNode.appendChild( table );
      this._table = table;
    }
    
    this.reset = function()
    {
      this._table.remove();
      this._init();
    }
    this.show = function()
    {
      this._positionTable();
      this._table.show();
    }
    this.hide = function()
    {
      this._table.hide();
    }
    
    this.toggle = function()
    {
      this._positionTable();
      this._table.toggle();
    }

    this.visible = function()
    {
      return this._table.visible();
    }

    this.rowCount = function()
    {
      return this._rowCount;
    }

    this.appendRow = function( data )
    {
      if ( this._rowCount == 0 )
      {
        this._table.deleteRow( 1 ); // remove no data row
      }
    
      var inp     = Element.extend( document.createElement( 'input' ) );
      inp.type    = 'radio';
      inp.name    = this._id + '_radio';
      inp.observe( 'click', this._select.bindAsEventListener( this, this._rowCount, inp ) );
      var row = Element.extend( this._table.insertRow( -1 ) );
      row.className = 'gwui_flightsel_flight';
      row.observe( 'mouseover', function() { this.className = 'gwui_flightsel_flight_hover'; }.bindAsEventListener( row ) );
      row.observe( 'mouseout',  function() { this.className = 'gwui_flightsel_flight'; }.bindAsEventListener( row ) );
      row.observe( 'click', this._select.bindAsEventListener( this, this._rowCount, inp ) );
      var tdInp = row.insertCell( -1 );
      tdInp.appendChild( inp );
      var tdDate = row.insertCell( -1 );
      tdDate.appendChild( document.createTextNode( data.date ) );
      var tdDepTime = row.insertCell( -1 );
      tdDepTime.appendChild( document.createTextNode( data.departureTime ) );
      var tdArrTime = row.insertCell( -1 );
      tdArrTime.appendChild( document.createTextNode( data.arrivalTime ) );
      var tdFlightNo = row.insertCell( -1 );
      tdFlightNo.appendChild( document.createTextNode( data.flightNo ) );
      var tdVia = row.insertCell( -1 );
      var viaTxt;
      if ( data.via )
      {
        viaTxt = data.via;
      }
      else
      {
        viaTxt = '-';
      }
      tdVia.appendChild( document.createTextNode( viaTxt ) );
      
      data._inp = inp; // required for IE workaround (see _select() method)
      this._data[ this._rowCount ] = data;
      this._rowCount++;
    }
    this._positionTable = function()
    {
      var pos                = Position.cumulativeOffset( this._anchor );
      this._table.style.left = pos[ 0 ] + 'px';
      this._table.style.top  = pos[ 1 ] + this._anchor.getHeight() - 1 + 'px';
    }
    
    this._select = function( event, index, inp )
    {
      // workaround for IE because IE doesn't uncheck other radio buttons even when they are in same select group!!
      for( var i = 0; i < this._data.length; i++ )
      {
        this._data[ i ]._inp.checked = false;
      }
    
      inp.checked = true;
      this.hide();
      if ( this.callback )
      {
        this.callback( this._data[ index ] );
      }
    }
    this._init( id );
  }
}