// preloader
jQuery(window).on('load', function() {
  jQuery('#status').fadeOut();
  jQuery('#preloader').delay(350).fadeOut('slow');
  jQuery('body').delay(350).css({'overflow':'visible'});
})

// toggle button
jQuery(function($){
  $( '.toggle-nav button' ).click( function(e){
    $( 'body' ).toggleClass( 'show-main-menu' );
    var element = $( '.sidenav' );
    classic_toy_store_trapFocus( element );
  });

  $( '.close-button' ).click( function(e){
    $( '.toggle-nav button' ).click();
    $( '.toggle-nav button' ).focus();
  });
  $( document ).on( 'keyup',function(evt) {
    if ( $( 'body' ).hasClass( 'show-main-menu' ) && evt.keyCode == 27 ) {
      $( '.toggle-nav button' ).click();
      $( '.toggle-nav button' ).focus();
    }
  });
});

function classic_toy_store_trapFocus(element) {
  var focusableElementsSelector = 'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])';
  var focusableEls = element.find(focusableElementsSelector).filter(':visible');
  var firstFocusableEl = focusableEls[0];
  var lastFocusableEl = focusableEls[focusableEls.length - 1];
  var KEYCODE_TAB = 9;

  element.on('keydown', function(e) {
    var isTabPressed = (e.key === 'Tab' || e.keyCode === KEYCODE_TAB);
    if (!isTabPressed) {
      return;
    }

    if (e.shiftKey) {
      // Shift + Tab
      if (document.activeElement === firstFocusableEl) {
        lastFocusableEl.focus();
        e.preventDefault();
      }
    } else {
      // Tab
      if (document.activeElement === lastFocusableEl) {
        firstFocusableEl.focus();
        e.preventDefault();
      }
    }
  });
}

jQuery(document).ready(function () {
  // Sticky Header
  jQuery(window).scroll(function () {
    var sticky = jQuery('.header-sticky'),
        scroll = jQuery(this).scrollTop();

    if (scroll >= 100) {
      sticky.addClass('header-fixed');
    } else {
      sticky.removeClass('header-fixed');
    }

    // Scroll to Top Button
    if (scroll > 0) {
      jQuery('#button').fadeIn();
    } else {
      jQuery('#button').fadeOut();
    }
  });

  jQuery('#button').click(function () {
    jQuery("html, body").animate({
      scrollTop: 0
    }, 600);
    return false;
  });

  classic_toy_store_search_focus();
});

// product cat
jQuery(document).ready(function(){
  jQuery(".product-categories").hide();
  jQuery("button.product-btn").click(function(){
    jQuery(".product-categories").toggle();
  });
  jQuery(document).click(function(event) {
    if (!jQuery(event.target).closest('.product-categories, .product-btn').length) {
      jQuery(".product-categories").hide();
    }
  });
});

// Banner Title Color
jQuery(document).ready(function () {
  jQuery("#banner-cat .bannerbox .banner-title").each(function () {
    var t = jQuery(this).text().trim();
    var splitT = t.split(" ");
    var newText = "";

    splitT.forEach((word, index) => {
      let totalWords = splitT.length;

      if (index === 0) {
        newText += "<span class='first-word'>" + word + "</span> ";
      } else if (index === 2 || index === 3) {
        if (index === 2) newText += "<span class='third-fourth'>";
        newText += word + " ";
        if (index === 3) newText += "</span>";
      } else if (index === totalWords - 1) {
        newText += "<span class='last-word'>" + word + "</span> ";
      } else if (index === totalWords - 3) {
        newText += "<span class='third-from-last'>" + word + "</span> ";
      } else {
        newText += word + " ";
      }
    });

    jQuery(this).html(newText.trim());
  });
});

// Product Section Title Color
jQuery(document).ready(function () {
  jQuery("#featured-pro-section .blog-bx .featured-pro-title").each(function () {
    var t = jQuery(this).text().trim();
    var splitT = t.split(" ");
    var newText = "";

    var middleIndex = Math.floor(splitT.length / 2);

    for (var i = 0; i < splitT.length; i++) {
      if (i === 0) {
        newText += "<span class='first-word'>" + splitT[i] + "</span> ";
      } else if (i === middleIndex) {
        newText += "<span class='middle-word'>" + splitT[i] + "</span> ";
      } else if (i === splitT.length - 1) {
        newText += "<span class='last-word'>" + splitT[i] + "</span> ";
      } else {
        newText += splitT[i] + " ";
      }
    }

    jQuery(this).html(newText.trim());
  });
});

// Progress Bar
jQuery(document).ready(function($){

  $(window).on("scroll", function() {

      var scrollTop = $(window).scrollTop();
      var docHeight = $(document).height();
      var winHeight = $(window).height();
      var scrollPercent = (scrollTop / (docHeight - winHeight)) * 100;

      $("#progress-bar").css("width", scrollPercent + "%");
  });

});

//sticky copyright
window.addEventListener('scroll', function() {
  var classic_toy_store_footer = document.querySelector('.sticky-copyright');
  if (!classic_toy_store_footer) return;

  var classic_toy_store_scrollTop = window.scrollY || document.documentElement.classic_toy_store_scrollTop;

  if (classic_toy_store_scrollTop >= 100) {
    classic_toy_store_footer.classList.add('active-sticky');
  }
});
