// 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' );
    dry_cleaning_services_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 dry_cleaning_services_trapFocus( element, namespace ) {
  var dry_cleaning_services_focusableEls = element.find( 'a, button' );
  var dry_cleaning_services_firstFocusableEl = dry_cleaning_services_focusableEls[0];
  var dry_cleaning_services_lastFocusableEl = dry_cleaning_services_focusableEls[dry_cleaning_services_focusableEls.length - 1];
  var KEYCODE_TAB = 9;

  element.keydown( function(e) {
    var isTabPressed = ( e.key === 'Tab' || e.keyCode === KEYCODE_TAB );

    if ( !isTabPressed ) {
      return;
    }

    if ( e.shiftKey ) /* shift + tab */ {
      if ( document.activeElement === dry_cleaning_services_firstFocusableEl ) {
        dry_cleaning_services_lastFocusableEl.focus();
        e.preventDefault();
      }
    } else /* tab */ {
      if ( document.activeElement === dry_cleaning_services_lastFocusableEl ) {
        dry_cleaning_services_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;
  });

  dry_cleaning_services_search_focus();
});

// Slider
jQuery(document).ready(function() {
  var owl = jQuery('#slider-cat .owl-carousel');
  owl.owlCarousel({
    loop: true,
    margin: 0,
    nav: false,
    dots: true,
    rtl: false,
    items: 1,
    autoplay: true,
  });

  // Add numbers with leading zeros to the first and last dots
  var totalItems = jQuery('.owl-dot').length;
  jQuery('.owl-dot').each(function(index) {
    if(index === 0) {
      jQuery(this).append('<span class="dot-number top-number">01</span>');
    }
    if(index === totalItems - 1) {
      var lastNum = (index + 1).toString().padStart(2, '0');
      jQuery(this).append('<span class="dot-number bottom-number">' + lastNum + '</span>');
    }
  });
});