// Menus 
function virtual_assistant_menu_open_nav() {
  jQuery(".sidenav").addClass('show');
}
function virtual_assistant_menu_close_nav() {
  jQuery(".sidenav").removeClass('show');
}

( function( window, document ) {
  function virtual_assistant_keepFocusInMenu() {
    document.addEventListener( 'keydown', function( e ) {
      const virtual_assistant_nav = document.querySelector( '.sidenav' );

      if ( ! virtual_assistant_nav || ! virtual_assistant_nav.classList.contains( 'show' ) ) {
        return;
      }
      const elements = [...virtual_assistant_nav.querySelectorAll( 'input, a, button' )],
        virtual_assistant_lastEl = elements[ elements.length - 1 ],
        virtual_assistant_firstEl = elements[0],
        virtual_assistant_activeEl = document.activeElement,
        tabKey = e.keyCode === 9,
        shiftKey = e.shiftKey;

      if ( ! shiftKey && tabKey && virtual_assistant_lastEl === virtual_assistant_activeEl ) {
        e.preventDefault();
        virtual_assistant_firstEl.focus();
      }

      if ( shiftKey && tabKey && virtual_assistant_firstEl === virtual_assistant_activeEl ) {
        e.preventDefault();
        virtual_assistant_lastEl.focus();
      }
    } );
  }
  virtual_assistant_keepFocusInMenu();
} )( window, document );

jQuery('document').ready(function($){
	// 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;
	});
});

// Banner Title
jQuery(document).ready(function() {
  jQuery("#banner .banner-content .banner-title").each(function() {
    var t = jQuery(this).text();
    var splitT = t.split(" ");
    var newText = "";
    
    for (var i = 0; i < splitT.length; i++) {
      if (i === 1) { // 2nd word (index starts at 0)
        newText += "<span class='inner-text'><span class='banner-bg-text'>";
        newText += splitT[i] + " ";
      } else if (i === 2) { // 3rd word
        newText += splitT[i] + " ";
        newText += "</span></span>"; // Close the span after 3rd word
      } else {
        newText += splitT[i] + " ";
      }     
    }   
    
    jQuery(this).html(newText.trim());
  });
});

// Title Color (Team Section)
jQuery(document).ready(function() {
  jQuery("#team-section .section-text").each(function() {
    var t = jQuery(this).text();
    var splitT = t.split(" ");
    var newText = "";
    for (var i = 0; i < splitT.length; i++) {
      if (i == splitT.length - 1) { // Check for the last word
        newText += "<span class='title-text'>";
        newText += splitT[i] + " ";
        newText += "</span>";
      } else {
        newText += splitT[i] + " ";
      }     
    }   
    jQuery(this).html(newText.trim());
  });
});

// homepage sidebar
jQuery(document).ready(function () {
  function virtual_assistant_search_loop_focus(element) {
    var virtual_assistant_focus = element.find('select, input, textarea, button, a[href]');
    var virtual_assistant_firstFocus = virtual_assistant_focus[0];  
    var virtual_assistant_lastFocus = virtual_assistant_focus[virtual_assistant_focus.length - 1];
    var KEYCODE_TAB = 9;

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

      if (!isTabPressed) { 
        return; 
      }

      if ( e.shiftKey ) /* shift + tab */ {
        if (document.activeElement === virtual_assistant_firstFocus) {
        virtual_assistant_lastFocus.focus();
          e.preventDefault();
        }
        } else /* tab */ {
        if (document.activeElement === virtual_assistant_lastFocus) {
          virtual_assistant_firstFocus.focus();
          e.preventDefault();
        }
      }
    });
  }

  jQuery('.toggle-btn a').click(function(){
    jQuery(".header-sidebar").addClass('show');
    virtual_assistant_search_loop_focus(jQuery('.header-sidebar'));
  });

  jQuery('.header-sidebar .close-pop a').click(function(){
    jQuery(".header-sidebar").removeClass('show');
  });
});

// Video Popup
(function( $ ) {
  $(document).ready(function(){
    $('#openBtn').on('click', function() {
      $('#videoOverlay').css('display', 'flex');
    });
    $('.close-btn').on('click', function() {
      $('#videoOverlay').hide();
    });
  });
})( jQuery );

/*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');
  }
});
/* Progress Bar */
document.addEventListener("DOMContentLoaded", function () {
    const virtual_assistant_progressBar =
        document.getElementById("virtual_assistant_elemento_progress_bar");
    if (!virtual_assistant_progressBar) return;
    window.addEventListener("scroll", function () {
        const virtual_assistant_scrollTop =
            document.documentElement.scrollTop || document.body.scrollTop;
        const virtual_assistant_height =
            document.documentElement.scrollHeight -
            document.documentElement.clientHeight;
        const virtual_assistant_scrolled =
            (virtual_assistant_scrollTop / virtual_assistant_height) * 100;
        virtual_assistant_progressBar.style.width =
            virtual_assistant_scrolled + "%";
    });
});