$('img').on('contextmenu', function(e) {
  e.preventDefault();
});
$('canvas').on('contextmenu', function(e) {
  e.preventDefault();
});

$(function () {
	if($( window ).width() <= 991){
		$("#advance_menu ul.lvl_1 > li.parent > a").click(function(e) {
			e.preventDefault();
		})

		$("#advance_menu ul.lvl_3 > li > a").click(function(e) {
			location.reload();
		})
	}

	if($("#product-preview-box").length && $("#nav_tabs").length){
		$("#advance_menu ul.lvl_3 > li > a").on("click", function() {
			if($(`#nav_tabs li a[href='#${$(this).attr("href").split("#")[1]}']`).length){
				$(`#nav_tabs li a[href='#${$(this).attr("href").split("#")[1]}']`).click()

				$('html, body').animate({
					scrollTop: parseInt($('.box-01').offset().top - 150 )
				}, 1);
			}
		})
	}
})

$(function () {
	if ($(".illustration_product_slider").length) {
		var swiperProductSlider = new Swiper(".illustration_product_slider", {
			navigation: {
				nextEl: ".swiper-slide-next",
				prevEl: ".swiper-slide-prev",
			},
		})
	}
})

$(function () {
	let windowWidth = $(window).width()
	function alignmentBlocks() {
		if (windowWidth >= 992) {
			let productImgHeight = $(
				"#product-preview-box .product-preview.active .product_info_img .illustration_product_slider"
			).height()

			let productTextHeight = $(
				"#product-preview-box .product-preview.active .product_info_text"
			).height()

			// if (productTextHeight > productImgHeight) {
				let productHeightDifference = productTextHeight - productImgHeight
				let productTilesHeight = $(
					"#product-preview-box .product-preview.active .product_info_text ul"
				).height()

				let productTilesFooterHeight = $(
					"#product-preview-box .product-preview.active .product_info_text .product_info_text__footer"
				).outerHeight()

				$(
					"#product-preview-box .product-preview .product_info_text ul"
				).removeAttr("style")

				$(
					"#product-preview-box .product-preview.active .product_info_text ul"
				).css({
					"max-height": productImgHeight - productTilesFooterHeight - (productTilesFooterHeight / 2.5),
					"min-height": productImgHeight - productTilesFooterHeight - (productTilesFooterHeight / 2.5),
					overflow: "auto",
				})
			// }
		}
	}

	function activateProductBox(boxHref) {
		$(".product-preview", productList).removeClass("active")
		$(productList).find(`#${boxHref}`).addClass("active")
	}

	function activateCharacteristicsBox(boxHref) {
		$("#characteristics-box .scontainer-content .row").removeClass("active")
		$(
			`#characteristics-box .scontainer-content .row[group="${boxHref}"]`
		).addClass("active")
	}

	function activateProductColors(boxHref = "") {
		if (boxHref === "") {
			$("#product-colors .product-colors").first().addClass("active")
		} else if ($("#product-colors .product-colors").length > 1) {
			$("#product-colors .product-colors").removeClass("active")
			$("#product-colors .product-colors").each(function () {
				if ($(this).hasClass(boxHref)) {
					$(this).addClass("active")
				}
			})
		}
	}

  function activateTurenPopup(boxHref = "") {
		if (boxHref === "") {
			$("#turen-popup .row").first().addClass("active")
		} else if ($("#turen-popup .row").length > 1) {
			$("#turen-popup .row").removeClass("active")
			$("#turen-popup .row").each(function () {
				if ($(this).hasClass(boxHref)) {
					$(this).addClass("active")
				}
			})
		}
	}

	function activateOurCatalog(boxHref = "") {

		if (boxHref === "") {
			$("#our-catalog .row").first().addClass("active")
		} else if ($("#our-catalog .row").length > 1) {
			$("#our-catalog .row").removeClass("active")
			$("#our-catalog .row").each(function () {
				if ($(this).hasClass(boxHref)) {
					$(this).addClass("active")
				}
			})
		}
	}

	function activateProductPopup(boxHref = "") {
		if (boxHref === "") {
			$("#product-popup-box .product-popup-box-list").first().addClass("active")
		} else if ($("#product-popup-box .product-popup-box-list").length > 1) {
			$("#product-popup-box .product-popup-box-list").removeClass("active")
			$("#product-popup-box .product-popup-box-list").each(function () {
				if ($(this).hasClass(boxHref)) {
					$(this).addClass("active")
				}
			})
		}
	}

	var productList = $("#product-preview-box .scontainer-content")
	$(".product-preview", productList).first().addClass("active")

	$("#nav_tabs li a").on("click", function (e) {
		if (!$(this).hasClass("link")) {
			e.preventDefault();
		}
		
		var boxHref = $(this).attr("href").substring(1)
		$(this).parent().parent().find("li").removeClass("active")
		$(this).parent().addClass("active")

		activateProductBox(boxHref)
		activateCharacteristicsBox(boxHref)
		$('.product-preview.active .product_info_img .swiper-slide img').load(function() {
      alignmentBlocks()
    })
		alignmentBlocks()
    if($("#product-colors").length) { activateProductColors(boxHref) }
    if($("#turen-popup").length){ activateTurenPopup(boxHref) }
		if($("#our-catalog .scontainer-content >.row").length > 1){ activateOurCatalog(boxHref) }
		if($("#product-popup-box .product-popup-box-list").length > 1){ activateProductPopup(boxHref) }

		activateProductPopup()
	})

	$("#nav_tabs li").first().addClass("active")
	$("#characteristics-box .scontainer-content .row").first().addClass("active")
  if($("#product-colors").length) { activateProductColors() }
  if($("#turen-popup").length){ activateTurenPopup() }
  if($("#our-catalog .scontainer-content >.row").length > 1){ activateOurCatalog() }
	if($("#product-popup-box .product-popup-box-list").length > 1){ activateProductPopup() }

  $('.product-preview.active .product_info_img .swiper-slide img').load(function() {
    alignmentBlocks()
  })
})

