/* Script on ready
---------------------------------*/
$(document).ready(function(){

    /* sticky footer function */
    StickyFooter();

    /* carousel */
    if($('.carousel').length >= 1){
        $('.carousel.fade-slide').slick({
            dots: false,
            arrows: true,
            infinite: true,
            speed: 1000,
            fade: true,
            adaptiveHeight: true,
            autoplay: true,
            autoplaySpeed: 5000
        });


        var count = $('.carousel.slide .item').length,
            item = 1;
        if(count >= 3){
            item = 3
        }else if(count >= 2){
            item = 2
        }else{
            item = 1
        }
        $('.carousel.slide').slick({
            dots: false,
            slidesToShow: item,
            slidesToScroll: 1,
            infinite: true,
            adaptiveHeight: true,
            responsive: [
                {
                    breakpoint: 991,
                    settings: {
                        slidesToShow: 2
                    }
                },
                {
                    breakpoint: 767,
                    settings: {
                        slidesToShow: 1
                    }
                },
            ]
        });

        $('.video-slider').slick({
            dots: false,
            slidesToShow: 2,
            slidesToScroll: 1,
            infinite: true,
            adaptiveHeight: true,
            responsive: [
                {
                    breakpoint: 991,
                    settings: {
                        slidesToShow: 1
                    }
                },
                {
                    breakpoint: 767,
                    settings: {
                        slidesToShow: 2
                    }
                },
                {
                    breakpoint: 640,
                    settings: {
                        slidesToShow: 1
                    }
                },
            ]
        });

        $('.carousel.slide-banner-image').slick({
            dots: false,
            slidesToShow: 1,
            slidesToScroll: 1,
            infinite: true,
            adaptiveHeight: true,
        });
    }

    /* navigation menu in Mobile */
    $(".mobile-nav ul ul").before('<em class="fa fa-angle-right menu-caret" aria-hidden="true"></em>');
    $(".mobile-nav ul .menu-caret").click(function(){
        $(this).stop().toggleClass("menu-open");
        $(this).next().stop().slideToggle("500") ;
    });

    /* features sidebar */
    $('.categories-mobile .btn').click(function(){
        $('.categories-sidebar').stop().addClass('is-open');
        $('body').stop().addClass('modal-open');
    })
    $('.categories-sidebar .close').click(function(){
        $('.categories-sidebar').stop().removeClass('is-open');
        $('body').stop().removeClass('modal-open');
    })

    /* accordion in sidebar */
    $('.content-slider').slick({
        dots: false,
        slidesToShow: 2,
        speed: 1000,
        autoplay: false,
        responsive: [
            {
                breakpoint: 767,
                settings: {
                    slidesToShow: 1
                }
            },
        ]
    });

    $(".accordian-list em").click(function(){
        $(".accordian-list em").not(this).parent().removeClass('acc-open');
        $(".accordian-list em").not(this).next().slideUp();
        $(".accordian-list em").not(this).removeClass("is-open");
        $(this).next().stop().slideToggle(200);
        $(this).toggleClass("is-open");
        $(this).parent().toggleClass('acc-open');
        $('.content-slider').slick('setPosition',0);
    });
    $(".accordian-list li a").click(function(){$(this).next('em').trigger('click')});

    $(".navbar-toggle").click(function(){$("html").stop().toggleClass("is-open");});

    /* search site input focus on modal open */
    $('.search-form').on('shown.bs.modal', function () {$('input.search-keyword').focus();});

    /* header search as slide */
    $('.navbar-right .header-search').click(function(){
        if($(this).parent().hasClass('header-search-open')){$(this).parent().removeClass('header-search-open');}
        else{
            $(this).parent().addClass('header-search-open');
            $('.navbar-right .search-keyword').focus().select();
        }
    });

    /* Back to top */
    $('.back-to-top a').click(function(){
        $('html,body').animate({
            scrollTop: 0
        },800);
    });

    /*  */
    $('.full-link > a').click(function(){
        $(this).parent().find('ul').stop().slideToggle();
        $(this).parent().toggleClass('link-open');
    })

     /* Added video in slider */
    $(".fade-slide.business-slider").on("afterChange", function () {
        $('.business-slider .item ').each(function () {
            if ($(this).hasClass('slick-current')) {
                var vid = $(this).find('video');
                if (vid.length) {$(vid)[0].play();}
            } else {
                var vid = $(this).find('video');
                if (vid.length) {$(vid)[0].pause();}
            }
        });
    });



});

