// menus 
function reseller_store_menu_open_nav() {
	window.reseller_store_responsiveMenu=true;
	jQuery(".sidenav").addClass('show');
}
function reseller_store_menu_close_nav() {
	window.reseller_store_responsiveMenu=false;
 	jQuery(".sidenav").removeClass('show');
}
jQuery(function($){
 	"use strict";
 	jQuery('.main-menu > ul').superfish({
		delay: 500,
		animation: {opacity:'show',height:'show'},
		speed: 'fast'
 	});
});

jQuery(document).ready(function () {
	window.reseller_store_currentfocus=null;
  	reseller_store_checkfocusdElement();
	var reseller_store_body = document.querySelector('body');
	reseller_store_body.addEventListener('keyup', reseller_store_check_tab_press);
	var reseller_store_gotoHome = false;
	var reseller_store_gotoClose = false;
	window.reseller_store_responsiveMenu=false;
 	function reseller_store_checkfocusdElement(){
	 	if(window.reseller_store_currentfocus=document.activeElement.className){
		 	window.reseller_store_currentfocus=document.activeElement.className;
	 	}
 	}
 	function reseller_store_check_tab_press(e) {
		"use strict";
		// pick passed event or global event object if passed one is empty
		e = e || event;
		var activeElement;

		if(window.innerWidth < 999){
		if (e.keyCode == 9) {
			if(window.reseller_store_responsiveMenu){
			if (!e.shiftKey) {
				if(reseller_store_gotoHome) {
					jQuery( ".main-menu ul:first li:first a:first-child" ).focus();
				}
			}
			if (jQuery("a.closebtn.mobile-menu").is(":focus")) {
				reseller_store_gotoHome = true;
			} else {
				reseller_store_gotoHome = false;
			}

		}else{

			if(window.reseller_store_currentfocus=="responsivetoggle"){
				jQuery( "" ).focus();
			}}}
		}
		if (e.shiftKey && e.keyCode == 9) {
		if(window.innerWidth < 999){
			if(window.reseller_store_currentfocus=="header-search"){
				jQuery(".responsivetoggle").focus();
			}else{
				if(window.reseller_store_responsiveMenu){
				if(reseller_store_gotoClose){
					jQuery("a.closebtn.mobile-menu").focus();
				}
				if (jQuery( ".main-menu ul:first li:first a:first-child" ).is(":focus")) {
					reseller_store_gotoClose = true;
				} else {
					reseller_store_gotoClose = false;
				}

			}else{

			if(window.reseller_store_responsiveMenu){
			}}}}
		}
	 	reseller_store_checkfocusdElement();
	}
});

