// Murlidhar Mohol site — v10 "तिरंगा". Original code · Xonware Technologies.
(function () {
  var reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

  // Preloader
  var loader = document.getElementById('loader');
  window.addEventListener('load', function () {
    if (loader) setTimeout(function () { loader.classList.add('gone'); }, reduced ? 0 : 350);
  });
  setTimeout(function () { if (loader) loader.classList.add('gone'); }, 2500); // failsafe

  // Scroll progress thread + header shrink
  var progress = document.getElementById('progress');
  var header = document.querySelector('.header');
  var ring = document.querySelector('#toTop circle');
  var toTop = document.getElementById('toTop');
  function onScroll() {
    var h = document.documentElement;
    var max = h.scrollHeight - h.clientHeight;
    var p = max > 0 ? h.scrollTop / max : 0;
    if (progress) progress.style.transform = 'scaleX(' + p + ')';
    if (header) header.classList.toggle('shrink', h.scrollTop > 40);
    if (toTop) toTop.classList.toggle('show', h.scrollTop > 500);
    if (ring) ring.style.strokeDashoffset = 151 - 151 * p;
  }
  window.addEventListener('scroll', onScroll, { passive: true });
  onScroll();
  if (toTop) toTop.addEventListener('click', function () {
    window.scrollTo({ top: 0, behavior: reduced ? 'auto' : 'smooth' });
  });

  // Mobile nav (full-screen overlay)
  var nav = document.getElementById('nav');
  var toggle = document.getElementById('navToggle');
  if (toggle && nav) {
    toggle.addEventListener('click', function () {
      var open = nav.classList.toggle('open');
      document.body.classList.toggle('nav-lock', open);
      toggle.textContent = open ? '✕' : '☰';
    });
    nav.addEventListener('click', function (e) {
      var dd = e.target.closest('.dropdown > a');
      if (dd && getComputedStyle(toggle).display !== 'none') {
        e.preventDefault(); dd.parentElement.classList.toggle('open'); return;
      }
      if (e.target.tagName === 'A') {
        nav.classList.remove('open');
        document.body.classList.remove('nav-lock');
        toggle.textContent = '☰';
      }
    });
  }

  // HERO SLIDER — crossfade + Ken Burns + tricolor progress dots + swipe
  var slider = document.querySelector('.hero-slider');
  if (slider) {
    var slides = slider.querySelectorAll('.slide');
    var dots = slider.querySelectorAll('.dot');
    var idx = 0, timer = null, MS = 6500;
    slider.style.setProperty('--slide-ms', MS + 'ms');
    function go(n) {
      idx = (n + slides.length) % slides.length;
      slides.forEach(function (s, i) { s.classList.toggle('on', i === idx); });
      dots.forEach(function (d, i) {
        d.classList.remove('on');
        var f = d.querySelector('.fill'); if (f) { f.style.animation = 'none'; void f.offsetWidth; f.style.animation = ''; }
        if (i === idx) d.classList.add('on');
      });
      restart();
    }
    function restart() {
      clearInterval(timer);
      if (!reduced) timer = setInterval(function () { go(idx + 1); }, MS);
    }
    dots.forEach(function (d, i) { d.addEventListener('click', function () { go(i); }); });
    var prev = slider.querySelector('.arrow.prev'), next = slider.querySelector('.arrow.next');
    if (prev) prev.addEventListener('click', function () { go(idx - 1); });
    if (next) next.addEventListener('click', function () { go(idx + 1); });
    var x0 = null;
    slider.addEventListener('touchstart', function (e) { x0 = e.touches[0].clientX; }, { passive: true });
    slider.addEventListener('touchend', function (e) {
      if (x0 === null) return;
      var dx = e.changedTouches[0].clientX - x0;
      if (Math.abs(dx) > 48) go(idx + (dx < 0 ? 1 : -1));
      x0 = null;
    }, { passive: true });
    document.addEventListener('visibilitychange', function () {
      if (document.hidden) clearInterval(timer); else restart();
    });
    go(0);
  }

  // Reveal on scroll
  var items = document.querySelectorAll('.reveal');
  if ('IntersectionObserver' in window && !reduced) {
    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]');
  function setFinal(el) { el.textContent = toDev(el.dataset.target) + (el.dataset.suffix || ''); }
  if (nums.length && 'IntersectionObserver' in window && !reduced) {
    var cio = new IntersectionObserver(function (entries) {
      entries.forEach(function (en) {
        if (!en.isIntersecting) return;
        var el = en.target, target = parseInt(el.dataset.target, 10), 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(setFinal); }

  // Gallery 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'); });
  }

  // Subtle card tilt (pointer devices only)
  if (matchMedia('(hover:hover) and (pointer:fine)').matches && !reduced) {
    document.querySelectorAll('.card').forEach(function (card) {
      card.addEventListener('mousemove', function (e) {
        var r = card.getBoundingClientRect();
        var rx = ((e.clientY - r.top) / r.height - .5) * -4;
        var ry = ((e.clientX - r.left) / r.width - .5) * 4;
        card.style.transform = 'translateY(-7px) rotateX(' + rx + 'deg) rotateY(' + ry + 'deg)';
      });
      card.addEventListener('mouseleave', function () { card.style.transform = ''; });
    });
  }
})();
