// Navbar Scroll Effect
const navbar = document.querySelector('.navbar');

window.addEventListener('scroll', () => {
    if (window.scrollY > 50) {
        navbar.classList.add('scrolled');
    } else {
        navbar.classList.remove('scrolled');
    }
});

// Rename asset files by matching the generated prefix
async function setupAssets() {
    console.log("Assets are assumed to be correctly named via powershell script.");
}
setupAssets();


// Mobile Menu Toggle
const menuToggle = document.querySelector('.menu-toggle');
const navLinks = document.querySelector('.nav-links');

menuToggle.addEventListener('click', () => {
    navLinks.classList.toggle('active');
});

// Intersection Observer for Scroll Animations
const observerOptions = {
    root: null,
    rootMargin: '0px',
    threshold: 0.1
};

const observer = new IntersectionObserver((entries, observer) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            entry.target.style.animationPlayState = 'running';
            // Stop observing once animated
            observer.unobserve(entry.target);
        }
    });
}, observerOptions);

// Pause animations initially, they will be triggered by observer
document.querySelectorAll('.fade-in-up, .fade-in').forEach(el => {
    el.style.animationPlayState = 'paused';
    observer.observe(el);
});



// Hero Slideshow
const slides = document.querySelectorAll('.hero-slide');
const dots = document.querySelectorAll('.slider-dots .dot');
const prevBtn = document.getElementById('heroPrev');
const nextBtn = document.getElementById('heroNext');
let currentSlide = 0;
let slideInterval;

function initSlider() {
    if(slides.length === 0) return;
    showSlide(currentSlide);
    startSlideTimer();
}

function showSlide(index) {
    slides.forEach(slide => slide.classList.remove('active'));
    dots.forEach(dot => dot.classList.remove('active'));
    
    currentSlide = (index + slides.length) % slides.length;
    
    slides[currentSlide].classList.add('active');
    dots[currentSlide].classList.add('active');
}

function nextSlide() {
    showSlide(currentSlide + 1);
}

function prevSlide() {
    showSlide(currentSlide - 1);
}

function startSlideTimer() {
    slideInterval = setInterval(nextSlide, 5000);
}

function resetSlideTimer() {
    clearInterval(slideInterval);
    startSlideTimer();
}

if (prevBtn && nextBtn) {
    prevBtn.addEventListener('click', () => { prevSlide(); resetSlideTimer(); });
    nextBtn.addEventListener('click', () => { nextSlide(); resetSlideTimer(); });
}

if (dots.length > 0) {
    dots.forEach(dot => {
        dot.addEventListener('click', (e) => {
            const index = parseInt(e.target.getAttribute('data-slide'));
            showSlide(index);
            resetSlideTimer();
        });
    });
}

initSlider();

