// Gallery modal
document.addEventListener('click', function(e) {
    var thumb = e.target.closest('.lw-gallery__thumb');
    if (thumb) {
        var modal = document.getElementById('lw-modal-gallery');
        var img = modal.querySelector('.lw-modal__img');
        img.src = thumb.dataset.full;
        modal.classList.add('lw-modal--open');
        document.body.style.overflow = 'hidden';
        return;
    }
    var close = e.target.closest('.lw-modal__close, .lw-modal__overlay');
    if (close) {
        var modal = document.getElementById('lw-modal-gallery');
        modal.classList.remove('lw-modal--open');
        document.body.style.overflow = '';
        return;
    }
});

document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') {
        var modal = document.getElementById('lw-modal-gallery');
        if (modal.classList.contains('lw-modal--open')) {
            modal.classList.remove('lw-modal--open');
            document.body.style.overflow = '';
        }
    }
});

const btnCloseModalPrograms = document.querySelector('.btn-close-programs');
const btnCloseModalMentors = document.querySelector('.btn-close-mentors');
const modalPrograms = document.querySelector('.modal-programs');
const modalMentors = document.querySelector('.modal-mentors');

const modalCampRequestForm = document.querySelector('.modal-camp-request-form');

btnCloseModalPrograms.addEventListener("click", function (event) {
 modalPrograms.classList.remove('open');
});
btnCloseModalMentors.addEventListener("click", function (event) {
 modalMentors.classList.remove('open');
});

document.addEventListener("click", function (event) {
 const target = event.target;

	if (target.classList.contains("btn-close-request-form-camp")) {
		document.body.style.overflow = "";
		modalCampRequestForm.classList.add("hidden");
		return;
	}

	const campBtn = target.closest('.btn-write-camp');
	if (campBtn) {
		if (campBtn.tagName === 'A') event.preventDefault();

		window.b24campLoaded = true;

		document.body.style.overflow = "hidden";
		modalCampRequestForm.classList.remove("hidden");

		var container = modalCampRequestForm.querySelector('.b24-form-container');
		if (!container.querySelector('.b24-form')) {
			var fForm = modalCampRequestForm.dataset.b24Form;
			var fLoader = modalCampRequestForm.dataset.b24Loader;
			if (fForm && fLoader) {
				var s = document.createElement('script');
				s.setAttribute('data-b24-form', fForm);
				s.setAttribute('data-skip-moving', 'true');
				s.text = "(function(w,d,u){var s=d.createElement('script');s.async=true;s.src=u+'?'+(Date.now()/180000|0);var h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h);})(window,document,'" + fLoader + "');";
				container.appendChild(s);
			}
		}
		return;
	}

 if (target.classList.contains("btn-program")) {
	const card = target.closest('.programs__card');
	modalPrograms.querySelector('.modal-programs__img').innerHTML = card.querySelector('.programs__image').outerHTML;
	modalPrograms.querySelector('.programs__item_title').textContent = card.querySelector('.programs__name').textContent;
	modalPrograms.querySelector('.programs__item_price').textContent = card.querySelector('.programs__price').textContent;
	modalPrograms.querySelector('.programs__content').innerHTML = card.querySelector('.programs_content').innerHTML;
	modalPrograms.classList.add('open');
	return;
 }
 if (target.classList.contains("btn-mentors")) {
	modalMentors.querySelector('.modal-programs__img').innerHTML = target.parentNode.querySelector('.mentors__item_img').outerHTML;
	modalMentors.querySelector('.modal-mentors_name').textContent = target.parentNode.querySelector('.mentors__item_name').textContent;
	modalMentors.querySelector('.modal-mentors_job').textContent = target.parentNode.querySelector('.mentors__item_text').textContent;
	modalMentors.querySelector('.mentors__about-col_text').textContent = target.parentNode.querySelector('.mentor_education').textContent;

	modalMentors.querySelector('.modal-mentors__directions').innerHTML = target.parentNode.querySelector('.HTMLdirection').innerHTML;
	modalMentors.querySelector('.mentors__experience-col_text').textContent = target.parentNode.querySelector('.mentor_experience').textContent;
	modalMentors.querySelector('.modal-additional__education_list').innerHTML = target.parentNode.querySelector('.mentor_add_experience').innerHTML;
	modalMentors.classList.add('open');
	return;
 }
});
document.querySelectorAll('.box__accordion_label').forEach(function(label) {
    label.addEventListener('keydown', function(e) {
        if (e.key === 'Enter' || e.key === ' ') {
            e.preventDefault();
            this.click();
        }
    });
});
