/home/bdqbpbxa/demo-subdomains/u-pod.goodface.com.ua/wp-content/themes/upod/assets/js/script.js
const windowWidth = $(window).outerWidth();



function getRandomNumber(min, max) {

  return Math.floor(Math.random() * (max - min) + min);

}





// Mask for phone input



if ($('.phone-mask').length) {

  $('.phone-mask').inputmask('mask', {

    showMaskOnHover: false,

    'mask': '+38 999 999 99 99'

  });

}





// Block body



const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;



function blockBody() {

  if ($('html').hasClass('no-scroll')) {



    let scrollTop = $('html').attr('data-scroll');



    $('html').removeClass('no-scroll');

    $('html').attr('style', '');



    $('html').css('right', 0 + 'px');

    $('.header').css('right', 0 + 'px');

    $('.preloader').css('padding-right', 0 + 'px');



    $(document).scrollTop(scrollTop);



  } else {

    let scrollTop = $(document).scrollTop();



    $('html').addClass('no-scroll');

    $('html').css({

      top: '-' + scrollTop + 'px'

    });



    $('html').css('right', scrollbarWidth + 'px');

    $('.header').css('right', scrollbarWidth + 'px');

    $('.preloader').css('padding-right', scrollbarWidth + 'px');



    $('html').attr('data-scroll', scrollTop);



  }

}





// Animation scroll



function animationLine() {

  if (!$('.line-around').length) return;
  
  let line = $('.line-around');

  line.each(function () {
    let path = $(this).find('svg path');
    let lineLength = path.get(0).getTotalLength();

    $(this).addClass('animation');
    $(this).attr('data-length', lineLength);

    path.attr('stroke-dasharray', `0, ${lineLength}`)
  });
};

$(document).ready(function() {
  animationLine();
})

// $(document).ready(function() {
//   $('.main-section .title').find('em').addClass('line-around').addClass('-bot')
//   .append('<svg width="363" height="90" viewBox="0 0 363 90" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M186 15C244.667 13.3334 361.4 19.2 359 56C356 102 39 88 10.0002 65C-14.3387 45.6966 31.0002 5 216 3" stroke="url(#paint0_linear)" stroke-width="6" stroke-linecap="round" stroke-dasharray="786.9127197265625, 786.9127197265625"></path> <defs> <linearGradient id="paint0_linear" x1="6.00001" y1="2.99997" x2="359" y2="88" gradientUnits="userSpaceOnUse"> <stop offset="0" stop-color="#F7186D"></stop> <stop offset="0.505208" stop-color="#FFBE41"></stop> <stop offset="1" stop-color="#D2F500"></stop> </linearGradient> </defs> </svg>');

//   $('.brands-section .title').find('em').addClass('line-around').addClass('-bot')
//   .append('<svg width="304" height="72" viewBox="0 0 304 72" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M175.92 69C115.416 67.1911 -3.59658 56.2264 4.38192 26.8388C14.355 -9.89579 279.078 -0.976292 299.586 31.848C307.076 43.8353 288.102 66.2548 161.957 56.0594" stroke="url(#paint1_linear)" stroke-width="6" stroke-linecap="round" stroke-dasharray="642.8526000976562, 642.8526000976562"></path> <defs> <linearGradient id="paint1_linear" x1="299.586" y1="69" x2="7.2821" y2="-10.7458" gradientUnits="userSpaceOnUse"> <stop offset="0" stop-color="#18CFF7"></stop> <stop offset="0.489583" stop-color="#5161E9"></stop> <stop offset="1" stop-color="#B500F5"></stop> </linearGradient> </defs> </svg>');

