/**
 * CloudForge Theme - Interactive Scripts
 * Minimal, dependency-free JavaScript for the CloudForge dark theme.
 */
(function () {
    'use strict';

    // ---- Hero Slider ----
    var slides = document.querySelectorAll('.hero-slide');
    var dots = document.querySelectorAll('.hero-dot');
    var currentSlide = 0;
    var slideInterval = null;

    function goToSlide(index) {
        if (slides.length === 0) return;
        slides[currentSlide].classList.remove('active');
        if (dots[currentSlide]) dots[currentSlide].classList.remove('active');
        currentSlide = index;
        slides[currentSlide].classList.add('active');
        if (dots[currentSlide]) dots[currentSlide].classList.add('active');
    }

    function nextSlide() {
        goToSlide((currentSlide + 1) % slides.length);
    }

    if (slides.length > 1) {
        slideInterval = setInterval(nextSlide, 6000);
        dots.forEach(function (dot, i) {
            dot.addEventListener('click', function () {
                clearInterval(slideInterval);
                goToSlide(i);
                slideInterval = setInterval(nextSlide, 6000);
            });
        });
    }

    // ---- Mobile Navigation Toggle ----
    var menuToggle = document.querySelector('.menu-toggle');
    var mainNav = document.getElementById('mainNav');
    if (menuToggle && mainNav) {
        menuToggle.addEventListener('click', function () {
            mainNav.classList.toggle('show');
        });
    }

    // ---- Submenu Toggle on Mobile ----
    var hasChildrenItems = document.querySelectorAll('.menu-item-has-children > a');
    hasChildrenItems.forEach(function (link) {
        link.addEventListener('click', function (e) {
            if (window.innerWidth <= 768) {
                e.preventDefault();
                link.parentElement.classList.toggle('expanded');
            }
        });
    });

    // ---- Back to Top ----
    var backToTop = document.querySelector('.back-to-top');
    if (backToTop) {
        window.addEventListener('scroll', function () {
            if (window.scrollY > 400) {
                backToTop.classList.add('visible');
            } else {
                backToTop.classList.remove('visible');
            }
        }, { passive: true });
        backToTop.addEventListener('click', function (e) {
            e.preventDefault();
            window.scrollTo({ top: 0, behavior: 'smooth' });
        });
    }

    // ---- Scroll Reveal (Intersection Observer) ----
    var revealElements = document.querySelectorAll('.feature-card, .product-card, .news-card, .case-card, .service-card, .team-card');
    if ('IntersectionObserver' in window && revealElements.length > 0) {
        var observer = new IntersectionObserver(function (entries) {
            entries.forEach(function (entry) {
                if (entry.isIntersecting) {
                    entry.target.classList.add('revealed');
                    observer.unobserve(entry.target);
                }
            });
        }, { threshold: 0.1, rootMargin: '0px 0px -50px 0px' });

        revealElements.forEach(function (el, i) {
            el.style.transitionDelay = (i % 4) * 80 + 'ms';
            observer.observe(el);
        });
    } else {
        // Fallback: just show them
        revealElements.forEach(function (el) {
            el.classList.add('revealed');
        });
    }

    // ---- Search Form Enhancement ----
    var searchInput = document.querySelector('.search-bar input[name="q"]');
    if (searchInput) {
        searchInput.addEventListener('keydown', function (e) {
            if (e.key === 'Enter' && this.value.trim() === '') {
                e.preventDefault();
                this.focus();
            }
        });
    }

    // ---- Product Gallery Thumbnails ----
    var thumbs = document.querySelectorAll('.product-detail-thumb');
    var mainImage = document.querySelector('.product-detail-main-image img');
    if (thumbs.length > 0 && mainImage) {
        thumbs.forEach(function (thumb) {
            var img = thumb.querySelector('img');
            thumb.addEventListener('click', function () {
                var src = img.getAttribute('src');
                mainImage.setAttribute('src', src);
                thumbs.forEach(function (t) { t.classList.remove('active'); });
                thumb.classList.add('active');
            });
        });
    }

    // ---- Sticky Header Shadow on Scroll ----
    var header = document.querySelector('.header');
    if (header) {
        window.addEventListener('scroll', function () {
            if (window.scrollY > 10) {
                header.classList.add('scrolled');
            } else {
                header.classList.remove('scrolled');
            }
        }, { passive: true });
    }

    // ---- Contact Form Validation ----
    var contactForm = document.getElementById('contactForm');
    if (contactForm) {
        contactForm.addEventListener('submit', function (e) {
            var requiredFields = contactForm.querySelectorAll('[required]');
            var valid = true;
            requiredFields.forEach(function (field) {
                if (!field.value.trim()) {
                    valid = false;
                    field.classList.add('error');
                } else {
                    field.classList.remove('error');
                }
            });
            if (!valid) {
                e.preventDefault();
            }
        });
    }

    // ---- Active Nav Highlight ----
    var path = window.location.pathname;
    var params = new URLSearchParams(window.location.search);
    var currentPage = params.get('p') || 'home';
    var navLinks = document.querySelectorAll('.main-menu > li');
    navLinks.forEach(function (li) {
        var link = li.querySelector('a');
        if (!link) return;
        var href = link.getAttribute('href');
        if (currentPage === 'home' && (href === '/' || href === '/index.php')) {
            li.classList.add('active');
        }
    });
})();
