//
////
// jquery.specialFinderController.js
//
// the table manager allows support for sorting, highlighting and reloading a table
// needs to be placed to a <table> Object with <tbody> and <thead>
// exact one row in thead and tbody has to be set, since the plugin needs the info to
// set up the specialFinderController


/**
 * $Id: jquery.specialFinderController.js 408 2008-12-10 17:11:06Z Igor.Lyubimov $
 **/

//   _sfcCache    _sfcId   sfc  SpecialFinderController   specialFinderController    müssen für neues Plugin ersetzt werden

(function($){

  $.fn.extend({
    specialFinderController : function(options)
    {
      if (!$.event._sfcCache) $.event._sfcCache = [];

      // initialise the date picker controller with the relevant settings...
      options = $.extend(
      {
        id : '',
        $context : ''

      }
      , options
      );

      return this.each(
        function()
        {
          var $this = $(this);
          var alreadyExists = true;

          if (!this._sfcId) {
            this._sfcId = $.event.guid++;
            $.event._sfcCache[this._sfcId] = new SpecialFinderController(this);
            alreadyExists = false;
          }

          var controller = $.event._sfcCache[this._sfcId];

          controller.init(options);

          $this.sfcInit();
        }
        )
    },

    sfcInit : function()
    {

      C.log("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");

      $("#finderTargetSearch").ulTree({});



      $("#specialFinderTable").tableManager(
      {
        renderDirective : {
          'tr' : 'line <- data',   // no 'tbody tr' since we are rendering directly into the body fragment and not into the whole table
          'tr[airlinecode]' : 'line.airlinecode',
          'tr[flightnumber]' : 'line.flightnumber',
          'tr[date]' : 'line.date',
          'td.columnDate' : 'line.dateShort',
          'td.columnDay' : 'line.day',
          'td.columnDeparture' : 'line.departure',
          'td.columnArrival' : 'line.arrival',
          'td.columnAirline' : 'line.airline',
          'td.columnPrice' : 'line.price'
        },
        headers: {
          1: {                                                  // 1 means: sort column 1 (index based 0) using the dayparser plugin
            sorter: 'dayparser'
          }
        },
        id : "#specialFinderTable",
        JSONaction : "result_flights",
        updateCallback : function(){

        },
        selectCallback : function($tab){
        },
        transformCallback : function(response)
        {
          return response;
        },
        sortColumn: 'columnDate',
        'cssAsc': 'asc',
        'cssDesc' : 'desc',
        'cssHeader' :'list',
        'emptyRow' : '<tr><td class="columnDate"></td><td class="columnDepartureAirport"></td><td class="columnDestination"></td><td class="columnAirline"></td><td class="columnPrice"></td></tr>'
      } );

    },


    _sfcDestroy : function()
    {
    // TODO - implement this?
    }
  });

  // private internal function to cut down on the amount of code needed where we forward
  // dp* methods on the jQuery object on to the relevant DatePicker controllers...
  var _w = function(f, a1, a2, a3)
  {
    return this.each(
      function()
      {
        var c = _getController(this);
        if (c) {
          c[f](a1, a2, a3);
        }
      }
      );
  };

  function SpecialFinderController(ele)
  {
    this.ele = ele;

    // initial values...
    this.id				=	undefined;
    this.$context			=	undefined;

  };
  $.extend(
    SpecialFinderController.prototype,
    {
      init : function(s)
      {
        C.log(s.renderDirective);
        this.id = s.id;
        this.$context = $(s.id);

      }

    }
    );


  function _getController(ele)
  {
    if (ele._sfcId) return $.event._sfcCache[ele._sfcId];
    return false;
  };

  // make it so that no error is thrown if bgIframe plugin isn't included (allows you to use conditional
  // comments to only include bgIframe where it is needed in IE without breaking this plugin).
  if ($.fn.bgIframe == undefined) {
    $.fn.bgIframe = function() {
      return this;
    };
  };


  // clean-up
  $(window)
  .bind('unload', function() {
    var els = $.event._sfcCache || [];
    for (var i in els) {
      $(els[i].ele)._dpDestroy();
    }
  });


})(jQuery);

