jQuery(function ($) {
	$('.show-more-colors').on('click', function (e) {
		e.preventDefault()

		var $parent = $(this).closest('.product-gallery-colors')
		var $productColors = $parent.find(
			'.product-colors .elementor-image-gallery .gallery'
		)

		var isActive = $productColors.hasClass('active')

		if (isActive) {
			$productColors.animate({ maxHeight: '128px' }, 300)
			$productColors.removeClass('active')
		} else {
			var scrollHeight = $productColors[0].scrollHeight
			$productColors.animate({ maxHeight: scrollHeight + 'px' }, 300)
			$productColors.addClass('active')
		}
	})
})

jQuery(function ($) {
	$('.show-more-drafts').on('click', function (e) {
		e.preventDefault()

		var parent = $(this).closest('.product-gallery-drafts')
		var productColors = parent.find(
			'.product-drafts .elementor-image-gallery .gallery'
		)

		var isActive = productColors.hasClass('active')

		if (isActive) {
			productColors.animate({ maxHeight: '210px' }, 300)
			productColors.removeClass('active')
		} else {
			var scrollHeight = productColors[0].scrollHeight
			productColors.animate({ maxHeight: scrollHeight + 'px' }, 300)
			productColors.addClass('active')
		}
	})
})

jQuery(function ($) {
	$('.show-more-tabs').on('click', function (e) {
		e.preventDefault()

		var tabsList = $(this).siblings('ul')

		var isActive = tabsList.hasClass('active')

		if (isActive) {
			tabsList.animate({ maxHeight: '140px' }, 300)
			tabsList.removeClass('active')
		} else {
			var scrollHeight = tabsList[0].scrollHeight
			tabsList.animate({ maxHeight: scrollHeight + 'px' }, 300)
			tabsList.addClass('active')
		}
	})
})


jQuery(function ($) {
	$('.show-more-gallery').on('click', function (e) {
		e.preventDefault()

		var parent = $(this).closest('.product-gallery-drafts')
		console.log(parent);
		var productColors = parent.find(
			'.product-drafts .elementor-image-gallery .gallery'
		)

		var isActive = productColors.hasClass('active')

		if (isActive) {
			productColors.animate({ maxHeight: '1030px' }, 300)
			productColors.removeClass('active')
		} else {
			var scrollHeight = productColors[0].scrollHeight
			productColors.animate({ maxHeight: scrollHeight + 'px' }, 300)
			productColors.addClass('active')
		}
	})
})

jQuery(function ($) {
	if ($('#contact-form').length) {
		let selects = $('#contact-form select')

		selects.each(function () {
			$('option:first', this).attr('disabled', 'disabled')
		})
	}
})

jQuery(function ($) {
    $('#contact-form select').each(function () {
        var $this = $(this),
            numberOfOptions = $(this).children('option').length,
            defaultOptionText = $this.attr('data-placeholder') || $this.children('option').eq(0).text(); // Отримуємо текст placeholder або використовуємо перший елемент за замовчуванням

        $this.addClass('select-hidden');
        $this.wrap('<div class="select"></div>');
        $this.after('<div class="select-custom-text">' + defaultOptionText + '</div>');

        var $styledSelect = $this.next('div.select-custom-text');
        var $list = $('<ul />', {
            class: 'select-custom-options',
        }).insertAfter($styledSelect);

        for (var i = 1; i < numberOfOptions; i++) {
            $('<li />', {
                text: $this.children('option').eq(i).text(),
                rel: $this.children('option').eq(i).val(),
            }).appendTo($list);
        }

        var $listItems = $list.children('li');

        $styledSelect.click(function (e) {
            e.stopPropagation();
            $('div.select-custom-text.active').not(this).each(function () {
                $(this).removeClass('active').next('ul.select-custom-options').slideUp();
            });
            $(this).toggleClass('active').next('ul.select-custom-options').slideToggle();
        });

        $listItems.click(function (e) {
            e.stopPropagation();
            $styledSelect.text($(this).text()).removeClass('active');
            $styledSelect.text($(this).text()).addClass('selected');
            $this.val($(this).attr('rel'));
            $list.slideUp();

        });

        $(document).click(function () {
            $styledSelect.removeClass('active');
            $list.slideUp();
        });
        $list.hide();
    });
});


jQuery(function ($) {
	$('#hp-acc-more-box').slideUp()

	$('#hp-acc-more-btn').on('click', function(e) {
		e.preventDefault()
		$('#hp-acc-more-box').slideToggle()
	})
})

jQuery(function ($) {
    var hash = window.location.hash;
    if (hash) {
        var hashValue = hash.substring(1).toLowerCase();
        var tabItem = $(`.eael-tabs-filter-types ul li input[filter="${hashValue}"]`);
		tabItem.trigger('click')

		var visibleItems = $(".eael-tabs-nav ul li:not([style*='display: none;'])");
		if (visibleItems.length > 0) {
			var firstVisibleItem = visibleItems.first()[0];
			setTimeout(function(){
				console.log(firstVisibleItem);
				firstVisibleItem.click();
			}, 500)
		}
    }
});

jQuery(document).ready(function($){
	$('a[phone]').on('click', function(e){
		e.preventDefault();
		var $this = $(this);
		var phone = $this.attr('phone');

		$.ajax({
			url: '/query-contacts.php',
			type: 'GET',
			data: { phone: phone },
			success: function(response) {
				if (response.phone) {
					var value = response.phone;
					$this.attr('href', 'tel:' + value);
					$this.find('span.elementor-icon-list-text').text(value);
				} else {
					console.log('Phone number not found in the response.');
				}
			},
			error: function() {
				console.log('Error with the AJAX request.');
			}
		});
	});
});