/*
Theme Name: Elito Child
Theme URI: http://themeforest.net/user/wpoceans/portfolio/
Author: wpoceans
Author URI: http://themeforest.net/user/wpoceans
Description: Elito - Portfolio WordPress Theme
Template:	elito
Version: 1.0
Text Domain: elito-child
*/

<?php
// Función para corregir la jerarquía de los encabezados
function corregir_jerarquia_encabezados_js() {
    ?>
    <script type="text/javascript">
		console.log('Ejecutando')
        document.addEventListener('DOMContentLoaded', function() {
            let headers = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
            let previousLevel = 0; // Para almacenar el nivel de encabezado anterior

            headers.forEach(function(header) {
                let currentLevel = parseInt(header.tagName[1]);
                let originalFontSize = window.getComputedStyle(header).fontSize;

                if (currentLevel > previousLevel + 1) {
                    let newLevel = previousLevel + 1;
                    header.outerHTML = `<h${newLevel} class="${header.className}" style="font-size: ${originalFontSize}; ${header.getAttribute('style') || ''}">${header.innerHTML}</h${newLevel}>`;
                    currentLevel = newLevel;
                }

                previousLevel = currentLevel;
            });
        });
    </script>
    <?php
}
add_action('wp_footer', 'corregir_jerarquia_encabezados_js');

// Función para inicializar los contadores en los encabezados
function inicializar_contadores_en_encabezados() {
    ?>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            let headersWithCounters = document.querySelectorAll('h1 .mil-counter, h2 .mil-counter, h3 .mil-counter, h4 .mil-counter, h5 .mil-counter, h6 .mil-counter');
            headersWithCounters.forEach(function(counter) {
                if (!counter.hasAttribute('data-initialized')) {
                    const target = +counter.getAttribute('data-number');
                    let current = 0;
                    const increment = target / 100;

                    const updateCounter = () => {
                        if (current < target) {
                            current = Math.min(current + increment, target);
                            counter.innerText = Math.ceil(current);
                            requestAnimationFrame(updateCounter);
                        } else {
                            counter.innerText = target;
                            counter.setAttribute('data-initialized', 'true');
                        }
                    };

                    updateCounter();
                }
            });
        });
    </script>
    <?php
}
add_action('wp_footer', 'inicializar_contadores_en_encabezados');

// Función para corregir el atributo alt de las imágenes
function corregir_alt_agregar_imagen() {
    ?>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function() {
            const images = document.querySelectorAll('img');
            images.forEach(function(image) {
                const altText = image.getAttribute('alt');
                if (altText) {
                    image.setAttribute('alt', altText + ' imagen');
                } else {
                    image.setAttribute('alt', 'Imagen');
                }
            });
        });
    </script>
    <?php
}
add_action('wp_footer', 'corregir_alt_agregar_imagen');
?>