/home/bdqbpbxa/demo-subdomains/gumballpay.goodface.com.ua/frontend/js/script.js
// Header dropdown opener

$(document).on('mouseenter mouseleave', '.header__accordion', function () {
  headerDropdown($(this));
});

// customClickOutsideCallback('.header__accordion.-open', function (e, selector) {
//   headerDropdown($(selector));
// });

function headerDropdown(opener) {
  let container = opener.closest('.header__accordion');
  let header = container.closest('.header');

  if (isPc) {
    header.toggleClass('-background-transition');

    if (container.hasClass('-open')) {
      container.removeClass('-open');
      header.addClass('--rounded');
      fullTransitionendCallback(opener, function () {
        let windowScrollChecker = window.scrollY == 0;
        if (windowScrollChecker && !container.hasClass('-open')) {
          header.removeClass('-filled');
        }
      }, 'transform');

    } else {
      fullTransitionendCallback(header, function (e) {
        if (header.hasClass('-background-transition')) {
          header.removeClass('--rounded');
          container.addClass('-open');
        }
      }, 'background-color');
      if (!header.hasClass('-filled')) {
        header.addClass('-filled');
      }
    }
  }
}


// Add arrow for link

$(document).ready(function () {
  let link = $(".link__bold");

  if (isPc) {
    link.each(function () {
      let linkText = $(this).text().trim();
      let array = linkText.split(" ");
      let lastWord = array.slice(-1);

      array.pop();
      array.push(`<span class="link-arrow">${lastWord}</span>`);
      let newText = array.join(" ");

      $(this).html(newText);
    });
  }
});


// Filled header after scroll

$(document).ready(headerFilledScroll);
$(window).scroll(headerFilledScroll);

function headerFilledScroll() {
  let scrollTop = window.scrollY;
  if (scrollTop > 0) {
    $('.header').addClass('-filled');
  } else {
    if ($('body').hasClass('-scroll-lock') || $('.header').hasClass('-background-transition')) {
      return
    } else {
      $('.header').removeClass('-filled');
    }
  }
}

// open mobile menu 
$(window).on('load', function () {
  let header = $('.header');
  if (header.hasClass('-white-bg')) {
    header.attr('data-default-color-is-white', true);
  }
})

$(document).on('click', '.mobile-menu-opener', function () {
  let header = $(this).closest('.header');
  if ($('body').hasClass('-scroll-lock')) {
    unlockScroll();
  } else {
    lockScroll()
  }
  if (header.attr('data-default-color-is-white')) {
    header.toggleClass('-white-bg');
  }
  $(this).toggleClass('-active');
  header.toggleClass('-open');
})


let solutionSwiper = false;
let solutionPageSwiper = false;

function swipersInit() {
  // Slide on solution on mobile
  let swiperContainer = $(".tabs__items");
  if (swiperContainer.length && !isPc) {
    solutionSwiper = new Swiper(swiperContainer[0], {
      slidesPerView: 1,
      spaceBetween: 16,
      speed: 600,
      loop: true,
      navigation: false,
      autoHeight: true,
      pagination: {
        el: swiperContainer.find(".swiper-pagination")[0],
        type: "bullets",
        clickable: true,
      },
      breakpoints: {
        761: {
          slidesPerView: 2,
        },
      },
    });
  }

  // Slide on solution on mobile
  let solutionsPageSliderContainer = $(".solution__cards");
  if (solutionsPageSliderContainer.length && !isPc) {
    solutionPageSwiper = new Swiper(solutionsPageSliderContainer[0], {
      slidesPerView: 1,
      spaceBetween: 16,
      speed: 600,
      loop: true,
      navigation: false,
      autoHeight: true,
      pagination: {
        el: solutionsPageSliderContainer.find(".swiper-pagination")[0],
        type: "bullets",
        clickable: true,
      },
      breakpoints: {
        761: {
          slidesPerView: 2,
        },
      },
    });
  }

  // Slider for benefits on mobile version
  let benefitsSliderContainer = $(".slider-benefits__items");
  if (benefitsSliderContainer.length && !isPc) {
    solutionPageSwiper = new Swiper(benefitsSliderContainer[0], {
      slidesPerView: 1,
      spaceBetween: 16,
      speed: 600,
      loop: true,
      navigation: false,
      autoHeight: true,
      pagination: {
        el: benefitsSliderContainer.find(".swiper-pagination")[0],
        type: "bullets",
        clickable: true,
      },
      breakpoints: {
        761: {
          slidesPerView: 2,
        },
      },
    });
  }

  let postSliderContainer = $('.blog-slider__items');
  if (postSliderContainer.length) {
    postSliderSwiper = new Swiper(postSliderContainer[0], {
      slidesPerView: 1,
      spaceBetween: 16,
      speed: 600,
      loop: true,
      navigation: false,
      autoHeight: true,
      pagination: {
        el: postSliderContainer.find(".swiper-pagination")[0],
        type: "bullets",
        clickable: true,
      },
      navigation: {
        nextEl: postSliderContainer.closest('.container').find(".blog-slider__arrows .next")[0],
        prevEl: postSliderContainer.closest('.container').find(".blog-slider__arrows .prev")[0],
      },
      breakpoints: {
        761: {
          slidesPerView: 2,
        },
      },
    });
  }


  // Slider on solutions page main screen
  // let solutionsMainSliderContainer = $(".solution-main-slider");
  // if (solutionsMainSliderContainer.length) {
  //   solutionMainSwiper = new Swiper(solutionsMainSliderContainer[0], {
  //     slidesPerView: 1,
  //     speed: 900,
  //     loop: true,
  //     effect: 'fade',
  //     autoHeight: true,
  //     fadeEffect: {
  //       crossFade: true,
  //       type: 'fraction',
  //     },
  //     navigation: {
  //       nextEl: solutionsMainSliderContainer.closest('.container').find(".page-number__arrows .next")[0],
  //       prevEl: solutionsMainSliderContainer.closest('.container').find(".page-number__arrows .prev")[0],
  //     },
  //     on: {
  //       init: function (swiper) {
  //         let lastChildIndex = solutionsMainSliderContainer.find('.swiper-slide-duplicate').data('swiper-slide-index') + 1;
  //         if (lastChildIndex < 10) {
  //           lastChildIndex = '0' + lastChildIndex;
  //         }

  //         solutionsMainSliderContainer.closest('.container').find('.page-number__where .all-slides-count').html(lastChildIndex);
  //       }
  //     }
  //   });

  //   solutionMainSwiper.on('slideChangeTransitionStart', function (swiper) {
  //     let activeSlide = solutionsMainSliderContainer.find('.swiper-slide-active').data('swiper-slide-index') + 1;

  //     if (activeSlide < 10) {
  //       activeSlide = '0' + activeSlide;
  //     }

  //     solutionsMainSliderContainer.closest('.container').find('.page-number__where .activeSlideIndex').html(activeSlide);
  //   })
  // }

  // if (isPc) {

  //   if (solutionPageSwiper) {
  //     if (solutionPageSwiper.destroyed) {
  //       console.log('+');
  //     } else {
  //       solutionPageSwiper.destroy();
  //     }
  //   }

  // }
}

