jQuery(document).ready(function ($) {
  $(window).scroll(function () {
    if ($(this).scrollTop() > 100) {
      $(".back-to-top a").fadeIn();
    } else {
      $(".back-to-top a").fadeOut();
    }
  });

  $(".back-to-top a").click(function () {
    $("html, body").animate({ scrollTop: 0 }, 800);
    return false;
  });

  const section = $('.main-slider');
  const owl = section.find('.owl-carousel');
  const dots = section.find('.custom-dots .wp-block-button');

  owl.owlCarousel({
    loop: true,
    margin: 15,
    nav: false,
    dots: false,
    items: 1,
    animateOut: 'fadeOut',
    animateIn: 'fadeIn',
    smartSpeed: 600,
    mouseDrag: false,
    touchDrag: false,
    pullDrag: false,
    freeDrag: false,
    autoplay: true
  });

  /* ---- Click on existing buttons ---- */
  dots.each(function(index){
    $(this).on('click', function(e){
      e.preventDefault();
      owl.trigger('to.owl.carousel', [index, 600, true]);
    });
  });

  /* ---- Update Active Button ---- */
  owl.on('changed.owl.carousel', function(e){
    let index = (e.item.index - e.relatedTarget._clones.length / 2 + e.item.count) % e.item.count;

    dots.removeClass('active')
        .eq(index)
        .addClass('active');
  });

  dots.eq(0).addClass('active');

  //Project slider
  function courier_company_initCarousel() {
    var $courier_company_carousel = $('.project-section .owl-carousel');

    $courier_company_carousel.owlCarousel({
      nav: true,
      navText: ["<span class='dashicons dashicons-arrow-left-alt2'></span>","<span class='dashicons dashicons-arrow-right-alt2'></span>"],
      dots: false,
      lazyLoad: true,
      autoplayTimeout: 4000,
      loop: true,
      center: true,
      autoplayHoverPause: false,
      autoplay: true,
      mouseDrag: true,
      responsive: {
        0: {
          items: 1,
          margin: 8,
        },
        420: {
          items: 1,
          margin: 12,
        },
        634: {
          items: 1,
          margin: 15,
        },
        768: {
          items: 3,
          margin: 15,
        },
        958: {
          items: 3,
          margin: 18,
        },
        992: {
          items: 3,
          margin: 17,
        },
        1200: {
          items: 3,
          margin:20,
        },
        1720: {
          items: 3,
          margin:25,
        }
      },
      onInitialized: courier_company_updateActiveCenter,
      onTranslated: courier_company_updateActiveCenter
    });
  }

  function courier_company_updateActiveCenter() {
    var $courier_company_section = $('.projects-slider');

    $courier_company_section.find('.owl-item')
      .removeClass('active-center');

    $courier_company_section.find('.owl-item.center')
      .addClass('active-center');
  }
  courier_company_initCarousel();

});

// Services Tab
document.addEventListener("click", e => {  if (e.target.parentElement && e.target.parentElement.classList.contains("tab-box")  ) { courier_company_redTab(e) }  });

[...document.querySelectorAll("div.main-tab div")].forEach((tabTile, index) => {
    tabTile.classList.toggle('active', index == 0)
  });
[...document.querySelectorAll(".tab-content")].forEach((tabcontent, index) => {
  tabcontent.classList.toggle('active', index == 0)
  });
function courier_company_redTab(e) {  
  let courier_company_tabTiles = [...document.querySelectorAll("div.main-tab div")];
  let courier_company_tabcontents = [...document.querySelectorAll(".tab-content")];
  let courier_company_activeTabIndex = courier_company_tabTiles.findIndex(tab => { return tab == e.target.parentElement })
  courier_company_tabTiles.forEach((tabTile, index) => {
    tabTile.classList.toggle('active', index === courier_company_activeTabIndex)
  })
  courier_company_tabcontents.forEach((tabcontent, index) => {
  tabcontent.classList.toggle('active', index === courier_company_activeTabIndex)
  })
}