$(function () {
  var $body = $('body');
  var $timetable = $('#timetable');

  if ($timetable.length === 0) {
    return false;
  }

  $(window).scrollTop(0);

  window.timetable_can_scroll = false;
  window.setTimeout(function () {
    window.timetable_can_scroll = true;
  }, 2500);

  var activated = false;

  function test_scroll() {
    if (!window.navigation_is_open) {
      if (activated) {
        if ($(window).scrollTop() < $(window).height() * 0.05 && window.timetable_can_scroll) {
          $body.removeClass('notification_open');
          window.prevent_scroll_calc = true;
          $timetable.attr('data-active', 0);
          $timetable.removeClass('active');
          activated = false;
        }
      } else {
        if ($(window).scrollTop() > $(window).height() * 0.05 && window.timetable_can_scroll) {
          $body.removeClass('notification_open');
          init_timetable_items();
        }
      }
    }
    window.requestAnimationFrame(test_scroll);

  }

  function init_timetable_items() {
    $timetable.attr('data-active', 1);

    $timetable.find('.timetable__item').each(function () {
      $(this).addClass('reveal_container')
    });
    window.setTimeout(function () {
      $timetable.addClass('active');

      window.requestAnimationFrame(function () {
        $timetable.find('.timetable__item').each(function () {
          var index = parseInt($(this).attr('data-id'));
          var elementWatcher = scrollMonitor.create(this);
          elementWatcher.enterViewport(function () {
            set_timetable_item(index);
          });
        });

        window.create_reveal_elements($timetable);

        window.prevent_scroll_calc = false;
        window.calc_scrolls();

        activated = true;
      });
    }, 1500);
  }

  window.requestAnimationFrame(test_scroll);

  var current_index = 0;
  var last_index = parseInt($timetable.attr('data-last'));
  var elements = document.getElementsByClassName('timetable__item');

  var $timetable_next = $('.timetable__next');
  var $timetable_clock_digital_time = $('#timetable__clock__digital__time');

  var $timetable_progress = $('#timetable__clock__progress');
  var timetable_clock_progress_max = parseFloat($timetable_progress.attr('stroke-dasharray'));

  $body.on('click', '.timetable__next', function (event) {
    event.preventDefault();
    if (!activated) {
      $(window).scrollTop($(window).height());
      init_timetable_items();
    }
  });

  // var handle_scroll = function(evt) {
  //   if (!can_scroll) {
  //     return false;
  //   }
  //   if (!evt) evt = event;
  //   var delta = event.wheelDelta;
  //   if (event.webkitDirectionInvertedFromDevice) delta = -delta;
  //   if (Math.abs(delta) > 20) {
  //     if (delta < 0) {
  //       prev_timetable_item();
  //     } else {
  //       next_timetable_item();
  //     }
  //   }
  // };
  // document.addEventListener('DOMMouseScroll', handle_scroll, false); // for Firefox
  // document.addEventListener('mousewheel', handle_scroll, false);

  function next_timetable_item() {
    set_timetable_item(current_index + 1);
  }

  //
  // function prev_timetable_item() {
  //   set_timetable_item(current_index - 1);
  // }
  //
  function set_timetable_item(index) {
    if (index <= last_index && index >= 0) {
      current_index = index;

      $timetable.attr('data-active', current_index);

      if (current_index === last_index) {
        $timetable_next.addClass('hidden');
      } else {
        $timetable_next.removeClass('hidden');
      }

      update_time_progess();
    }
  }

  function update_time_progess() {
    var $to = $(elements[current_index - 1]);
    var to_minutes = parseInt($to.attr('data-hour')) * 60 + parseInt($to.attr('data-minute'));
    var to_minutes_with_offset = to_minutes - (6 * 60);
    var total_progress = (1 / (12 * 60)) * to_minutes_with_offset;

    var start = $timetable_clock_digital_time.text();
    var start_time = start.split(':');
    var start_minutes = parseInt(start_time[0]) * 60 + parseInt(start_time[1]);
    var count_up = new CountUp('timetable__clock__digital__time', start_minutes, to_minutes, 0, 1.5, {
      useEasing: true,
      separator: ''
    });
    count_up.start();

    if (to_minutes) {
      $timetable_progress.attr('stroke-dashoffset', timetable_clock_progress_max * Math.abs(total_progress - 1));
    }
  }

  document.querySelector('.timetable__button a').addEventListener('click', function (event) {
    event.preventDefault();
    var player;
    var vimeo_id = document.querySelector('.timetable__button').getAttribute('data-video-vimeo-id');

    var pswpElement = document.querySelectorAll('.pswp')[0],
      gallery,
      options,
      items;

    options = {
      showHideOpacity: true,
      download: false,
      fullscreenEl: false,
      shareEl: false,
      bgOpacity: 0.85,
      mainClass: 'timetable__pswp',
    };

    items = [
      {
        html: '<div class="gallery__iframe"><div class="gallery__iframe__main"><iframe src="https://player.vimeo.com/video/' + vimeo_id + '" width="100%" height="100%" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe></div></div>',
      },
    ];

    var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);

    gallery.listen('close', function () {
      document.querySelector('#timetable').style.height = '';
      player.pause();
    });

    gallery.init();

    document.querySelector('#timetable').style.height = '100%';
      var iframe = document.querySelector('.pswp iframe');
      player = new Vimeo.Player(iframe);
      player.play();
  });

});