document.addEventListener("DOMContentLoaded",()=>{let h=document.getElementById("gallery");if(h){let i=document.getElementById("searchInput"),a=document.querySelector(".search-wrapper"),l=document.getElementById("prevPageBtn"),o=document.getElementById("nextPageBtn"),d=document.getElementById("pageInput"),c=document.querySelector(".pagination"),n=[],r=[],s=1,g=36;function m(e,t){s=e,h.innerHTML="";var a=(e-1)*g,n=a+g,a=t.slice(a,n);let r=document.createDocumentFragment();a.forEach(e=>{var t=document.createElement("div"),a=(t.className="card",t.setAttribute("data-title",e.name),t.setAttribute("data-image",e.imgPath),document.createElement("img")),n=e.imgPath.replace(/\.png$/i,".webp");a.src=encodeURI(e.imgPath),a.srcset=`${encodeURI(n)} 1x, ${encodeURI(e.imgPath)} 1x`,a.alt=e.name,a.width=781,a.height=781,a.loading="lazy",t.appendChild(a),r.appendChild(t)}),h.appendChild(r);n=t.length;(n=Math.ceil(n/g))<=1?c.style.display="none":(c.style.display="flex",l.disabled=1===s,o.disabled=s>=n,d.placeholder=s,d.value="",d.max=n);a=i.value.trim();a||1!==s?!a&&1<s?window.history.replaceState({},"","/?page="+s):a&&((t=new URLSearchParams).set("search",a),1<s&&t.set("page",s),window.history.replaceState({},"","/?"+t.toString())):window.history.replaceState({},"",window.location.origin),1<e&&h.scrollIntoView({behavior:"smooth"})}function p(){let t=i.value.trim().toLowerCase();m(1,r=n.filter(e=>e.name.toLowerCase().includes(t)))}fetch("/images.json").then(e=>{if(e.ok)return e.json();throw new Error("HTTP error! Status: "+e.status)}).then(e=>{n=e,r=n;var e=new URLSearchParams(window.location.search),t=e.get("search"),t=(t&&(i.value=t,p()),parseInt(e.get("page"),10));m(!isNaN(t)&&1<t?t:1,r),l.addEventListener("click",()=>{1<s&&m(s-1,r)}),o.addEventListener("click",()=>{var e=Math.ceil(r.length/g);s<e&&m(s+1,r)}),d.addEventListener("change",()=>{var e=parseInt(d.value,10),t=Math.ceil(r.length/g);1<=e&&e<=t&&m(e,r),d.value=""}),a.addEventListener("submit",e=>{e.preventDefault(),p()}),h.addEventListener("click",e=>{var e=e.target.closest(".card");e&&(e=e.getAttribute("data-title"))&&(e=e.toLowerCase().replace(/\s+/g,"-").replace(/[^a-z0-9-]/g,""),window.location.href="/"+e)})}).catch(e=>{console.error("Error loading gallery:",e),h.innerHTML='<p style="color: red; text-align: center;">Unable to load images. Please try refreshing the page.</p>'}),window.addEventListener("popstate",()=>{window.location.reload()})}});
