/**
 * $Id: jquery.airline_info.js,v 1.12 2009/04/17 12:49:45 igor Exp $
 **/

(function($){

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

      // 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._aiId) {
            this._aiId = $.event.guid++;
            $.event._aiCache[this._aiId] = new AirlineInformationController(this);
            alreadyExists = false;
          }

          var controller = $.event._aiCache[this._aiId];

          controller.init(options);
        }
        )
    },

    aiUpdateAirlineInformationController : function(onSuccess, onFailure)
    {
      if ($('#airlineInformation-container').is(':hidden'))
      {
        var data = $("#chooseFlight").cfcGetData();

        doJSON ('info_airlines', data, function(response)
        {
          var cSP = phpConst.searchParam;
          var cF = phpConst.flight;
          var airline1id = data[cSP.flightDirOutbound][cF.airlineId];
          var airline2id = data[cSP.flightDirReturn][cF.airlineId];

          var $left = $("#airlineInformation .panelLeft");
          var $right = $("#airlineInformation .panelRight");

          var info1 = response.data[airline1id];
          var info2 = response.data[airline2id];

          // PURE could not working if href contains javascript call
          // so we need to clear the attr and set it after call to (auto)Render
          $('#airlineInformation .header .close a').attr('href', '#');

          var directive = {
            '.header h3 .name': 'name',
            '.header h3 .slogan': 'slogan',
            '.content .image img[src]': 'image_file',
            '.content .text': 'info_text'
          };

          if (airline1id == airline2id)
          {
            $right.hide();
            $left.removeClass('panel');
          }
          else
          {
            $left.addClass('panel');
            $right.show();
            $right.render(info2, directive);
          }

          $left.render(info1, directive);

          // Workaround for IE (click function works only 1 time in IE),
          // so we need manipulate href attribute
          $('#airlineInformation .header .close a').attr('href',
            "javascript:$('.statusPanel').spbbFlightInfoButton();").attr(
            'onfocus', 'this.blur();');

          if (onSuccess)
            onSuccess();

        },onFailure, onFailure);
      }

    },


    _aiDestroy : 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 AirlineInformationController(ele)
  {
    this.ele = ele;

    // initial values...
    this.id				=	undefined;
    this.$context			=	undefined;
  };
  $.extend(
    AirlineInformationController.prototype,
    {
      init : function(s)
      {
        this.id = s.id;
        this.$context = $(s.id);
      }
    }
    );


  function _getController(ele)
  {
    if (ele._aiId) return $.event._aiCache[ele._aiId];
    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._aiCache || [];
    for (var i in els) {
      $(els[i].ele)._aiDestroy();
    }
  });


})(jQuery);
