$(function () {
  $('[type="tel"]').mask('+375 (XX) XXX-XX-XX');
})
const imageBg = new IntersectionObserver((entries, imgObserver) => {
  entries.forEach((entry) => {
    if (entry.isIntersecting) {
      const lazyBackground = entry.target
      lazyBackground.style.backgroundImage = `url(${lazyBackground.dataset.image})`
      lazyBackground.classList.remove('lzy_bg')
      imgObserver.unobserve(lazyBackground)
    }
  })
})
const arrBg = document.querySelectorAll('.lzy_bg')
arrBg.forEach((v) => {
  imageBg.observe(v)
})

const imageBlocBgkObserver = new IntersectionObserver(
  (entries, imgObserver) => {
    entries.forEach((entry) => {
      if (entry.isIntersecting) {
        const lazyContainer = entry.target
        const lazyBlockBg = lazyContainer.querySelectorAll('.lzy_bl')
        lazyBlockBg.forEach((elem) => {
          elem.style.backgroundImage = `url(${elem.dataset.image})`
          elem.classList.remove('lzy_bl')
        })
        lazyContainer.classList.remove('lzy_bg_container')
        imgObserver.unobserve(lazyContainer)
      }
    })
  },
)
const lzyBgContainer = document.querySelectorAll('.lzy_bg_container')
lzyBgContainer.forEach((v) => {
  imageBlocBgkObserver.observe(v)
})
// Скролл по клику
function currentYPosition() {
  // Firefox, Chrome, Opera, Safari
  if (self.pageYOffset) return self.pageYOffset
  // Internet Explorer 6 - standards mode
  if (document.documentElement && document.documentElement.scrollTop)
    return document.documentElement.scrollTop
  // Internet Explorer 6, 7 and 8init_pointer
  if (document.body.scrollTop) return document.body.scrollTop
  return 0
}

function elmYPosition(eID) {
  let elm = document.querySelector(eID)
  let y = elm.offsetTop
  let node = elm
  while (node.offsetParent && node.offsetParent != document.body) {
    node = node.offsetParent
    y += node.offsetTop
  }
  return y
}

function smoothScroll(eID) {
  let startY = currentYPosition()
  let stopY = elmYPosition(eID) - 50
  let distance = stopY > startY ? stopY - startY : startY - stopY
  if (distance < 100) {
    scrollTo(0, stopY)
    return
  }
  let speed = Math.round(distance / 100)
  if (speed >= 20) speed = 20
  let step = Math.round(distance / 30)
  let leapY = stopY > startY ? startY + step : startY - step
  let timer = 0
  if (stopY > startY) {
    for (let i = startY; i < stopY; i += step) {
      setTimeout('window.scrollTo(0, ' + leapY + ')', timer * speed)
      leapY += step
      if (leapY > stopY) leapY = stopY
      timer++
    }
    return
  }
  for (let i = startY; i > stopY; i -= step) {
    setTimeout('window.scrollTo(0, ' + leapY + ')', timer * speed)
    leapY -= step
    if (leapY < stopY) leapY = stopY
    timer++
  }
}
document.querySelectorAll('.scroll_to, .scroll_up').forEach((anchor) => {
  anchor.addEventListener('click', function (e) {
    e.preventDefault()
    console.log(e.target)
    if (e.target.getAttribute('href') != null) {
      if (document.querySelector(e.target.getAttribute('href'))) {
        smoothScroll(e.target.getAttribute('href'))
        overlayMenu.classList.remove('open_menu')
        overlayMenu.classList.add('close__burger')
      } else {
        // console.log(this.getAttribute('href'));
        window.location = `/${e.target.getAttribute('href')}`
      }
    }
  })
})

//

// const headSection = document.querySelector('.head__section')
// const headerFixed = document.querySelector('.sticky__header')
// const arrowFixed = document.querySelector('.scroll_up')
// // const aboutLink = document.querySelector('.about__link');
// // const mainHeadSection = document.querySelector(".head_section_main ");
// const htmlTag = document.querySelector('html')
// const windowHeight = window.screen.height / 3
// console.log(windowHeight)
// window.addEventListener('scroll', function () {
// if (htmlTag.getAttribute('data-scroll-dir-y') > 0) {
//   headerFixed.classList.add('scroll__down')
//   headerFixed.classList.remove('scroll__up')
// } else {
//   headerFixed.classList.remove('scroll__down')
//   headerFixed.classList.add('scroll__up')
// }

//   window.pageYOffset > 150
//     ? headerFixed.classList.add('fixed_header')
//     : headerFixed.classList.remove('fixed_header')
// })
// if (window.pageYOffset > windowHeight) headerFixed.classList.add('fixed_header')
// if (window.pageYOffset > 850) arrowFixed.classList.add('visible_arrow');

const btnBurger = document.querySelectorAll('.burger ')
const overlayMenu = document.querySelector('.header__menu')

btnBurger.forEach((elem) => {
  elem.onclick = () => {
    overlayMenu.classList.toggle('open')
    elem.classList.toggle('active')
  }
})


//////////////////////////
// PlaceHolder
//////////////////////////

const formWrap = document.querySelectorAll('.input__group')
formWrap.forEach((elem) => {
  const formInput = elem.querySelector('.order__input')
  const formLabel = elem.querySelector('.place_span')
  if (formInput) {
    formInput.addEventListener('focus', () => {
      formLabel.classList.add('fixed_span')
    })
    formInput.addEventListener('focusout', (e) => {
      console.log(e.target.value)
      if (e.target.value === '') {
        formLabel.classList.remove('fixed_span')
      }
    })
  }
})


// Share 
const shareBtn = document.querySelector('.share__btn')
const shareBlock = document.querySelector('.share__block')
if (shareBtn) {
  shareBtn.onclick = () => {
    shareBlock.classList.toggle('show')
  }
  document.addEventListener('click', function ({ target }) {
    if (!target.classList.contains('share__block') && !target.closest('.share__block') && !target.classList.contains('share__btn') && shareBlock.classList.contains('show')) {
      shareBlock.classList.remove('show')
    }
  })
}

// Copy
let copyText = window.location.href;
const linkBtn = document.querySelectorAll('.share__copy');
linkBtn.forEach((elem) => {
  elem.onclick = (e) => {    
    console.log(copyText);
    copyInfo(copyText);
  };
});

function copyInfo(copy) {
  console.log(copy);
  console.log(navigator.clipboard);
  navigator.clipboard.writeText(copy)
    .then(() => {
      navigator.clipboard
        .readText()
        .then((text) => {
          alert('Copyed ' + text);
        })
        .catch((err) => {
          console.log('Something went wrong', err);
        });
    })
    .catch((err) => {
      console.log('Something went wrong', err);
    });
}

const link = document.querySelectorAll('.sfsi_widget .sfsi_wDiv .sfsi_wicons .inerCnt a.sficn')
const linkImg = document.querySelectorAll('.sfsi_widget .sfsi_wDiv .sfsi_wicons .inerCnt a.sficn img')

link.forEach((elem, idx) => {
  const textLabel = document.createElement('span')
  textLabel.innerHTML = linkImg[idx].title
  elem.append(textLabel)
})

var wpcf7Elm = document.querySelector( '.wpcf7' );
if(wpcf7Elm){
	// Мониторим событие wpcf7submit у выбранного блока
wpcf7Elm.addEventListener( 'wpcf7submit', function( event ) {
     dataLayer.push( {
		  event: "application.submit",
		  details: {
			category: "application",
			action: "send",
		  }
		})
}, false );
}


