let App = function () {


  function initBootstrap() {

    $('[data-bs-toggle="tooltip"]').tooltip();
    $('[data-bs-toggle="popover"]').popover();


    if ($('.navbar-onepage').length > 0) {
      $('body').scrollspy({target: $('.navbar-onepage'), offset: ($('#nav-primary').height() + 21)});
    }

  }

  function initLightbox() {

    if (typeof GLightbox === 'undefined') return;
    /* fix for missing title attribute in lightbox a tags */
    $('.lightbox').each(function () {
      if (this.title === '') {
        $(this).attr('title', $("img", $(this)).attr('title'));
      }
      const parents = $(this).parents('figure');
      if (parents.length > 0) {
        if ($('.image-caption span.description', parents.get(0)).length > 0) {
          $(this).attr('data-description', $('.image-caption span.description', parents.get(0)).html());
        }
      }

    });

    const lightbox = GLightbox({
      selector: '.lightbox'
    });
    lightbox.on('open', () => {
      App.compensateScrollbar(true);
    });
    lightbox.on('close', () => {
      App.compensateScrollbar(false);
    });
  }


  function initCountdown() {
    if (typeof FlipDown === 'undefined') return;

    $('.countdown').each(function () {
      $(this).addClass('flipdown');
      let date = $(this).attr('data-endtime');
      let theme = $(this).attr('data-theme');
      if (theme === undefined || theme == '') {
        theme = 'dark';
      }
      new FlipDown(parseInt(date), $(this).attr('id'), {theme:theme}).start();
    });
  }

  function initHeader() {

    if ($('.toco3-meta-nav').length > 0) {

      let userNavbarHeight = $('.toco3-meta-nav').height();
      $(window).scroll(function () {
        if (document.documentElement.scrollTop > 100) {
          $('#nav-primary').addClass('navbar-shrinked');
        } else if (document.documentElement.scrollTop < 100 - userNavbarHeight - 5) {
          $('#nav-primary').removeClass('navbar-shrinked');
        }
      });
    }

  }


  function initEqualHeight() {

    if (typeof $('body').matchHeight() === 'undefined') return;
    $('.equal-height').matchHeight({
      byRow: true,
      property: 'min-height'
    });
    $('.ce-header').matchHeight({
      byRow: true,
      property: 'min-height'
    });

  }

  function initSmoothScroll() {

    /* from here on only for one pager */
    if ($('.navbar-onepage').length === 0) return;

    /* one page: link on logo */
    $('.navbar-onepage .navbar-brand a').on('click', function (event) {
      event.preventDefault();
      $('html, body').animate({
        scrollTop: 0
      }, 800, function () {

      });
    });

    /* close nav */
    $('.nav-onepage a').on('click', function (event) {
      $(this).closest('.navbar-collapse.show').collapse('hide');
    });

  }

  function initAjaxModals() {

    var modalTemplate = jQuery('<div class="modal fade" id="ajax-modal" tabindex="-1" role="dialog" aria-labelledby="ajaxModalLabel" aria-hidden="true">\n' +
      ' <button type="button" class="close" data-dismiss="modal" aria-label="Close">\n' +
      ' <span aria-hidden="true">&times;</span>\n' +
      ' </button>\n' +
      ' <div class="modal-dialog" role="document">\n' +
      ' <div class="modal-content">\n' +
      ' <div class="modal-header">\n' +
      ' <p class="h5 modal-title" id="ajaxModalLabel"></p>\n' +
      ' </div>\n' +
      ' <div class="modal-text"></div>\n' +
      ' <div class="modal-body">\n' +
      ' </div>\n' +
      ' </div>\n' +
      ' </div>\n' +
      '</div>').attr('id', 'modalContact');


    jQuery('body').append(modalTemplate);
    let modalBody = modalTemplate.find('.modal-body');

    let showContent = function (content) {

      modalBody.html(content);
      modalTemplate.find('.modal-title').html(modalBody.find('h2').html());
      modalBody.find('h2').remove();
      let form = modalTemplate.find('form');

      form.find('[type=submit]').on('click', function (e) {
        e.preventDefault();

        /* show spinner */


        let values = form.serialize();
        values = values += '&' + jQuery(e.target).attr('name') + '=' + jQuery(e.target).attr('value');

        jQuery.ajax({
          type: "POST",
          url: form.attr('action'),
          data: values,
          headers: {
            'Cache-Control': 'no-cache, no-store, must-revalidate',
            'Pragma': 'no-cache',
            'Expires': '0'
          },
          dataType: 'html',
          success: showContent
        });

      });

    };

    jQuery('.modal-ajax').on('click', function (e) {
      e.preventDefault();
      let url = jQuery(this).data('url');

      /* show spinner */

      jQuery.ajax({
        type: "POST",
        url: url,
        headers: {
          'Cache-Control': 'no-cache, no-store, must-revalidate',
          'Pragma': 'no-cache',
          'Expires': '0'
        },
        dataType: 'html',
        success: function (content) {
          showContent(content);
          modalTemplate.modal('show');

          /* hide spinner */

        }
      });
    });

  }

  function initAriaBar() {

    if (jQuery('.nav-aria').length === 0) return;

    jQuery('body').addClass('with-aria-bar');

    if ($.cookie('aria-high-contrast') == '1') {
      jQuery('body').addClass('high-ct-enabled');
      jQuery(this).addClass('active');
      jQuery('.btn-contrast').addClass('active');
    }

    jQuery('.btn-contrast').on('click', function (event) {
      event.preventDefault();
      if (jQuery(this).hasClass('active')) {
        jQuery('body').removeClass('high-ct-enabled');
        jQuery(this).removeClass('active');
        $.removeCookie('aria-high-contrast', {path: '/'});
      } else {
        jQuery('body').addClass('high-ct-enabled');
        jQuery(this).addClass('active');
        $.cookie('aria-high-contrast', '1', {path: '/'});
      }
    });

  }

  function initCompareSlider() {
    if ($(".beforeafterdefault").length > 0) {
      $(".beforeafterdefault").cndkbeforeafter({autoSliding: true, mode: 'drag'});
    }
  }

  function initScrollbarWidth(App) {
    // thx d.walsh
    var scrollDiv = document.createElement('div');
    scrollDiv.className = 'scrollbar-measure';
    document.body.appendChild(scrollDiv);
    var scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth;
    document.body.removeChild(scrollDiv);
    App.scrollbarWidth = scrollbarWidth;
  }

  function initAos() {
    if ($('[data-aos]').length > 0 && typeof aosIsInitialized !== 'undefined') {
      AOS.init();
    }
  }

  function initCookiemanButton() {
    if (typeof cookieman === 'undefined') return;

    $('<a />').addClass('cookieman-caller').click(function (event) {
      if ($('#cookieman-modal:visible').length === 0) {
        cookieman.show();
      }
    }).appendTo($('body'));

  }

  function initSearchbox() {
    $('#t3b__search__collapse__button--desktop').on('click', function (event) {
      $(this).css('opacity', '0');
      $('#tx_indexedsearch_word-default').focus();
    });
    $('#tx_indexedsearch_word-default').on('focusout', function (event) {
      $('.searchbar-toggler').css('opacity', '1');
      $('#t3b__search__collapse--desktop').collapse('hide');
    });

    $('#t3b__search__collapse__button--mobile').on('click', function (event) {
      $('#tx_indexedsearch_word-mobile').focus();
    });
  }

  function initResponsiveTable() {
    $('table.table-responsive-init').wrap('<div class="table-responsive"></div>');
  }

  return {

    scrollbarWidth: 0,

    init: function () {

      initBootstrap();
      initScrollbarWidth(this);
      initLightbox();
      initHeader();
      initEqualHeight();
      initSmoothScroll();
      initAjaxModals();
      initAriaBar();
      initCompareSlider();
      initAos();
      initCookiemanButton();
      initSearchbox();
      initResponsiveTable();
      initCountdown();

    },
    compensateScrollbar: function (compensate) {
      if (compensate) {
        $('body, .fixed-top, .fixed-bottom, .is-fixed').css('padding-right', this.scrollbarWidth);
      } else {
        $('body, .fixed-top, .fixed-bottom, .is-fixed').css('padding-right', 0);
      }
    }
  }
}();

