/* ─────────────────────────────────────────
   main.js — Nest Homestay (all JS here)
───────────────────────────────────────── */

document.addEventListener('DOMContentLoaded', function () {

  /* ════════════════════════════════════════
     1. GALLERY SLIDER
  ════════════════════════════════════════ */
  (function initGallery() {
    const track   = document.getElementById('galleryTrack');
    const dotsEl  = document.getElementById('galleryDots');
    const prevBtn = document.getElementById('galleryPrev');
    const nextBtn = document.getElementById('galleryNext');
    if (!track) return;

    const slides = track.querySelectorAll('.gallery-slide');
    let current  = 0;
    let timer;

    /* Build dots to match however many slides are in the HTML */
    slides.forEach(function (_, i) {
      const dot = document.createElement('button');
      dot.className = 'gallery-dot' + (i === 0 ? ' active' : '');
      dot.dataset.i = i;
      dotsEl.appendChild(dot);
    });

    function goTo(n) {
      current = (n + slides.length) % slides.length;
      track.style.transform = 'translateX(-' + (current * 100) + '%)';
      dotsEl.querySelectorAll('.gallery-dot').forEach(function (dot, i) {
        dot.classList.toggle('active', i === current);
      });
    }

    function resetTimer() {
      clearInterval(timer);
      timer = setInterval(function () { goTo(current + 1); }, 4500);
    }

    prevBtn.addEventListener('click', function () { goTo(current - 1); resetTimer(); });
    nextBtn.addEventListener('click', function () { goTo(current + 1); resetTimer(); });
    dotsEl.addEventListener('click', function (e) {
      if (e.target.dataset.i !== undefined) { goTo(+e.target.dataset.i); resetTimer(); }
    });

    resetTimer();
  })();


  /* ════════════════════════════════════════
     2. TESTIMONIALS SLIDER
  ════════════════════════════════════════ */
  (function initTestimonials() {
    const track   = document.getElementById('testiTrack');
    const dotsEl  = document.getElementById('testiDots');
    const prevBtn = document.getElementById('testiPrev');
    const nextBtn = document.getElementById('testiNext');
    if (!track) return;

    const slides = track.querySelectorAll('.testi-slide');
    let current  = 0;

    /* Build dots */
    slides.forEach(function (_, i) {
      const dot = document.createElement('button');
      dot.className = 'testi-dot' + (i === 0 ? ' active' : '');
      dot.dataset.i = i;
      dotsEl.appendChild(dot);
    });

    function goTo(n) {
      current = (n + slides.length) % slides.length;
      track.style.transform = 'translateX(-' + (current * 100) + '%)';
      dotsEl.querySelectorAll('.testi-dot').forEach(function (dot, i) {
        dot.classList.toggle('active', i === current);
      });
    }

    prevBtn.addEventListener('click', function () { goTo(current - 1); });
    nextBtn.addEventListener('click', function () { goTo(current + 1); });
    dotsEl.addEventListener('click', function (e) {
      if (e.target.dataset.i !== undefined) goTo(+e.target.dataset.i);
    });
  })();


 /* ════════════════════════════════════════
   CONTACT FORM (VALIDATION + SUBMIT)
════════════════════════════════════════ */
(function initContactForm() {
  const form = document.getElementById('contactForm');
  if (!form) return;

  form.addEventListener('submit', function (e) {

    const name    = form.querySelector('input[name="name"]').value.trim();
    const phone   = form.querySelector('input[name="phone"]').value.trim();
    const email   = form.querySelector('input[name="email"]').value.trim();
    const message = form.querySelector('textarea[name="message"]').value.trim();

    // Basic validation
    if (!name || !phone || !email || !message) {
      e.preventDefault(); // stop ONLY if invalid
      alert('Please fill all fields.');
      return;
    }

    // Optional: simple email check
    const emailPattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;
    if (!email.match(emailPattern)) {
      e.preventDefault();
      alert('Please enter a valid email.');
      return;
    }

    // ✅ If everything is valid → form will submit to PHP
    alert('Sending message...');
  });
})();


  /* ════════════════════════════════════════
     4. ACTIVE NAV ON SCROLL
  ════════════════════════════════════════ */
  (function initActiveNav() {
    const sections = document.querySelectorAll('section[id], div[id]');
    const navLinks = document.querySelectorAll('.nav-links a');

    window.addEventListener('scroll', function () {
      let currentId = '';
      sections.forEach(function (s) {
        if (window.scrollY >= s.offsetTop - 70) currentId = s.id;
      });
      navLinks.forEach(function (a) {
        a.classList.toggle('active', a.getAttribute('href') === '#' + currentId);
      });
    });
  })();

});