//   $('.benefits-section .title').find('em').addClass('line-around').addClass('-bot')
//   .append('<svg width="318" height="75" viewBox="0 0 318 75" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M131.56 3C268.747 5.63052 335.458 31.1662 307.87 51.4791C274.392 76.1284 25.5323 83.4623 4.45295 45.7192C-3.2451 31.9357 16.2563 6.15662 145.911 17.8797" stroke="url(#paint2_linear)" stroke-width="6" stroke-linecap="round" stroke-dasharray="673.008056640625, 673.008056640625"></path> <defs> <linearGradient id="paint2_linear" x1="3" y1="3" x2="288.993" y2="128.835" gradientUnits="userSpaceOnUse"> <stop offset="0" stop-color="#B8FF20"></stop> <stop offset="0.505208" stop-color="#17BCAF"></stop> <stop offset="1" stop-color="#165CE3"></stop> </linearGradient> </defs> </svg>');

//   $('.mission-section .title').find('em').addClass('line-around').addClass('-bot')
//   .append('<svg width="264" height="69" viewBox="0 0 264 69" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M135.839 52.7463C182.531 53.8991 261.91 53.502 260 28.0469C257.612 -3.77198 23.2196 -4.88109 6.53571 22.4784C-4.10459 39.9272 13.6292 59.376 159.715 65.0469" stroke="url(#paint3_linear)" stroke-width="6" stroke-linecap="round" stroke-dasharray="570.8710327148438, 570.8710327148438"></path> <defs> <linearGradient id="paint3_linear" x1="-7.42019" y1="63.0469" x2="268.627" y2="-13.4335" gradientUnits="userSpaceOnUse"> <stop offset="0" stop-color="#F7186D"></stop> <stop offset="0.505208" stop-color="#FFBE41"></stop> <stop offset="1" stop-color="#D2F500"></stop> </linearGradient> </defs> </svg>');

//   animationLine();
// });



function animation(scrollTop) {

  $('.animation').not('.animated').each(function () {
    let offsetTop = $(this).offset().top - 50;
    let windowHeight = window.innerHeight;

    if (($(this).hasClass('line-around')) && ((scrollTop + windowHeight) > offsetTop)) {

      $(this).addClass('animated');

      let path = $(this).find('path');
      let lineLength = $(this).attr('data-length');

      path.attr('stroke-dasharray', `${lineLength}, ${lineLength}`)

    } else if ((scrollTop + windowHeight) > offsetTop) {
      $(this).addClass('animated');
    }

  });



}



$(window).scroll(function () {

  var scrollTop = $(this).scrollTop();

  animation(scrollTop);

});





// Anchor scroll animation



$('.link-scroll').on("click", function (event) {

  event.preventDefault();



  if ($(this).closest('.mob-menu-block').length) {

    blockBody();

    $(this).closest('.mob-menu-block').removeClass('active');

    $('.header__menu-open').removeClass('active');

    $('.header').removeClass('light');

  }



  var id = $(this).attr('href');

  var top = $(id).offset().top - 72;



  $('body,html').animate({

    scrollTop: top

  }, 1000);

});





// Settings for header



let scrollNumber = 0;



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

  const header = $('.header-content');
  const headerContainer =  $('.header');

  let scroll = $(this).scrollTop();



  if (scroll <= 0 && $('.dark-bg').hasClass('active')) {

    header.removeClass('hide');

    return;

  }



  if (scroll <= scrollNumber || scroll <= 0) {

    header.removeClass('hide');
    headerContainer.removeClass('events-none')

  } else if (scroll > scrollNumber) {

    header.addClass('hide');
    headerContainer.addClass('events-none')

  }



  scrollNumber = scroll;

});





// Mobile menu