$(window).on('load resize', swipersInit);

// Check activate 

$(document).on('click', '.check__plus', function () {
  $(this).closest('.check').toggleClass('-active');
})

// Prepare text for show more on mobile
$(document).ready(function () {
  $('.need-text-accordion-on-mob').each(function () {
    let checker = $(this).find('p').length;
    if (checker < 2) return;

    let newHtml = '<div class="hidden-text"><div class="text-inner">';
    $(this).find('p').each(function (index) {
      if (index > 0) {
        let thisHtml = `<p>${$(this).html()}</p>`;
        newHtml += thisHtml;
        $(this).remove();
      }
    })
    newHtml += '</div></div>';
    $(this).append(newHtml);
  })
})


// Show more text on mobile

$(document).on('click', '.show-more-text', function () {
  let el = $(this);
  let previousText = el.text();
  let newText = el.data('change-text');

  el.text(newText);
  el.data('change-text', previousText);
  el.toggleClass('-active');

  let hiddenTextContainer = el.closest('.solution-blog__text').find('.hidden-text');
  if (!hiddenTextContainer.length) {
    console.log(hiddenTextContainer);
    hiddenTextContainer = el.closest('.text-between__inner').find('.hidden-text');
  }
  let hiddenTextInner = hiddenTextContainer.find('.text-inner');
  let accordionHeight = 0;

  if (el.hasClass('-active')) {
    accordionHeight = hiddenTextInner.innerHeight();
  }
  hiddenTextContainer.css('height', accordionHeight);
})

// Set default value to dropdowns

function defaultDropdownValue() {
  $('.--dropdown').each(function () {
    let firstPointText = $(this).find('.--dropdown__list-item:first-child').text().trim();
    $(this).find('input').val(firstPointText).change();
    $(this).find('.--dropdown__value').html(firstPointText);
  })
}

$(window).on('load', defaultDropdownValue);

$(document).on('click', '.anchor', function () {
  let section = $(this).closest('section');
  let sectionHeight = section.innerHeight();
  let headerHeight = $('.header').innerHeight();
  $('body, html').animate({
    scrollTop: sectionHeight - headerHeight
  }, 500)
})


$('.accept-cookie').click(function () {
  $(this).closest('.cookie-banner').addClass('-hide');
})

$('.modal-form-open').click(function () {
  lockScroll();
  $('.modal-form').addClass('-active');
})

$('.modal-form .close').click(function () {
  unlockScroll();
  $(this).closest('.modal-form.-active').removeClass('-active');
})

// Positions info buble

function checkPositionForInfoBubble() {
  $('.check__helper').each(function () {
    let helperVisible = $(this);
    let helperHiddenBubble = helperVisible.find('.check__helper-info');

    let windowInnerWidht = window.innerWidth;
    let rightOffset = helperHiddenBubble[0].getBoundingClientRect().right + 10;

    if (rightOffset > windowInnerWidht) {
      helperVisible.addClass('-bubble-left');
    } else {
      helperVisible.removeClass('-bubble-left');
    }

  })
}

$(window).on('load resize', checkPositionForInfoBubble);

$('.check__helper').click(function () {
  if (!isPc) {
    $(this).toggleClass('-active');
  }
})

customClickOutsideCallback('.check__helper.-active', function (e, selector) {
  $(selector).toggleClass('-active');
});