// 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' );
    cloud_storage_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 cloud_storage_services_trapFocus(element) {
  var focusableElementsSelector = 'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])';
  var focusableEls = element.find(focusableElementsSelector).filter(':visible');
  var firstFocusableEl = focusableEls[0];
  var lastFocusableEl = focusableEls[focusableEls.length - 1];
  var KEYCODE_TAB = 9;

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

    if (e.shiftKey) {
      // Shift + Tab
      if (document.activeElement === firstFocusableEl) {
        lastFocusableEl.focus();
        e.preventDefault();
      }
    } else {
      // Tab
      if (document.activeElement === lastFocusableEl) {
        firstFocusableEl.focus();
        e.preventDefault();
      }
    }
  });
}

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

// Sticky Header
jQuery('document').ready(function($){
  $(window).scroll(function(){
    var sticky = $('.is-sticky-on'),
      scroll = $(window).scrollTop();

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

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

// Team Slider
jQuery(document).ready(function($) {
  var owl = $('#team-section .owl-carousel').owlCarousel({
    loop: true,
    margin: 15,
    nav: false,
    dots: false,
    rtl: false,
    responsive: {
      0: { 
        items: 1 
      },
      768: { 
        items: 1 
      },
      992: { 
        items: 3 
      }
    },
    autoplay: true,
  });

  // custom navigation
  $('#team-section .blog-bx .custom-next').click(function() {
    owl.trigger('next.owl.carousel');
  });

  $('#team-section .blog-bx .custom-prev').click(function() {
    owl.trigger('prev.owl.carousel');
  });
});

// Progress Bar
jQuery(document).ready(function($){

  $(window).on("scroll", function() {

      var scrollTop = $(window).scrollTop();
      var docHeight = $(document).height();
      var winHeight = $(window).height();
      var scrollPercent = (scrollTop / (docHeight - winHeight)) * 100;

      $("#progress-bar").css("width", scrollPercent + "%");
  });

});

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

  var cloud_storage_services_scrollTop = window.scrollY || document.documentElement.cloud_storage_services_scrollTop;

  if (cloud_storage_services_scrollTop >= 100) {
    cloud_storage_services_footer.classList.add('active-sticky');
  }
});
