(function (){
"use strict";
function getProductCard(node){
var el=node;
while (el&&el!==document.body){
if(el.classList&&el.classList.contains("product")) return el;
el=el.parentElement;
}
return null;
}
function getArchiveImageEl(card){
if(!card) return null;
return (
card.querySelector(".bde-woo-product-image img") ||
card.querySelector(".woocommerce-LoopProduct-link img") ||
card.querySelector("a.woocommerce-LoopProduct-link img") ||
card.querySelector("img.attachment-woocommerce_thumbnail") ||
card.querySelector("img.wp-post-image") ||
null
);
}
function setImage(img, src){
if(!img||!src) return;
img.setAttribute("src", src);
img.setAttribute("data-src", src);
img.setAttribute("data-large_image", src);
img.setAttribute("data-o_src", src);
if(img.hasAttribute("srcset")) img.removeAttribute("srcset");
if(img.hasAttribute("sizes")) img.removeAttribute("sizes");
if(img.hasAttribute("data-srcset")) img.removeAttribute("data-srcset");
if(img.hasAttribute("data-lazy-srcset")) img.removeAttribute("data-lazy-srcset");
if(img.hasAttribute("data-lazy-src")) img.setAttribute("data-lazy-src", src);
var picture=img.closest("picture");
if(picture){
var sources=picture.querySelectorAll("source");
for (var i=0; i < sources.length; i++){
sources[i].setAttribute("srcset", src);
sources[i].setAttribute("data-srcset", src);
}}
var link=img.closest("a");
if(link){
var href=link.getAttribute("href")||"";
if(/\.(jpg|jpeg|png|webp|gif)(\?.*)?$/i.test(href)){
link.setAttribute("href", src);
}}
}
function setImageWithRetry(card, src){
var attempts=0;
var maxAttempts=12;
function apply(){
attempts++;
setImage(getArchiveImageEl(card), src);
if(attempts < maxAttempts) window.setTimeout(apply, 120);
}
apply();
}
function clearActive(container){
var active=container.querySelectorAll(".zoop-archive-color.is-active");
for (var i=0; i < active.length; i++){
active[i].classList.remove("is-active");
active[i].setAttribute("aria-pressed", "false");
}}
function activateSwatchImage(swatch){
if(!swatch) return;
var archiveWrap=swatch.closest(".zoop-archive-attrs");
if(!archiveWrap) return;
var card=getProductCard(archiveWrap);
if(!card) return;
var imageUrl=swatch.getAttribute("data-image-url")||archiveWrap.getAttribute("data-default-image")||"";
if(!imageUrl) return;
setImageWithRetry(card, imageUrl);
}
function activateFilteredImages(){
var wraps=document.querySelectorAll('.zoop-archive-attrs[data-active-umbrella-color]:not([data-active-umbrella-color=""])');
for (var i=0; i < wraps.length; i++){
var swatch=wraps[i].querySelector(".zoop-archive-color.is-active");
if(swatch) activateSwatchImage(swatch);
}}
function navigateUmbrellaFilter(link){
if(!link) return false;
if(document.documentElement.classList.contains("zoop-umbrella-filter-loading")) return true;
var url=new URL(window.location.href);
var href=link.getAttribute("href")||"";
var linkUrl=href ? new URL(href, window.location.href):null;
var color=link.getAttribute("data-zoop-umbrella-color")||(linkUrl ? linkUrl.searchParams.get("zoop_umbrella_color"):"")||"";
var shouldClear=link.getAttribute("data-zoop-umbrella-clear")==="true"||link.classList.contains("zoop-umbrella-filter__clear");
if(shouldClear){
url.searchParams.delete("zoop_umbrella_color");
}else if(color){
url.searchParams.set("zoop_umbrella_color", color);
}else{
return false;
}
url.searchParams.delete("paged");
url.searchParams.delete("product-page");
showUmbrellaLoading(link);
window.requestAnimationFrame(function (){
window.location.href=url.href;
});
return true;
}
function showUmbrellaLoading(link){
var filter=link.closest(".zoop-umbrella-filter");
document.documentElement.classList.add("zoop-umbrella-filter-loading");
if(filter){
filter.classList.add("is-loading");
filter.setAttribute("aria-busy", "true");
var active=filter.querySelectorAll(".is-loading, .is-pending");
for (var i=0; i < active.length; i++){
active[i].classList.remove("is-loading", "is-pending");
}
link.classList.add("is-loading", "is-pending");
link.setAttribute("aria-current", "true");
var status=filter.querySelector(".zoop-umbrella-filter__loading");
if(!status){
status=document.createElement("span");
status.className="zoop-umbrella-filter__loading";
status.setAttribute("aria-live", "polite");
filter.appendChild(status);
}
status.textContent=link.classList.contains("zoop-umbrella-filter__clear") ? "Valoma...":"Filtruojama...";
}}
document.addEventListener("click", function (e){
var umbrellaLink=e.target.closest(".zoop-umbrella-filter__swatch, .zoop-umbrella-filter__clear");
if(umbrellaLink&&navigateUmbrellaFilter(umbrellaLink)){
e.preventDefault();
return;
}
var swatch=e.target.closest(".zoop-archive-color");
if(!swatch) return;
e.preventDefault();
e.stopPropagation();
if(typeof e.stopImmediatePropagation==="function") e.stopImmediatePropagation();
var archiveWrap=swatch.closest(".zoop-archive-attrs");
if(!archiveWrap) return;
clearActive(archiveWrap);
swatch.classList.add("is-active");
swatch.setAttribute("aria-pressed", "true");
activateSwatchImage(swatch);
}, true);
if(document.readyState==="loading"){
document.addEventListener("DOMContentLoaded", function (){
activateFilteredImages();
});
}else{
activateFilteredImages();
}})();