$(function() {
  var $body = $('body');
  var $canvas = $('#canvas');
  var $navigation = $('#navigation');

  window.onpageshow = function(event) {
    if (event.persisted) {
      window.location.reload();
    }
  };

  $body.on('click', 'a', function(event) {
    var href = $(this).attr('href');
    var target = $(this).attr('target');
    var is_event = $(this).hasClass('event');
    var is_load = $(this).hasClass('button--load');
    if (href.indexOf('/') === 0 && !is_load && !target && !is_event && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey && !$('html').hasClass('cms-ready')) {
      event.preventDefault();
      $body.addClass('unload loading');
      window.setTimeout(function() {
        window.location = href;
      }, 50);
    }
  });

  $body.on('click', '.navigation__item__title', function(event) {
    event.preventDefault();
    var $this = $(this);
    var $navigation_main = $this.parents('.navigation__main');
    var current = $navigation_main.attr('data-active');
    if (current) {
      $navigation_main.removeAttr('data-active');
    } else {
      $navigation_main.attr('data-active', '');
      window.setTimeout(function() {
        $navigation_main.attr('data-active', $this.attr('data-id'));
      }, 220);
    }
  });

  $body.on('mouseenter', '.navigation__item', function(event) {
    var $this = $(this);
    var $navigation_main = $this.parents('.navigation__main');
    $navigation_main.attr('data-focus', $this.attr('data-id'));
  });

  $body.on('mouseleave', '.navigation__item', function(event) {
    var $this = $(this);
    var $navigation_main = $this.parents('.navigation__main');
    $navigation_main.removeAttr('data-focus');
  });

  $body.on('mouseenter', '.navigation__item__list a', function(event) {
    var $this = $(this);
    var $navigation_main = $this.parents('.navigation__main');
    $navigation_main.attr('data-hover', $this.parents('.navigation__item').attr('data-id'));
  });

  $body.on('mouseleave', '.navigation__item__list a', function(event) {
    var $this = $(this);
    var $navigation_main = $this.parents('.navigation__main');
    $navigation_main.removeAttr('data-hover');
  });

  $body.on('click', '.header__button--navigation', function(event) {
    if ($body.hasClass('search_open')) {
      if (!$body.hasClass('_search') && !$body.hasClass('search_results')) {
        event.preventDefault();
        window.close_search();
      } else {
        $(this).removeClass('event');
        $(this).click();
      }
    } else if ($body.hasClass('navigation_open')) {
      event.preventDefault();
      close_navigation();
    } else {
      event.preventDefault();
      open_navigation();
    }
  });

  var scroll_top = 0;

  function open_navigation() {
    window.timetable_can_scroll = false;
    window.navigation_is_open = true;
    $body.removeClass('notification_open');

    window.on_transitionend($navigation, function() {
      window.requestAnimationFrame(function() {
        $navigation.css('position', 'relative');
        $navigation.off(window.transitionend);
      });
    });

    window.requestAnimationFrame(function() {
      scroll_top = $(window).scrollTop();
      $canvas.css({
        'top': scroll_top * -1,
        'position': 'fixed'
      });
      window.requestAnimationFrame(function() {
        $body.addClass('navigation_open');
      });
    });
  }

  function close_navigation() {
    $body.removeClass('notification_open');
    $navigation.removeAttr('style');
    var $navigation_main = $navigation.find('.navigation__main');
    $body.removeClass('navigation_open');
    window.requestAnimationFrame(function() {
      $navigation_main.removeAttr('data-focus');
      $navigation_main.removeAttr('data-hover');
      $navigation_main.removeAttr('data-active');
      $canvas.removeAttr('style');
      $(window).scrollTop(scroll_top);
      window.navigation_is_open = false;
      window.timetable_can_scroll = true;
    });
  }
});