/**
 * $Id: jquery.errorMessageController.js 2502 2009-09-24 11:23:43Z Bjoern.Moenikes $
 **/

(function($){

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

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

          var controller = $.event._emcCache[this._emcId];
          controller.init(options);

          $this.emcInit();
        }
        )
    },

    emcInit : function()
    {
    /*
      $("#errorMessageBox").dialog({
        modal: true,
        autoOpen : false,
        resizable : false,
        width: 400,
        height: 400,
        overlay: {
          opacity: 0.5,
          background: "black"
        }
      });

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

    emcNothing : function(event)
    {
      return false;
    },

/*

    checkError926 : function(res)
    {
      C.log("checkError926");
      C.log(res);

      var found = -1;
      for(var i = 0; i<res.errorMessages.length; i++)
        if (res.errorMessages[i].indexOf("926") != -1)
          found = i;

          C.log(found);

      if (found < 0)
        res.errorMessages[res.errorMessages.length] = "Bitte wenden Sie sich an unser Service-Team unter Telefon 0203 - 987 10 987. Wir helfen weiter.";
      else if (res.errorMessages.length)
        {
          res.errorMessages[found] = "Bitte Fehlerhinweis ignorieren - einfach OK klicken und den \"grünen Weiter-Button\" erneut betätigen. Die Buchung wird dann ordnungsgemäß durchgeführt.<br/><br/>Wir möchten Sie bitten, diesen Fehler zu entschuldigen - wir arbeiten an einer Lösung. Vielen Dank für ihr Verständnis.";
          //res.errorMessages[res.errorMessages.length] = "926";
        }
    },
    
*/

    emcShow : function(headline, text, sessionInvalid, onAfter)
    {
      //LOG C.log("EMC SHOW<<<<<");
      //LOG C.log(headline);
      //LOG C.log(text);
      
      var $sm = getSlideManager();
  //    text = text.replace(/\\n/g,"<br />"); //.replace(/^\[\"/,"").replace(/\"\]$/,"");

      $("#errorMessagePanel .header h3").text(headline);
      $("#errorMessagePanel .errorMessage").html(text);


      var  action = undefined;

      if (sessionInvalid != true)
      {
        if ($.isFunction(onAfter) )
          action = onAfter;
        else
          action = function()
          {
            var storage = $sm.getStorage();
            $sm.setStorage("");
            $sm.hideError(undefined);
            if (storage && storage.length > 0)
              $sm.switchToPanel(storage);
          }
      }
      else
        action = function()
        {
          var $sm = undefined;
          if ($("div.tx-byebye-pi1").length > 0 || $("div.tx-byebye-pi3").length > 0)
          {
            window.location.href = getSelectionURL();
          }
          else if ($("div.tx-byebye-pi5").length > 0)
          {
            var selectionURL = "http://" + window.location.hostname;
            var pathName = '/no_cache/suchen/specialfinder.html';
            selectionURL += pathName;
            window.location.href = selectionURL;
          }

        };

      $("#errorMessagePanel a").removeAttr("href");
      $("#errorMessagePanel a").unbind("click");
      $("#errorMessagePanel a").bind("click",action);


      // hide on error link
      $("#errorMessagePanel .errorMessage a").attr("innerHTML","");
      $("#errorMessagePanel .errorMessage a").attr("href","");

      $sm.showError(function(){
        if ($("div.tx-byebye-pi1").length > 0)
          $(".statusPanel").spcRemovePressedState();
        if ($("div.tx-byebye-pi3").length > 0)
          $(".statuspanel").cbspcRemovePressedState();
      });
    },

    emcHide : 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.hideError();
    },
    _emcDestroy : 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 ErrorMessageController(ele)
  {
    this.ele = ele;

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

    }
    );


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


})(jQuery);