(function ($) {
    "use strict";
    /*---------------------
    === Navigation Scroll ===
    ----------------------*/

    var pgurl = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);

    $(".navbar-nav > li  a").each(function () {
        if ($(this).attr("href") == pgurl || $(this).attr("href") == '')
            $(this).addClass("active");
        $(this).parent("li").addClass("active");
    })

    $(window).on('scroll', function () {
        var sticky = $('.sticky-menu'),
            scroll = $(window).scrollTop();
        if (scroll >= 100) sticky.addClass('fixed');
        else sticky.removeClass('fixed');
    });

    /*---------------------
    === Main Slider Two  ===
    ----------------------*/
    if ($(".slider-block.animation-one .list-slider").length) {

        jQuery(".slider-block.animation-one .list-slider").slick({
            dots: false,
            arrows: true,
            prevArrow: '.prev-arrow',
            nextArrow: '.next-arrow',
            slidesToShow: 1,
            slidesToScroll: 1,
            touchThreshold: 100,
            infinite: false,
            swipe: true,
            swipeToSlide: true,
            autoplay: true,
            autoplaySpeed: 6000,
            fade: true,
            speed: 800,
            pauseOnFocus: false,
            pauseOnHover: false,
            pauseOnDotsHover: false,
            responsive: [{
                breakpoint: 768,
                settings: {
                    arrows: false
                }
            }]
        });

    }

    /*--------------------------
    === Hero Banner Animation ===
    -----------------------------*/

    var animateTextSlider = function animateTextSlider() {
        var sliderActive = document.querySelector('.slider-block .slick-list .slick-active');
        var img = sliderActive.querySelector('.bg-img img');
        var heading = sliderActive.querySelector('.container .medikia__hero-title');
        var button = sliderActive.querySelector('.container .block-button');
        var images = document.querySelectorAll('.slider-item .bg-img img');
        var headings = document.querySelectorAll('.slider-item .medikia__hero-title');
        var buttons = document.querySelectorAll('.slider-item .block-button');
        images.forEach(function (img) {
            img.classList.remove("animate__animated", "animate__fadeIn", "animate__delay-0-2s");
        });
        headings.forEach(function (p) {
            p.classList.remove("animate__animated", "animate__fadeInUp", "animate__delay-0-2s");
        });
        buttons.forEach(function (btn) {
            btn.classList.remove("animate__animated", "animate__fadeInUp", "animate__delay-0-8s");
        });
        img.classList.add("animate__animated", "animate__fadeIn", "animate__delay-0-2s");
        heading.classList.add("animate__animated", "animate__fadeInUp", "animate__delay-0-2s");
        button.classList.add("animate__animated", "animate__fadeInUp", "animate__delay-0-8s");
    };

    var prevArrow = document.querySelector('.slider-block.animation-one .prev-arrow');
    var nextArrow = document.querySelector('.slider-block.animation-one .next-arrow');
    if (prevArrow) {
        prevArrow.addEventListener('click', function () {
            animateTextSlider();
        });
        setInterval(function () {
            animateTextSlider();
        }, 100);
    }
    if (nextArrow) {
        nextArrow.addEventListener('click', function () {
            animateTextSlider();
        });
    }

    /*----------------------
    === circle box about ===
    ----------------------*/
    var textBoxCircle = document.querySelector('.text-box-circle .text-label');
    if (textBoxCircle) {
        textBoxCircle.innerHTML = textBoxCircle.innerText.split('').map(function (_char2, i) {
            return "<span style=\"transform: rotate(".concat(i * 10.5, "deg);\">").concat(_char2, "</span>");
        }).join('');
    }

    /*---------------------
    === Back To Top  ===
    ----------------------*/
    jQuery('#back-to-top').fadeOut();

    jQuery(window).on("scroll", function () {
        if (jQuery(this).scrollTop() > 250) {
            jQuery('#back-to-top').fadeIn(1400);
            jQuery('#back-to-top').addClass("active");
        } else {
            jQuery('#back-to-top').fadeOut(400);
            jQuery('#back-to-top').removeClass("active");
        }
    });
    
    jQuery('#top').on('click', function () {
        jQuery('top').tooltip('hide');
        jQuery('body,html').animate({
            scrollTop: 0
        }, 100);
        return false;
    });

    /*-----------------------
    ======== Preloader =====
    ------------------------*/
    $(window).on('load', function () {
        if ($('.preloader').length) {
            $('.preloader').fadeOut('slow');
        }
    });

    /*-----------------------
    ==== Odometer Counter =====
    ------------------------*/
    $(".medikia__counter-item").each(function () {
        $(this).isInViewport(function (status) {
            if (status === "entered") {
                for (var i = 0; i < document.querySelectorAll(".odometer").length; i++) {
                    var el = document.querySelectorAll('.odometer')[i];
                    el.innerHTML = el.getAttribute("data-odometer-final");
                }
            }
        });
    });

    /*-----------------------
    ==== Pricing Plan =====
    ------------------------*/
    function pricingTrigger() {
        $("[data-plan-active]").each(function () {
            var id = $(this).attr("data-plan-id");
            var plan = $(this).attr("data-plan-active");
            if (plan == "monthly") {
                $("[data-pricing-trigger='" + id + "'][data-target='monthly']")?.addClass("active");
                $("[data-pricing-trigger='" + id + "'][data-target='yearly']")?.removeClass("active");
                $("[data-pricing-trigger='" + id + "'].toggle")?.attr("data-target", "yearly");
                $("[data-pricing-trigger='" + id + "'].toggle")?.removeClass("active");
            } else if (plan == "yearly") {
                $("[data-pricing-trigger='" + id + "'][data-target='monthly']")?.removeClass("active");
                $("[data-pricing-trigger='" + id + "'][data-target='yearly']")?.addClass("active");
                $("[data-pricing-trigger='" + id + "'].toggle")?.addClass("active");
                $("[data-pricing-trigger='" + id + "'].toggle")?.attr("data-target", "monthly");
            }

        })
        $('[data-pricing-trigger]').on('click', function (e) {
            var id = $(e.target).attr('data-pricing-trigger');
            var target = $(e.target).attr('data-target');
            $("[data-plan-id='" + id + "'] .dynamic-value").each(function () {
                let yearPrice = $(this).attr('data-yearly');
                let monthPrice = $(this).attr('data-monthly');

                if (target == 'monthly') {
                    $(this).text(monthPrice);
                    $("[data-pricing-trigger][data-target='monthly']:not(.toggle)").addClass("active");
                    $("[data-pricing-trigger][data-target='yearly']:not(.toggle)").removeClass("active");
                    $("[data-pricing-trigger].toggle")?.removeClass("active");
                    $("[data-pricing-trigger].toggle").attr("data-target", "yearly");
                }
                if (target == 'yearly') {
                    $(this).text(yearPrice);
                    $("[data-pricing-trigger][data-target='monthly']:not(.toggle)").removeClass("active");
                    $("[data-pricing-trigger][data-target='yearly']:not(.toggle)").addClass("active");
                    $("[data-pricing-trigger].toggle")?.removeClass("active");
                    $("[data-pricing-trigger].toggle")?.addClass("active")
                    $("[data-pricing-trigger].toggle").attr("data-target", "monthly");
                }
            });
        });
    }
    pricingTrigger();

    /*-----------------------
    ==== Testimonial slider ====
    ------------------------*/
    var testimonialSlider = new Swiper(".medikia__testimonial-slider", {
        loop: true,
        freemode: true,
        slidesPerView: 3.8,
        spaceBetween: 24,
        centeredSlides: true,
        allowTouchMove: true,
        speed: 500,
        navigation: {
            nextEl: ".testimonial-button-next",
            prevEl: ".testimonial-button-prev",
        },
        breakpoints: {
            0: {
                slidesPerView: 1.2
            },
            480: {
                slidesPerView: 1.5
            },
            768: {
                slidesPerView: 2
            },
            992: {
                slidesPerView: 2
            },
            1200: {
                slidesPerView: 3.4
            },
            1400: {
                slidesPerView: 3.8
            }
        },
    });

    /*-----------------------
    ==== Parterns slider ====
    ------------------------*/
    var medikiaPartners = new Swiper(".medikia__partners-slider", {
        loop: true,
        freemode: true,
        slidesPerView: 5,
        spaceBetween: 10,
        centeredSlides: false,
        allowTouchMove: true,
        autoplay: {
            delay: 2500,
            disableOnInteraction: false,
        },
        speed: 500,
        breakpoints: {
            0: {
                slidesPerView: 2
            },
            480: {
                slidesPerView: 2
            },
            768: {
                slidesPerView: 4
            },
            992: {
                slidesPerView: 5
            },
            1200: {
                slidesPerView: 5
            },
            1400: {
                slidesPerView: 6
            }
        },
    });

    /*-----------------------
    ==== Testimonial Home 2 ====
    ------------------------*/
    if ($(".medikia__testimonial-2 .container .list-comment").length) {
        $(".medikia__testimonial-2 .container .list-comment").slick({
            dots: true,
            arrows: false,
            slidesToShow: 2,
            slidesToScroll: 2,
            touchThreshold: 100,
            swipe: true,
            swipeToSlide: true,
            autoplay: false,
            autoplaySpeed: 3000,
            speed: 500,
            pauseOnFocus: false,
            pauseOnHover: false,
            pauseOnDotsHover: false,
            infinite: true,
            responsive: [{
                breakpoint: 1170,
                settings: {
                    slidesToShow: 2,
                    slidesToScroll: 1
                }
            }, {
                breakpoint: 992,
                settings: {
                    slidesToShow: 1,
                    slidesToScroll: 1
                }
            }, {
                breakpoint: 768,
                settings: {
                    slidesToShow: 1,
                    slidesToScroll: 1
                }
            }]
        });
    } else {
        console.log("Element not found. Slick slider not applied.");
    }

    /*---------------------
    === Contact Form  ===
    ----------------------*/
    var form = $('#contact-form');

    // Get the messages div.
    var formMessages = $('.form-message');

    $(form).on('submit', function (e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        })
            .done(function (response) {
                // Make sure that the formMessages div has the 'success' class.
                $(formMessages).removeClass('error');
                $(formMessages).addClass('success');

                // Set the message text.
                $(formMessages).text(response);

                // Clear the form.
                $('#contact-form input,#contact-form textarea').val('');
            })
            .fail(function (data) {
                // Make sure that the formMessages div has the 'error' class.
                $(formMessages).removeClass('success');
                $(formMessages).addClass('error');

                // Set the message text.
                if (data.responseText !== '') {
                    $(formMessages).text(data.responseText);
                } else {
                    $(formMessages).text('Oops! An error occurred. Message could not be sent.');
                }
            });
    });

})(jQuery);