jQuery(document).ready(function () {
  App.init();
});


let mmenu;


let MmenuWrapper = function () {

  function start() {
    let navTitle = document.head.querySelector("[name=navtitle]") ? document.head.querySelector("[name=navtitle]").content : '';

    mmenu = new Mmenu("#sidemenu", {
      navbar: {
        title: navTitle
      },
      navbars: [{
        position: 'bottom',
        height: 2,
        content: ['<div id="mobile-footer"></div>']
      }],
      iconbar : {
        "use": true,
        "top": [
          "<div id='mobile-profile-icon'></div>"]
        }
    }, {
      offCanvas: {
        page: {
          selector: "#page"
        }
      },
      classNames: {
        selected: 'current'
      }
    });
    const api = mmenu.API;
    api.bind("openPanel:before",
      (panel) => {
        var scrollTopPosition = $(window).scrollTop();
        $('.fixed-top').css('position', 'absolute').css('top', scrollTopPosition + 'px');
      }
    );
    api.bind("closePanel:after",
      (panel) => {
        $('.fixed-top').css('position', 'fixed').css('top', '0px');
      }
    );
    api.bind("openPanel:after",
      (panel) => {
        populateNextAndPreviousPanels(panel);
      }
    );

    let openers = document.querySelectorAll('[data-mm-target="#mobile-menu"]');
    [].forEach.call(openers, function (opener) {
      opener.addEventListener('click', (evnt) => {
        evnt.preventDefault();
        api.open();
        populateNextAndPreviousPanels(getOpenPanel());
      });
    });

    $('#mobile-footer').html($('#nav-mobile-footer').html());
    $('#mobile-profile-icon').replaceWith($('#nav-mobile-iconbar').html());


    getOpenPanel().find('ul').attr('data-loaded', 'true');
    //populateNextAndPreviousPanels(getOpenPanel());var pageId = $('body').attr('id').split('_')[1];
  }

  function loadMmenuData(pageIds) {
    //console.debug('loadMmenuData');
    //console.debug(pageIds);
    $.ajax({
      url: $('#sidemenu').attr('data-ajax-menu-url'),
      data: {
        pageIds: pageIds.join(',')
      },
      context: document.body
    }).done(function (data) {
      let uls = $('<div />').html(data).find('ul');
      uls.each(function (index) {
        let id = $(this).attr('data-id');
        if ($('ul[data-id=' + id + ']').length > 0) {
          $('ul[data-id=' + id + ']').attr('data-loaded', 'true');
          let lastElement;
          $(this).children('li').each(function (index) {
            let pid = $(this).attr('data-id');
            if ($('ul[data-id=' + id + '] > li[data-id=' + pid + ']').length > 0) {
              // skip element
              lastElement = $('li[data-id=' + pid + ']');
            } else {
              if (index === 0) {
                lastElement = $(this).prependTo($('ul[data-id=' + id + ']'));
              } else {
                lastElement = $(this).insertAfter(lastElement);
              }
            }
          });
        } else if ($('.mm-listitem[data-id=' + id + ']').length > 0) {
          $('.mm-listitem[data-id=' + id + ']').append($(this));
        }
      });

    });

  }

  function populateNextAndPreviousPanels(panel) {

    //if ($('.navbar-toggler').get(0).offsetParent === null) return;

    //console.debug('populateNextAndPreviousPanels');
    let pageIds = [];
    //console.debug(panel)
    $('li.sub', panel).each(function () {
      if ($('ul[data-id=' + $(this).attr('data-id') + '][data-loaded=true]').length === 0) {
        if ($(this).attr('data-mm-child') !== undefined && $(this).attr('data-mm-child') !== '') {
          $('#' + $(this).attr('data-mm-child') + ' > ul > li.sub').each(function () {
            pageIds.push($(this).attr('data-id'));
          });
        } else {
          pageIds.push($(this).attr('data-id'));
        }
      }
    });
    // parent
    let parentId = $(panel).attr('data-mm-parent');
    if ($('#' + parentId).length > 0 && $('ul[data-id=' + $('#' + parentId).parent().attr('data-id') + '][data-loaded=true]').length === 0) {
      pageIds.push($('#' + parentId).parent().attr('data-id'));
    }
    if (pageIds.length > 0) {
      loadMmenuData(pageIds);
    }
  }

  function getOpenPanel() {
    return $('.mm-panel--opened');
  }

  return {
    init: function () {
      start();
    }
  }
}();


document.addEventListener(
  "DOMContentLoaded", () => {
    MmenuWrapper.init();
  }
);


function loadJS(url) {
  // adding the script tag to the head
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = url;

  // fire the loading
  head.appendChild(script);
}
