// Slider functionality
const slides = document.querySelectorAll('.slide');
let currentSlide = 0;

function showSlide(index) {
    slides.forEach((slide, i) => {
        slide.classList.toggle('active', i === index);
    });
}

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

function prevSlide() {
    currentSlide = (currentSlide - 1 + slides.length) % slides.length;
    showSlide(currentSlide);
}

if (slides.length > 0) {
    showSlide(currentSlide);
    setInterval(nextSlide, 5000);
}

// Video mute/unmute
const video = document.getElementById('security-video');
const muteBtn = document.getElementById('mute-btn');

if (video && muteBtn) {
    muteBtn.addEventListener('click', () => {
        video.muted = !video.muted;
        muteBtn.textContent = video.muted ? '🔇' : '🔊';
    });
}

// Mobile navigation dropdown
const navToggle = document.querySelector('.nav-toggle');
const nav = document.querySelector('nav');
const navLinks = document.querySelectorAll('.nav-links a');

if (nav && navToggle) {
    navToggle.addEventListener('click', () => {
        const isOpen = nav.classList.toggle('nav-open');
        navToggle.setAttribute('aria-expanded', String(isOpen));
    });

    navLinks.forEach((link) => {
        link.addEventListener('click', () => {
            if (window.innerWidth <= 768) {
                nav.classList.remove('nav-open');
                navToggle.setAttribute('aria-expanded', 'false');
            }
        });
    });

    window.addEventListener('resize', () => {
        if (window.innerWidth > 768) {
            nav.classList.remove('nav-open');
            navToggle.setAttribute('aria-expanded', 'false');
        }
    });
}

// Services accordion behavior
const serviceBlocks = document.querySelectorAll('.services-page .service-block');

if (serviceBlocks.length > 0) {
    serviceBlocks.forEach((block) => {
        const heading = block.querySelector('h3');

        if (!heading) {
            return;
        }

        heading.setAttribute('role', 'button');
        heading.setAttribute('tabindex', '0');
        heading.setAttribute('aria-expanded', 'false');

        const toggleBlock = () => {
            const isOpen = block.classList.toggle('is-open');
            heading.setAttribute('aria-expanded', String(isOpen));
        };

        heading.addEventListener('click', toggleBlock);
        heading.addEventListener('keydown', (event) => {
            if (event.key === 'Enter' || event.key === ' ') {
                event.preventDefault();
                toggleBlock();
            }
        });
    });
}

// Homepage services accordion behavior
const homeServiceCards = document.querySelectorAll('.services-section .service-card');

if (homeServiceCards.length > 0) {
    homeServiceCards.forEach((card) => {
        const heading = card.querySelector('h3');

        if (!heading) {
            return;
        }

        heading.setAttribute('role', 'button');
        heading.setAttribute('tabindex', '0');
        heading.setAttribute('aria-expanded', 'false');

        const toggleCard = () => {
            const isOpen = card.classList.toggle('is-open');
            heading.setAttribute('aria-expanded', String(isOpen));
        };

        heading.addEventListener('click', toggleCard);
        heading.addEventListener('keydown', (event) => {
            if (event.key === 'Enter' || event.key === ' ') {
                event.preventDefault();
                toggleCard();
            }
        });
    });
}