/**
 * $Id: jquery.helpCenterController.js,v 1.19 2009/06/22 08:39:26 bjoern Exp $
 **/

(function($){

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

      // initialise the date picker controller with the relevant settings...
      options = $.extend(
      {
        id : '',
        $context : '',
        renderDirective : '',                  // needed for autorenderer
        emptyRow : ' ',
        updateCallback : undefined, // function is beeing called after update
        selectCallback : undefined, // function is beeing called after change of selection
        JSONaction : undefined,
        buttons : undefined

      }
      , options
      );

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

          if (!this._hccId) {
            this._hccId = $.event.guid++;
            $.event._hccCache[this._hccId] = new HelpCenterController(this);
            alreadyExists = false;
          }

          var controller = $.event._hccCache[this._hccId];
          controller.init(options);
        }
        )
    },
    hccSetRenderDirective : function(directive)
    {
      return _w.call(this, 'setUpdateCallback', directive);
    },
    hccSetEmptyRow : function(emptyRow)
    {
      return _w.call(this, 'setEmptyRow', emptyRow);
    },
    hccSetUpdateCallback : function(callback)
    {
      return _w.call(this, 'setUpdateCallback', callback);
    },
    hccSetSelectCallback : function(callback)
    {
      return _w.call(this, 'setSelectCallback', callback);
    },
    hccSetDefaultHelpText : function()
    {
      return _w.call(this, 'setDefaultHelpText');
    },

    hccBindCallback : function(target)
    {
      return _w.call(this, 'bindCallback', target);
    },

    hccSetHelpText : function(icon,caption,subcaption,description)
    {
      return _w.call(this, 'setHelpText', icon,caption,subcaption,description);
    },
    hccSetJSONAction : function(action)
    {
      return _w.call(this, 'setJSONAction', action);
    },
    hccGetRenderDirective : function()
    {
      var c = _getController(this[0]);
      if (c) {
        return c.getRenderDirective();
      }
      return null;
    },
    hccGetButtons : function()
    {
      var c = _getController(this[0]);
      if (c) {
        return c.getButtons();
      }
      return null;
    },
    _hccDestroy : 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, a4)
  {
    return this.each(
      function()
      {
        var c = _getController(this);
        if (c) {
          c[f](a1, a2, a3, a4);
        }
      }
      );
  };

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

    // initial values...
    this.id				=	undefined;
    this.$context			=	undefined;
    this.renderDirective = '';                  // needed for autorenderer
    this.emptyRow = ' ';
    this.updateCallback = undefined; // function is beeing called after update
    this.selectCallback = undefined; // function is beeing called after change of selection
    this.JSONaction = undefined;
    this.buttons = undefined;
  };
  $.extend(
    HelpCenterController.prototype,
    {
      enterCallback : function()
      {
        var getButtonText = function (prefix, suffix)
        {
          var text = getText (prefix + suffix);
          if (text == prefix + suffix)
          {
            text = getText (prefix);
          }

          return text;
        }

        //        for (var xi = 0; xi < buttonsForHelp.length; xi++)
        //        {
        //          C.log(buttonsForHelp[xi]);
        //          C.log($(buttonsForHelp[xi]));
        //        }

        {
          var button = ($(this).attr("id"));
//          C.log("*****");
//          C.log(button);
          var $sm = $("#bookingArea").amGetStepManager();
          var engine = undefined;
          if ($sm.stepStatus[phpConst.searchParam.engineKind] == phpConst.engineKind.individualTravel)
            engine = "individualTravel";
          else if ($sm.stepStatus[phpConst.searchParam.engineKind] == phpConst.engineKind.flightOnly)
            engine = "flightOnly";
          else if ($sm.stepStatus[phpConst.searchParam.engineKind] == phpConst.engineKind.hotelOnly)
            engine = "hotelOnly";
          else if ($sm.stepStatus[phpConst.searchParam.engineKind] == phpConst.engineKind.flyDrive)
            engine = "flyDrive";

          var step = undefined;
          if ($sm.stepStatus[phpConst.step.currentStep] == phpConst.step.flight)
            step = "flight";
          else if ($sm.stepStatus[phpConst.step.currentStep] == phpConst.step.hotel)
            step = "hotel";
          else if ($sm.stepStatus[phpConst.step.currentStep] == phpConst.step.rentalCar)
            step = "rentalCar";

          //C.log('txt.helpCenter.'+button + '.caption'+'.'+ engine +'.'+ step);
          //C.log('class="' + $(this).attr('class') + '"');

          if (/*engine && step && */!$(this).find('a').hasClass('disabled'))
          {
            var prefix = 'txt.helpCenter.' + button;
            var suffix = "";
            if ( $("div.tx-byebye-pi1").length > 0)  // event routine for non ssl part:
              suffix = '.'+ engine +'.'+ step;
            else if ( $("div.tx-byebye-pi3").length > 0)  // event routine for non ssl part:
              suffix = '.booking.'+ $("#commitProgressBar").cbpbGetStep();
            $(".helpCenter").hccSetHelpText(button,
              getButtonText(prefix + '.caption', suffix),
              getButtonText(prefix + '.subcaption', suffix),
              getButtonText(prefix + '.description', suffix)
              );
          }
          else
          {
            $(".helpCenter").hccSetDefaultHelpText();
          }
        }

      },

      leaveCallback : function() {
        $(".helpCenter").hccSetDefaultHelpText();
      },

      bindCallback : function(target)
      {
        var $target = $(target);
        
        $target.unbind("mouseenter");
        $target.unbind("mouseleave");
        $target
          .bind("mouseenter",this.enterCallback)
          .bind("mouseleave",this.leaveCallback);
      },

      init : function(s)
      {
        this.id = s.id;
        this.$context = $(s.id);
        this.renderDirective = s.renderDirective;
        this.emptyRow = s.emptyRow;
        this.updateCallback = s.updateCallback;
        this.selectCallback = s.selectCallback;
        this.JSONaction = s.JSONaction;
        this.buttons = s.buttons;

        this.setDefaultHelpText();

        // add text for help center
        var buttons = this.getButtons();
        var buttonsForHelp = buttons.split(" ");


        for (var i = 0; i < buttonsForHelp.length; i++)
          this.bindCallback(buttonsForHelp[i]);
          
      },

      setDefaultHelpText : function()
      {
        this.setHelpText("default", getText('txt.helpCenter.default.caption'),
          getText('txt.helpCenter.default.subcaption'),
          getText('txt.helpCenter.default.description'));

      },

      setHelpText : function(id, caption, subcaption, description)
      {
        if (id == undefined) {
          this.setDefaultHelpText();
          return;
        }

        var $hc = $(".helpCenter");

        var buttons = this.getButtons();
        // "#Id #ID #id"
        buttons = buttons.replace(/#/g,"").replace(/ /g,"Info ");

        $hc.find(" .caption").removeClass(buttons).addClass(id+"Info");
        $hc.find(" .caption").text(caption);
        $hc.find(" .subcaption").text(subcaption);
        $hc.find(" .description").text(description);
      },

      setRenderDirective : function(d)
      {
        if (d) {
          this.renderDirective = d;
        }
      },
      setEmptyRow : function(d)
      {
        if (d) {
          this.emptyRow = d;
        }
      },
      setUpdateCallback : function(a)
      {
        if (a && typeof(a) == 'function') {
          a = [a];
        }
        this.updateCallback = this.updateCallback.concat(a);
      },
      setSelectCallback : function(a)
      {
        if (a && typeof(a) == 'function') {
          a = [a];
        }
        this.selectCallback = this.selectCallback.concat(a);
      },
      getRenderDirective : function(d)
      {
        return(this.renderDirective);
      },
      getEmptyRow : function(d)
      {
        return(this.emptyRow);
      },
      getUpdateCallback : function(a)
      {

        return(this.updateCallback);
      },
      getSelectCallback : function(a)
      {
        return(this.selectCallback);
      },
      getButtons : function(a)
      {
        return(this.buttons);
      }

    }
    );

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


})(jQuery);
