/*
Theme Name: FC United Child Theme
Theme URI: https://fc-united.axiomthemes.com
Description: FC United is a Premium WordPress theme that has built-in support for popular Page Builders, slider with swipe gestures, and is SEO- and Retina-ready. The unique system of inheritance and override options allows setting up individual parameters for different sections of your site and supported plugins.
Author: AxiomThemes
Author URI: https://axiomthemes.com
Version: 2.0.1
Tested up to: 6.9
Requires at least: 5.5
Requires PHP: 7.4
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: blog, e-commerce, portfolio, grid-layout, one-column, two-columns, three-columns, four-columns, left-sidebar, right-sidebar, custom-background, custom-colors, custom-header, custom-logo, custom-menu, editor-style, featured-image-header, featured-images, flexible-header, footer-widgets, full-width-template, microformats, post-formats, sticky-post, theme-options, threaded-comments, translation-ready, block-styles, wide-blocks
Text Domain: fc-united
Template: fc-united
*/


/* =Child-Theme customization starts here
------------------------------------------------------------ */
/* Obliga a la tabla interna y a sus casillas a borrar el fondo blanco */
document.addEventListener("DOMContentLoaded", function () {

    function procesarTarjetas() {
        document.querySelectorAll('.sp-template-event-blocks').forEach(function (tarjeta) {

            // Evitar doble ejecución
            if (tarjeta.dataset.procesada === "1") return;
            tarjeta.dataset.procesada = "1";

            var tabla = tarjeta.querySelector('table.sp-event-blocks');
            if (!tabla) return;

            var torneoLiga = tarjeta.querySelector('h4');
            var estadio    = tarjeta.querySelector('.sp-event-venue');
            var fecha      = tarjeta.querySelector('.sp-event-date');
            var resultado  = tarjeta.querySelector('.sp-event-results');
            var hora       = tarjeta.querySelector('.sp-event-time');

            // Ocultar N/D de forma limpia (sin romper layout)
            if (resultado) {
                var textoResultado = resultado.textContent.trim();
                if (textoResultado === "N/D" || textoResultado === "") {
                    resultado.style.visibility = "hidden";
                } else {
                    // Asegurar visibilidad y estilo del resultado real
                    resultado.style.visibility = "visible";
                    resultado.style.display = "block";
                    var linkResultado = resultado.querySelector('a');
                    if (linkResultado) {
                        linkResultado.style.color = "#ffffff";
                        linkResultado.style.textDecoration = "none";
                    }
                    // Si hay resultado, ocultar la hora para no duplicar
                    if (hora) hora.style.display = "none";
                }
            }

            // Reordenar: Estadio arriba de la tabla
            if (estadio) {
                tabla.parentNode.insertBefore(estadio, tabla);
            }

            // Reordenar: Fecha justo debajo del torneo (h4), antes de la tabla
            if (torneoLiga && fecha) {
                torneoLiga.parentNode.insertBefore(fecha, torneoLiga.nextSibling);
            }

            // Inyectar botones solo una vez
            if (tarjeta.querySelector('.botones-partido-final')) return;

            var contenedorBotones = document.createElement('div');
            contenedorBotones.className = 'botones-partido-final';
            contenedorBotones.innerHTML =
                '<div class="linea-separadora-botones">' +
                    '<a href="https://wa.me/56954676864" target="_blank" class="btn-abonar">Abónate</a>' +
                    '<a href="https://marketicket.cl" target="_blank" class="btn-ticket">Ticket 🔸</a>' +
                '</div>';

            tarjeta.appendChild(contenedorBotones);
        });
    }

    // Primer intento inmediato
    procesarTarjetas();

    // Segundo intento a los 300ms por si SportsPress carga tarde via AJAX
    setTimeout(procesarTarjetas, 300);

    // Tercer intento a 800ms para páginas muy lentas o con caché
    setTimeout(procesarTarjetas, 800);
});
