(function($) {
  /**
   * Hero
   */
  function init_jpl_block_hero_js() {
    /* hero down-arrow scroll to next element */
    $('.hero .down-arrow').click(function (e) {
      var $block = $(this).parents('.block').next();
      if ($block.offset()) {
        $('html,body').stop().animate({ scrollTop: $block.offset().top }, 800);
      }
    });

    /* hero video play/pause */
    $('.hero .video-play-pause-button').click(function (e) {
      var $t = $(this);
      var video = $t.parents('.hero').find('video').get(0);
      var $placeholder = $t.parents('.hero').find('.vid-placeholder');
      var html  = '<video loop muted autoplay playsinline poster="' + $placeholder.attr('data-poster') + '">';
          html += '<source src="' + $placeholder.attr('data-src') + '" type="' + $placeholder.attr('data-type') + '">';
          html += '</video>';

      if (video == undefined) {
        $placeholder.replaceWith(html);

        $t.find('.screen-reader-text').text('Pause Background Video');
        $t.toggleClass('playing')
      } else {
        if ($t.hasClass('playing')) {
          $t.find('.screen-reader-text').text('Play Background Video');
          video.pause();
        } else {
          $t.find('.screen-reader-text').text('Pause Background Video');
          video.play();
        }
        $t.toggleClass('playing')
      }
    });

    /* trigger video play if mobile navigation is hidden */
    if ($('.nav-toggle').css('display') == 'none') {
      $('.hero .video-play-pause-button').trigger('click');
    }
  }

  /**
   * Initialize block on page load
   */
  $(document).ready(function() {
    init_jpl_block_hero_js();
  });

  /**
   * Initialize dynamic block preview (editor).
   */
  if (window.acf) {
    window.acf.addAction('render_block_preview/type=block-hero', init_jpl_block_hero_js);
  }
})(jQuery);
