(function ($) {
    "use strict";

    // Spinner - removed for faster loading
    
    
    // WOW.js removed for better performance


    // Sticky Navbar - optimized with throttling
    let ticking = false;
    $(window).scroll(function () {
        if (!ticking) {
            window.requestAnimationFrame(function() {
                if ($(window).scrollTop() > 90) {
                    $('.nav-bar').addClass('sticky-top shadow');
                } else {
                    $('.nav-bar').removeClass('sticky-top shadow');
                }
                ticking = false;
            });
            ticking = true;
        }
    });
    
    
    // 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 - optimized with throttling
    let backToTopTicking = false;
    $(window).scroll(function () {
        if (!backToTopTicking) {
            window.requestAnimationFrame(function() {
                if ($(window).scrollTop() > 300) {
                    $('.back-to-top').fadeIn(200);
                } else {
                    $('.back-to-top').fadeOut(200);
                }
                backToTopTicking = false;
            });
            backToTopTicking = true;
        }
    });
    $('.back-to-top').click(function () {
        $('html, body').animate({scrollTop: 0}, 600);
        return false;
    });


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


    // Modal Video
    $(document).ready(function () {
        var $videoSrc;
        $('.btn-play').click(function () {
            $videoSrc = $(this).data("src");
        });
        console.log($videoSrc);

        $('#videoModal').on('shown.bs.modal', function (e) {
            $("#video").attr('src', $videoSrc + "?autoplay=1&amp;modestbranding=1&amp;showinfo=0");
        })

        $('#videoModal').on('hide.bs.modal', function (e) {
            $("#video").attr('src', $videoSrc);
        })
    });


    // Header carousel - optimized
    $(".header-carousel").owlCarousel({
        autoplay: true,
        smartSpeed: 800,
        items: 1,
        dots: false,
        loop: true,
        nav : true,
        navText : [
            '<i class="bi bi-chevron-left"></i>',
            '<i class="bi bi-chevron-right"></i>'
        ]
    });


    // Service carousel - optimized
    $(".service-carousel").owlCarousel({
        autoplay: true,
        smartSpeed: 600,
        center: true,
        margin: 25,
        dots: true,
        loop: true,
        nav : false,
        responsive: {
            0:{
                items:1
            },
            576:{
                items:2
            },
            768:{
                items:3
            },
            992:{
                items:2
            },
            1200:{
                items:3
            }
        }
    });


    // Testimonials carousel - optimized
    $(".testimonial-carousel").owlCarousel({
        autoplay: true,
        smartSpeed: 600,
        center: true,
        dots: false,
        loop: true,
        nav : true,
        navText : [
            '<i class="bi bi-arrow-left"></i>',
            '<i class="bi bi-arrow-right"></i>'
        ],
        responsive: {
            0:{
                items:1
            },
            768:{
                items:2
            },
            992:{
                items:3
            }
        }
    });

    // Service cards -> detail sections (per-service pages)
    $(function () {
        var $details = $('.service-detail');

        function openServiceDetail(targetId) {
            if (!targetId) return;
            var $target = $(targetId);
            if (!$target.length) return;

            // Activate selected detail, hide others
            $details.removeClass('is-active');
            $target.addClass('is-active');

            // Smooth scroll, with small offset for navbar
            var offsetTop = $target.offset().top - 90;
            $('html, body').animate({ scrollTop: offsetTop }, 600);
        }

        // Click on arrow button inside service cards
        $('.service-card-trigger').on('click', function (e) {
            var targetId = $(this).data('service-target');
            if (targetId && targetId.charAt(0) === '#') {
                e.preventDefault();
                openServiceDetail(targetId);
            }
        });

        // Handle direct hash navigation on load (e.g. index.html#service-opremanje)
        var initialHash = window.location.hash;
        if (initialHash && $(initialHash).hasClass('service-detail')) {
            // Defer to ensure layout is ready
            setTimeout(function () {
                openServiceDetail(initialHash);
            }, 150);
        }

        // Close service detail and scroll back to service cards
        function closeServiceDetail() {
            $details.removeClass('is-active');
            if (window.history && window.history.replaceState) {
                window.history.replaceState(null, '', window.location.pathname + window.location.search);
            }
            var $usluge = $('#usluge');
            if ($usluge.length) {
                var offsetTop = $usluge.offset().top - 90;
                $('html, body').animate({ scrollTop: offsetTop }, 500);
            }
        }

        $('.service-detail-close').on('click', function () {
            closeServiceDetail();
        });
    });
 
})(jQuery);

