(function () {
  function ready(fn) {
    if (document.readyState !== 'loading') fn();
    else document.addEventListener('DOMContentLoaded', fn);
  }

  ready(function () {
    var navToggle = document.querySelector('.nav-toggle');
    var nav = document.querySelector('.main-nav');
    if (navToggle && nav) {
      navToggle.addEventListener('click', function () {
        nav.classList.toggle('open');
      });
    }

    var slides = document.querySelectorAll('.hero-slide');
    var dots = document.querySelectorAll('.hero-dots button');
    var active = 0;
    function showSlide(index) {
      if (!slides.length) return;
      active = (index + slides.length) % slides.length;
      slides.forEach(function (slide, i) { slide.classList.toggle('active', i === active); });
      dots.forEach(function (dot, i) { dot.classList.toggle('active', i === active); });
    }
    dots.forEach(function (dot) {
      dot.addEventListener('click', function () {
        showSlide(parseInt(dot.getAttribute('data-target'), 10) || 0);
      });
    });
    if (slides.length > 1) {
      window.setInterval(function () { showSlide(active + 1); }, 5500);
    }

    document.querySelectorAll('.thumb').forEach(function (btn) {
      btn.addEventListener('click', function () {
        var src = btn.getAttribute('data-src');
        var img = document.querySelector('#mainPhoto img');
        if (src && img) img.setAttribute('src', src);
        document.querySelectorAll('.thumb').forEach(function (item) { item.classList.remove('active'); });
        btn.classList.add('active');
      });
    });
  });
})();
