(function ($) {
    "use strict";

    // Spinner
    var spinner = function () {
        setTimeout(function () {
            if ($('#spinner').length > 0) {
                $('#spinner').removeClass('show');
            }
        }, 1);
    };
    spinner();
    
    
    // Initiate the wowjs
    new WOW({
        offset: 60,
        mobile: false,
        live: false
    }).init();

    // ── Typewriter effect (hero) ────────────────────────────
    const heroTyped = document.getElementById('hero-typed');
    if (heroTyped) {
        const strings = [
            'soporte técnico especializado.',
            'soluciones de redes y servidores.',
            'hosting y mail marketing.',
            'ventas de productos informáticos.',
        ];
        let si = 0, ci = 0, deleting = false, delay = 110;

        function typeLoop() {
            const cur = strings[si];
            heroTyped.textContent = deleting
                ? cur.substring(0, ci - 1)
                : cur.substring(0, ci + 1);
            deleting ? ci-- : ci++;

            if (!deleting && ci === cur.length) {
                delay = 1800; deleting = true;
            } else if (deleting && ci === 0) {
                deleting = false;
                si = (si + 1) % strings.length;
                delay = 380;
            } else {
                delay = deleting ? 55 : 110;
            }
            setTimeout(typeLoop, delay);
        }
        setTimeout(typeLoop, 800);
    }


    // Sticky Navbar + transparente sobre fondos oscuros
    var hasDarkHero = document.querySelector('.hero-dark') || document.querySelector('.page-hero');
    var navbar = document.querySelector('.navbar');
    var brandLogo = document.querySelector('.navbar-brand img');

    function syncNavbarLogo() {
        if (!navbar || !brandLogo) {
            return;
        }

        var defaultLogo = brandLogo.dataset.logoDefault;
        var homeLogo = brandLogo.dataset.logoHome;
        brandLogo.src = navbar.classList.contains('navbar-home') ? homeLogo : defaultLogo;
    }

    if (hasDarkHero) { $('.navbar').addClass('navbar-home'); }
    syncNavbarLogo();

    $(window).scroll(function () {
        if ($(this).scrollTop() > 45) {
            $('.navbar').addClass('sticky-top shadow-sm').removeClass('navbar-home');
        } else {
            $('.navbar').removeClass('sticky-top shadow-sm');
            if (hasDarkHero) { $('.navbar').addClass('navbar-home'); }
        }

        syncNavbarLogo();
    });
    
    
    // Dropdown on mouse hover
    const $dropdown = $(".dropdown");
    const $dropdownToggle = $(".dropdown-toggle");
    const $dropdownMenu = $(".dropdown-menu");
    const showClass = "show";
    
    $(window).on("load resize", function() {
        if (this.matchMedia("(min-width: 992px)").matches) {
            $dropdown.hover(
            function() {
                const $this = $(this);
                $this.addClass(showClass);
                $this.find($dropdownToggle).attr("aria-expanded", "true");
                $this.find($dropdownMenu).addClass(showClass);
            },
            function() {
                const $this = $(this);
                $this.removeClass(showClass);
                $this.find($dropdownToggle).attr("aria-expanded", "false");
                $this.find($dropdownMenu).removeClass(showClass);
            }
            );
        } else {
            $dropdown.off("mouseenter mouseleave");
        }
    });
    
    
    // Back to top button
    $(window).scroll(function () {
        if ($(this).scrollTop() > 300) {
            $('.back-to-top').fadeIn('slow');
        } else {
            $('.back-to-top').fadeOut('slow');
        }
    });
    $('.back-to-top').click(function () {
        $('html, body').animate({scrollTop: 0}, 1500, 'easeInOutExpo');
        return false;
    });


    // Facts counter
    $('[data-toggle="counter-up"]').counterUp({
        delay: 10,
        time: 2000
    });


    // Testimonials carousel
    $(".testimonial-carousel").owlCarousel({
        autoplay: true,
        smartSpeed: 1000,
        margin: 25,
        dots: true,
        loop: true,
        center: true,
        responsive: {
            0:{
                items:1
            },
            576:{
                items:1
            },
            768:{
                items:2
            },
            992:{
                items:3
            }
        }
    });
    
})(jQuery);

