jQuery(document).ready(function($) {

/*------------------------------------------------
            DECLARATIONS
------------------------------------------------*/

    var loader                  = $('#loader');
    var loader_container        = $('#preloader');
    var scroll                  = $(window).scrollTop();  
    var scrollup                = $('.backtotop');
    var dropdown_toggle         = $('.main-navigation button.dropdown-toggle');
    var menu_toggle             = $('#masthead .menu-toggle');
    var primary_menu            = $('#masthead ul.nav-menu');
     var filtering               = $('.filtering-posts');


/*------------------------------------------------
            PRELOADER
------------------------------------------------*/

$('#loader').fadeOut();
$('#loader-container').fadeOut();

$('.display-none').css({'display' : 'block'});

/*------------------------------------------------
                BACK TO TOP
------------------------------------------------*/

 $(window).scroll(function(){
    if ($(this).scrollTop() > 1) {
    $('.backtotop').fadeIn();
    } else {
    $('.backtotop').fadeOut();
    }
    });
    $('.backtotop').click(function() {
    $('html, body').animate({scrollTop: '0px'}, 800);
    return false;
});

/*------------------------------------------------
            NAVIGATION
------------------------------------------------*/

    menu_toggle.click(function() {
        primary_menu.slideToggle();
        $(this).toggleClass('active');
        $('#masthead .main-navigation').toggleClass('menu-open');
        $('.menu-overlay').toggleClass('active');
       
    });

    dropdown_toggle.click(function() {
        $(this).toggleClass('active');
       $(this).parent().find('.sub-menu').first().slideToggle();
    });

    $('.main-navigation ul li.search-menu a').click(function(e) {
        e.preventDefault();
        $(this).toggleClass('search-active');
        $('.main-navigation #search').fadeToggle();
        $('.main-navigation .search-field').focus();
    });

     $(window).scroll(function() {
        if ($(this).scrollTop() > 1) {
            $('.menu-sticky #masthead').addClass('nav-shrink');
        }
        else {
            $('.menu-sticky #masthead').removeClass('nav-shrink');
        }
    });

     if ($(window).width() < 1024) {
        $( ".nav-menu ul.sub-menu li:last-child" ).focusout(function() {
            dropdown_toggle.removeClass('active');
            $('.main-navigation .sub-menu').slideUp();
        });
     }
----------------------------------------------
               SLICK SLIDER
------------------------------------------------*/
$('.news-ticker-slider').slick();

$('#main-slider .regular').slick();

$('.featured-events-slider').slick();

$('.featured-news-slider').slick();

$('.testimonial-slider').slick({
    responsive: [
    {
      breakpoint: 992,
      settings: {
        slidesToShow: 2
      }
    },
    {
      breakpoint: 599,
      settings: {
        slidesToShow: 1
      }
    }
  ]
});


/*------------------------------------------------
                    PARALLAX   
------------------------------------------------*/
$.stellar({
    horizontalScrolling: false,
    verticalOffset: 3000
});

/*------------------------------------------------
                    COUNTER   
------------------------------------------------*/
function count($this){
    var current = parseInt($this.html(), 10);
    current = current + 1; /* Where 50 is increment */
    $this.html(++current);
    if(current > $this.data('count')){
        $this.html($this.data('count'));
    } 
    else {    
        setTimeout(function(){count($this)}, 50);
    }
}        
    
$(".stat-count").each(function() {
    $(this).data('count', parseInt($(this).html(), 10));
    $(this).html('0');
    count($(this));
});

/*------------------------------------------------
                    EVENTS AND NEWS   
------------------------------------------------*/

$(".featured-events-slider .slick-item").hover(function(){
    var imgurl = $(this).data("hoverimage");
    $(this).css("background-image", "url(" + imgurl + ")")
}, function(){
    $(this).css("background-image", "").fadeIn('800');
});


$(".featured-news-slider .slick-item").hover(function(){
    var imgurl = $(this).data("hoverimage");
    $(this).css("background-image", "url(" + imgurl + ")")
}, function(){
    $(this).css("background-image", "").fadeIn('800');
});


/*------------------------------------------------
                ABROAD COUNTRIES
------------------------------------------------*/
if( $(window).width() > 992 ) {
    var activeCountry = $('.abroad-countries-list .hentry:nth-child(2)');
    activeCountry.addClass('active');

    $('.abroad-countries-list .hentry').mouseenter(function() {
        $('.abroad-countries-list .hentry').removeClass('active');
        $(this).addClass('active');
    });

    $('.abroad-countries-list .hentry').mouseleave(function() {
        $('.abroad-countries-list .hentry').removeClass('active');
        $('.abroad-countries-list .hentry:nth-child(2)').addClass('active');
    });
}

/*------------------------------------------------
                SERVICES
------------------------------------------------*/
if( $(window).width() < 992 ) {
    $('#main-services .hentry:nth-child(2)').insertAfter('#main-services .hentry:nth-child(3)');
    $('#main-services .hentry:nth-child(3)').insertAfter('#main-services .hentry:nth-child(2)');
}

if($('#content section').hasClass('map')) {
    $('body').addClass('map-enabled');
}

/*------------------------------------------------
                SECTION DISABLED   
------------------------------------------------*/
if($('body.home #main-slider').hasClass('main-featured-slider')) {
    $('body').addClass('main-slider-enabled');
}
else {
    $('body').addClass('main-slider-disabled');
}

/*------------------------------------------------
                TABS   
------------------------------------------------*/
$('.tp-tabs li').click(function() {
    var tab_id = $(this).attr('data-tab');

    $('ul.tp-tabs li').removeClass('active');
    $(this).addClass('active');
    $('.tab-content').removeClass('active');
    $('.tab-content').hide();
    $("#"+tab_id).show();
});

/*------------------------------------------------
                STICKY POST   
------------------------------------------------*/

if($('.blog .archive-blog-wrapper article').hasClass('sticky')) {
    $('body').addClass('has-sticky-post');
}
else {
    $('body').addClass('no-sticky-post');
}

/*------------------------------------------------
                Header Custom Info   
------------------------------------------------*/

$('#masthead .widget_call_to_action li').click(function() {
   $(this).toggleClass('active');
   if($('#masthead .widget_call_to_action li').hasClass('active')) {
       $('#masthead .widget_call_to_action li').removeClass('active');
       $(this).addClass('active');
   }
});

$(document).click(function (e) {
    var container = $("#masthead .widget_call_to_action li");
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        $('#masthead .widget_call_to_action li').removeClass('active');
    }
});
/*--------------------------------------------------------------
 Keyboard Navigation
----------------------------------------------------------------*/
if( $(window).width() < 1024 ) {
    $('#primary-menu').find("li").last().bind( 'keydown', function(e) {
        if( e.which === 9 ) {
            e.preventDefault();
            $('#masthead').find('.menu-toggle').focus();
        }
    });

    $('#primary-menu > li:last-child button:not(.active)').bind( 'keydown', function(e) {
        if( e.which === 9 ) {
            e.preventDefault();
            $('#masthead').find('.menu-toggle').focus();
        }
    });

    $('#search').find("button").unbind('keydown');

}
else {
    $('#primary-menu').find("li").unbind('keydown');

    $('#search').find("button").bind( 'keydown', function(e) {
        var tabKey              = e.keyCode === 9;
        var shiftKey            = e.shiftKey;

        if( tabKey ) {
            e.preventDefault();
            $('#search').hide();
            $('.search-menu > a').removeClass('search-active').focus();
        }

        if( shiftKey && tabKey ) {
            e.preventDefault();
            $('#search').show();
            $('.main-navigation .search-field').focus();
            $('.search-menu > a').addClass('search-active');
        }
    });

    $('.search-menu > a').on('keydown', function (e) {
        var tabKey              = e.keyCode === 9;
        var shiftKey            = e.shiftKey;
        
        if( $('.search-menu > a').hasClass('search-active') ) {
            if ( shiftKey && tabKey ) {
                e.preventDefault();
                $('#search').hide();
                $('.search-menu > a').removeClass('search-active').focus();
            }
        }
    });
}

$(window).resize(function() {
    if( $(window).width() < 1024 ) {
        $('#primary-menu').find("li").last().bind( 'keydown', function(e) {
            if( e.which === 9 ) {
                e.preventDefault();
                $('#masthead').find('.menu-toggle').focus();
            }
        });

        $('#primary-menu > li:last-child button:not(.active)').bind( 'keydown', function(e) {
            if( e.which === 9 ) {
                e.preventDefault();
                $('#masthead').find('.menu-toggle').focus();
            }
        });

        $('#search').find("button").unbind('keydown');

    }
    else {
        $('#primary-menu').find("li").unbind('keydown');

        $('#search').find("button").bind( 'keydown', function(e) {
            var tabKey              = e.keyCode === 9;
            var shiftKey            = e.shiftKey;

            if( tabKey ) {
                e.preventDefault();
                $('#search').hide();
                $('.search-menu > a').removeClass('search-active').focus();
            }

            if( shiftKey && tabKey ) {
                e.preventDefault();
                $('#search').show();
                $('.main-navigation .search-field').focus();
                $('.search-menu > a').addClass('search-active');
            }
        });

        $('.search-menu > a').on('keydown', function (e) {
            var tabKey              = e.keyCode === 9;
            var shiftKey            = e.shiftKey;
            
            if( $('.search-menu > a').hasClass('search-active') ) {
                if ( shiftKey && tabKey ) {
                    e.preventDefault();
                    $('#search').hide();
                    $('.search-menu > a').removeClass('search-active').focus();
                }
            }
        });
    }
});

menu_toggle.on('keydown', function (e) {
    var tabKey    = e.keyCode === 9;
    var shiftKey  = e.shiftKey;

    if( menu_toggle.hasClass('active') ) {
        if ( shiftKey && tabKey ) {
            e.preventDefault();
            primary_menu.slideUp();
            $('.main-navigation').removeClass('menu-open');
            $('.menu-overlay').removeClass('active');
            menu_toggle.removeClass('active');
        };
    }
});

/*------------------------------------------------
                END JQUERY
------------------------------------------------*/

});