"use strict";(function(){function a(a){H.tags=a;const c=o();a&&(a.forEach(function(a){D.appendChild(m(a,-1!==c.indexOf(a.name)))}),c&&0<c.length&&(C.style.display="")),b(c)}function b(a){c();let b;b=a&&0<a.length?{start:H.renderIndex,tags:a,articleType:H.articleType}:{start:H.renderIndex,articleType:H.articleType},e(b)}function c(){H.loading=!0,x.style.display="none",y.style.display=""}function d(){H.loading=!1,x.style.display=H.total===H.renderIndex||0===H.renderIndex?"none":"",y.style.display="none"}function e(a){fetch(location.pathname+t,{method:"POST",body:JSON.stringify(a)}).then(function(a){return a.json()}).then(function(a){const{articles:b,count:c,total:e,errorMsg:f}=a;f?w.innerHTML="<p>"+f+"</p>":(H.total=e,0<c?(0<H.renderIndex?(H.articles+=b,H.renderIndex+=c):(H.articles=b,H.renderIndex=c),h(b)):(H.articles="",H.renderIndex=0,H.count=0,i()),g()),j(),d(),H.initialized||(H.initialized=!0)})}function f(){fetch(location.pathname+u).then(function(a){return a.json()}).then(function(b){b.tags&&a(b.tags)})}function g(){const a=G.split(" ");F.textContent=a[0]+" "+H.renderIndex+" "+a[1]+" "+H.total+" "+a[2]+""}function h(a){E.style.display="none",w.innerHTML+=a}function i(){E.style.display="",w.innerHTML=""}function j(){s.onCollection(document.getElementsByClassName("xp-news-center--item"),function(a){s.onCollection(a.getElementsByTagName("i"),function(a){"ukategorisert"===a.getAttribute("data-name")||a.parentNode.addEventListener("click",function(){C.style.display="",p(a.getAttribute("data-name"))})})})}function k(){H.tagPanelOpen=!1,D.style.display="none",F.style.display="none",s.onCollection(z.getElementsByClassName("klp-animation"),function(a){s.removeClasses(a,["is-active"])}),s.removeClasses(B,["klp-display-hidden"]),s.addClasses(A,["klp-display-hidden"])}function l(){H.tagPanelOpen=!0,D.style.display="",F.style.display="",s.onCollection(z.getElementsByClassName("klp-animation"),function(a){s.addClasses(a,["is-active"])}),s.addClasses(B,["klp-display-hidden"]),s.removeClasses(A,["klp-display-hidden"])}function m(a,b){const c=s.createElement("input",["klp-checkbox__control"]);c.type="checkbox",c.value=a.displayName,c.name=a.name,c.id=a.name,c.checked=b;const d=document.createElement("span");d.textContent=a.displayName;const e=s.createElement("label",["klp-checkbox"],[d]);e.htmlFor=a.name,c.addEventListener("change",n(c,a)),c.addEventListener("keypress",function(a){"Enter"===a.key&&c.click()}),c.addEventListener("focus",function(){e.classList.add("klp-checkbox-focus")}),c.addEventListener("blur",function(){e.classList.remove("klp-checkbox-focus")}),H.checkboxes[a.name]=c;const f=s.createElement("div",["klp-grid-container__item","klp-grid-container__item--span-lg-col-3","klp-grid-container__item--span-md-col-6","klp-grid-container__item--span-sm-col-2"],[c,e]);return f.style="grid-column-start: auto",f}function n(a,b){return function(){if(a.checked)C.style.display="",p(b.name);else{const a=q(b.name);a||r()}}}function o(){const a=[],b=location.href.split("#!");return 1<b.length&&0<b[1].length&&window.decodeURI(b[1]).split(",").forEach(function(b){const c=H.tags.find(function(a){return a.name===b});c&&a.push(c.name)}),a}function p(a){const b=location.href.split("#!");let c="";if(1<b.length&&0<b[1].length){const d=window.decodeURI(b[1]).split(",");if(-1===d.indexOf(a))c=b[1]+","+a;else return!1}else c=a;return location.href=b[0]+"#!"+c,!0}function q(a){const b=location.href.split("#!");if(1<b.length&&0<b[1].length){const c=window.decodeURI(b[1]).split(",");if(c.splice(c.indexOf(a),1),0<c.length)return location.href=b[0]+"#!"+c.join(","),!0}return!1}function r(){location.href=location.href.split("#!")[0]+"#!",C.style.display="none"}const s=window._xp,t="/_/service/com.klp.enonic.cms/news-articles",u="/_/service/com.klp.enonic.cms/tags",v=document.getElementById("newsSection"),w=document.getElementById("newsGrid"),x=document.getElementById("moreNewsButton"),y=document.getElementById("loadingAnimation"),z=document.getElementById("filterNewsButton"),A=document.getElementById("hideFilterText"),B=document.getElementById("showFilterText"),C=document.getElementById("resetFilterButton"),D=document.getElementById("tagPanel"),E=document.getElementById("emptyResultText"),F=document.getElementById("resultCountText"),G=F.innerText,H={articles:[],tags:{},checkboxes:{},renderIndex:0,tagPanelOpen:!1,loading:!1,initialized:!1,articleType:v.getAttribute("article-type")};(function(){D.style.display="none",E.style.display="none",C.style.display="none",F.style.display="none",x.style.display="none",f(),x.addEventListener("click",function(){b(o())}),z.addEventListener("click",function(){H.tagPanelOpen?k():l()}),C.addEventListener("click",r)})(),window.addEventListener("hashchange",function(){H.renderIndex=0,s.removeChildren(w),E.style.display="none";const a=o();Object.keys(H.checkboxes).forEach(function(b){H.checkboxes[b].checked=-1!==a.indexOf(b)}),b(a,0<a.length)})})();