jQuery(function($) {
    "use strict";

    // Keyboard loop function (Fix for ReferenceError)
    function hr_staffing_agency_Keyboard_loop(nav) {
        if (!nav.length) return;

        var focusable = nav.find('a, button, input, select, textarea').filter(':visible');
        var first = focusable.first();
        var last = focusable.last();

        nav.on('keydown', function(e) {
            if (e.key === 'Tab') {
                if (e.shiftKey) {
                    if ($(document.activeElement).is(first)) {
                        e.preventDefault();
                        last.focus();
                    }
                } else {
                    if ($(document.activeElement).is(last)) {
                        e.preventDefault();
                        first.focus();
                    }
                }
            }
        });
    }

    // Scroll to top functionality
    $(window).on('scroll', function() {
        if ($(this).scrollTop() >= 50) {
            $('#return-to-top').fadeIn(200);
        } else {
            $('#return-to-top').fadeOut(200);
        }
    });

    $('#return-to-top').on('click', function() {
        $('body,html').animate({ scrollTop: 0 }, 500);
    });

    // Side navigation toggle
    $('.gb_toggle').on('click', function() {
        var nav = $('.side_gb_nav');
        nav.toggleClass('active');

        // Call keyboard accessibility function
        hr_staffing_agency_Keyboard_loop(nav);
    });

    // Preloader fade out
    setTimeout(function() {
        $(".loader").fadeOut("slow");
    }, 1000);

});

// Mobile responsive menu
function hr_staffing_agency_menu_open_nav() {
    jQuery(".sidenav").addClass('open');
}

function hr_staffing_agency_menu_close_nav() {
    jQuery(".sidenav").removeClass('open');
}

// tab js
document.addEventListener("DOMContentLoaded", function () {

  const tabs = document.querySelectorAll(".tab-btn");
  const contents = document.querySelectorAll(".tab-content");

  function hr_staffing_agency_initOwl(el) {
    if (window.jQuery && el) {
      let $hr_staffing_agency_el = jQuery(el);

      // destroy safely if already initialized
      if ($hr_staffing_agency_el.hasClass('owl-loaded')) {
        $hr_staffing_agency_el.trigger('destroy.owl.carousel');
        $hr_staffing_agency_el.removeClass('owl-loaded owl-hidden');
        $hr_staffing_agency_el.find('.owl-stage-outer').children().unwrap();
      }

      // re-init
      $hr_staffing_agency_el.owlCarousel({
        loop: true,
        margin: 20,
        nav: false,
        dots: true,
        responsive:{
          0:{ items:1 },
          768:{ items:2 },
          1000:{ items:3 }
        }
      });
    }
  }

  // INIT FIRST TAB (visible one)
  const firstTab = document.querySelector(".tab1");
  if (firstTab) {
    firstTab.style.display = "block";
    hr_staffing_agency_initOwl(firstTab);
  }

  tabs.forEach(tab => {
    tab.addEventListener("click", function (e) {

      e.preventDefault();

      // active class
      tabs.forEach(t => t.classList.remove("active"));
      this.classList.add("active");

      // hide all tabs
      contents.forEach(c => {
        c.style.display = "none";
      });

      const target = this.getAttribute("data-tab");
      const activeTab = document.querySelector("." + target);

      if (activeTab) {
        activeTab.style.display = "block";

        // IMPORTANT: delay + reinit
        setTimeout(() => {
          hr_staffing_agency_initOwl(activeTab);
        }, 100);
      }

    });
  });

});