/* SmartThink Global Technology — Website interactions */

(function () {
    'use strict';

    // Reveal on scroll
    const revealElements = document.querySelectorAll('.st-section .section-header, .st-section h2, .pillar-card, .industry-tile, .timeline-step, .lab-tile, .academy-tile, .diff-item, .recent-article, .featured-article');
    revealElements.forEach(function (el) {
        el.classList.add('reveal');
    });

    const observer = new IntersectionObserver(function (entries) {
        entries.forEach(function (entry) {
            if (entry.isIntersecting) {
                entry.target.classList.add('visible');
            }
        });
    }, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });

    document.querySelectorAll('.reveal').forEach(function (el) {
        observer.observe(el);
    });

    // Mobile menu toggle
    const mainNav = document.getElementById('mainNav');
    const menuToggle = document.querySelector('.st-mobile-toggle');
    if (menuToggle && mainNav) {
        menuToggle.addEventListener('click', function () {
            mainNav.classList.toggle('open');
            const icon = this.querySelector('i');
            if (icon) {
                icon.classList.toggle('fa-bars');
                icon.classList.toggle('fa-times');
            }
        });
    }

    // Mobile mega-menu toggles
    document.querySelectorAll('.has-mega > .st-nav-link').forEach(function (link) {
        link.addEventListener('click', function (e) {
            if (window.innerWidth < 1024) {
                e.preventDefault();
                this.parentElement.classList.toggle('open');
                const icon = this.querySelector('i');
                if (icon) {
                    icon.classList.toggle('fa-chevron-down');
                    icon.classList.toggle('fa-chevron-up');
                }
            }
        });
    });

    // Header scrolled state
    const header = document.querySelector('.st-header');
    if (header) {
        window.addEventListener('scroll', function () {
            if (window.scrollY > 80) {
                header.classList.add('scrolled');
            } else {
                header.classList.remove('scrolled');
            }
        });
    }

    // Contact page inquiry routing
    const inquiryCards = document.querySelectorAll('.inquiry-card');
    if (inquiryCards.length) {
        inquiryCards.forEach(function (card) {
            card.addEventListener('click', function () {
                const target = this.getAttribute('data-form');
                inquiryCards.forEach(function (c) { c.classList.remove('selected'); });
                this.classList.add('selected');
                document.querySelectorAll('.routed-form').forEach(function (f) { f.classList.add('d-none'); });
                const form = document.getElementById(target);
                if (form) {
                    form.classList.remove('d-none');
                    form.scrollIntoView({ behavior: 'smooth', block: 'start' });
                }
            });
        });
    }

    // Back to top
    const backToTop = document.getElementById('backToTop');
    if (backToTop) {
        window.addEventListener('scroll', function () {
            if (window.scrollY > 400) {
                backToTop.classList.add('visible');
            } else {
                backToTop.classList.remove('visible');
            }
        });
        backToTop.addEventListener('click', function () {
            window.scrollTo({ top: 0, behavior: 'smooth' });
        });
    }
})();
