var search = window.location.search;
if (search.indexOf('?page=1') === -1 && (search.indexOf('?page=') >= 0 || search.indexOf('&page=') >= 0)) {
  document.getElementsByTagName("body")[0].classList.add('hidden');
  window.location = window.location.pathname;
}

$(function() {
  'use strict';

  var $body = $('body');

  $body.on('click', '.list__button', function(event) {
    event.preventDefault();
    var $button = $(this);
    var $load_frame = $button.parents('.load__frame');

    $button.width($button.width());
    window.requestAnimationFrame(function() {
      $load_frame.addClass('loading');
      $load_frame.height($load_frame.outerHeight());
      var load_frame_id = $load_frame.attr('id');
      var $load_replace = $button.parents('.load__replace');
      $.get($button.attr('href'), function(data) {
        var loaded_data = $(data).find('#' + load_frame_id + ' .load__main').html();
        $load_frame.addClass('loaded');
        window.setTimeout(function() {
          $load_replace.replaceWith(loaded_data);
          window.on_transitionend($load_frame, function(event) {
            if (!event || event.target === this) {
              $load_frame.removeAttr('style');
              $load_frame.removeClass('loading loaded');
              $load_frame.off(window.transitionend)
            }
          });
          $load_frame.height($load_frame.find('.load__main').height());
          window.create_reveal_elements($load_frame);
          init_auto_load();
        }, 200);
      });
    });
  });

  function init_auto_load() {
    $('.load__replace--auto').each(function() {
      var that = this;
      var elementWatcher = scrollMonitor.create(that);
      elementWatcher.enterViewport(function() {
        $(that).find('a').trigger('click');
      });
    });
  }
  init_auto_load();
});