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

/*------------------------------------------------
            DECLARATIONS
------------------------------------------------*/
    var loader                  = $('#loader');
    var loader_container        = $('#preloader');
    var scroll = $(window).scrollTop();  
    var scrollup = $('.backtotop');
    var menu_toggle = $('.menu-toggle');
    var nav_menu = $('.main-navigation ul.nav-menu');
    var featured_slider = $('.featured-slider-wrapper');
    var slider_widget = $('.featured-slider .slider-posts');
    var highlights_slider = $('.highlights-wrapper');
    var hero_slider = $('.hero-slider');
    var project_slider = $('.project-slider');
    var client_slider = $('.client-slider');
    var fitnesscat_slider = $('.fitnesscat-slider');
    var trending_slider      = $('.trending-slider');
    var posts_height = $('.blog-posts-wrapper article .post-item');
    var masonry_gallery = $('.grid');
    var portfolio_gallery = $('.portfolio-grid');
    var testimonial_slider    = $('.default-testimonial');
    var modern_testimonial_slider    = $('.modern-testimonial');
    var naturegallery_slider    = $('.naturegallery-slider');
    var naturefeatured_slider    = $('.naturefeatured-slider');

/* -----------------------------------------
    Preloader
    ----------------------------------------- */
    loader_container.delay(1000).fadeOut();
    loader.delay(1000).fadeOut("slow");

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

    $(window).scroll(function() {
        if ($(this).scrollTop() > 1) {
            scrollup.css({bottom:"50px"});
        } 
        else {
            scrollup.css({bottom:"-100px"});
        }
    });

    scrollup.click(function() {
        $('html, body').animate({scrollTop: '0px'}, 800);
        return false;
    });

/*------------------------------------------------
            MAIN NAVIGATION
------------------------------------------------*/
    $('#top-bar > button').click(function(){
        $('#top-bar .wrapper').slideToggle();
        $('#top-bar').toggleClass('topbar-menu-active');
    });
   
    menu_toggle.click(function(){
        $(this).toggleClass('active');
        nav_menu.slideToggle();
        $('body').toggleClass('menu-toggled');
    });

    $('.main-navigation .nav-menu .menu-item-has-children > a').after( $('<button class="dropdown-toggle"><i class="fa fa-angle-down"></i></button>') );

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

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

     if( $(window).width() < 2000 ) {
         $('.modern-menu #primary-menu').find("li").last().bind( 'keydown', function(e) {
            if( e.which === 9 ) {
                e.preventDefault();
                $('#masthead').find('.menu-toggle').focus();
            }
        });
        $('.modern-menu #primary-menu').find("li").last('button:not(.active)').bind( 'keydown', function(e) {
            if( e.which === 9 ) {
                e.preventDefault();
                $('#masthead').find('.menu-toggle').focus();
            }
        });
    }
    $(window).resize(function() {
        if( $(window).width() < 2000 ) {
             $('.modern-menu #primary-menu').find("li").last().bind( 'keydown', function(e) {
                if( e.which === 9 ) {
                    e.preventDefault();
                    $('#masthead').find('.menu-toggle').focus();
                }
            });
            $('.modern-menu #primary-menu').find("li").last('button:not(.active)').bind( 'keydown', function(e) {
                if( e.which === 9 ) {
                    e.preventDefault();
                    $('#masthead').find('.menu-toggle').focus();
                }
            });
        }
        else {
            $( '#primary-menu > li:last-child' ).unbind('keydown');
        }
    });
    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').find("li").last('button:not(.active)').bind( 'keydown', function(e) {
            if( e.which === 9 ) {
                e.preventDefault();
                $('#masthead').find('.menu-toggle').focus();
            }
        });
    }
    else {
        $( 'body:not(.modern-menu) #primary-menu > li:last-child' ).unbind('keydown');
    }
    $(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').find("li").last('button:not(.active)').bind( 'keydown', function(e) {
                if( e.which === 9 ) {
                    e.preventDefault();
                    $('#masthead').find('.menu-toggle').focus();
                }
            });
        }
        else {
            $( 'body:not(.modern-menu) #primary-menu > li:last-child' ).unbind('keydown');
        }
    });

    if( $(window).width() < 1024 ) {
        $( '#top-bar' ).find('li').last().bind( 'keydown', function(e) {
            if( e.which === 9 ) {
                e.preventDefault();
                $('#top-bar').find('button').focus();
            }
        });
    }
    else {
        $( '#top-bar' ).find('li').unbind('keydown');
    }
    $(window).resize(function() {
        if( $(window).width() < 1024 ) {
            $( '#top-bar li' ).find('li').bind( 'keydown', function(e) {
                if( e.which === 9 ) {
                    e.preventDefault();
                    $('#top-bar').find('button').focus();
                }
            });
        }
        else {
            $( '#top-bar li' ).find('li').unbind('keydown');
        }
    });

    $('#masthead .menu-toggle').on('keydown', function (e) {
    var tabKey = e.keyCode === 9;
    var shiftKey = e.shiftKey;
    if( $('#masthead .menu-toggle').hasClass('active') ) {
        if ( shiftKey && tabKey ) { 
            e.preventDefault();
            nav_menu.slideUp();
            $('#masthead .menu-toggle').removeClass('active');
        };
    }
}); 
if ( $(window).width() > 1024 ) {
    $('.header-nine.align-logo-center .site-branding').insertAfter('#site-navigation ul.nav-menu > li:nth-child(4)');
}

