<!-- Footer Ad -->
<?php 
$footer_ad = get_option('pulsegrid_footer_ad', '');
if (!empty($footer_ad)) : 
?>
<div class="footer-ad-container">
    <div class="container">
        <?php echo $footer_ad; ?>
    </div>
</div>
<?php endif; ?>

<footer class="site-footer">
    <div class="container">
        <div class="footer-content">
            <?php 
            $footer_description = get_theme_mod('pulsegrid_footer_description', 'PulseGrid - Tu portal de descarga de juegos favorito.');
            if (!empty($footer_description)) : 
            ?>
            <p class="footer-description"><?php echo esc_html($footer_description); ?></p>
            <?php endif; ?>
            
            <?php 
            $footer_copyright = get_theme_mod('pulsegrid_footer_copyright', '© ' . date('Y') . ' PulseGrid. Todos los derechos reservados.');
            ?>
            <p class="footer-copyright"><?php echo wp_kses_post($footer_copyright); ?></p>
        </div>
    </div>
</footer>

<!-- Back to Top Button -->
<button id="back-to-top" class="back-to-top" aria-label="Volver arriba">
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <polyline points="18 15 12 9 6 15"></polyline>
    </svg>
</button>

<script>
// Trailer Modal Functionality
document.addEventListener('DOMContentLoaded', function() {
    const trailerBtns = document.querySelectorAll('.featured-trailer-btn');
    const trailerModal = document.getElementById('trailer-modal');
    const trailerIframe = document.getElementById('trailer-iframe');
    const modalClose = document.querySelector('.trailer-modal-close');
    const modalOverlay = document.querySelector('.trailer-modal-overlay');
    
    if (trailerBtns.length > 0 && trailerModal) {
        // Open modal - attach to all trailer buttons
        trailerBtns.forEach(function(trailerBtn) {
            trailerBtn.addEventListener('click', function() {
                const trailerUrl = this.getAttribute('data-trailer-url');
                
                // Convert YouTube URL to embed format
                let embedUrl = trailerUrl;
                if (trailerUrl.includes('youtube.com/watch?v=')) {
                    const videoId = trailerUrl.split('v=')[1].split('&')[0];
                    embedUrl = 'https://www.youtube.com/embed/' + videoId + '?autoplay=1';
                } else if (trailerUrl.includes('youtu.be/')) {
                    const videoId = trailerUrl.split('youtu.be/')[1].split('?')[0];
                    embedUrl = 'https://www.youtube.com/embed/' + videoId + '?autoplay=1';
                }
                
                trailerIframe.src = embedUrl;
                trailerModal.classList.add('active');
                document.body.style.overflow = 'hidden';
            });
        });
        
        // Close modal function
        function closeModal() {
            trailerModal.classList.remove('active');
            trailerIframe.src = '';
            document.body.style.overflow = '';
        }
        
        // Close on button click
        if (modalClose) {
            modalClose.addEventListener('click', closeModal);
        }
        
        // Close on overlay click
        if (modalOverlay) {
            modalOverlay.addEventListener('click', closeModal);
        }
        
        // Close on ESC key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape' && trailerModal.classList.contains('active')) {
                closeModal();
            }
        });
    }
});
</script>

<?php wp_footer(); ?>
</body>
</html>
