/**
 * $Id: jquery.loadMessageController.js 2033 2009-04-01 09:41:15Z Bjoern.Moenikes $
 **/

(function($){

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

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

          var controller = $.event._lmcCache[this._lmcId];
          controller.init(options);
          $this.lmcInit();
        }
        )
    },

    lmcInit : function()
    {
    /*
      $("#loadMessageBox").dialog({
        modal: true,
        autoOpen : false,
        resizable : false,
        overlay: {
          opacity: 0.5,
          background: "black"
        }
      });

      $("#loadMessageBox").removeClass("hidden");
      */
    },

    lmcShow : function(id, varFunc1, varFunc2, lmcShowCallback)
    {      
      var lsc = lmcShowCallback;


      var $sm = undefined;
      if ($("div.tx-byebye-pi1").length > 0)
        $sm = $("#bookingArea").amGetSlideManager();
      if ($("div.tx-byebye-pi3").length > 0)
        $sm = $("#bookingArea").amGetSlideManager();
      if ($("div.tx-byebye-pi5").length > 0)
        $sm = $("#bookingArea").sfcGetSlideManager();


      $sm.showLoading(lsc);


      var $lmp = $("#loadMessagePanel .informationPanel .content");
      if (!id || id=="")
        id = 'Default';
      $(".caption",$lmp).text(getText('txt.wait.'+id+'.caption'));
      if (!(varFunc1 && typeof(varFunc1) == 'function'))
        varFunc1 = function(){
          return('');
        }
      if (!(varFunc2 && typeof(varFunc2) == 'function'))
        varFunc2 = function(){
          return('');
        }

//      C.log("lmcShow");
//      C.log(id);
      $(".sub1",$lmp).text(getText('txt.wait.'+id+'.sub1'));
      $(".sub2",$lmp).text(getText('txt.wait.'+id+'.sub2', varFunc1(), varFunc2()));
      return;
    },

    lmcHide : function()
    {
      var $sm = undefined;
      if ($("div.tx-byebye-pi1").length > 0)
        $sm = $("#bookingArea").amGetSlideManager();
      if ($("div.tx-byebye-pi3").length > 0)
        $sm = $("#bookingArea").amGetSlideManager();
      if ($("div.tx-byebye-pi5").length > 0)
        $sm = $("#bookingArea").sfcGetSlideManager();

      $sm.hideLoading();
      return;
    },
    _lmcDestroy : 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 LoadMessageController(ele)
  {
    this.ele = ele;

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

    }
    );


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


})(jQuery);