"use strict";

(function ($) {
	/**
	 * Document ready function
	 */
	$(document).ready(function () {

		var entryContentImages = jQuery('img.wp-post-image');

		entryContentImages.each(function () {

			var alt = jQuery(this).attr('alt');
			var title = jQuery(this).attr('title');

			if (typeof alt === 'undefined') {
				jQuery(this).attr('alt', jQuery(this).attr('title'));
				console.log('ALT');
			}

			if (typeof title === 'undefined') {
				jQuery(this).attr('title', jQuery(this).attr('alt'));
				console.log('TITLE');
			}

			if (typeof title === 'undefined' && typeof title === 'undefined') {
				console.log('No Alt and Title Tags: ' + jQuery(this).attr('src'));
				jQuery(this).attr('alt', 'Torebka, skóra, skórzana, damska, bag, bags, handmade, torba, upominek, women, prezent, pasek, look, moda, fashion, pracownia, projekt, projektant, model, torebki, blog, Sklep, personalizacja, personalizacja inicjałami, pasek, łańcuszek, rączka, Fabiola, naturalna, licowa, zamszowa, zamsz, lico, srebro, złoto, celebrytki,  prostokątna, Amelia, Amelia I s');
				jQuery(this).attr('title', 'Torebka, skóra, skórzana, damska, bag, bags, handmade, torba, upominek, women, prezent, pasek, look, moda, fashion, pracownia, projekt, projektant, model, torebki, blog, Sklep, personalizacja, personalizacja inicjałami, pasek, łańcuszek, rączka, Fabiola, naturalna, licowa, zamszowa, zamsz, lico, srebro, złoto, celebrytki,  prostokątna, Amelia, Amelia I s');
			}
		});

		var owl = $('#new-home-carousel');
		var customprice = parseFloat(0).toFixed(0);
		var lastprice = parseFloat(0).toFixed(0);
		var productprice = $("p.price.single-product-prices").find("span.woocommerce-Price-amount.amount:last").text();
		var productprice = parseFloat(productprice.replace("zł", ""));
		var lastvaltextarea = 0;
		var lastvalinput = 0;
		var lastvalinputcm = 0;
		var lastvalinputcmshow = 0;
		var actualval = "";

		var SHOW_ERROR = true;

		$(".choose-carousel").each(function () {
			if ($(this).find("input:checked")) {
				var name = $(this).find(".optionname").first().text();
			}
		});

		$('#koszyk').submit(function (e) {
			setTimeout(function () {
				$('#koszyk').trigger('reset');
			}, 100);
		});

		$(".choose-carousel .wcpa_image").click(function () {
			var price = $(this).closest(".wcpa_form_item").find(".wcpa_price").text();
			var pricevalue = parseFloat($(this).closest(".wcpa_form_item").find(".price_value").text());
			var lastprice = parseFloat($(this).closest(".wcpa_form_item").find(".wcpa_priceouter").attr("last-price"));
			customprice = customprice - lastprice;
			$(this).closest(".wcpa_form_item").find(".wcpa_priceouter").attr("last-price", pricevalue);
			var price = parseFloat($(this).closest(".wcpa_form_item").find(".price_value").text());
			customprice = customprice + price;
			$(".wcpa_options_total").find(".price_value").text(customprice);
			$(".wcpa_total").find(".price_value").text(productprice + customprice);
			var name = $(this).find(".optionname").text();
			$(this).closest(".wcpa_form_item").find(".selectedoptions-name").text(name);
			if (price != "0") {
				var price = $(this).closest(".wcpa_form_item").find(".wcpa_priceouter").show();
			} else {
				var price = $(this).closest(".wcpa_form_item").find(".wcpa_priceouter").hide();
			}
			var price = $(this).closest(".wcpa_form_item").find(".default_wcpa").hide();
			$(this).closest(".wcpa_form_item").find(".selectedoptions-name").css("color", "green").slideUp(100).slideDown(100);
			if (name == actualval) {
				console.log("Variant are selected");
				$(this).closest(".wcpa_form_item").find(".selectedoptions-name").text("...").css("color", "black");
			}
			$(".selectedoptions").click();
		});

		$(".wcpa_has_price.textarea").on("keyup change", function () {
			var val = $(this).closest(".wcpa_form_item").find(".wcpa_has_price").val().length;
			if (val == 0) {
				val = 0;
			} else {
				val = 1;
			}
			if (lastvaltextarea != val) {
				if (val == 1) {
					customprice = customprice + 10;
				}
				else if (val == 0) {
					customprice = customprice - 10;
				}
			}
			lastvaltextarea = val;
			$(".wcpa_options_total").find(".price_value").text(parseFloat(customprice));
			$(".wcpa_total").find(".price_value").text(parseFloat(productprice) + parseFloat(customprice));
		});

		$(".personalizacja.wcpa_has_price.text").on("keyup change", function () {
			var val = $(this).closest(".wcpa_form_item").find(".wcpa_has_price").val().length;
			if (val == 0) {
				val = 0;
			} else {
				val = 1;
			}
			if (lastvalinput != val) {
				if (val == 1) {
					customprice = customprice + 55;
				}
				else if (val == 0) {
					customprice = customprice - 55;
				}
			}
			lastvalinput = val;
			$(".wcpa_options_total").find(".price_value").text(parseFloat((customprice)));
			$(".wcpa_total").find(".price_value").text(parseFloat(productprice) + parseFloat((customprice)));
		});

		$(".cm.wcpa_has_price.text").on("keyup change", function () {
			var val = $(this).closest(".wcpa_form_item").find(".wcpa_has_price").val().length;
			var text = $(this).closest(".wcpa_form_item").find(".optionname").html();
			$(this).closest(".wcpa_form_item").find(".selectedoptions.textoptions").hide();
			if (val == 0) {
				val = 0;
			} else {
				val = 1;
			}
			if (lastvalinputcm != val) {
				if (val == 1) {
					customprice = customprice + 10;
					var text = $(this).closest(".wcpa_form_item").find(".optionname").html(text + ' <font color="green"><b>+10 zł</b></font>');
				}
				else if (val == 0) {
					customprice = customprice - 10;
					var text = $(this).closest(".wcpa_form_item").find(".optionname").html(text.replace(' <font color="green"><b>+10 zł</b></font>', ''));
				}
			}
			lastvalinputcm = val;
			$(".wcpa_options_total").find(".price_value").text(parseFloat(customprice));
			$(".wcpa_total").find(".price_value").text(parseFloat(productprice) + parseFloat(customprice));
		});

		$(".cmshow.wcpa_has_price.text").on("keyup change", function () {
			var val = $(this).closest(".wcpa_form_item").find(".wcpa_has_price").val().length;
			var text = $(this).closest(".wcpa_form_item").find(".optionname").html();
			$(this).closest(".wcpa_form_item").find(".selectedoptions.textoptions").hide();
			if (val == 0) {
				val = 0;
			} else {
				val = 1;
			}
			if (lastvalinputcmshow != val) {
				if (val == 1) {
					customprice = customprice + 10;
					var text = $(this).closest(".wcpa_form_item").find(".optionname").html(text + ' <br><font color="green"><b>Dopłata 10 zł</b></font>');
				}
				else if (val == 0) {
					customprice = customprice - 10;
					var text = $(this).closest(".wcpa_form_item").find(".optionname").html(text.replace(' <br><font color="green"><b>Dopłata 10 zł</b></font>', ''));
				}
			}
			lastvalinputcmshow = val;
			$(".wcpa_options_total").find(".price_value").text(parseFloat(customprice));
			$(".wcpa_total").find(".price_value").text(parseFloat(productprice) + parseFloat(customprice));
		});

		$("#personalizebutton").click(function () {
			//$(".wcpa_form_outer").toggle();
			$(this).toggleClass("active");
			if ($(this).is(".active")) {
				//$(".wcpa_form_outer").css("display", "flex");
			}
			return false;
		});

		$(".selectedoptions").click(function () {
			if ($(this).is(".active")) {
				$(this).parent().find(".image-group").hide();
				$(".selectedoptions").removeClass("active");
			}
			else {
				actualval = $(this).find(".selectedoptions-name").text();
				$(".selectedoptions").removeClass("active");
				$(this).addClass("active");
				var element = $(this).parent().find(".image-group");
				$(".image-group").hide();
				element.show();
				if ($(".cmshow_parent").css('display') == 'none') {
					$(".cmshow_parent").closest(".wcpa_form_item").find(".wcpa_has_price").val('');
					var val = $(".cmshow_parent").closest(".wcpa_form_item").find(".wcpa_has_price").val().length;
					var text = $(".cmshow_parent").closest(".wcpa_form_item").find(".optionname").html();
					$(".cmshow_parent").closest(".wcpa_form_item").find(".selectedoptions.textoptions").hide();
					if (val == 0) {
						val = 0;
					} else {
						val = 1;
					}
					if (lastvalinputcmshow != val) {
						if (val == 1) {
							customprice = customprice + 10;
							var text = $(".cmshow_parent").closest(".wcpa_form_item").find(".optionname").html(text + ' <br><font color="green"><b>Dopłata 10 zł</b></font>');
						}
						else if (val == 0) {
							customprice = customprice - 10;
							var text = $(".cmshow_parent").closest(".wcpa_form_item").find(".optionname").html(text.replace(' <br><font color="green"><b>Dopłata 10 zł</b></font>', ''));
						}
					}
					lastvalinputcmshow = val;
					$(".wcpa_options_total").find(".price_value").text(parseFloat(customprice));
					$(".wcpa_total").find(".price_value").text(parseFloat(productprice) + parseFloat(customprice));
				}
			}
		});

		$(".backimggroup").click(function () {
			$(".selectedoptions").click();
		});

		owl.owlCarousel({
			items: 1,
			loop: true,
			margin: 0,
			autoplay: true,
			autoplayTimeout: 3000,
			autoplayHoverPause: true,
			dotsContainer: '#new-home-pagination',
			onRefresh: callback
		});

		$('.choose-carousel').owlCarousel({
			loop: false,
			margin: 0,
			nav: true,
			autoplay: false,
			autoplayTimeout: 3000,
			autoplayHoverPause: true,
			responsive: {
				0: {
					items: 2
				},

				1240: {
					items: 4
				}
			}
		});

		$("#personalize-home-right-down a").click(function () {
			var target = $(this).data("link");

			if ($(".hiddendiv:visible").length == 0) {
				$("." + target).slideDown();
			}
			else {
				$(".hiddendiv:visible").slideUp("slow", function () {
					$("." + target).slideDown();
				});
			}
			return false;
		});

		$(".hiddendiv .closebutton").click(function () {
			$(".hiddendiv:visible").slideUp();
		});

		$('.personalize-carousel').owlCarousel({

			loop: false,

			autoplay: true,
			autoplayTimeout: 3000,
			autoplayHoverPause: true,
			nav: true,
			responsive: {

				0: {
					items: 1,
					margin: 10,
				},

				640: {
					items: 3,
					margin: 10,
				},

			}
		});

		function callback(event) {
			var items = event.item.count;
			var item = event.item.index + 1;
			$('#count-slide').text(items);
			$('#current-slide').text(item);
		}

		owl.on('changed.owl.carousel', function (event) {
			var items = event.page.count;
			var item = event.page.index + 1;
			$('#count-slide').text(items);
			$('#current-slide').text(item);
		});

		$('.product-carousel .products.columns-1').addClass("owl-carousel");

		$('.product-carousel .products.columns-1').owlCarousel({

			loop: false,

			autoplay: true,
			autoplayTimeout: 3000,
			autoplayHoverPause: true,
			nav: true,
			responsive: {

				0: {
					items: 1,
					margin: 10,
				},

				640: {
					items: 3,
					margin: 10,
				}

			}
		});

		$("#contact-right a").click(function () {
			$("#form-bottom").slideToggle();
			return false;
		})
		$(".showreviews").click(function () {
			$(".showreviews").hide(100)
			$("#review_form_wrapper").find("#review_form").show(200);
		});


		/**
		 * AOS init
		 */
		AOS.init({
			duration: 800,
		});


		var images = document.getElementsByClassName('simpleParallax');
		var instance = new simpleParallax(images, {
			delay: .7,
			transition: 'cubic-bezier(0,0,0,1)'
		});
		instance.refresh();


		/**
		 * Navbar menu
		 */
		$('.navbar-btn, .navbar-overlay').on('click', function () {
			var button = $(this),
				target = $('.' + button.attr('data-target'))[0],
				scroll = window.innerWidth - $(document).width();

			if (target) {
				$(target).toggleClass('active');
				$('html').css('overflow', 'hidden');
				$('body').css('padding-right', scroll);

				if (button.is('.double')) {
					$('html').css('overflow', 'unset');
					$('body').css('padding-right', 0);
					$('.navbar-nav ul, .navbar-nav li').removeAttr('style', '').removeClass('active');
					$('[data-target="' + button.attr('data-target') + '"]:not(.double)').toggleClass('collapsed');
				}

				else {
					$(button).toggleClass('collapsed');
				}
			}
		});

		// Navbar menu mobile
		$('.navbar-nav .svg-icon').on('click', function (event) {
			event.preventDefault();

			if ($(this).closest('.navbar-nav').is('.active')) {

				var currentIcon = $(this),
					currentItem = currentIcon.closest('.nav-item');

				if (!currentItem.hasClass('active')) {
					currentItem.addClass('active');
				}

				// Animate Submenu
				currentItem.children('.dropdown-menu').stop(true, true).slideToggle(400, function () {
					currentItem.toggleClass('active');

					if ($(this).is(':hidden')) {
						currentItem.removeClass('active');
					} else {
						currentItem.addClass('active');
					}
				});
			}
		});


		/**
		 * Gallery Shortcode
		 */
		$('.content .gallery').each(function () {
			$(this).magnificPopup({
				delegate: 'a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]',
				type: 'image',
				closeOnContentClick: false,
				closeBtnInside: false,
				mainClass: 'mfp-with-zoom mfp-img-mobile',
				removalDelay: 500,
				image: {
					verticalFit: true,
				},
				gallery: {
					enabled: true,
					tCounter: '<span class="mfp-counter">%curr% z %total%</span>'
				},
				zoom: {
					enabled: true,
					duration: 250,
					opener: function (element) {
						return element.find('img');
					}
				}
			});
		});


		/**
		 * Wpcf7 Validate Fix
		 */
		$(document).on('focus', '.wpcf7 input, .wpcf7 textarea', function () {
			$(this).removeClass('wpcf7-not-valid');
		});
		$(document).on('mouseover', '.wpcf7-response-output', function () {
			$(this).delay(2000).slideUp(function () {
				$(this).empty().removeAttr('style');
			});
		});


		/**
		 * Validate cart form
		 */
		// var requiredFieldID = ['wcpa_form_id_892940', 'wcpa_form_id_892932', 'wcpa_form_id_900071', 'wcpa_form_id_874486', 'wcpa_form_id_895752', 'wcpa_form_id_900518', 'wcpa_form_id_895144', 'wcpa_form_id_914836', 'wcpa_form_id_892942', 'wcpa_form_id_892941', 'wcpa_form_id_907454', 'wcpa_form_id_901453', 'wcpa_form_id_892832', 'wcpa_form_id_901718'];

		jQuery(".cart .single_add_to_cart_button").click(function () {
			var requiredFieldID = [];
			jQuery("div.wcpa_form_outer").find(".wcpa_form_item").each(function () {
				requiredFieldID.push(this.id);
			});

			for (var i = 0; i < requiredFieldID.length; i++) {
				if (jQuery('#' + requiredFieldID[i]).find('.required_ast').length) {
					var fieldName = jQuery("#" + requiredFieldID[i] + " .selectedoptions-name").text();

					if (fieldName.length == 0) {
						console.log("field: " + requiredFieldID[i] + " are empty");
						jQuery("#" + requiredFieldID[i] + " .selectedoptions").addClass("borderRequired");
						jQuery('html, body').animate({
							scrollTop: jQuery("#" + requiredFieldID[0]).offset().top - 30
						}, 500);
					}
					else if (fieldName.length != 0) {
						console.log("field: " + requiredFieldID[i] + " are not empty");
						jQuery("#" + requiredFieldID[i] + " .selectedoptions").removeClass("borderRequired");
					}
				}
			}
		});


		/**
		 * Counter shortcode
		 */
		if (jQuery('.counter').length > 0 && undefined !== jQuery('.counter').countUp) {
			jQuery('.counter').countUp();
		}

	});
})(jQuery);