$(function() {
  var scrolls = [];

  window.prevent_scroll_calc = false;
  var $timetable = $('#timetable');
  if ($timetable.length > 0) {
    window.prevent_scroll_calc = true;
  }

  function calc_scrolls() {
    scrolls = [];
    var i;
    var scroll_elements = document.querySelectorAll('.scroll');
    for (i = 0; i < scroll_elements.length; i++) {
      var element = scroll_elements[i];
      var height = element.offsetHeight;
      var offset_top = $(element).offset().top;
      var center_offset = offset_top + (height / 2);

      var ease_multiplier = parseFloat(element.getAttribute('data-ease-multiplier'));
      if (!ease_multiplier) {
        ease_multiplier = 1;
      }

      scrolls.push({
        offset: center_offset,
        element: element,
        mod: element.getAttribute('data-scroll-mod'),
        ease_multiplier: ease_multiplier
      });
    }
  }

  if (!window.prevent_scroll_calc) {
    calc_scrolls();
  }
  window.calc_scrolls = calc_scrolls;

  var scroll_top = 0;

  function change_scrolls() {
    // if (window.is_scroll_animating) {
    //   return false;
    // }
    var window_height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    var half_window_height = window_height / 2;
    var window_scroll_top = document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop;
    scroll_top = window_scroll_top + half_window_height;
    for (var i = 0; i < scrolls.length; i++) {
      var item = scrolls[i];
      var relative_offset = item.offset - scroll_top;
      relative_offset = relative_offset * 0.05;

      var css_value = 'translateY';
      var css_unit = 'px';
      var appling_offset = relative_offset * item.ease_multiplier;

      if (item.mod === 'rotate') {
        css_value = 'rotate';
        css_unit = 'deg';
      }
      if (item.mod === 'horizontal') {
        css_value = 'translateX';
      }

      item.element.style.transform = "" +
        css_value + "(" + appling_offset + css_unit + ")";
      item.element.style.webkitTransform = "" +
        css_value + "(" + appling_offset + css_unit + ")";
      item.element.style.MozTransform = "" +
        css_value + "(" + appling_offset + css_unit + ")";
      item.element.style.msTransform = "" +
        css_value + "(" + appling_offset + css_unit + ")";
      item.element.style.OTransform = "" +
        css_value + "(" + appling_offset + css_unit + ")";
    }
  }

  function watch_scrolls() {
    if (!window.navigation_is_open) {
      change_scrolls();
    }
    window.requestAnimationFrame(watch_scrolls);
  }

  window.requestAnimationFrame(watch_scrolls);

  var resize_window_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  window.addEventListener('resize', function(event) {
    var window_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    if (document.querySelector('html').classList.contains('touchevents') && resize_window_width === window_width) {
      return false;
    }
    resize_window_width = window_width;
    if (!window.prevent_scroll_calc) {
      calc_scrolls();
    }
    window.requestAnimationFrame(function() {
      change_scrolls();
    });
  }, false);
});