$(function() {

  function create_reveal_elements($container) {
    var $reveal_containers = $container.find('.reveal_container');
    $reveal_containers.each(function() {
      var reveal_container = this;
      var elementWatcher = scrollMonitor.create(reveal_container);
      elementWatcher.enterViewport(function() {
        reveal_elements(reveal_container);
      });
      elementWatcher.fullyEnterViewport(function() {
        reveal_elements(reveal_container);
      });
    });

    var $reveal_selfs = $container.find('.reveal_self');
    $reveal_selfs.each(function() {
      var reveal_self = this;
      var elementWatcher = scrollMonitor.create(reveal_self);
      elementWatcher.enterViewport(function() {
        reveal_element(reveal_self);
      });
      elementWatcher.fullyEnterViewport(function() {
        reveal_element(reveal_self);
      });
    });
  }

  create_reveal_elements($('html'));

  function remove_reveal_animation(event) {
    if (!event || event.target === this) {
      $(this).removeClass('reveal_animation');
      $(this).off(window.transitionend, remove_reveal_animation);
    }
  }

  function reveal_elements(reveal_container) {
    $(reveal_container).find('.reveal').each(function() {
      reveal_element(this);
    });
  }

  function reveal_element(element) {
    window.requestAnimationFrame(function() {
      window.on_transitionend($(element), remove_reveal_animation);
      $(element).removeClass('reveal');
    });
  }

  window.create_reveal_elements = create_reveal_elements;

  reveal_element($('.header'));
  reveal_element($('.header__logo'));
  reveal_elements($('.timetable__clock__frame'));
  reveal_element($('.timetable__next'));
  reveal_element($('.timetable__start__background'));

});