/* ============================================================
   HEAD BANGERS — shared site behaviour
   ============================================================ */
(function () {
  'use strict';

  var header = document.querySelector('.site-header');
  var mm = document.querySelector('.mobile-menu');
  var burger = document.querySelector('.burger');

  if (burger && mm) {
    burger.addEventListener('click', function () {
      document.body.classList.toggle('menu-open');
    });
    mm.querySelectorAll('a').forEach(function (a) {
      a.addEventListener('click', function () {
        document.body.classList.remove('menu-open');
      });
    });
  }

  if (header) {
    function onScroll() {
      if (window.scrollY > 24) header.classList.add('scrolled');
      else header.classList.remove('scrolled');
    }
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
  }

  var revealEls = [].slice.call(document.querySelectorAll('[data-reveal]'));
  revealEls.forEach(function (el) {
    var d = parseInt(el.getAttribute('data-delay') || '0', 10);
    if (d) el.style.transitionDelay = (d * 0.08) + 's';
  });

  function reveal(el) {
    el.classList.add('in');
    el.style.opacity = '1';
    el.style.transform = 'none';
    setTimeout(function () {
      el.style.transition = 'none';
      el.style.opacity = '1';
      el.style.transform = 'none';
    }, 1100);
  }

  function checkReveal() {
    var vh = window.innerHeight || document.documentElement.clientHeight;
    for (var i = revealEls.length - 1; i >= 0; i--) {
      var el = revealEls[i];
      var r = el.getBoundingClientRect();
      if (r.top < vh * 0.9 && r.bottom > -40) {
        reveal(el);
        revealEls.splice(i, 1);
      }
    }
  }

  var ticking = false;
  function onReveal() {
    if (ticking) return;
    ticking = true;
    setTimeout(function () {
      checkReveal();
      ticking = false;
    }, 80);
  }

  window.addEventListener('scroll', onReveal, { passive: true });
  window.addEventListener('resize', onReveal, { passive: true });
  checkReveal();
  setTimeout(checkReveal, 200);
  window.addEventListener('load', function () {
    setTimeout(checkReveal, 60);
  });

  document.querySelectorAll('.acc-head').forEach(function (h) {
    h.addEventListener('click', function () {
      var item = h.closest('.acc-item');
      var body = item.querySelector('.acc-body');
      var open = item.classList.contains('open');
      if (open) {
        item.classList.remove('open');
        body.style.maxHeight = 0;
      } else {
        item.classList.add('open');
        body.style.maxHeight = body.scrollHeight + 'px';
      }
    });
  });

  var chips = document.querySelectorAll('[data-filter]');
  if (chips.length) {
    chips.forEach(function (chip) {
      chip.addEventListener('click', function () {
        var f = chip.getAttribute('data-filter');
        chips.forEach(function (c) {
          c.classList.toggle('on', c === chip);
        });
        document.querySelectorAll('[data-slot]').forEach(function (row) {
          var match = f === 'all' || row.getAttribute('data-slot').indexOf(f) > -1;
          row.style.display = match ? '' : 'none';
        });
        document.querySelectorAll('[data-day]').forEach(function (col) {
          var any = col.querySelectorAll('[data-slot]:not([style*="none"])').length;
          col.classList.toggle('empty-day', any === 0);
        });
      });
    });
  }

  var dayTabs = document.querySelectorAll('[data-daytab]');
  if (dayTabs.length) {
    dayTabs.forEach(function (t) {
      t.addEventListener('click', function () {
        var d = t.getAttribute('data-daytab');
        dayTabs.forEach(function (x) {
          x.classList.toggle('on', x === t);
        });
        document.querySelectorAll('[data-day]').forEach(function (col) {
          col.classList.toggle('day-hidden', col.getAttribute('data-day') !== d);
        });
      });
    });
  }
})();