if (windowWidth < 1240) {

  let openMenuAnimation = false;

  let headerDropdownAnimation = false;



  $('.hamburger').on('click', function () {

    if (!openMenuAnimation) {

      openMenuAnimation = true;



      let container = $(this).closest('.header-content');

      let menu = container.find('.header__nav-block');

      let bg = $('.dark-bg');

      let allList = container.find('.header-dropdown__list');



      if ($(this).hasClass('active')) {

        allList.slideUp(400);

      }



      blockBody();

      $(this).toggleClass('active');

      bg.toggleClass('active');

      menu.slideToggle(400);



      setTimeout(function () {

        openMenuAnimation = false;

      }, 400);

    }

  });



  $('.header-dropdown').on('click', function () {

    if (!headerDropdownAnimation) {

      headerDropdownAnimation = true;



      let container = $(this).closest('.header__nav-list');

      let box = $(this).closest('.header-dropdown-box');

      let list = box.find('.header-dropdown__list');

      let allList = container.find('.header-dropdown__list');

      let allButton = container.find('.header-dropdown');



      if ($(this).hasClass('active')) {



        $(this).removeClass('active');

        list.slideUp(400);



      } else {



        allButton.removeClass('active');

        allList.slideUp(400);

        $(this).addClass('active');

        list.slideDown(400);



      }



      setTimeout(function () {

        headerDropdownAnimation = false;

      }, 400);

    }

  });

}



$('.dark-bg').on('click', function (e) {

  if ($(e.target).closest('.header').length) {

    return;

  }



  let container = $('.header-content');

  let menu = container.find('.header__nav-block');

  let allList = container.find('.header-dropdown__list');



  blockBody();

  menu.slideUp(400);

  allList.slideUp(400);

  $(this).removeClass('active');

  $('.hamburger').removeClass('active');

});


// Animation in main block

$(window).on('load', function () {
  if (!$('.main-section__animation-container').length) return;

  // if (windowWidth > 1240) {

  //   $('.main-section__animation-block').each(function () {
  //     let randomNumerDuration = getRandomNumber(50, 75);
  //     let randomNumberDelay = getRandomNumber(25, 50);
  
  //     $(this).find('.main-section__animation-list').css({
  //       animationDuration: `${randomNumerDuration}s`,
  //       animationDelay: `-${randomNumberDelay}s`,
  //     })
  //   });

  // }

  if (windowWidth <= 1240) {
    let animationList = $('.main-section__animation-list');
    let animationBox = animationList.eq(0).find('.main-section__animation-box');
    let numberOfAnimationBox = animationBox.length;
    let cof = 4;

    animationList.css('animation-duration', `${numberOfAnimationBox * cof}s`);
    
  }
  
});





// Animation logo bottom



$(window).on('load', function () {

  if (!$('.scroll-block').length) return;



  let scrollBlock = $('.scroll-block');



  scrollBlock.each(function () {

    let box = $(this).find('.scroll-box');

    let container = box.closest('.scroll-container');

    let boxWidth = box.outerWidth();

    let numberOfBoxForWindow = Math.ceil(windowWidth / boxWidth) * 2;



    for (i = 1; numberOfBoxForWindow > i; i++) {

      let boxHtml = box.clone();

      container.append(boxHtml);

    }



    let currentBox = $(this).find('.scroll-box');



    currentBox.each(function () {

      $(this).css({

        animationPlayState: 'running'

      })

    });

  });

});





// Validation