$(window).resize(function() {
    if ( $(window).width() < 1024 )
        $('.header-nine.align-logo-center .site-branding').insertBefore('#site-navigation');
    else
        $('.header-nine.align-logo-center .site-branding').insertAfter('#site-navigation ul.nav-menu > li:nth-child(4)');            
});
/* -----------------------------------------
Tabs
----------------------------------------- */
$('.post-tabs-wrapper').each(function(index) {
    $(this).find('.post-tab-container:not(:first-child)').hide();
    $(this).find('.post-tabs li:not(:first-child) a').addClass('inactive');
});
$('.post-tabs li a').click(function() {
    var t = $(this).attr('href');
    $(this).closest('.post-tabs-wrapper').find('.post-tabs li a').addClass('inactive');
    $(this).removeClass('inactive');
    $(this).closest('.post-tabs-wrapper').find('.post-tab-container').hide();
    $(this).closest('.post-tabs-wrapper').find(t).fadeIn('slow');
    return false;
});

/*------------------------------------------------
            SLICK SLIDER
------------------------------------------------*/
    featured_slider.slick({
        responsive: [
    {
        breakpoint: 992,
        settings: {
            slidesToShow: 1
        }
    },
    {
        breakpoint: 767,
        settings: {
            slidesToShow: 1
        }
    }
    ]
    });

    highlights_slider.slick({
        responsive: [
    {
        breakpoint: 1200,
        settings: {
            slidesToShow: 2
        }
    },
    {
        breakpoint: 767,
        settings: {
            slidesToShow: 1,
            arrows: false
        }
    }
    ]
    });

    $('.gallery-slider').slick({
    asNavFor: '.gallery-slider-nav'
});

$('.slider-posts-widget .slider-posts').slick();

$('.gallery-slider-nav').slick({
    slidesToShow: 2,
    slidesToScroll: 1,
    asNavFor: '.gallery-slider',
    arrows: true,
    dots: false,
    infinite: true,
    focusOnSelect: true,
    autoplay:false,
    responsive: [

    {
        breakpoint: 992,
            settings: {
            slidesToShow: 2,
            arrows: false
        }
    }
    ]
});

/*------------------------------------------------
            MATCH HEIGHT
------------------------------------------------*/

    $('.single #primary .navigation a').matchHeight();
    $('#mustread .entry-container').matchHeight();
    $('#tips .entry-container').matchHeight();
    $('#featured  .entry-container').matchHeight();
    $('#timetable  .timetable-items').matchHeight();
    $('#features .features-item-wrapper .features-content').matchHeight();
    $('#project .entry-container').matchHeight();
    $('.blog-six .blog-posts-wrapper article').matchHeight();
    $('#recent article .entry-container').matchHeight();

/*------------------------------------------------
                Tabs
------------------------------------------------*/

$('#portfolio ul.tabs li').click(function(event) {
    event.preventDefault();

    var tab_id = $(this).attr('data-tab');

    $('#portfolio ul.tabs li').removeClass('active');
    $('#portfolio .tab-content').removeClass('active');
    $('#portfolio .tab-content').fadeOut();
    $(this).addClass('active');
    $("#"+tab_id).fadeIn();

});

$('#gallerypost ul.tabs li').click(function(event) {
    event.preventDefault();

    var tab_id = $(this).attr('data-tab');

    $('#gallerypost ul.tabs li').removeClass('active');
    $('#gallerypost .tab-content').removeClass('active');
    $('#gallerypost .tab-content').fadeOut();
    $(this).addClass('active');
    $("#"+tab_id).fadeIn();

});

$('#details ul.tabs li').click(function(event) {
    event.preventDefault();

    var tab_id = $(this).attr('data-tab');

    $('#details ul.tabs li').removeClass('active');
    $('#details .tab-content').removeClass('active');
    $('#details .tab-content').fadeOut();
    $(this).addClass('active');
    $("#"+tab_id).fadeIn();

});

/*------------------------------------------------
            MASONRY GALLERY
------------------------------------------------*/
    
    masonry_gallery.imagesLoaded( function() {
        masonry_gallery.packery({
            itemSelector: '.grid-item'
        });
    });    
    portfolio_gallery.imagesLoaded( function() {
        portfolio_gallery.packery({
            itemSelector: '.portfolio-grid-item'
        });
    });


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

});