// Actualités Page JavaScript

document.addEventListener('DOMContentLoaded', function() {
    initializeNewsBlocks();
    initializeNavigation();
});

// Initialize news block interactions
function initializeNewsBlocks() {
    const newsBlocks = document.querySelectorAll('.news-block');
    
    newsBlocks.forEach(block => {
        // Enhanced hover effects
        block.addEventListener('mouseenter', function() {
            const icon = this.querySelector('.news-icon');
            if (icon) {
                icon.style.animationPlayState = 'paused';
                icon.style.transform = 'scale(1.2) rotate(10deg)';
            }
        });
        
        block.addEventListener('mouseleave', function() {
            const icon = this.querySelector('.news-icon');
            if (icon) {
                icon.style.animationPlayState = 'running';
                icon.style.transform = 'scale(1) rotate(0deg)';
            }
        });
        
        // Add focus support for accessibility
        block.addEventListener('focus', function() {
            this.style.outline = '2px solid var(--primary-yellow)';
            this.style.outlineOffset = '2px';
        });
        
        block.addEventListener('blur', function() {
            this.style.outline = 'none';
        });
        
        // Make blocks keyboard navigable
        block.setAttribute('tabindex', '0');
        
        // Handle keyboard interaction
        block.addEventListener('keydown', function(e) {
            if (e.key === 'Enter' || e.key === ' ') {
                e.preventDefault();
                // Toggle expanded state on keyboard interaction
                this.classList.toggle('keyboard-expanded');
            }
        });
    });
}

// Initialize navigation
function initializeNavigation() {
    const navItems = document.querySelectorAll('nav li');
    
    navItems.forEach(item => {
        item.addEventListener('click', function() {
            const text = this.textContent.trim().toLowerCase();
            
            switch(text) {
                case 'accueil':
                    window.location.href = 'index.php';
                    break;
                case 'services':
                    window.location.href = 'services.php';
                    break;
                case 'réalisations':
                    window.location.href = 'realisations.php';
                    break;
                case 'actualités':
                    // Already on this page
                    break;
                case 'contact':
                    window.location.href = 'contact.php';
                    break;
            }
        });
    });
}

// Add staggered animations on scroll
function addScrollAnimations() {
    const observerOptions = {
        threshold: 0.1,
        rootMargin: '0px 0px -50px 0px'
    };
    
    const observer = new IntersectionObserver((entries) => {
        entries.forEach((entry, index) => {
            if (entry.isIntersecting) {
                setTimeout(() => {
                    entry.target.style.opacity = '1';
                    entry.target.style.transform = 'translateY(0)';
                }, index * 100);
                observer.unobserve(entry.target);
            }
        });
    }, observerOptions);
    
    const newsBlocks = document.querySelectorAll('.news-block');
    newsBlocks.forEach(block => {
        // Reset initial state for scroll animation
        block.style.opacity = '0';
        block.style.transform = 'translateY(30px)';
        block.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
        observer.observe(block);
    });
}

// Enhanced icon animations based on news type
function enhanceIconAnimations() {
    const newsBlocks = document.querySelectorAll('.news-block');
    
    newsBlocks.forEach(block => {
        const icon = block.querySelector('.news-icon');
        const newsId = block.dataset.news;
        
        if (icon) {
            // Add specific animations based on news type
            switch(newsId) {
                case '1': // Rocket for website launch
                    icon.style.animation = 'float 3s ease-in-out infinite, rocket 4s ease-in-out infinite';
                    break;
            }
        }
    });
}

// Add custom keyframe animations via CSS
function addCustomAnimations() {
    const style = document.createElement('style');
    style.textContent = `
        @keyframes rocket {
            0%, 100% { transform: translateY(0px) rotate(0deg); }
            25% { transform: translateY(-5px) rotate(-5deg); }
            75% { transform: translateY(-5px) rotate(5deg); }
        }
        
        .keyboard-expanded .news-preview {
            opacity: 0;
            visibility: hidden;
        }
        
        .keyboard-expanded .news-expanded {
            opacity: 1;
            visibility: visible;
        }
    `;
    document.head.appendChild(style);
}

// Initialize all enhancements
function initializeEnhancements() {
    addCustomAnimations();
    enhanceIconAnimations();
    
    // Add scroll animations with delay
    setTimeout(() => {
        addScrollAnimations();
    }, 500);
}

// Run enhancements when page loads
window.addEventListener('load', initializeEnhancements);

// Handle reduced motion preference
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
    const newsBlocks = document.querySelectorAll('.news-block');
    newsBlocks.forEach(block => {
        const icon = block.querySelector('.news-icon');
        if (icon) {
            icon.style.animation = 'none';
        }
    });
}