document.addEventListener( 'wpcf7submit', function( event ) {
  theTarget = $(event.target);
  let name = theTarget.find('.name');
  let phone = theTarget.find('.phone');
  let email = theTarget.find('.email');
  let checkbox = $(this).find('.checkbox input');

  let isValid = true;

  theTarget.find('.input-box').removeClass('error');
  theTarget.find('.input-box').removeClass('valid');
  checkbox.closest('.checkbox').removeClass('error');

  function addError(inputName) {
    inputName.closest('.input-box').addClass('error');
  }

  if (name.length) {
    if (name.val().length < 1) {
      isValid = false;
      addError(name);
    } else {
      name.closest('.input-box').addClass('valid');
    }
  }

  if (phone.length) {
    if (phone.inputmask("isComplete")) {
      phone.closest('.input-box').addClass('valid');
    } else {
      isValid = false;
      addError(phone);
    }
  }

  if (email.length) {
    let emailVal = email.val();
    if (emailVal.includes('@gmail.com') || emailVal.includes('@email.com') || emailVal.includes('@email.ru')) {
      email.closest('.input-box').addClass('valid');
    } else {
      isValid = false;
      addError(email);
    }
  }

  if (checkbox.length) {
    if (!checkbox.is(':checked')) {
      isValid = false;
      checkbox.closest('.checkbox').addClass('error')
    }
  }

  if (isValid) {
    let email = theTarget.find('.email').val();
    if (theTarget.closest('.thank-you-block').length) {
      let container = theTarget.closest('.thank-you-block');
      let thanksWindow = container.find('.thank-you-window');
      let form = container.find('.thank-you-form');
      let txt = thanksWindow.find('.text').html().replace('[email]', email);
      thanksWindow.find('.text').html(txt);
      thanksWindow.addClass('active');
      form.addClass('hide');
    }

    if (theTarget.closest('.form-modal').length) {
      let container = theTarget.closest('.modal-block');
      let thanksWindow = container.find('.thank-you-modal');
      let modal = container.find('.modal');
      let txt = thanksWindow.find('.text').html().replace('[email]', email);
      thanksWindow.find('.text').html(txt);

      thanksWindow.addClass('active');
      modal.addClass('hide');
    }
  }
}, false);

document.addEventListener( 'wpcf7mailsent', function( event ) {
  $('.error').removeClass('error');
  $('.valid').removeClass('valid');
}, false);

function removeError(ths) {
  ths.removeClass('error');
}

$('.input-box').on('mouseenter', function () {
  removeError($(this));
});

$('.checkbox').on('mouseenter', function () {
  removeError($(this));
});

$('.input-box input').on('input', function () {
  removeError($(this).closest('.input-box'));
  $(this).closest('.input-box').removeClass('valid');
});





// Settings for modal window



let modalAnimate = false;



function endedModalAnimation() {

  setTimeout(function () {

    modalAnimate = false;

  }, 400)

}



function hideTastes(modal) {

  let list = modal.find('.tastes-list');



  if (!list.find('.number-hide-tastes').length) {

    setTimeout(function () {

      tastesListSettings(list);

    }, 400)

  }

}



$('.modal-wrapper').click(function (e) {

  if ($(this).hasClass('start-modal')) return;



  if (!modalAnimate) {

    if ($(e.target).closest('.modal, .thank-you-modal').length) {
      return;
    }
    modalAnimate = true;

    blockBody();
    $(this).removeClass('active');

    if ($(this).closest('.product-modal').length || $(this).hasClass('.product-modal')) {
      hideTastes($(this));
    }



    endedModalAnimation();

  }

});



$('.open-modal-window').on('click', function () {
  if (!modalAnimate) {
    modalAnimate = true;

    blockBody();
    $('.form-modal').addClass('active');

    endedModalAnimation();

  }

});


