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


    // Nivo slider 
    $('#slider').nivoSlider({
        effect: 'fade'
    });

    // Sticky Sidebar
    $('.sidebar').theiaStickySidebar({

        'additionalMarginTop': 10
    });

    /* -----------------------------------------
    Navigation
    ----------------------------------------- */
    $('.menu-toggle').click(function() {
        $(this).toggleClass('open');
    });

    $('.menu-toggle').click(function() {
        $('.main-navigation').toggleClass('toggled');
    });

    
    /* -----------------------------------------
    Keyboard Navigation
    ----------------------------------------- */

    $(window).on('load resize', function() {
        if ($(window).width() < 1024 && $(window).width() >= 768) {
            $('.main-navigation').find("a").unbind('keydown');
            $('.main-navigation').find("li").last().bind('keydown', function(e) {
                if (e.which === 9) {
                    e.preventDefault();
                    $('#site-header').find('.menu-toggle').focus();
                }
            });
        } else if ($(window).width() < 768) {
            $('.main-navigation').find("li").unbind('keydown');
            $('.main-navigation').find("a").last().bind('keydown', function(e) {
                if (e.which === 9) {
                    e.preventDefault();
                    $('#site-header').find('.menu-toggle').focus();
                }
            });
        } else {
            $('.main-navigation').find("li").unbind('keydown');
            $('.main-navigation').find("a").unbind('keydown');
        }
    });

    
     
    /* -----------------------------------------
    Navigation menu
    ----------------------------------------- */
    var primary_menu_toggle = $('.menu-toggle');
    primary_menu_toggle.on('keydown', function(e) {
        var tabKey = e.keyCode === 9;
        var shiftKey = e.shiftKey;

        if (primary_menu_toggle.hasClass('open')) {
            if (shiftKey && tabKey) {
                e.preventDefault();
                $('.main-navigation').toggleClass('toggled');
                primary_menu_toggle.removeClass('open');
            };
        }
    });

    /* -----------------------------------------
    Home Tab section
    ----------------------------------------- */    
    $('.tab-content').hide();
    $('#tab1').show();
    $('body').on('click', '.tab-nav ul li a', function(e) {
        e.preventDefault();
        var parentLi = $(this).parent();
        var activeA = parentLi.siblings().find('a.active');
        var activeARel = activeA.attr('rel');
        console.log( activeARel );
        $(this).parents('div.tab-wrapper').find('div#' + activeARel).hide();
        activeA.removeClass('active');
        $(this).addClass('active');
        var currentARel = $(this).attr('rel');
        $(this).parents('div.tab-wrapper').find('div#' + currentARel).show();
    });

     
    /* -----------------------------------------
    Category Navigation
    ----------------------------------------- */    
    $(".ms-toggle-menu-wrap.ms-click-open .ms-toggle-label").on("click", function (e) {
        e.preventDefault();
        $(this).next(".ms-toggle-menu").toggleClass("ms-active");
        msKeyboardLoop($('.ms-product-menu'));
    });

    $('.ms-menu-toggle').on('click', function () {
        $(this).parents('.ms-main-navigation').find('.ms-main-menu').slideToggle();
        $(this).parents('.ms-main-navigation').toggleClass('toggled');
        msKeyboardLoop($('.ms-main-menu'));
        return false;
    });

    $(window).resize(function () {
        if ($(window).width() < 1000) {
            $('.ms-main-navigation .ms-dropdown').removeClass('ms-open');
        }

        if ($(window).width() > 768) {
            $('.ms-toggle-menu .ms-dropdown').removeClass('ms-open');
        }
    }).resize();

    var msKeyboardLoop = function (elem) {
        var tabbable = elem.find('select, input, textarea, button, a').filter(':visible');

        var firstTabbable = tabbable.first();
        var lastTabbable = tabbable.last();
        /*set focus on first input*/
        firstTabbable.focus();

        /*redirect last tab to first input*/
        lastTabbable.on('keydown', function (e) {
            if ((e.which === 9 && !e.shiftKey)) {
                e.preventDefault();
                firstTabbable.focus();
            }
        });

        /*redirect first shift+tab to last input*/
        firstTabbable.on('keydown', function (e) {
            if ((e.which === 9 && e.shiftKey)) {
                e.preventDefault();
                lastTabbable.focus();
            }
        });

        /* allow escape key to close insiders div */
        elem.on('keyup', function (e) {
            if (e.keyCode === 27) {
                elem.hide();
            }
            ;
        });
    };


   

    /* -----------------------------------------
    Go to Top
    ----------------------------------------- */
    $(window).scroll(function(){ 
        if ($(this).scrollTop() > 100) { 
            $('#scroll').fadeIn(); 
        } else { 
            $('#scroll').fadeOut(); 
        } 
    }); 
    $('#scroll').click(function(){ 
        $("html, body").animate({ scrollTop: 0 }, 600); 
        return false; 
    }); 

});


jQuery( function( $ ) {
  $( document ).on( 'added_to_wishlist removed_from_wishlist', function() {
    $.get( yith_wcwl_l10n.ajax_url, {
      action: 'yith_wcwl_update_wishlist_count'
    }, function( data ) {
      $('.logo-love').children('.wishlist-count').html( data.count );
    } );
  } );
} );

jQuery(window).load(function () {
    jQuery("#pre-loader").delay(300).fadeOut();
    jQuery(".loader-wrapper").delay(500).fadeOut("slow");
});

(function ($) {
    "use strict";

    var msKeyboardLoop = function (elem) {

        var tabbable = elem.find('select, input, textarea, button, a').filter(':visible');

        var firstTabbable = tabbable.first();
        var lastTabbable = tabbable.last();
        /*set focus on first input*/
        firstTabbable.focus();

        /*redirect last tab to first input*/
        lastTabbable.on('keydown', function (e) {
            if ((e.which === 9 && !e.shiftKey)) {
                e.preventDefault();
                firstTabbable.focus();
            }
        });

        /*redirect first shift+tab to last input*/
        firstTabbable.on('keydown', function (e) {
            if ((e.which === 9 && e.shiftKey)) {
                e.preventDefault();
                lastTabbable.focus();
            }
        });

        /* allow escape key to close insiders div */
        elem.on('keyup', function (e) {
            if (e.keyCode === 27) {
                elem.hide();
            }
        });
    };

});