/* Script on load
----------------------------------*/
$(window).load(function() {

    /* sticky footer function */
    StickyFooter()

    equalheight( $('.video-equal'));

    if($(window).width() >= 481 ){
        equalheight( $('.video-top-cover .equalheight'));
    }
    if($(window).width() >= 768 ){
        equalheight( $('.equalheight'));
        equalheight( $('footer .innerbox'));
    }

    $('.home-aboutus .ha-icon').height($('.home-aboutus .ha-icon').width());

    /* Isotop */
    if($('.grid').length > 0)
    {
        $('.grid').isotope({
            itemSelector: '.grid-i-cover',
            masonry: {}});
    }
    $('.content-ul-set').find('ul').addClass('content-half-li cf');

    $(document).on('click', '.continent-list', function (e) {
        e.preventDefault();

        var _li = $(this).parent('li');
        if (_li.hasClass('active')) {
            _li.removeClass('active');
            _li.find('.region-countries').slideUp();
        } else {
            // _li.siblings().removeClass('active');
            $('.region-countries').slideUp().parent().removeClass('active');
            _li.addClass('active');
            _li.find('.region-countries').slideDown();
        }
    });

    $(document).on('click', '.responsive-arrow', function (e) {
        e.preventDefault();
        $(this).toggleClass('active');
        $(this).parent('.sidebar-block').toggleClass('open');
    });


    $(document).on('click', '.country-toggle-btn', function (e) {
        e.preventDefault();
        $(this).toggleClass('active');
        $('.sidebar-block').toggleClass('open');
    });

    $(document).on('click', '.list-toggler', function (e) {
        e.preventDefault();
        if ($(this).hasClass('active')) {
            $('.accordion-content').slideUp();
            $('.list-toggler').removeClass('active');

        } else {
            $('.accordion-content').slideUp();
            $('.list-toggler').removeClass('active');
            $(this).toggleClass('active');
            $(this).next().slideDown();
        }
    });

    $(document).on('click', '.popup-btn', function(e){
        e.preventDefault();
        $('.map-popup').addClass('active');
        $('body').addClass('popup-open');
    });

    $(document).on('click', '.close-popup', function (e) {
        e.preventDefault();
        $('.map-popup').removeClass('active');
        $('body').removeClass('popup-open');
    });

});

/* Script on resize
---------------------------------*/
$(window).resize(function() {

    /* sticky footer function */
    setTimeout(function(){
        StickyFooter();
        $('.home-aboutus .ha-icon').height($('.home-aboutus .ha-icon').width());
        if($(window).width() >= 768 ){
            equalheight( $('.equalheight'));
            equalheight( $('footer .innerbox'));
        }
        equalheight( $('.video-equal'));
    },50);

});

/* Script on scroll
---------------------------------*/
$( window ).scroll( function() {
    /* sticky header */
    if($('.categories-btn-outer .btn').length >= 1){
        if( $(window).scrollTop() > $( '.categories-mobile' ).offset().top){
            $( '.categories-btn-outer' ).stop().addClass( 'stuck' );
        }else{
            $( '.categories-btn-outer' ).stop().removeClass( 'stuck' );
        }
    }
});

/* Script all functions
----------------------------------*/

/* sticky footer function */
function StickyFooter(){
    var st = $('footer').outerHeight()
    $('#wrapper').css({
        'margin-bottom':- st,
        'padding-bottom': st
    })
}

/* equal height */
equalheight = function( container ) {
    var currentTallest = 0,
        currentRowStart = 0,
        rowDivs = new Array(),
        $el,
        topPosition = 0;
    $( container ).each(function() {

        $el = $( this );
        $( $el ).height( 'auto' )
        topPostion = $el.position().top;

        if ( currentRowStart != topPostion ) {
            for ( currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++ ) {rowDivs[currentDiv].height(currentTallest);}
            rowDivs.length = 0; // empty the array
            currentRowStart = topPostion;
            currentTallest = $el.height();
            rowDivs.push($el);
        } else {
            rowDivs.push( $el );
            currentTallest = ( currentTallest < $el.height() ) ? ( $el.height() ) : ( currentTallest );
        }
        for ( currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++ ) {
            rowDivs[currentDiv].height( currentTallest );
        }
    });
}