// opem choosen product in modal
$('.open-product-modal').on('click', function () {
  var index = $(this).index();
  if (!modalAnimate) {
    modalAnimate = true;

    blockBody();

    var productModal = $('.product-modal');
    var object_item = product_obj[index];

    productModal.find('.tastes-list').html(' ');
    productModal.find('.data-list').html(' ');
    productModal.find('.modal-product-info .attention').remove();
    productModal.find('.modal-product-info-block .img').removeClass('-fill');

      productModal.find('.modal-product-info-block .img img').attr('src', object_item['img']).attr('alt', object_item['alt']);

      if (object_item['attention_condition'] == 1) {
        productModal.find('.modal-product-info').prepend('<div class="attention"><p class="text text-3">' + object_item['attention_text'] + '</p></div>');
      }

      if (object_item['count_of_puff'].length > 0) {
        productModal.find('.data-list').append('<li><p class="data-name">' + object_item['count_of_puff_name'] + '</p><p class="data">' + object_item['count_of_puff'] + '</p> </li>')
      }

      if (object_item['count_of_nikotin'].length > 0) {
        productModal.find('.data-list').append('<li><p class="data-name">' + object_item['count_of_nikotin_name'] + '</p><p class="data">' + object_item['count_of_nikotin'] + '</p> </li>')
      }

      if (object_item['size'].length > 0) {
        productModal.find('.data-list').append('<li><p class="data-name">' + object_item['size_name'] + '</p><p class="data">' + object_item['size'] + '</p> </li>')
      }

      if (object_item['weight'].length > 0) {
        productModal.find('.data-list').append('<li><p class="data-name">' + object_item['weight_name'] + '</p><p class="data">' + object_item['weight'] + '</p> </li>')
      }

      if (object_item['capacity'].length > 0) {
        productModal.find('.data-list').append('<li><p class="data-name">' + object_item['capacity_name'] + '</p><p class="data">' + object_item['capacity'] + '</p> </li>')
      }
      
      if (object_item['filled_img'] == 'true') {
        productModal.find('.modal-product-info-block .img').addClass('-fill');
      }
      
      var taste_li_list = '';

      object_item['taste_list'].forEach(function(item, index, array) {
        taste_li_list += '<li>' + item + '</li>'
      })

      productModal.find('.tastes-list').attr('data-lang', object_item['lang'])
      .attr('data-one', object_item['data_one'])
      .attr('data-two', object_item['data_two'])
      .attr('data-more', object_item['data_more'])
      .prepend(taste_li_list);

      productModal.find('.name').html(object_item['name']);
      productModal.find('.price').html(object_item['price']);
    

    tastesListSettings(productModal.find('.tastes-list'));
    productModal.addClass('active');
    endedModalAnimation();
  }

});


$('.oldr').click(function() {
  if (localStorage.getItem('yearsOld') == null) {
    localStorage.setItem('yearsOld', 'old');
  }
})


$('.close-modal').on('click', function () {
  if (!modalAnimate) {
    modalAnimate = true;

    blockBody();
    $(this).closest('.modal-wrapper').removeClass('active');

    let productModal = $(this).closest('.product-modal');

    if (productModal.length) {
      hideTastes(productModal); 
    }
    endedModalAnimation();
  }
});

$(document).ready(function() {
  if (localStorage.getItem('yearsOld')) {
    if (localStorage.getItem('yearsOld') == 'old') {
      $('.oldr').click();
    }
  }
})



$('.open-message').on('click', function () {

  $(this).closest('.modal').addClass('hide');

  $(this).closest('.modal-block').find('.modal-message').addClass('active');

});





// Modal of product -> Tastes list

let textForHiddenTastes;

function tastesListLangSettings(ths, number) {
  let currentList = ths;
  let lang = currentList.attr('data-lang');
  let lastNumber = number.toString().split('').pop();



  if (lang === 'ru' || lang === 'uk') {
    let textForOne = currentList.attr('data-one');
    let textForTwo = currentList.attr('data-two');
    let textForMore = currentList.attr('data-more');

    if (number != 11 && lastNumber == 1) {
      textForHiddenTastes = textForOne;
    } else if (number != 12 && number != 13 && number != 14 && (lastNumber == 2 || lastNumber == 3 || lastNumber == 4)) {
      textForHiddenTastes = textForTwo;
    } else {
      textForHiddenTastes = textForMore;
    }

  } else if (lang === 'en') {
    let textForOne = currentList.attr('data-one');
    let textForMore = currentList.attr('data-more');
    if (number == 1) {
      textForHiddenTastes = textForOne;
    } else {
      textForHiddenTastes = textForMore;
    }

  }
}



