document.addEventListener('DOMContentLoaded', function () {
  const topBar = document.querySelector('.top-bar');
  const header = document.querySelector('.site-header');
  const messages = document.querySelectorAll('.top-bar-message');

  if (!topBar || !header || messages.length === 0) return;

  let lastScrollY = window.scrollY || 0;
  let currentIndex = 0;
  const intervalTime = 3000; // 3 seconds

  // -----------------------------
  // Messages rotation
  // -----------------------------
  function showMessage(index) {
    messages.forEach((el, i) => {
      el.style.display = i === index ? 'inline-block' : 'none';
    });
  }

  showMessage(currentIndex);

  setInterval(function () {
    currentIndex = (currentIndex + 1) % messages.length;
    showMessage(currentIndex);
  }, intervalTime);

  // -----------------------------
  // Scroll behavior (Napoleon)
  // -----------------------------
  window.addEventListener(
    'scroll',
    function () {
      const currentY = window.scrollY || 0;

      if (currentY > lastScrollY && currentY > 50) {
        // scrolling down
        topBar.classList.add('top-bar-hidden');
        header.style.top = '0px';
      } else {
        // scrolling up
        topBar.classList.remove('top-bar-hidden');
        header.style.top = 'var(--top-bar-height)';
      }

      lastScrollY = currentY;
    },
    { passive: true }
  );
});
