$(function ($) {
    "use strict";
    if ($('.ts-main-menu').length > 0) {
        $(".ts-main-menu").navigation({
            effect: "fade",
            mobileBreakpoint: 992,
        });
    }
    if ($(".header-search").length > 0) {
        var todg = true;
        $(".header-search >a").on("click", function (e) {
            e.preventDefault();
            if (todg) {
                $(".header-search-form").fadeIn("slow");
                todg = false;
            } else {
                $(".header-search-form").fadeOut("slow");
                todg = true;
            }
        });

        $(document).on('mouseup', function (e) {
            var container = $(".header-search");

            if (!container.is(e.target) && container.has(e.target).length === 0) {
                $(".header-search-form").fadeOut("slow");
                todg = true;
            }

        });
    }

    if ($(".widget-thin-min-slider .swiper-wrapper").length > 0) {
        var widget_thin_min_slider = new Swiper('.widget-thin-min-slider .slider', {
            direction: 'vertical',
            navigation: {
                nextEl: '.widget-thin-min-slider .swiper-button-next',
                prevEl: '.widget-thin-min-slider .swiper-button-prev',
            },
            preloadImages: false,
            loop: true,
            autoplay: {
                delay: 5000,
            },
            lazy: true,
            on: {
                init: function () {
                    var swiper = this;
                    var slideHover = function () {
                        if (!swiper.pagination.bullets || swiper.pagination.bullets.length === 0) {
                            return setTimeout(slideHover, 200);
                        }
                        $(swiper.pagination.bullets).on("mouseover", function () {
                            var index = parseInt($(this).attr("data-id"));
                            if (swiper.loopedSlides) {
                                index = (index + 1)
                            }
                            swiper.slideTo(index);
                        });
                    };
                    setTimeout(slideHover, 50);
                }
            },
            pagination: {
                el: '.widget-thin-min-slider .swiper-pagination',
                renderBullet: function (index, className) {
                    var id = index;
                    if (this.loopedSlides) {
                        index = (index + 1);
                    }
                    var slideLink = this.slides[index].querySelector("a");


                    var buttonHtml = '<span data-id="' + id + '" class="' + className + '">' + (id + 1) + '</span>';

                    if (slideLink) {
                        buttonHtml = '<a href="' + slideLink.getAttribute("href") + '" target="' + slideLink.getAttribute("target") + '">' + buttonHtml + "</a>";
                    }
                    return buttonHtml;
                },
            }
        });

        $(".widget-thin-min-slider").mouseenter(function() {
            widget_thin_min_slider.autoplay.stop();
        });
        $(".widget-thin-min-slider").mouseleave(function() {
            widget_thin_min_slider.autoplay.start();
        });
    }

    if ($(".widget-thin-slider .swiper-wrapper").length > 0) {
        var widget_thin_slider = new Swiper('.widget-thin-slider .slider', {
            preloadImages: false,
            loop: true,
            speed: 1,
            autoplay: {
                delay: 5000,
            },
            lazy: true,
            on: {
                init: function () {
                    var swiper = this;
                    var slideHover = function () {
                        if (!swiper.pagination.bullets || swiper.pagination.bullets.length === 0) {
                            return setTimeout(slideHover, 200);
                        }
                        $(swiper.pagination.bullets).on("mouseover", function () {
                            var index = parseInt($(this).attr("data-id"));
                            if (swiper.loopedSlides) {
                                index = (index + 1)
                            }
                            swiper.slideTo(index);
                        });
                    };
                    setTimeout(slideHover, 50);
                }
            },
            pagination: {
                el: '.widget-thin-slider .swiper-pagination',
                renderBullet: function (index, className) {
                    var id = index;
                    if (this.loopedSlides) {
                        index = (index + 1);
                    }
                    var slideLink = this.slides[index].querySelector("a");


                    var buttonHtml = '<span data-id="' + id + '" class="' + className + '">' + (id + 1) + '</span>';

                    if (slideLink) {
                        buttonHtml = '<a href="' + slideLink.getAttribute("href") + '" target="' + slideLink.getAttribute("target") + '">' + buttonHtml + "</a>";
                    }
                    return buttonHtml;
                },
            },
            breakpoints: {
                479: {
                    pagination: {
                        el: '.widget-thin-slider .swiper-pagination',
                        renderBullet: function (index, className) {
                            var id = index;
                            if (this.loopedSlides) {
                                index = (index + 1);
                            }
                            var slideLink = this.slides[index].querySelector("a");


                            var buttonHtml = '<span data-id="' + id + '" class="' + className + ' bullets"></span>';

                            if (slideLink) {
                                buttonHtml = '<a href="' + slideLink.getAttribute("href") + '" target="' + slideLink.getAttribute("target") + '">' + buttonHtml + "</a>";
                            }
                            return buttonHtml;
                        },
                    }
                }
            }
        });
    }
    if ($(".widget-main-slider .swiper-wrapper").length > 0) {
        var widget_main_slider = new Swiper('.widget-main-slider #mainSlider1', {
            navigation: {
                nextEl: '.widget-main-slider .swiper-button-next',
                prevEl: '.widget-main-slider .swiper-button-prev',
            },
            speed: 1,
            preloadImages: false,
            loop: true,
            autoplay: {
                delay: 5000,
            },
            lazy: true,
            on: {
                init: function () {
                    var swiper = this;
                    var slideHover = function () {
                        if (!swiper.pagination.bullets || swiper.pagination.bullets.length === 0) {
                            return setTimeout(slideHover, 200);
                        }
                        $(swiper.pagination.bullets).on("mouseover", function () {
                            var index = parseInt($(this).attr("data-id"));
                            if (swiper.loopedSlides) {
                                index = (index + 1)
                            }
                            swiper.slideTo(index);
                        });
                    };
                    setTimeout(slideHover, 50);
                }
            },
            pagination: {
                el: '.widget-main-slider .swiper-pagination',
                renderBullet: function (index, className) {
                    var id = index;
                    if (this.loopedSlides) {
                        index = (index + 1);
                    }
                    var slideLink = this.slides[index].querySelector("a");


                    var buttonHtml = '<span data-id="' + id + '" class="' + className + '">' + (id + 1) + '</span>';

                    if (slideLink) {
                        buttonHtml = '<a href="' + slideLink.getAttribute("href") + '" target="' + slideLink.getAttribute("target") + '">' + buttonHtml + "</a>";
                    }
                    return buttonHtml;
                },
            },
            breakpoints: {
                479: {
                    pagination: {
                        el: '.widget-main-slider .swiper-pagination',
                        renderBullet: function (index, className) {
                            var id = index;
                            if (this.loopedSlides) {
                                index = (index + 1);
                            }
                            var slideLink = this.slides[index].querySelector("a");


                            var buttonHtml = '<span data-id="' + id + '" class="' + className + ' bullets"></span>';

                            if (slideLink) {
                                buttonHtml = '<a href="' + slideLink.getAttribute("href") + '" target="' + slideLink.getAttribute("target") + '">' + buttonHtml + "</a>";
                            }
                            return buttonHtml;
                        },
                    }
                }
            }
        });
    }

    if ($('.catsld').length) {
        $('.catsld').each(function (index) {
            var prvBT = $(this).find('ins.prev');
            var nxtBT = $(this).find('ins.next');

            var tabId = $($(this)[0]).parent('.tab-pane').attr('id');
            var catsldpag = $('#' + tabId + ' .swiper-numeric');
            var i = 1;
            var mnsLink = 'undefined';
            $('#' + tabId).find('.swiper-slide').each(function () {
                mnsLink = $(this).find('a').eq(2).attr('href');
                if (mnsLink === undefined || mnsLink === null) {
                    mnsLink = $(this).find('a').attr('href');
                }
                catsldpag.append('<a href="' + mnsLink + '" class="num" target="_blank">' + i + '</a>');
                i++;
            });

            var catsld = new Swiper($(this)[0], {
                effect: 'fade', fade: {crossFade: true},
                navigation: {
                    nextEl: nxtBT,
                    prevEl: prvBT,
                },
                loop: true,
                spaceBetween: 0,
                autoplay: {
                    delay: 7000,
                },
                preventClicks: false,
                preloadImages: false,
                lazy: {
                    loadPrevNext: true,
                    loadPrevNextAmount: 2
                },
                on: {
                    init: function () {
                        catsldpag.children('a').eq(0).addClass('act');
                    },

                },
            });
            catsld.on('transitionStart', function () {

                var ai = parseInt($('#' + tabId + ' .swiper-slide-active').data("swiper-slide-index"));
                catsldpag.children('a').removeClass('act');
                catsldpag.children('a').eq(ai).addClass('act');
            });


            catsldpag.children('a').mouseover(function () {
                var indx = $(this).index() + 1;
                catsld.slideTo(indx, 0);
            });

            $(".catsld").mouseenter(function() {
                catsld.autoplay.stop();
            });
            $(".catsld").mouseleave(function() {
                catsld.autoplay.start();
            });
        });
    }

    if ($('.thusld').length) {
        var thusld = new Swiper('.thusld', {
            prevButton: '.thusld ins.prev', nextButton: '.thusld ins.next',
            loop: true,
            autoplay: {
                delay: 5000,
            },
            preloadImages: false,
            lazy: true,
            preventClicks: false,
            spaceBetween: 1,
        });
        swiperThumbs(thusld, {element: 'swiper-thumbnails', activeClass: 'act'});

        $(".thusld").mouseenter(function() {
            thusld.autoplay.stop();
        });
        $(".thusld").mouseleave(function() {
            thusld.autoplay.start();
        });
    }

    if ($('.tickbx.last').length) {
        var ticksld = new Swiper('.ticksld', {
            direction: 'vertical',
            loop:true,
            autoplay: {
                delay: 5000,
            },
            spaceBetween: 0
        });
        $(".ticksld").mouseenter(function() {
            ticksld.autoplay.stop();
        });
        $(".ticksld").mouseleave(function() {
            ticksld.autoplay.start();
        });
    }

    if ($('.tickbx.last').length) {
        var ticksld2 = new Swiper('.ticksld2', {
            direction: 'vertical',
            loop:true,
            autoplay: {
                delay: 5000,
            },
            spaceBetween: 0
        });
        $(".ticksld2").mouseenter(function() {
            ticksld2.autoplay.stop();
        });
        $(".ticksld2").mouseleave(function() {
            ticksld2.autoplay.start();
        });
    }

    if ($('.newsld').length) {
        var prvBT = $('.nwsmin').find('ins.prev');
        var nxtBT = $('.nwsmin').find('ins.next');

        var newsld = new Swiper('.newsld', {
            navigation: {
                nextEl: nxtBT,
                prevEl: prvBT,
            },
            loop:true,
            autoplay: {
                delay: 7000,
            },
            slidesPerView: 4, slidesPerGroup: 4,
            preventClicks: false,
            breakpoints: {
                479: {slidesPerView: 1, slidesPerGroup: 1, spaceBetween: 0},
                767: {slidesPerView: 2, slidesPerGroup: 2, spaceBetween: 15},
                991: {slidesPerView: 3, slidesPerGroup: 3, spaceBetween: 15}
            },
            onInit: function () {
                $('.newsld .card').matchHeight();
            },
            spaceBetween: 15
        });
    }

    if ($('.autsld').length) {
        var prvBT = $('.authbx').find('ins.prev');
        var nxtBT = $('.authbx').find('ins.next');
        var autsld = new Swiper('.autsld', {
            navigation: {
                nextEl: nxtBT,
                prevEl: prvBT,
            },
            slidesPerView: 3, slidesPerGroup: 3,
            preventClicks: false,
            breakpoints: {
                479: {slidesPerView: 1, slidesPerGroup: 1, spaceBetween: 0},
                767: {slidesPerView: 2, slidesPerGroup: 2, spaceBetween: 0},
                991: {slidesPerView: 3, slidesPerGroup: 3, spaceBetween: 0}
            },
            loop: true,
            autoplay: {
                delay: 5000,
            },
            spaceBetween: 0
        });

        $(".autsld").mouseenter(function() {
            autsld.autoplay.stop();
        });
        $(".autsld").mouseleave(function() {
            autsld.autoplay.start();
        });
    }

    if ($(".widget-author-vertical-slider .swiper-wrapper").length > 0) {
        var swiper = new Swiper('.widget-author-vertical-slider', {
            direction: 'vertical',
            slidesPerView: 4,
            loop: false,
            navigation: {
                nextEl: '.author-sidebar .swiper-button-next',
                prevEl: '.author-sidebar .swiper-button-prev',
            },
            breakpoints: {
                1495: {
                    slidesPerView: 4,
                    centeredSlides: false,
                }
            }
        });
        $(".widget-author-vertical-slider").mouseenter(function() {
            swiper.autoplay.stop();
        });
        $(".widget-author-vertical-slider").mouseleave(function() {
            swiper.autoplay.start();
        });
    }


    if ($("#video-tab-scrollbar").length > 0) {
        $("#video-tab-scrollbar").mCustomScrollbar({
            mouseWheel: true,
            scrollButtons: { enable: true }
        });
    }


});

$(document).ready(function () {
    var pos = $('.menu').offset().top;
    $(function(){
        createSticky($('.menu'));
    });

    function createSticky(sticky) {

        if (typeof sticky !== "undefined") {
            var win = $(window);

            win.on("scroll", function() {
                win.scrollTop() >= pos ? sticky.addClass("fixed") : sticky.removeClass("fixed");
            });
        }
    }
});
