(function($) {
    'use strict';

    // Off canvas sidebar
    var OffCanvas = function() {
        $('#off-canvas-toggle').on('click', function() {
            $('body').toggleClass("canvas-opened");
        });

        $('.dark-mark').on('click', function() {
            $('body').removeClass("canvas-opened");
        });
        $('.off-canvas-close').on('click', function() {
            $('body').removeClass("canvas-opened");
        });
    };

    // Search form
    var openSearchForm = function() {
        $('.search-close').hide();
        $('button.search-icon').on('click', function() {
            $(this).hide();
            $('body').toggleClass("open-search-form");
            $('.search-close').show();
            $("html, body").animate({ scrollTop: 0 }, "slow");
        });
        $('.search-close').on('click', function() {
            $(this).hide();
            $('body').removeClass("open-search-form");
            $('button.search-icon').show();
        });
    };

    // Mobile menu
    var mobileMenu = function() {
        var menu = $('ul#navigation');
        if (menu.length) {
            menu.slicknav({
                prependTo: ".mobile_menu",
                closedSymbol: '+',
                openedSymbol: '-'
            });
        };
    };

    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-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();
        });
    }

    // Slick slider
    var customSlickSlider = function() {
        // Featured slider 1
        $('.featured-slider-1-items').slick({
            dots: false,
            infinite: true,
            speed: 500,
            arrows: true,
            slidesToShow: 1,
            autoplay: true,
            autoplaySpeed:5000,
            loop: true,
            adaptiveHeight: true,
            fade: true,
            cssEase: 'linear',
            prevArrow: '<button type="button" class="slick-prev"><i class="ti-arrow-left"></i></button>',
            nextArrow: '<button type="button" class="slick-next"><i class="ti-arrow-right"></i></button>',
            appendArrows: '.arrow-cover',
        });
        // post-carausel-1-items
        $('.post-carausel-1-items').slick({
            dots: false,
            infinite: true,
            speed: 1000,
            arrows: true,
            slidesToShow: 3,
            slidesToScroll: 1,
            autoplay: true,
            loop: true,
            adaptiveHeight: true,
            cssEase: 'linear',
            prevArrow: '<button type="button" class="slick-prev"><i class="ti-arrow-left"></i></button>',
            nextArrow: '<button type="button" class="slick-next"><i class="ti-arrow-right"></i></button>',
            appendArrows: '.post-carausel-1-arrow',
            centerPadding: 50,
            responsive: [{
                    breakpoint: 1024,
                    settings: {
                        slidesToShow: 4,
                        slidesToScroll: 4,
                        infinite: true,
                        dots: false,
                    }
                },
                {
                    breakpoint: 991,
                    settings: {
                        slidesToShow: 3,
                        slidesToScroll: 3
                    }
                },
                {
                    breakpoint: 480,
                    settings: {
                        slidesToShow: 2,
                        slidesToScroll: 2
                    }
                }
            ]
        });

        // post-carausel-2
        $('.post-carausel-2').slick({
            dots: true,
            infinite: true,
            speed: 1500,
            arrows: false,
            slidesToShow: 4,
            slidesToScroll: 2,
            autoplay: true,
            loop: true,
            adaptiveHeight: true,
            cssEase: 'linear',
            centerPadding: 50,
            responsive: [{
                    breakpoint: 1024,
                    settings: {
                        slidesToShow: 4,
                        slidesToScroll: 4,
                        infinite: true,
                        dots: false,
                    }
                },
                {
                    breakpoint: 991,
                    settings: {
                        slidesToShow: 3,
                        slidesToScroll: 3
                    }
                },
                {
                    breakpoint: 480,
                    settings: {
                        slidesToShow: 1,
                        slidesToScroll: 1
                    }
                }
            ]
        });

        $('.featured-slider-2-items').slick({
            fade: true,
            asNavFor: '.featured-slider-2-nav',
            arrows: true,
            prevArrow: '<button type="button" class="slick-prev"><i class="ti-arrow-left"></i></button>',
            nextArrow: '<button type="button" class="slick-next"><i class="ti-arrow-right"></i></button>',
            appendArrows: '.arrow-cover',
        });

        $('.featured-slider-2-nav').slick({
            slidesToShow: 4,
            slidesToScroll: 1,
            asNavFor: '.featured-slider-2-items',
            dots: false,
            arrows: false,
            centerMode: true,
            focusOnSelect: true,
            centerPadding: 0,
            responsive: [{
                    breakpoint: 1024,
                    settings: {
                        slidesToShow: 3,
                    }
                },
                {
                    breakpoint: 991,
                    settings: {
                        slidesToShow: 2,
                    }
                },
                {
                    breakpoint: 480,
                    settings: {
                        slidesToShow: 1,
                    }
                }
            ]
        });
    };

    //Header sticky
    var headerSticky = function() {
        $(window).on('scroll', function() {
            var scroll = $(window).scrollTop();
            if (scroll < 200) {
                $(".header-sticky ").removeClass("sticky-bar");
            } else {
                $(".header-sticky").addClass("sticky-bar");
            }
        });
    };

    // Scroll up to top
    var scrollToTop = function() {
        $.scrollUp({
            scrollName: 'scrollUp', // Element ID
            topDistance: '300', // Distance from top before showing element (px)
            topSpeed: 300, // Speed back to top (ms)
            animation: 'fade', // Fade, slide, none
            animationInSpeed: 200, // Animation in speed (ms)
            animationOutSpeed: 200, // Animation out speed (ms)
            scrollText: '<i class="ti-arrow-up"></i>', // Text for element
            activeOverlay: false, // Set CSS color to display scrollUp active point, e.g '#00FFFF'
        });
    };

    //sidebar sticky
    var stickySidebar = function() {
        $('.sticky-sidebar').theiaStickySidebar();
    };

    var articleSticky = function () {
        $(".entry-main-content .sharing-container > .sharing").length > 0 && $(window).width() >= 768 && $(window).on("scroll", function () {
            var t = $(this).scrollTop();
            $(".news-detail").each(function () {
                var n = $(".header-bottom").outerHeight() + 40;
                t >= $(this).find(".entry-main-content").offset().top - n ? ($(this).find(".sharing-container").children(".sharing").addClass("sticky"), t >= $(this).offset().top + $(this).height() - ($(this).find(".sharing").height() + 280) ? ($(this).find(".sharing-container").children(".sharing").removeClass("sticky"), $(this).find(".sharing-container").children(".sharing").css({
                    bottom: "0px",
                    top: "auto"
                })) : $(this).find(".sharing-container").children(".sharing").addClass("sticky").css({
                    bottom: "initial",
                    top: n + "px"
                })) : $(this).find(".sharing-container").children(".sharing").removeClass("sticky").css({
                    bottom: "auto",
                    top: "0"
                })
            })
        })
    }

    //Custom scrollbar
    var customScrollbar = function() {
        var $ = document.querySelector.bind(document);
        var ps = new PerfectScrollbar('.custom-scrollbar');
    };

    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();
        });
    }

    /* WOW active */
    new WOW().init();

    //Load functions
    $(document).ready(function() {
        OffCanvas();
        customScrollbar();
        scrollToTop();
        headerSticky();
        stickySidebar();
        customSlickSlider();
        mobileMenu();
        openSearchForm();
        articleSticky();
    });

})(jQuery);