function tastesListSettings(ths) {
  let li = ths.find('li');
  let liLength = li.length;


  if (liLength > 14) {
    for (i = 14; i <= liLength; i++) {
      li.eq(i).fadeOut(0);
    }


    let hiddenTastesNumber = liLength - 14;
    tastesListLangSettings(ths, hiddenTastesNumber);
    let tagForHiddenTastes = `<li class="number-hide-tastes">+${hiddenTastesNumber} ${textForHiddenTastes}</li>`;
    $(ths).append(tagForHiddenTastes);

  }

}



$(window).ready(function () {

  let list = $('.tastes-list');



  if (list) {
    list.each(function () {
      tastesListSettings($(this));
    });
  }
});



$(document).on('click', '.number-hide-tastes', function () {

  let list = $(this).closest('.tastes-list');

  let li = list.find('li');



  $(this).animate({

    opacity: '0'

  }, 10, function () {

    $(this).remove();

  });



  li.fadeIn(0);

});





// Dropdown and filters for map



function checkBlockScroll() {

  let el = $('.shops-list__content');

  let checkEl = el[0];



  if (checkEl.scrollHeight != checkEl.clientHeight) {

    el.removeClass('no-scroll');

  } else {

    el.addClass('no-scroll');

  }

}



function checkListItem(ths, dataAttr, list) {

  let container = ths.closest('.map-settings');

  let thisItemAttr = ths.attr(dataAttr);

  let checkBlock = container.find(list);

  let checkList = checkBlock.find('.dropdown-list');

  let item = checkList.find('li');



  if (ths.hasClass('check-all')) {

    item.fadeIn();

    return;

  }



  item.not('.check-all').each(function () {

    let attr = $(this).attr(dataAttr);



    if (attr.indexOf(thisItemAttr) == -1) {

      $(this).fadeOut();

    } else {

      $(this).fadeIn();

    }

  });

}



function checkShops(ths) {

  let container = ths.closest('.map-settings');



  let cityList = container.find('.map-setting.city');

  let cityItemActive = cityList.find('.dropdown-list li.active');

  let cityItemActiveAttr = cityItemActive.attr('data-city');



  let brandList = container.find('.map-setting.brand');

  let brandItemActive = brandList.find('.dropdown-list li.active');

  let brandItemActiveAttr = brandItemActive.attr('data-brand');



  let shopsList = container.find('.shops-list');

  let shopsListItem = shopsList.find('li');



  shopsListItem.removeClass('active');



  shopsListItem.each(function () {

    let shopCity = $(this).attr('data-city');

    let shopBrand = $(this).attr('data-brand');



    if (cityItemActiveAttr && brandItemActiveAttr) {

      if (shopCity.indexOf(cityItemActiveAttr) != -1 && shopBrand.indexOf(brandItemActiveAttr) != -1) {

        $(this).show();

      } else {

        $(this).hide();

      }

    }



    if (!cityItemActiveAttr && brandItemActiveAttr) {

      if (shopBrand.indexOf(brandItemActiveAttr) != -1) {

        $(this).show();

      } else {

        $(this).hide();

      }

    }



    if (cityItemActiveAttr && !brandItemActiveAttr) {

      if (shopCity.indexOf(cityItemActiveAttr) != -1) {

        $(this).show();

      } else {

        $(this).hide();

      }

    }



    if (!cityItemActiveAttr && !brandItemActiveAttr) {

      $(this).show();

    }

  });



  $(window).trigger('shops-filter');



  checkBlockScroll();

}



$('.map-setting.dropdown').find('.dropdown-button').on('click', function () {

  let dropdown = $(this).closest('.dropdown');

  let block = $(this).closest('.map-settings');

  let allDropdown = block.find('.dropdown');

  let mapSetting = block.find('.map-setting');



  if (dropdown.hasClass('active')) {

    mapSetting.removeClass('opacity')

    dropdown.removeClass('active');

    return;

  }



  mapSetting.removeClass('opacity');

  allDropdown.removeClass('active');



  dropdown.addClass('active');

  mapSetting.not('.active').addClass('opacity');

});



