// 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' );
    garden_farm_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 garden_farm_trapFocus( element, namespace ) {
  var garden_farm_focusableEls = element.find( 'a, button' );
  var garden_farm_firstFocusableEl = garden_farm_focusableEls[0];
  var garden_farm_lastFocusableEl = garden_farm_focusableEls[garden_farm_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 === garden_farm_firstFocusableEl ) {
        garden_farm_lastFocusableEl.focus();
        e.preventDefault();
      }
    } else /* tab */ {
      if ( document.activeElement === garden_farm_lastFocusableEl ) {
        garden_farm_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;
  });

  garden_farm_search_focus();
});

// search
function garden_farm_search_focus() {

  /* First and last elements in the menu */
  var garden_farm_search_firstTab = jQuery('.serach_inner input[type="search"]');
  var garden_farm_search_lastTab  = jQuery('button.search-close'); /* Cancel button will always be last */

  jQuery(".search-open").click(function(e){
    e.preventDefault();
    e.stopPropagation();
    jQuery('body').addClass("search-focus");
    garden_farm_search_firstTab.focus();
  });

  jQuery("button.search-close").click(function(e){
    e.preventDefault();
    e.stopPropagation();
    jQuery('body').removeClass("search-focus");
    jQuery(".search-open").focus();
  });

  /* Redirect last tab to first input */
  garden_farm_search_lastTab.on('keydown', function (e) {
    if (jQuery('body').hasClass('search-focus'))
    if ((e.which === 9 && !e.shiftKey)) {
      e.preventDefault();
      garden_farm_search_firstTab.focus();
    }
  });

  /* Redirect first shift+tab to last input*/
  garden_farm_search_firstTab.on('keydown', function (e) {
    if (jQuery('body').hasClass('search-focus'))
    if ((e.which === 9 && e.shiftKey)) {
      e.preventDefault();
      garden_farm_search_lastTab.focus();
    }
  });

  /* Allow escape key to close menu */
  jQuery('.serach_inner').on('keyup', function(e){
    if (jQuery('body').hasClass('search-focus'))
    if (e.keyCode === 27 ) {
      jQuery('body').removeClass('search-focus');
      garden_farm_search_lastTab.focus();
    };
  });
}

// Slider
jQuery(document).ready(function() {
  jQuery('.owl-carousel').owlCarousel({
    loop: true,
    margin: 0,
    nav: true,
    navText: ["<i class='fa-solid fa-arrow-left'></i>", "<i class='fa-solid fa-arrow-right'></i>"],
    dots: false,
    rtl: false,
    items: 1,
    autoplay: true,
  });
});

// slider heading
jQuery(document).ready(function() {
  jQuery("#slider-cat .text-content h1 a").each(function() {
    var garden_farm_second_t = jQuery(this).text();
    var garden_farm_second_splitT = garden_farm_second_t.split(" ");
    var garden_farm_second_lastSecondIndex = garden_farm_second_splitT.length - 2;
    var garden_farm_second_lastIndex = garden_farm_second_splitT.length - 1;
    var garden_farm_second_newText = "";
  
    if (garden_farm_second_splitT.length > 1) {

        var lastTwoWords = garden_farm_second_splitT[garden_farm_second_lastSecondIndex] + " " + garden_farm_second_splitT[garden_farm_second_lastIndex];
  
        garden_farm_second_newText = garden_farm_second_splitT.slice(0, garden_farm_second_lastSecondIndex).join(" ") + " ";

        garden_farm_second_newText += "<span class='lastclass' style='background: var(--first-theme-color); padding: 5px 7px; border-radius: 10px; display: inline-block;'>" + lastTwoWords + "</span>";
    } else {
        garden_farm_second_newText = garden_farm_second_t;
    }
  
    jQuery(this).html(garden_farm_second_newText);
  });

  jQuery(".lastclass").each(function() {
    var garden_farm_headingText = jQuery(this).text();
    var garden_farm_words = garden_farm_headingText.split(" ");
    var garden_farm_lastWordIndex = garden_farm_words.length - 1;
    var garden_farm_newText = "";

    if (garden_farm_words.length > 1) {
      garden_farm_newText = garden_farm_words.slice(0, garden_farm_lastWordIndex).join(" ") + " ";
      garden_farm_newText += "<span class='last-word'  style='color: #000000; background:var(--second-theme-color); border-radius: 5px; padding: 0 8px;'>" + garden_farm_words[garden_farm_lastWordIndex] + "</span>";
    } else {
      garden_farm_newText = garden_farm_headingText;
    }

    jQuery(this).html(garden_farm_newText);
  });

});
