// Slidemanager
// manages panels
// ensure, that only one main panel and a maximum of one subpanel is visible
// if a new main panel becomes visible, all sub panels are minimized
// if a new sub panel becomes visible, a maybe visible sub panel is will be hidden
function SlideManager()
{


  if ($("div.tx-byebye-pi1").length > 0)
  {
    //var old = "#loadingScreen  #commitBooking #specialFinder";
    //var old2 = "#transferInformation #errorMsg ";
    this.mainPanel = "#chooseFlight #chooseHotel #chooseCar";
    this.subPanel = "#information #information2 #modifySearch #airlineInformation #filterFlights #filterHotels #filterCars #loadMessagePanel #errorMessagePanel";
  }
  if ($("div.tx-byebye-pi3").length > 0)
  {
    this.mainPanel = "#commitStep1 #commitStep2 #commitStep3 #commitStep4 #commitStep5";
    this.subPanel = "#information #information2 #loadMessagePanel #errorMessagePanel";
  }
  if ($("div.tx-byebye-pi5").length > 0)
  {
    this.mainPanel = "#specialFinder";
    this.subPanel = "#loadingScreen #information #information2 #errorMsg #loadMessagePanel #errorMessagePanel";
  }


  this.specialPanel = "";
  this.displayedMainPanel = null;
  this.displayedSubPanel = null;
  this.displayedSpecialPanel = null;

  this.storage = null;

  this.loadingShowing = 0;
  this.errorShowing = 0;
  this.hasUserBlock = false;

  this.initBoxes();
  this.initAgencyVersion();
  //    C.log("hier");

  this.animQueue = [];
  //this.animStepInProgress = false;

  this.animObject = function(typ, onAfter, pane){
    this.type = typ;
    this.onAfterFunction = onAfter;
    this.panel = pane;
  }
// this.init();
}
$.extend(
  SlideManager.prototype,
  {


    init : function()
    {
      this.minimizeAllPanel("");
      this.displayedMainPanel = null;
      this.displayedSubPanel = null;
      this.displayedSpecialPanel = null;

    //  this.toggleLoading();
    },


    initAgencyVersion : function()
    {
      if (agency_version == true)
        $(".agencyVersion", $("#bookingArea")).show();
      else
        $(".agencyVersion", $("#bookingArea")).hide();
    },


    initalizeScrollStates : function(onAfterFunction)
    {
      var item = function(con, $pan, panel) {
        this.container = con;
        this.$container = $(con);
        this.$panel = $pan;
        this.panel = panel;
      };


      var allPanel = this.mainPanel + " " + this.subPanel; // + " " + this.specialPanel;
      var panel = allPanel.split(" ");

      
      var items = [];
      for (var j = 0; j < panel.length; j++)
      {
        var $pan = $(panel[j]);
        if ($pan && $pan.length > 0)
        {
          var panId = $pan.attr("id");
          var con = "#"+panId+"-container";
          items.push( new item(con, $pan, panel) );
        }
      }

      var rekInit = function()
      {
        if (items.length == 0)
          return;

        var item = items.pop();
        


        item.$container.show().scrollTo('.dummy',{
          onAfter: function(){
            //          C.log(item);
            item.$container.hide();
            if (items.length > 0)
              rekInit();
            else if ($.isFunction(onAfterFunction))
              onAfterFunction();
          }
        });
      }

      getSlideManager().executeAction(function(){
        rekInit();
      });
    },

    initBoxes : function()
    {
      $.scrollTo.defaults.axis = 'xy';

      var allPanel = this.mainPanel + " " + this.subPanel; // + " " + this.specialPanel;
      var panel = allPanel.split(" ");

      for (var j = 0; j < panel.length; j++)
      {
        var $pan = $(panel[j]);

//        C.log("$pan: ["+panel[j])
//        C.log($pan);

        if ($pan && $pan.length > 0)
        {

          var panId = $pan.attr("id");
          var scroll= "<div id = '"+panId+"-scroll"+"' class='"+ (($pan.hasClass("fromRight") || $pan.hasClass("fromLeft")) ? "hbox" : "vbox") +"'>" ;
          $pan.parent().attr("id",panId+"-container").hide();
          $pan.wrap(scroll);

          if ($pan.hasClass("fromBottom"))
            ($('<div class="fromBottom dummy"></div>').insertBefore($pan));
          else if ($pan.hasClass("fromRight"))
            ($('<div class="fromRight dummy"></div>').insertBefore($pan));
          else if ($pan.hasClass("fromTop"))
            ($('<div class="fromTop dummy"></div>').insertAfter($pan));
          else if  ($pan.hasClass("fromLeft"))
            ($('<div class="fromLeft dummy"></div>').insertAfter($pan));
        }
        else
        {
//          C.log("error in Main App initBoxes()");
//          C.log("+>"+panel[j]+"<-");
//          C.log($pan);
          continue;
        }
      }
    },

    // javascript:$("#bookingArea").amGetSlideManager().logStatus();
    logStatus : function()
    {
      var logInfo =
      {
        statStorage : this.storage,
        statLoadingShowing : this.loadingShowing,
        statErrorShowing : this.errorShowing,
        statDisplayedMainPanel : this.displayedMainPanel,
        statDisplayedSubPanel : this.displayedSubPanel,
        statDisplayedSpecialPanel : this.displayedSpecialPanel
      };
      C.log(logInfo);
      return;
    },


    setStorage : function(store)
    {
      this.storage = store;
    },
    getStorage : function()
    {
      return(this.storage);
    },


    getScrollInfo : function($panel, hidden)
    {
      var val = {};
      if ($panel.hasClass("fromLeft"))
        val = !hidden ? {
          left : '775px',
          top : '0px'
        } : {
          left: '0px',
          top: '0px'
        };
      else if ($panel.hasClass("fromRight"))
        val = !hidden ? {
          left: '0px',
          top: '0px'
        } : {
          left: '775px',
          top: '0px'
        };
      else if ($panel.hasClass("fromTop"))
        val = !hidden ? {
          left: '0px',
          top: '455px'             // was: 750px;
        } : {
          left: '0px',
          top: '0px'
        };
      else if ($panel.hasClass("fromBottom"))
        val = !hidden ? {
          left: '0px',
          top: '0px'
        } : {
          left: '0px',
          top: '395px'           // was: 750px
        };
      else
      {
        C.log("error in AppMain getScrollInfo");
        C.log($panel);
        C.log(hidden);
      }

      return val;
    },

    executeAction : function(action, data)
    {
      this.animQueue.push(new this.animObject(9, action, data));
      this.nextAnimStep();
    },

    showLoading : function(onAfterFunc)
    {
      this.animQueue.push(new this.animObject(9, function(){
        getSlideManager().setLoadingShowing(true);
      }, undefined));
      this.animQueue.push(new this.animObject(8, onAfterFunc, "#loadMessagePanel"));
      this.nextAnimStep();
    },
    hideLoading : function(onAfterFunc)
    {
      this.animQueue.push(new this.animObject(7, onAfterFunc, "#loadMessagePanel"));
      this.animQueue.push(new this.animObject(9, function(){
        getSlideManager().setLoadingShowing(false);
      }, undefined));
      this.nextAnimStep();
    },
    showError : function(onAfterFunc)
    {
      this.animQueue.push(new this.animObject(9, function(){
        getSlideManager().setErrorShowing(true);
      }, undefined));
      this.animQueue.push(new this.animObject(8, onAfterFunc, "#errorMessagePanel"));
      this.nextAnimStep();
    },
    hideError : function(onAfterFunc)
    {
      this.animQueue.push(new this.animObject(7, onAfterFunc, "#errorMessagePanel"));
      this.animQueue.push(new this.animObject(9, function(){
        getSlideManager().setErrorShowing(false);
      }, undefined));
      this.nextAnimStep();
    },
    switchToPanel : function(panel, onAfterFunc)
    {
      this.animQueue.push(new this.animObject(8, onAfterFunc, panel));
      this.nextAnimStep();
    },
    slideHide : function(panel, onAfterFunc)
    {
//      C.log("XX slideHide:" + panel);
      this.animQueue.push(new this.animObject(7, onAfterFunc, panel));
      this.nextAnimStep();
    },
    slideShow : function(panel, onAfterFunc)
    {
//      C.log("XX slideShow:" + panel);
      this.animQueue.push(new this.animObject(8, onAfterFunc, panel));
      this.nextAnimStep();
    },
    slideToggle : function(panel, onAfterFunc)
    {
//      C.log("XX slideToggle:" + panel);
      var act = 7;
      if ($(panel+"-container").is(":hidden"))
        act = 8;
      this.animQueue.push(new this.animObject(act, onAfterFunc,  panel));
      this.nextAnimStep();
    },
    hideSubPanel : function(onAfterFunc)
    {
//          C.log("hideSUBPanel");
      var $sm = $("#bookingArea").amGetSlideManager();
      if ($sm.displayedSubPanel != undefined && $sm.displayedSubPanel.length > 0)
        $sm.slideHide($sm.displayedSubPanel, onAfterFunc);
      else if ($.isFunction(onAfterFunc))
        onAfterFunc();
    },
    hideMainPanel : function(onAfterFunc)
    {
      var innerHideMainPanel = function(onAfterFunc)
      {
//        C.log("innerHideMainPanel");
        var $sm = $("#bookingArea").amGetSlideManager();
//        C.log($sm.displayedMainPanel);
        if ($sm.displayedMainPanel != undefined && $sm.displayedMainPanel.length > 0)
          $sm.slideHide($sm.displayedMainPanel, onAfterFunc);
        else if ($.isFunction(onAfterFunc))
          onAfterFunc();
      }
      
//      C.log("hideMainPanel");

      var $sm = $("#bookingArea").amGetSlideManager();
      var onAfterFunction = onAfterFunc;
      if ($sm.displayedSubPanel != undefined && $sm.displayedSubPanel.length > 0)
        $sm.hideSubPanel(function(){
          innerHideMainPanel(onAfterFunction);
        });
      else
        innerHideMainPanel(onAfterFunc);
    },

    isAnimRunning : function()
    {
      if ($("#bookingArea").hasClass("animating"))
        return true;
      else
        return false;
    },

    setLoadingShowing : function(val)
    {
      this.loadingShowing = val;
    },

    isLoadingShowing : function()
    {
      return(this.loadingShowing);
    },

    setErrorShowing : function(val)
    {
      this.errorShowing = val;
    },
    isErrorShowing : function()
    {
      return(this.errorShowing);
    },

    isUserBlock : function()
    {
      return this.hasUserBlock;
    },

    setUserBlock : function(block)
    {
      this.hasUserBlock = block;
    },




    nextAnimStep : function()
    {
      // ensure that only one instance of nextAnimStep is running
      if ( $("#bookingArea").hasClass("animating") || this.animQueue == undefined || this.animQueue.length == 0) // already animating? break
      {
//        C.log("nextAnimStep, break: 'animating'-class set");
        return;
      }
//      C.log("---------------------------------");
//      C.log("Starting new Animationsequence:")
      $("#bookingArea").addClass("animating"); // start animation mode


      var queue = this.animQueue;
      var innerNextAnimStep = function()
      {

        var $sm = getSlideManager();

        // check, if animation step is available, break if not
        if (queue == undefined || queue.length == 0)
        {
          $("#bookingArea").removeClass("animating"); // start animation mode
//          C.log("queue ist empty or undefined");
//          C.log("removing animating tag");
//          C.log("---------------------------------");
          return;
        }

        // start next animation
        var aObj = queue.shift();
//        C.log("Starting new animation sequence");
//        C.log(aObj);

        switch(aObj.type)
        {
          case 9: // action
            if ($.isFunction(aObj.onAfterFunction()))
              aObj.onAfterFunction(aObj.panel);
            innerNextAnimStep();
            break;

          case 7:  // HIDE
            var panel = aObj.panel;
            var $panel = $(panel);
            var $container = $panel.parent().parent();
            var val = $sm.getScrollInfo($panel, false);

            if (   $sm.displayedMainPanel != panel
              && $sm.displayedSubPanel != panel
              && $sm.displayedSpecialPanel != panel
              )
              {
//              C.log("invalid panel, aborting: "+panel);
//              C.log("removing animating tag");
//              C.log("---------------------------------");
              $("#bookingArea").removeClass("animating");
              return;
            }

            if ($sm.displayedMainPanel == panel)
              $sm.displayedMainPanel = null;
            else if ($sm.displayedSubPanel == panel)
              $sm.displayedSubPanel = null;
            else if ($sm.displayedSpecialPanel == panel)
              $sm.displayedSpecialPanel = null;

            $panel.parent().parent().scrollTo(val, phpConst.slide.time,{
              onAfter: function(){
                $container.hide();
                //C.log("executeSlideHide callback");
                if (aObj.onAfterFunction && $.isFunction(aObj.onAfterFunction))
                  aObj.onAfterFunction();
                innerNextAnimStep();
              }
            });
            break;

          case 8: // SHOW
            var hidePanel = function(panel, onAfterFunc)
            {
//              C.log("hidePanel: "+panel);
              var $panel = $(panel);
              var $container = $panel.parent().parent();
              var val = $sm.getScrollInfo($panel, false);
              if ($sm.displayedMainPanel == panel)
                $sm.displayedMainPanel = null;
              else if ($sm.displayedSubPanel == panel)
                $sm.displayedSubPanel = null;
              else if ($sm.displayedSpecialPanel == panel)
                $sm.displayedSpecialPanel = null;
              else {
//                C.log("hide panel not found, aborting");
                return;
              }
              $container.scrollTo(val, phpConst.slide.time,{
                onAfter: function(){
                  $container.hide();
                  if ($.isFunction(onAfterFunc))
                    onAfterFunc();
                }
              });
            }

            var showPanel = function(panel, onAfterFunc)
            {
//              C.log("showPanel"+ panel);
              var val = 0;
              var $panel = $(panel);
              var $container = $panel.parent().parent();
              val = $sm.getScrollInfo($panel, true);

              if ($sm.mainPanel.indexOf(panel) != -1 &&
                $sm.displayedMainPanel == null)
                $sm.displayedMainPanel = panel;
              else if ($sm.subPanel.indexOf(panel) != -1 &&
                $sm.displayedSubPanel == null)
                $sm.displayedSubPanel = panel;
              else if ($sm.specialPanel.indexOf(panel) != -1 &&
                $sm.displayedSpecialPanel == null)
                $sm.displayedSpecialPanel = panel;
              else {
                C.log("show Panel not found, aborting");
                return;
              }

              $container.show();
              //              $container.scrollTo('.dummy');
              $container.scrollTo(val, phpConst.slide.time,{
                onAfter: function(){
                  if ($.isFunction(onAfterFunc))
                    onAfterFunc();
                }
              });
            }


            // new Panel not visible
            var changeMainPanel = function()
            {
              var checkMainPanel = function()
              {
//                C.log("changeMainPanel");
//                C.log(aObj);
//                C.log($sm.displayedMainPanel);
                if ($sm.displayedMainPanel != null) // if there is a main, hide it
                  hidePanel($sm.displayedMainPanel, function(){
                    showPanel(aObj.panel, onAfterWrapper);
                  });
                else
                  showPanel(aObj.panel, onAfterWrapper);
              }
              var onAfterWrapper = function()
              {
                if (aObj && $.isFunction(aObj.onAfterFunction))
                  aObj.onAfterFunction();
                innerNextAnimStep();
              }

              if ($sm.displayedSubPanel != null) // if there is a subPanel, hide it
                hidePanel($sm.displayedSubPanel, function(){
                  checkMainPanel();
                });
              checkMainPanel();
            }




            var changeSubPanel = function()
            {
              var onAfterWrapper = function()
              {
                if (aObj && $.isFunction(aObj.onAfterFunction))
                  aObj.onAfterFunction();
                innerNextAnimStep();
              }


//              C.log("changeSubPanel" + aObj.panel);
//              C.log(aObj);
              if ($sm.displayedSubPanel != null) // if there is a subPanel, hide it
                hidePanel($sm.displayedSubPanel, function(){
                  showPanel(aObj.panel, onAfterWrapper);
                });
              else // show new sub panel
                showPanel(aObj.panel, onAfterWrapper);
            }


            /////////////////////////////////////////
            //              if (aObj.panel == undefined)
            //              {
            //                innerNextAnimStep();
            //                return;
            //              }
            //
            // check, if panel is valid
            //              if ( ($sm.mainPanel.search(aObj.panel) != -1) && ($sm.subPanel.search(aObj.panel) != -1) )
            //              {
            //                innerNextAnimStep();
            //                return;
            //              }

            // if panel is visible mainPanel and no subpanel present: abort
            if  (($sm.displayedMainPanel == aObj.panel) && ($sm.displayedSubPanel == null))
            {
              innerNextAnimStep();
              return;
            }

            // if new panel equals already visible subpanel, abort
            // or if panel is already visible, but subpanel covers it: hide subpanel and abort
            if (  ($sm.displayedSubPanel == aObj.panel) )
              innerNextAnimStep();
            else if (  ($sm.displayedMainPanel == aObj.panel) && ($sm.displayedSubPanel != null)  )
            {
              hidePanel(aObj.panel, innerNextAnimStep);
            }
            else
            {
              // determine, what to do
              if ($sm.mainPanel.search(aObj.panel) != -1)  // change a main Panel?
                changeMainPanel();
              else // we are a subpanel
                changeSubPanel();
            }
            break;

          default:

            break;
        }
      }
      innerNextAnimStep(); // start/continue animation



    },

    ////   ]]]]]]
    // minimize all items but submitted exceptions
    minimizeAllPanel : function(exceptions)
    {
      
      if ($("div.tx-byebye-pi1").length > 0)
      {
        // for each element check if an execption has mit submitted
        // if not, hide the element
        if (exceptions.search(/chooseFlight/) == -1)
          $("#chooseFlight").parent().parent().hide();
        if (exceptions.search(/information/) == -1)
          $("#information").parent().parent().hide();
        if (exceptions.search(/chooseHotel/) == -1)
          $("#chooseHotel").parent().parent().hide();
        if (exceptions.search(/airlineInformation/) == -1)
          $("#airlineInformation").parent().parent().hide();
        if (exceptions.search(/hotelInformation/) == -1)
          $("#hotelInformation").parent().parent().hide();
        if (exceptions.search(/chooseCar/) == -1)
          $("#chooseCar").parent().parent().hide();
        if (exceptions.search(/carInformation/) == -1)
          $("#carInformation").parent().parent().hide();
        if (exceptions.search(/filterCars/) == -1)
          $("#filterCars").parent().hide().css({
            "background-color":"red"
          });
        if (exceptions.search(/commitBooking/) == -1)
          $("#commitBooking").parent().parent().hide();
        if (exceptions.search(/modifySearch/) == -1)
          $("#modifySearch").parent().parent().hide();
        if (exceptions.search(/filterFlights/) == -1)
          $("#filterFlights").parent().parent().hide();
        if (exceptions.search(/filterHotels/) == -1)
          $("#filterHotels").parent().parent().hide();
        if (exceptions.search(/filterCars/) == -1)
          $("#filterCars").parent().hide().css({
            "background-color":"red"
          });
        if (exceptions.search(/specialFinder/) == -1)
          $("#specialFinder").parent().parent().hide();
        if (exceptions.search(/errorMessage/) == -1)
          $("#errorMessage").parent().parent().hide();
      }


      if ($("div.tx-byebye-pi3").length > 0)
      {
        // for each element check if an execption has mit submitted
        // if not, hide the element
        if (exceptions.search(/commitStep1/) == -1)
          $("#commitStep1").parent().parent().hide();
        if (exceptions.search(/commitStep2/) == -1)
          $("#commitStep2").parent().parent().hide();
        if (exceptions.search(/commitStep3/) == -1)
          $("#commitStep3").parent().parent().hide();
        if (exceptions.search(/commitStep4/) == -1)
          $("#commitStep4").parent().parent().hide();
        if (exceptions.search(/commitStep5/) == -1)
          $("#commitStep5").parent().parent().hide();
        if (exceptions.search(/information/) == -1)
          $("#information").parent().parent().hide();
      }


      if ($("div.tx-byebye-pi5").length > 0)
      {
        // for each element check if an execption has mit submitted
        // if not, hide the element
        if (exceptions.search(/information/) == -1)
          $("#information").parent().parent().hide();
        if (exceptions.search(/modifySearch/) == -1)
          $("#modifySearch").parent().parent().hide();
        if (exceptions.search(/specialFinder/) == -1)
          $("#specialFinder").parent().parent().hide();
        if (exceptions.search(/errorMessage/) == -1)
          $("#errorMessage").parent().parent().hide();
      }
    },

    __dummy : function()
    {

    }

  }
  );