$('.map-setting.dropdown').find('.dropdown-list li').on('click', function () {

  let text = $(this).text().split();

  let container = $(this).closest('.map-settings');

  let block = $(this).closest('.dropdown');

  let textBox = block.find('.dropdown-button p');

  let li = block.find('.dropdown-list li');



  li.removeClass('active');

  $(this).addClass('active');

  block.removeClass('active');

  container.find('.map-setting').removeClass('opacity');

  textBox.text(text);



  // Filter for map



  if (block.hasClass('city')) {

    let ths = $(this);

    let dataAttr = 'data-city';

    let list = '.map-setting.brand';



    checkListItem(ths, dataAttr, list);

    checkShops(ths);

  }



  if (block.hasClass('brand')) {

    let ths = $(this);

    let dataAttr = 'data-brand';

    let list = '.map-setting.city';



    checkListItem(ths, dataAttr, list);

    checkShops(ths);

  }

});



$(document).click(function (e) {

  if ($(e.target).closest('.dropdown').length) {

    return;

  }



  $('.dropdown').removeClass('active');

  $('.map-setting').removeClass('opacity');

});





// Tabs in politic page



$(window).on('load resize', function () {

  if (!$('.tabs-container').length) return;



  $('.tabs-container').each(function () {

    const tabBox = $(this).find('.tabs'),
      tabBoxOffsetLeft = tabBox.offset().left,
      tab = tabBox.find('.tab'),
      activeTab = tabBox.find('.tab.active'),
      line = $(this).find('.tabs-line');

    tabBox.attr('data-offsetLeft', tabBoxOffsetLeft);
    const tabBoxAttr = tabBox.attr('data-offsetLeft');



    tab.each(function () {
      let tabOffset = $(this).offset().left;
      let tabWidth = $(this).outerWidth();

      $(this).attr('data-transform', tabOffset - tabBoxAttr);
      $(this).attr('data-width', tabWidth);

      $(this).css({
        transitionDuration: '0.4s'
      })

    });

    let activeTabWidth = activeTab.attr('data-width');
    line.width(activeTabWidth);

  });

});



$('.tab').on('click', function () {

  const container = $(this).closest('.tabs-container'),
    item = container.find('.item'),
    tab = container.find('.tab'),
    line = container.find('.tabs-line');

  let index = $(this).index();

  // добавляю в посилання id таба
  var hash = "#" + $(this).attr('id');
  history.pushState(null, "", "#" + $(this).attr('id'));

  if ($(this).closest('.all-ebooks-catalog').length || $(this).closest('.blog-posts-section').length) {
    tab.removeClass('active');
    tab.eq(index).addClass('active');
  } else {
    tab.removeClass('active');
    item.removeClass('active');


    tab.eq(index).addClass('active');
    item.eq(index).addClass('active');
  }



  let activeTab = container.find('.tab.active');
  let activeTabWidth = activeTab.attr('data-width');
  let activeTabDataTransform = activeTab.attr('data-transform');



  line.css({
    'transform': `translate3d(${activeTabDataTransform}px, 0, 0)`,
    'width': `${activeTabWidth}px`
  });

});



$(window).on('load', function () {

  if (!$('.tabs-container').length) return;

  let id = window.location.hash;

  if (id) {
    $(this).scrollTop(0);
    $(`${id}`).click();
  }

});





// "Show more"



$('.open-accordion').on('click', function () {

  let block = $(this).closest('.accordion-block');

  let accordion = block.find('.hide-accordion');



  accordion.addClass('flex');

  accordion.slideDown(400);

  $(this).slideUp(300);

});





// Slider

$(window).on('load', function() {

  if (!$('.swiper-wrapper').length) return;

  const responseSwiper = new Swiper('.response-slider', {
    loop: true,
    speed: 500,
    slidesPerView: "auto",
    slidesPerGroup: 1,
    spaceBetween: 0,
    centeredSlides: true,
    
    breakpoints: {
      0: {
        navigation: false,
        pagination: {
          el: '.swiper-pagination',
          type: 'bullets',
          clickable: true,
        },
      },

      1241: {
        pagination: false,
        navigation: {
          nextEl: '.next-response',
          prevEl: '.prev-response',
        },

      }

    }

  });

});