$(function () {
	$("#product-popup-box .box-data ul li").on('click', function(){
		$("#product-popup-box .box-data ul li").not($(this)).find('p').removeClass('active')
		$(this).find('p').toggleClass("active")
	})
})

$(function () {
	setTimeout( function(){
		var productDescriptionBox = $("#product-description-box")
		var productDescriptionText = $(".col-right", productDescriptionBox)
		var descriptionImgHeight = $(".col-left img", productDescriptionBox).height()
		var maxDescriptionHeightMax = $(".col-right ul", productDescriptionBox).height()

		if( $(productDescriptionText).height() > descriptionImgHeight){
			var maxDescriptionHeightMin = descriptionImgHeight - 30 - 150
			productDescriptionText.addClass("active")

			var actionBtn = `<div class="action-btn"></div>`
			$(productDescriptionText).append(actionBtn)

			$("ul", productDescriptionText).css({
				"height": maxDescriptionHeightMin,
			})

			$($(".action-btn", productDescriptionText).on('click', function() {
				$(this).toggleClass('active');

				if($(this).hasClass('active')) {
					$("ul", productDescriptionText).css({
						"height": maxDescriptionHeightMax,
					})
				} else {
					$("ul", productDescriptionText).css({
						"height": maxDescriptionHeightMin,
					})
				}
			}))
		}
	}, 100)
})

$(function () {
	$(".anchor").on("click", function () {
		let menuHeight = $("header").height()

		$("html, body").animate(
			{
				scrollTop: $("#product-colors").offset().top - menuHeight - 20,
			},
			1000
		)
	})
})
//* product colors changer (show more...)
$(function () {
  if($("#product-colors").length) {
		setTimeout( function(){
			var litesList = $("#product-colors .color-tiles").find(".action-btn").parent()
			litesList.attr("status", false)

			var closeUl = 0

			if($( document ).width() >= 992) {
				closeUl = 17 * 3
			} else {
				closeUl = 17 * 3
			}
			$(litesList.find("ul").children('li:lt(4)')).each(function() {
				closeUl += $(this).height();
			});

			litesList.find("ul").addClass("closed")
			litesList.find("ul").css({
				"max-height" : closeUl
			})

			litesList.find(".action-btn").on("click", function () {
				var thisParent = $(this).parent()
				thisParent.find("ul").toggleClass('closed')
				$(this).toggleClass('active')

				if (thisParent.attr('status') === "false") {
					thisParent.attr('status', "true");
				} else {
					thisParent.attr('status', "false");
				}

				if(thisParent.find("ul").hasClass("closed")){
					thisParent.find("ul").css({
						"max-height" : closeUl
					})
				} else {
					thisParent.find("ul").css({
						"max-height" : "unset"
					})
				}
			})
		}, 100)
  }
})

$(function() {
	$('#product-colors .col-right ul img').each(function() {
		$(this).wrap('<a class="fancybox" href="' + $(this).attr('src') + '"></a>');
	});
	
	$('.fancybox').fancybox({
		closeBtn		: false,
		helpers		: {
			buttons	: {}
		}
	});
})

$(function () {
	if($('.scroller-box').length) {
		setTimeout( function(){
			var scrollerBox = $(".scroller-box")
			$(scrollerBox).each(function() {

				var thisBox = $(this);
				var thisBoxTxtHeight = $(".col-text .col-data", thisBox).outerHeight();
				var thisBoxImgHeight = $(".col-img img", thisBox).height();

				if( $(".col-img img", thisBox).height() - 100 < $(".col-text .col-data", thisBox).outerHeight()){
					var actionBtn = `<div class="action-btn"></div>`
					$(".col-text .col-data", thisBox).append(actionBtn)

					$(".col-text .col-data", thisBox).css({
						"height": thisBoxImgHeight - 200,
						"margin-bottom": 60
					})

					$(".col-text .col-data .action-btn", thisBox).on("click", function() {
						$(this).toggleClass('active');

						if($(this).hasClass('active')) {
							$(".col-text .col-data", thisBox).css({
								"height": thisBoxTxtHeight
							})
						} else {
							$(".col-text .col-data", thisBox).css({
								"height": thisBoxImgHeight - 200,
							})
						}
					})
				}
			})
		}, 100)
	}
})

$(function() {
  if ($('.accordion-box').length) {
    var accordionBtn = `<div class="accordion-btn"></div>`;
    $('li h3', this).append(accordionBtn);

    $(".accordion-box li h3").on("click", function() {
      var isActive = $(this).hasClass("active");
      $(".accordion-box li h3").removeClass("active");
      $(".accordion-box li h3 .accordion-btn").removeClass("active");
      $(".accordion-box li h3").parent().find("p").slideUp();

      if (!isActive) {
        $(this).addClass("active");
				$('.accordion-btn', this).addClass("active");
        $(this).parent().find("p").slideDown();
      }
    });
  }
});