jQuery('document').ready(function($){
	// product share icons
	jQuery('.share-btn-parent').on('click', function(){
    jQuery(this).parent('.socialbox').find('.show-icon').toggleClass('show_hide');
  });
  jQuery('.share-btn-parent').on('click', function(){
    jQuery(this).find('.share-i').toggleClass('green');
  });

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

  // Sticky Header
  $(window).scroll(function(){
		var sticky = $('.header-sticky'),
			scroll = $(window).scrollTop();

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

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

  var scrollTop = window.scrollY || document.documentElement.scrollTop;
  var windowHeight = window.innerHeight;
  var documentHeight = document.documentElement.scrollHeight;

  var isBottom = scrollTop + windowHeight >= documentHeight-100;

  if (scrollTop >= 100 && !isBottom) {
    sticky.classList.add('copyright-fixed');
  } else {
    sticky.classList.remove('copyright-fixed');
  }
});

// Scroller
jQuery(document).ready(function () {
	jQuery(window).scroll(function () {
    if (jQuery(this).scrollTop() > 100) {
      jQuery('.scrollup i').fadeIn();
    } else {
      jQuery('.scrollup i').fadeOut();
    }
	});
	jQuery('.scrollup i').click(function () {
    jQuery("html, body").animate({
      scrollTop: 0
    }, 600);
    return false;
	});
});

// Product section
jQuery('document').ready(function(){
  var owl = jQuery('#main-product .owl-carousel');
    owl.owlCarousel({
    margin:20,
    autoplayTimeout: 1000,
    loop: true,
    dots:true,
    nav:true,
    navText : ['<i class="fa fa-chevron-left" aria-hidden="true"></i>','<i class="fa fa-chevron-right" aria-hidden="true"></i>'],
    responsive: {
      0: {
        items: 1,
      },
      600: {
        items: 2
      },
      1024: {
        items: 3,
        margin:10
      },
      1200: {
        items: 3,
        margin:10,
      }
    },
    autoplayHoverPause : true,
    mouseDrag: true
  });
});

jQuery(document).ready(function () {
    // Iterate through each product section with a timer
    document.querySelectorAll('.main-product-section').forEach((element) => {
        const timerElem = jQuery(element).find('.auction-timmer-name').attr('data-date');
        if (timerElem) {
            reseller_store_our_events_timer(element, timerElem);
        } else {
            jQuery(element).find('.the_timer').html("Timer not updated.");
        }
    });
});

function reseller_store_our_events_timer(element, mydate) {
    const countDownDate = new Date(mydate).getTime();

    const x = setInterval(() => {
        const now = new Date().getTime();
        const distance = countDownDate - now;

        if (distance < 0) {
            clearInterval(x);
            jQuery(element).find('.the_timer').html("EXPIRED");
            return;
        }

        const days = Math.floor(distance / (1000 * 60 * 60 * 24));
        const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((distance % (1000 * 60)) / 1000);

        const timerHtml = `
            <span class='numbers'>${days}<br><span class='nofont'>Days</span></span>
            <span class='numbers'>${hours}<br><span class='nofont'>Hours</span></span>
            <span class='numbers'>${minutes}<br><span class='nofont'>Minutes</span></span>
            <span class='numbers'>${seconds}<br><span class='nofont'>Seconds</span></span>
        `;

        jQuery(element).find('.the_timer').html(timerHtml);
    }, 1000);
}

// slider haeding
jQuery(document).ready(function () {
  jQuery('.slide_splice').each(function() {
    let targetElement = jQuery(this);
    let wordNumber = targetElement.attr('data-word');
    var textContent = targetElement.text().trim();
    var words = textContent.split(' ');
    if (words.length >= wordNumber) {
      var newWords = [];
      for (var i = 0; i < words.length; i++) {
        if (i === wordNumber - 1) {
          newWords.push('<span class="second-word">' + words[i] + '</span>');
        } else {
          newWords.push(words[i]);
        }
      }
      var newSentence = newWords.join(' ');
      targetElement.html(newSentence);
    }
  });
});

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

  var scrollTop = window.scrollY || document.documentElement.scrollTop;
  var windowHeight = window.innerHeight;
  var documentHeight = document.documentElement.scrollHeight;

  var isBottom = scrollTop + windowHeight >= documentHeight-100;

  if (scrollTop >= 100 && !isBottom) {
    sticky.classList.add('sidebar-fixed');
  } else {
    sticky.classList.remove('sidebar-fixed');
  }
});


// search product
jQuery(document).ready(function () {
  	function reseller_store_search_loop_focus(element) {
	  var reseller_store_focus = element.find('select, input, textarea, button, a[href]');
	  var reseller_store_firstFocus = reseller_store_focus[0];  
	  var reseller_store_lastFocus = reseller_store_focus[reseller_store_focus.length - 2];
	  var KEYCODE_TAB = 9;

	  element.on('keydown', function reseller_store_search_loop_focus(e) {
	    var isTabPressed = (e.key === 'Tab' || e.keyCode === KEYCODE_TAB);
	    if (!isTabPressed) { 
	      return; 
	    }
	    if ( e.shiftKey ) /* shift + tab */ {
	      if (document.activeElement === reseller_store_firstFocus) {
	        reseller_store_lastFocus.focus();
	          e.preventDefault();
	        }
	      } else /* tab */ {
	      if (document.activeElement === reseller_store_lastFocus) {
	        reseller_store_firstFocus.focus();
	          e.preventDefault();
	        }
	      }
	  });
	}
	jQuery('.search-box span a').click(function(){
	  jQuery(".serach_outer").slideDown(1000);
		reseller_store_search_loop_focus(jQuery('.serach_outer'));
  });
  jQuery('.closepop a').click(function(){
    jQuery(".serach_outer").slideUp(1000);
  });
});
/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const reseller_store_progressBar =
        document.getElementById("reseller_store_elemento_progress_bar");
    if (!reseller_store_progressBar) return;
    window.addEventListener("scroll", function () {
        const reseller_store_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const reseller_store_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const reseller_store_scrolled =
            (reseller_store_scrollTop / reseller_store_height) * 100;
        reseller_store_progressBar.style.width =
            reseller_store_scrolled + "%";
    });
});