// Murlidhar Mohol site — v9. Authored by Xonware Technologies.
(function () {
  // Header shrink
  var header = document.querySelector('.header');
  window.addEventListener('scroll', function () {
    if (header) header.classList.toggle('shrink', window.scrollY > 40);
  }, { passive: true });

  // Mobile nav
  var nav = document.getElementById('nav');
  var toggle = document.getElementById('navToggle');
  if (toggle && nav) {
    toggle.addEventListener('click', function () {
      var open = nav.classList.toggle('open');
      toggle.textContent = open ? '✕' : '☰';
    });
    nav.addEventListener('click', function (e) {
      if (e.target.closest('.dropdown > a')) {
        e.preventDefault();
        e.target.closest('.dropdown').classList.toggle('open');
        return;
      }
      if (e.target.tagName === 'A') { nav.classList.remove('open'); toggle.textContent = '☰'; }
    });
  }

  // Scroll reveal
  var items = document.querySelectorAll('.reveal');
  if ('IntersectionObserver' in window) {
    var io = new IntersectionObserver(function (entries) {
      entries.forEach(function (en) {
        if (en.isIntersecting) { en.target.classList.add('in'); io.unobserve(en.target); }
      });
    }, { threshold: 0.12 });
    items.forEach(function (el) { io.observe(el); });
  } else { items.forEach(function (el) { el.classList.add('in'); }); }

  // Animated counters (Devanagari digits)
  var dev = ['०','१','२','३','४','५','६','७','८','९'];
  function toDev(n) { return String(n).replace(/\d/g, function (d) { return dev[d]; }); }
  var nums = document.querySelectorAll('.num[data-target]');
  if (nums.length && 'IntersectionObserver' in window) {
    var cio = new IntersectionObserver(function (entries) {
      entries.forEach(function (en) {
        if (!en.isIntersecting) return;
        var el = en.target, target = parseInt(el.dataset.target, 10);
        var suffix = el.dataset.suffix || '', t0 = null;
        function step(ts) {
          if (!t0) t0 = ts;
          var p = Math.min((ts - t0) / 1400, 1);
          el.textContent = toDev(Math.round(target * (1 - Math.pow(1 - p, 3)))) + suffix;
          if (p < 1) requestAnimationFrame(step);
        }
        requestAnimationFrame(step);
        cio.unobserve(el);
      });
    }, { threshold: 0.5 });
    nums.forEach(function (el) { cio.observe(el); });
  } else {
    nums.forEach(function (el) { el.textContent = toDev(el.dataset.target) + (el.dataset.suffix || ''); });
  }

  // Lightbox
  var lb = document.getElementById('lightbox');
  if (lb) {
    var lbImg = lb.querySelector('img');
    document.querySelectorAll('[data-lightbox]').forEach(function (a) {
      a.addEventListener('click', function (e) {
        e.preventDefault();
        lbImg.src = a.getAttribute('href');
        lb.classList.add('open');
      });
    });
    lb.addEventListener('click', function (e) {
      if (e.target !== lbImg) lb.classList.remove('open');
    });
    document.addEventListener('keydown', function (e) {
      if (e.key === 'Escape') lb.classList.remove('open');
    });
  }

  // Back to top
  var toTop = document.getElementById('toTop');
  if (toTop) {
    window.addEventListener('scroll', function () {
      toTop.classList.toggle('show', window.scrollY > 500);
    }, { passive: true });
    toTop.addEventListener('click', function () {
      window.scrollTo({ top: 0, behavior: 'smooth' });
    });
  }
})();
