(function(){
  var slides = document.querySelectorAll('#heroSlider .hero-slide');
  var contents = document.querySelectorAll('#heroSlider .hero-slide-content');
  var dots = document.querySelectorAll('#heroSlider .hero-dot');
  if(slides.length < 2) return;
  var index = 0;
  function show(i){
    index = (i + slides.length) % slides.length;
    slides.forEach(function(s,idx){ s.classList.toggle('active', idx===index); });
    contents.forEach(function(c,idx){ c.classList.toggle('active', idx===index); });
    dots.forEach(function(d,idx){ d.classList.toggle('active', idx===index); });
  }
  dots.forEach(function(d){
    d.addEventListener('click', function(){ show(parseInt(d.getAttribute('data-index'),10)); restart(); });
  });
  var timer = null;
  function play(){ timer = setInterval(function(){ show(index+1); }, 7000); }
  function restart(){ clearInterval(timer); play(); }
  play();
})();

(function(){
  var btn = document.getElementById('backToTop');
  if(!btn) return;
  var nav = document.querySelector('.nav-shell');
  var shown = false;
  function onScroll(){
    var y = window.pageYOffset || document.documentElement.scrollTop;
    var past = y > 500;
    if(past !== shown){
      shown = past;
      btn.classList.toggle('visible', shown);
    }
    if(nav){ nav.classList.toggle('is-stuck', y > 5); }
  }
  btn.addEventListener('click', function(){
    window.scrollTo({ top: 0, behavior: 'smooth' });
  });
  window.addEventListener('scroll', onScroll, { passive: true });
  onScroll();
})();