// Play video



$('.video-box').on('click', function () {

  if (!$(this).hasClass('active')) {



    let videoSrc = $(this).attr('data-src');

    let html = $(this).html();



    $(this).html(`

    <iframe width="560" height="315" src="${videoSrc}?rel=0&autoplay=1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

  `)



    $(this).addClass('active');



  }

});





// Lazyload



$(window).ready(function () {
  let top = $(window).scrollTop(),
    height = $(window).outerHeight(),
    windowViewport = top + height,
    lazyload = $('.lazyload');

    $('.main-section__animation-box .lazyload').each(function() {  
      $(this).removeAttr('srcset');
      $(this).removeClass('lazyload');
    })



  lazyload.each(function () {
    let offsetTop = $(this).offset().top;


    if (offsetTop <= windowViewport) {
      $(this).removeAttr('srcset');
      $(this).removeClass('lazyload');
    }
  });

});



$(window).scroll(function () {

  if (!$('.lazyload').length) return;



  let scroll = $(this).scrollTop();

  let lazyload = $('.lazyload');



  if (scroll > 0) {

    lazyload.each(function () {

      $(this).removeAttr('srcset');

      $(this).removeClass('lazyload');

    });

  }

});





// General settings



let preloader = $('.preloader');
let startModal = $('.start-modal');


function removePreloader() {
  if (!startModal.hasClass('active') && $('html').hasClass('no-scroll')) {
    blockBody();
  }

  preloader.animate({
    opacity: '0'
  }, 1000, function () {
    $(this).remove();
  });

}



$(window).ready(function () {
  if (startModal.hasClass('active') || preloader.hasClass('active')) {
    blockBody();
  }

});



$(window).on('load', function () {
  $(window).scroll();



  $('.modal-wrapper').removeClass('hide');

  if (preloader.hasClass('active')) {
    preloader.addClass('animate');

    setTimeout(function () {
      removePreloader();
    }, 1000);

  }

});



$(window).on('resize load', () => {
  if ($('#map').length) {
    checkBlockScroll();
  }
});


$('.checkbox').on('click', function() {
  var input = $(this).find('input');
  if(input.prop( "checked") == true) {
    input.prop( "checked", false );
  } else {
    input.prop( "checked", true );
  }
  $(this).find('label').toggleClass('active');

})


function isElementOnViewport(element) {

  var scrollTop = $(window).scrollTop();
  var scrollBottom = scrollTop + window.innerHeight;

  var elementOffsetTop = element.offset().top;
  var elementOffsetBottom = elementOffsetTop + element.height();

  if (scrollBottom > elementOffsetTop && scrollTop < elementOffsetBottom) {
    return true;
  } else {
    return false;
  }

}

function checkOnViewport() {
  $('.check-on-viewport').each(function () {

    var element = $(this);

    if (isElementOnViewport(element)) {
      if (element.hasClass('not-on-viewport')) {
        element.removeClass('not-on-viewport');
      }
    } else {
      if (element.hasClass('not-on-viewport') == false) {
        element.addClass('not-on-viewport');
      }
    }

  });
}

$(window).on('scroll', checkOnViewport);

$(window).on('load', function() {
  if ($('.parallax').length) {
    $('.parallax').enllax();
  }
})

$(document).ready(function() {
  const isMac = navigator.platform.toLowerCase().indexOf('mac') != -1;
  const isIpad = navigator.platform.toLowerCase().indexOf('ipad') != -1;
  const isIphone = navigator.platform.toLowerCase().indexOf('iphone') != -1;

  if (isMac || isIpad || isIphone) {
    $('body').addClass('apple');
  }
})