document.addEventListener("DOMContentLoaded", function (){
let globalModal=null;
let globalModalImg=null;
let globalCaptionDiv=null;
let globalThumbs=null;
let globalCurrentIndex=0;
let globalImageList=[];
document.addEventListener('keydown', handleKeydown);
function handleKeydown(e){
if(!globalModal||globalModal.style.display!=='flex') return;
if(e.key==='Escape'){
globalModal.style.display='none';
window.hasLightboxKeyListener=false;
}else if(e.key==='ArrowLeft'){
showImage(globalCurrentIndex - 1);
}else if(e.key==='ArrowRight'){
showImage(globalCurrentIndex + 1);
}}
function showImage(newIndex){
if(newIndex < 0) newIndex=globalImageList.length - 1;
if(newIndex >=globalImageList.length) newIndex=0;
globalCurrentIndex=newIndex;
globalModalImg.src=globalImageList[globalCurrentIndex].full;
globalCaptionDiv.innerHTML=globalImageList[globalCurrentIndex].caption||'';
globalThumbs.querySelectorAll('img').forEach((thumb, i)=> {
thumb.classList.toggle('active', i===globalCurrentIndex);
});
}
function openModal(index, imageList){
globalCurrentIndex=index;
globalImageList=imageList;
if(!globalModal){
globalModal=document.createElement('div');
globalModal.className='custom-lightbox';
globalModal.innerHTML=`
<span class="close">&times;</span>
<div class="position__relative">
<img class="modal-content" id="lightbox-img">
<div class="arrow prev"><svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M11.7305 19.5452C12.0234 19.2639 12.188 18.8824 12.188 18.4847C12.188 18.0869 12.0234 17.7055 11.7305 17.4242L3.99615 9.9992L11.7305 2.5742C12.0151 2.29129 12.1726 1.91239 12.1691 1.51909C12.1655 1.1258 12.0012 0.749579 11.7115 0.471467C11.4218 0.193355 11.0299 0.0356033 10.6202 0.0321857C10.2105 0.0287681 9.81584 0.17996 9.52115 0.453197L0.682088 8.9387C0.389165 9.21999 0.224609 9.60145 0.224609 9.9992C0.224609 10.3969 0.389165 10.7784 0.682088 11.0597L9.52115 19.5452C9.81416 19.8264 10.2115 19.9844 10.6258 19.9844C11.0402 19.9844 11.4375 19.8264 11.7305 19.5452Z" fill="#042C06"></path></svg></div>
<div class="arrow next"><svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1.26947 19.5452C0.976551 19.2639 0.811996 18.8824 0.811996 18.4847C0.811996 18.0869 0.976551 17.7055 1.26947 17.4242L9.00385 9.9992L1.26947 2.5742C0.984853 2.29129 0.827362 1.91239 0.830922 1.51909C0.834482 1.1258 0.998808 0.749579 1.28851 0.471467C1.57821 0.193355 1.9701 0.0356033 2.37979 0.0321857C2.78947 0.0287681 3.18416 0.17996 3.47885 0.453197L12.3179 8.9387C12.6108 9.21999 12.7754 9.60145 12.7754 9.9992C12.7754 10.3969 12.6108 10.7784 12.3179 11.0597L3.47885 19.5452C3.18584 19.8264 2.78848 19.9844 2.37416 19.9844C1.95984 19.9844 1.56249 19.8264 1.26947 19.5452Z" fill="#042C06"></path></svg></div>
<div class="lightbox-caption" id="lightbox-caption" style="position: absolute;bottom: 10px;"></div>
</div>
<ul class="lightbox-thumbnails"></ul>
`;
document.body.appendChild(globalModal);
globalModalImg=globalModal.querySelector('#lightbox-img');
globalCaptionDiv=globalModal.querySelector('#lightbox-caption');
globalThumbs=globalModal.querySelector('.lightbox-thumbnails');
globalModal.querySelector('.close').addEventListener('click', ()=> {
globalModal.style.display='none';
});
globalModal.querySelector('.prev').addEventListener('click', ()=> {
showImage(globalCurrentIndex - 1);
});
globalModal.querySelector('.next').addEventListener('click', ()=> {
showImage(globalCurrentIndex + 1);
});
}
globalModal.style.display='flex';
if(!window.hasLightboxKeyListener){
}
showImage(index);
globalThumbs.innerHTML='';
imageList.forEach((img, i)=> {
const li=document.createElement('li');
const figure=document.createElement('figure');
const thumb=document.createElement('img');
thumb.src=img.thumb;
if(i===index) thumb.classList.add('active');
thumb.addEventListener('click', ()=> openModal(i, imageList));
figure.appendChild(thumb);
li.appendChild(figure);
globalThumbs.appendChild(li);
});
}
const galleries=document.querySelectorAll('.gallery, .wp-block-gallery');
const fullscreenBtnTemplate=document.createElement('button');
fullscreenBtnTemplate.innerHTML='<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"><g clip-path="url(#clip0_285_304)"><mask id="mask0_285_304" style="mask-type:luminance" maskUnits="userSpaceOnUse" x="0" y="0" width="16" height="16"><path d="M16 0H0V16H16V0Z" fill="white"/></mask><g mask="url(#mask0_285_304)"><path d="M14.6666 0H5.3334C4.60019 0 4 0.600195 4 1.3334V10.6666C4 11.3998 4.60019 12 5.3334 12H14.6666C15.3998 12 16 11.3998 16 10.6666V1.3334C16 0.600195 15.3998 0 14.6666 0ZM12.332 6L12.3326 4.37359L7.37238 9.33336L6.6666 8.62496L11.6244 3.66801L9.99944 3.6666V2.6666H12.666C13.0346 2.66738 13.3326 2.96539 13.332 3.3334L13.3334 6.00058L12.332 6Z" fill="#7FFB89"/><path d="M6.6666 13.3334V14.6666H1.3334V9.33336H2.6666V8H0.666602C0.300195 8 0 8.30016 0 8.66664V15.3334C0 15.6992 0.300195 16 0.666602 16H7.3334C7.69981 16 8 15.6992 8 15.3334V13.3334H6.6666Z" fill="#7FFB89"/></g></g><defs><clipPath id="clip0_285_304"><rect width="16" height="16" fill="white"/></clipPath></defs></svg>';
fullscreenBtnTemplate.classList.add('lightbox-fullscreen-btn');
galleries.forEach((gallery)=> {
const imgs=gallery.querySelectorAll('a, figure a, figure');
if(imgs.length===0) return;
const imageList=[];
const addedSrcs=new Set();
imgs.forEach((el)=> {
const img=el.querySelector('img');
if(img){
let baseSrc=img.src.split('?')[0];
const anchor=el.tagName==='A' ? el:el.querySelector('a');
if(anchor&&anchor.href){
baseSrc=anchor.href.split('?')[0];
}
if(!addedSrcs.has(baseSrc)){
let caption='';
const fig=el.closest('figure');
if(fig){
const figcaption=fig.querySelector('figcaption');
if(figcaption) caption=figcaption.innerHTML;
}else if(img.alt){
caption=img.alt;
}
imageList.push({ full: baseSrc, thumb: img.src, caption });
addedSrcs.add(baseSrc);
}
img.addEventListener('click', (e)=> {
e.preventDefault();
const index=imageList.findIndex(item=> item.thumb===img.src);
if(index!==-1){
openModal(index, imageList);
}});
}});
if(imageList.length===0) return;
if(gallery.classList.contains('galeria-embutida')){
gallery.innerHTML='';
const mainImg=document.createElement('img');
mainImg.src=imageList[0].full;
mainImg.classList.add('lightbox-main');
const captionDiv=document.createElement('div');
captionDiv.classList.add('lightbox-caption-embutida');
const captionText=imageList[0].caption||'';
if(captionText.trim()===''){
captionDiv.classList.add('caption-vazio');
}else{
captionDiv.classList.remove('caption-vazio');
}
captionDiv.innerHTML=`<span>${captionText}</span>`;
const prev=document.createElement('div');
prev.classList.add('lightbox-arrow', 'left');
prev.innerHTML=`
<svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M11.7305 19.5452C12.0234 19.2639 12.188 18.8824 12.188 18.4847C12.188 18.0869 12.0234 17.7055 11.7305 17.4242L3.99615 9.9992L11.7305 2.5742C12.0151 2.29129 12.1726 1.91239 12.1691 1.51909C12.1655 1.1258 12.0012 0.749579 11.7115 0.471467C11.4218 0.193355 11.0299 0.0356033 10.6202 0.0321857C10.2105 0.0287681 9.81584 0.17996 9.52115 0.453197L0.682088 8.9387C0.389165 9.21999 0.224609 9.60145 0.224609 9.9992C0.224609 10.3969 0.389165 10.7784 0.682088 11.0597L9.52115 19.5452C9.81416 19.8264 10.2115 19.9844 10.6258 19.9844C11.0402 19.9844 11.4375 19.8264 11.7305 19.5452Z" fill="#042C06"/>
</svg>`;
const next=document.createElement('div');
next.classList.add('lightbox-arrow', 'right');
next.innerHTML=`<svg width="13" height="20" viewBox="0 0 13 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M1.26947 19.5452C0.976551 19.2639 0.811996 18.8824 0.811996 18.4847C0.811996 18.0869 0.976551 17.7055 1.26947 17.4242L9.00385 9.9992L1.26947 2.5742C0.984853 2.29129 0.827362 1.91239 0.830922 1.51909C0.834482 1.1258 0.998808 0.749579 1.28851 0.471467C1.57821 0.193355 1.9701 0.0356033 2.37979 0.0321857C2.78947 0.0287681 3.18416 0.17996 3.47885 0.453197L12.3179 8.9387C12.6108 9.21999 12.7754 9.60145 12.7754 9.9992C12.7754 10.3969 12.6108 10.7784 12.3179 11.0597L3.47885 19.5452C3.18584 19.8264 2.78848 19.9844 2.37416 19.9844C1.95984 19.9844 1.56249 19.8264 1.26947 19.5452Z" fill="#042C06"/>
</svg>
`;
const thumbsContainer=document.createElement('ul');
thumbsContainer.classList.add('lightbox-thumbnails');
thumbsContainer.classList.add('scrollbar__none');
let currentIndex=0;
function updateMainImage(index){
currentIndex=index;
mainImg.src=imageList[index].full;
const captionText=imageList[index].caption||'';
captionDiv.innerHTML=`<span>${captionText}</span>`;
if(captionText.trim()===''){
captionDiv.classList.add('caption-vazio');
}else{
captionDiv.classList.remove('caption-vazio');
}
thumbsContainer.querySelectorAll('img').forEach((thumb, i)=> {
thumb.classList.toggle('active', i===index);
});
}
imageList.forEach((img, i)=> {
const li=document.createElement('li');
const figure=document.createElement('figure');
const thumb=document.createElement('img');
thumb.src=img.thumb;
if(i===0) thumb.classList.add('active');
thumb.addEventListener('click', ()=> updateMainImage(i));
figure.appendChild(thumb);
li.appendChild(figure);
thumbsContainer.appendChild(li);
});
prev.addEventListener('click', ()=> {
const newIndex=(currentIndex - 1 + imageList.length) % imageList.length;
updateMainImage(newIndex);
});
next.addEventListener('click', ()=> {
const newIndex=(currentIndex + 1) % imageList.length;
updateMainImage(newIndex);
});
const fsBtn=fullscreenBtnTemplate.cloneNode(true);
fsBtn.addEventListener('click', ()=> openModal(currentIndex, imageList));
gallery.appendChild(prev);
gallery.appendChild(mainImg);
gallery.appendChild(captionDiv);
gallery.appendChild(next);
gallery.appendChild(thumbsContainer);
gallery.appendChild(fsBtn);
}
else if(gallery.classList.contains('galeria-lightbox')){
imgs.forEach((el, i)=> {
const anchor=el.tagName==='A' ? el:el.querySelector('a');
const clickable=anchor||el;
const baseSrc=anchor?.href?.split('?')[0]||el?.src?.split('?')[0];
const imgIndex=imageList.findIndex((item)=> item.full===baseSrc);
if(clickable&&imgIndex >=0){
clickable.addEventListener('click', (e)=> {
e.preventDefault?.();
openModal(imgIndex, imageList);
});
}});
}});
});
function fechaclickMenu(){
const nav=document.getElementById('navMain');
const header=document.querySelector('header');
if(nav&&header){
nav.classList.toggle('active');
document.body.style.cssText='overflow-y:scroll';
header.style.top="0";
}}
document.addEventListener('DOMContentLoaded', function(){
const lupa=document.querySelector('.lupa__search');
const caixaMenu=document.querySelector('.caixa__menu');
const header=document.querySelector('header');
if(lupa&&caixaMenu&&header){
lupa.addEventListener('click', function(event){
event.stopPropagation();
const isHidden =
caixaMenu.style.display==='none' ||
caixaMenu.style.display==='';
const input=caixaMenu.querySelector('input');
if(isHidden){
const headerHeight=header.offsetHeight;
if(window.innerWidth <=1000){
Object.assign(caixaMenu.style, {
display: 'flex',
top: `${headerHeight - 2}px`
});
}else{
Object.assign(caixaMenu.style, {
display: 'block',
top: ''
});
}
lupa.classList.add('active');
if(input){
input.focus();
}}else{
caixaMenu.style.display='none';
lupa.classList.remove('active');
}});
document.addEventListener('click', function(event){
const isVisible =
caixaMenu.style.display==='block' ||
caixaMenu.style.display==='flex';
if(isVisible &&
!caixaMenu.contains(event.target) &&
!lupa.contains(event.target)
){
caixaMenu.style.display='none';
lupa.classList.remove('active');
}});
}});
document.addEventListener("DOMContentLoaded", function (){
const header=document.querySelector(".header__group");
const body=document.body;
const menus=[
{
btn: document.getElementById("btnMainMobile"),
nav: document.getElementById("navMain"),
extraBtn: document.getElementById("searchButton_mobile"),
texto: document.getElementById("textoHamburquer"),
labelOpen: "Abrir Menu",
labelClose: "Fechar Menu",
textoOpen: "Menu",
textoClose: "Close"
},
{
btn: document.getElementById("btnMenuEditorias"),
nav: document.getElementById("navEditorias"),
labelOpen: "Abrir Editorias",
labelClose: "Fechar Editorias"
}
];
function toggleMenu(menu, event){
if(event.type==="touchstart") event.preventDefault();
menus.forEach(m=> {
if(m!==menu) closeMenu(m);
});
const isActive=menu.nav.classList.toggle("active");
menu.btn.classList.toggle("active", isActive);
menu.extraBtn?.classList.toggle("active", isActive);
header.style.transition="top 0.3s ease-in-out";
menu.btn.setAttribute("aria-expanded", isActive);
menu.btn.setAttribute("aria-label", isActive ? menu.labelClose:menu.labelOpen);
if(menu.texto){
menu.texto.innerHTML=isActive ? menu.textoClose:menu.textoOpen;
}
body.classList.toggle("bloqueia_rolagem", isActive);
}
function closeMenu(menu){
if(menu.nav&&menu.nav.classList.contains("active")){
menu.nav.classList.remove("active");
menu.btn.classList.remove("active");
menu.extraBtn?.classList.remove("active");
menu.btn.setAttribute("aria-expanded", "false");
menu.btn.setAttribute("aria-label", menu.labelOpen);
if(menu.texto){
menu.texto.innerHTML=menu.textoOpen;
}
body.classList.remove("bloqueia_rolagem");
}}
function closeMenuIfClickedOutside(event){
menus.forEach(menu=> {
if(menu.nav &&
!menu.nav.contains(event.target) &&
!menu.btn.contains(event.target) &&
!menu.extraBtn?.contains(event.target)
){
closeMenu(menu);
}});
}
menus.forEach(menu=> {
if(menu.btn&&menu.nav){
menu.btn.addEventListener("click", e=> toggleMenu(menu, e));
menu.btn.addEventListener("touchstart", e=> toggleMenu(menu, e));
}});
document.addEventListener("click", closeMenuIfClickedOutside);
document.addEventListener("touchstart", closeMenuIfClickedOutside);
});
document.addEventListener("DOMContentLoaded", function (){
const dropdownItems=document.querySelectorAll(".dropdown_links__mobile");
dropdownItems.forEach((item)=> {
const link=item.querySelector("a");
const submenu=item.querySelector(".submenu__menu_hamburguer");
if(!submenu) return;
link.addEventListener("click", function (event){
event.preventDefault();
item.classList.toggle("active");
});
});
});
document.addEventListener("DOMContentLoaded", function (){
var menuItems=document.querySelectorAll('.has__submenu');
var menuOverlay=document.getElementById('menuoverlay');
if(menuItems.length===0) return;
menuItems.forEach(function (menuItem){
var link=menuItem.querySelector('a');
var subMenu=menuItem.querySelector('.submenu');
if(!link||!subMenu) return;
function showOverlay(){
menuOverlay.style.display='block';
subMenu.style.display='block';
subMenu.classList.add('ativo');
}
function hideOverlay(){
menuOverlay.style.display='none';
subMenu.style.display='none';
subMenu.classList.remove('ativo');
}
link.addEventListener('mouseenter', showOverlay);
subMenu.addEventListener('mouseenter', showOverlay);
link.addEventListener('mouseleave', hideOverlay);
subMenu.addEventListener('mouseleave', hideOverlay);
link.addEventListener('click', function (e){
if(window.innerWidth <=768){
e.preventDefault();
if(subMenu.classList.contains('ativo')){
hideOverlay();
}else{
showOverlay();
}}
});
document.addEventListener('click', function (e){
if(window.innerWidth <=768){
if(!menuItem.contains(e.target)){
hideOverlay();
}}
});
});
});
const abrirRedesModal=document.getElementById("abrir_redes_modal");
const redesModal=document.getElementById("redes_modal");
const fecharRedesModal=document.querySelector(".fechar_redes_modal");
function abrirModal(){
redesModal.style.display="flex";
document.body.style.overflow="hidden";
}
function fecharModal(){
redesModal.style.display="none";
document.body.style.overflow="";
}
if(abrirRedesModal&&redesModal){
abrirRedesModal.onclick=abrirModal;
}
if(fecharRedesModal&&redesModal){
fecharRedesModal.onclick=fecharModal;
}
if(redesModal){
window.onclick=function (event){
if(event.target===redesModal){
fecharModal();
}};}
document.addEventListener("DOMContentLoaded", function (){
const forms=document.querySelectorAll('.newsletter__form');
if(forms.length===0) return;
let recaptchaWidgetId=null;
let activeForm=null;
function openRecaptchaModal(){
const modal=document.getElementById('recaptchaModal');
modal.classList.add('active');
modal.setAttribute('aria-hidden', 'false');
if(recaptchaWidgetId===null){
recaptchaWidgetId=grecaptcha.render('recaptchaContainer', {
sitekey: '6LdCvCwsAAAAAIlMN_mrt9YfpESlktTtY-ONOqYg'
});
}}
function closeRecaptchaModal(){
const modal=document.getElementById('recaptchaModal');
modal.classList.remove('active');
modal.setAttribute('aria-hidden', 'true');
}
const recaptchaModal=document.getElementById('recaptchaModal');
if(recaptchaModal){
recaptchaModal.addEventListener('click', function (e){
if(e.target.classList.contains('recaptcha-modal__overlay')){
closeRecaptchaModal();
if(typeof grecaptcha!=='undefined'){
grecaptcha.reset();
}
activeForm=null;
}});
}
document.addEventListener('keydown', function (e){
if(e.key==='Escape'){
const modal=document.getElementById('recaptchaModal');
if(modal&&modal.classList.contains('active')){
closeRecaptchaModal();
if(typeof grecaptcha!=='undefined'){
grecaptcha.reset();
}
activeForm=null;
}}
});
forms.forEach(function (form){
const formStart=form.querySelector('.form__start');
if(formStart){
formStart.value=Math.floor(Date.now() / 1000);
}
const urlOrigem=form.querySelector('.url_origem');
if(urlOrigem){
urlOrigem.value=window.location.href;
}
form.addEventListener('submit', function (e){
e.preventDefault();
activeForm=form;
const formMessage=form.closest('.newsletter__grid').querySelector('.form__message');
function showMessage(html, success=false){
formMessage.style.display='block';
formMessage.innerHTML=html;
if(success){
setTimeout(()=> {
window.location.href='/obrigado/';
}, 5000);
}else{
setTimeout(()=> {
formMessage.style.display='none';
}, 5000);
}}
const checkbox=form.querySelector('.lgpd');
if(!checkbox||!checkbox.checked){
showMessage('<p style="color:red;">Você precisa aceitar os termos.</p>');
return;
}
if(typeof grecaptcha==='undefined'){
showMessage('<p style="color:red;">Erro ao carregar o reCAPTCHA.</p>');
return;
}
openRecaptchaModal();
});
});
const confirmBtn=document.getElementById('confirmRecaptcha');
if(!confirmBtn) return;
confirmBtn.addEventListener('click', function (){
const recaptchaResponse=grecaptcha.getResponse();
if(!recaptchaResponse){
alert('Confirme que você não é um robô.');
return;
}
closeRecaptchaModal();
if(!activeForm) return;
const formMessage=activeForm.closest('.newsletter__grid').querySelector('.form__message');
function showMessage(html, success=false){
formMessage.style.display='block';
formMessage.innerHTML=html;
if(success){
setTimeout(()=> {
window.location.href='/obrigado/';
}, 5000);
}else{
setTimeout(()=> {
formMessage.style.display='none';
}, 5000);
}}
const formData=new FormData(activeForm);
formData.append('g-recaptcha-response', recaptchaResponse);
fetch('https://terramundi.pinkandbrain.com/wp-content/themes/terramundi/inc/tema/rdstation-newsletter.php', {
method: 'POST',
body: formData
})
.then(res=> res.json())
.then(data=> {
if(data.success){
showMessage('<p class="mb0" style="color: var(--secundaria-3);">Inscrição realizada com sucesso!</p>', true);
activeForm.reset();
const formStart=activeForm.querySelector('.form__start');
if(formStart){
formStart.value=Math.floor(Date.now() / 1000);
}}else{
showMessage(`<p style="color:red;">Erro: ${data.error}</p>`);
console.error(data.details||data.error);
}
grecaptcha.reset();
activeForm=null;
})
.catch(err=> {
showMessage('<p style="color:red;">Erro inesperado. Tente novamente.</p>');
console.error(err);
grecaptcha.reset();
activeForm=null;
});
});
});
document.addEventListener("DOMContentLoaded", function(){
var lazyBackgrounds=[].slice.call(document.querySelectorAll(".lazy__image"));
if("IntersectionObserver" in window){
let lazyBackgroundObserver=new IntersectionObserver(function(entries, observer){
entries.forEach(function(entry){
if(entry.isIntersecting){
entry.target.classList.add("visible");
lazyBackgroundObserver.unobserve(entry.target);
}});
});
lazyBackgrounds.forEach(function(lazyBackground){
lazyBackgroundObserver.observe(lazyBackground);
});
}});
document.addEventListener("DOMContentLoaded", function (){
const header=document.querySelector(".header__group.fixed");
const menuTopo=document.querySelector(".menu__topo");
const progressBar=document.getElementById("progress-bar");
const btnMenuSearch=document.getElementById("btnMenuSearch");
let lastScrollTop=0;
const updateMenuPosition=()=> {
if(btnMenuSearch&&btnMenuSearch.classList.contains("active")){
header.style.top="0";
return;
}
const scrollTop=window.scrollY;
const menuHeight=menuTopo.offsetHeight + 2;
if(scrollTop > 120){
if(scrollTop > lastScrollTop){
header.style.top=`-${menuHeight}px`;
header.style.transition="top 0.3s ease-in-out";
}else{
header.style.top="0";
}}else{
header.style.top="0";
}
lastScrollTop=scrollTop;
};
window.addEventListener("scroll", updateMenuPosition);
});
function copyURL(){
var url=window.location.href;
var tempInput=document.createElement("input");
tempInput.value=url;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand ("copy");
document.body.removeChild(tempInput);
alert("URL copiada com sucesso!");
}
document.addEventListener('DOMContentLoaded', function (){
const postContent=document.querySelector('.content__single');
const progressBar=document.getElementById("progress-bar");
if(!postContent||!progressBar) return;
let isTracking=false;
const updateProgressBar=()=> {
if(!isTracking) return;
const contentHeight=postContent.scrollHeight;
const viewportHeight=window.innerHeight;
const scrollPosition=window.scrollY;
const postOffsetTop=postContent.offsetTop - 30;
const distanceScrolled=scrollPosition - postOffsetTop;
let scrollPercent=(distanceScrolled / (contentHeight - viewportHeight)) * 100;
scrollPercent=Math.max(0, Math.min(100, scrollPercent));
progressBar.style.width=scrollPercent + '%';
const contentBottom=postContent.getBoundingClientRect().bottom;
if(contentBottom <=0){
progressBar.classList.remove("active");
}else{
progressBar.classList.add("active");
}};
const checkStart=()=> {
const scrollPosition=window.scrollY;
const startPoint=postContent.offsetTop - 30;
if(scrollPosition >=startPoint){
if(!isTracking){
isTracking=true;
requestAnimationFrame(updateProgressBar);
}}else{
if(isTracking){
isTracking=false;
progressBar.classList.remove("active");
progressBar.style.width='0%';
}}
};
progressBar.classList.remove("active");
progressBar.style.width='0%';
window.addEventListener('scroll', ()=> {
checkStart();
updateProgressBar();
});
checkStart();
});
function getCookie(name){
const value=`; ${document.cookie}`;
const parts=value.split(`; ${name}=`);
if(parts.length===2) return parts.pop().split(";").shift();
return null;
}
function setCookie(name, value, days){
let expires="";
if(days){
const date=new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires="; expires=" + date.toUTCString();
}
document.cookie=`${name}=${value||""}${expires}; path=/`;
}
function updateFavoritesBadge(){
const badge=document.getElementById("favoritesBadge");
const btnFavoritos=document.getElementById("btnMenuFavoritos");
const favorites=document.cookie
.split("; ")
.find((row)=> row.startsWith("bookmarked_posts="));
let count=0;
if(favorites){
const value=favorites.split("=")[1];
const ids=value.split(",").filter(Boolean);
count=ids.length;
}
if(badge){
badge.textContent=count;
badge.style.display=count > 0 ? "inline-block":"none";
}
if(btnFavoritos){
if(count > 0){
btnFavoritos.classList.add("active");
}else{
btnFavoritos.classList.remove("active");
}}
}
document.addEventListener("DOMContentLoaded", function (){
const cookieName="bookmarked_posts";
let bookmarked=getCookie(cookieName);
let ids=[];
if(bookmarked){
ids=bookmarked.split(",").filter(Boolean);
}
updateFavoritesBadge();
const bookmarkButtons=document.querySelectorAll(".bookmark__icon_cards");
bookmarkButtons.forEach(function (button){
const postId=button.getAttribute("data-post-id");
if(ids.includes(postId)){
button.classList.add("bookmar_ativado");
}
button.addEventListener("click", function (event){
event.preventDefault();
event.stopPropagation();
const targetButton=event.currentTarget.closest(".bookmark__icon_cards");
const postId=targetButton.getAttribute("data-post-id");
const postTitle=targetButton.getAttribute("data-post-title");
let index=ids.indexOf(postId);
if(index > -1){
ids.splice(index, 1);
targetButton.classList.remove("bookmar_ativado");
alert("Postagem removida da sua lista de favoritos.");
}else{
ids.push(postId);
targetButton.classList.add("bookmar_ativado");
window.dataLayer=window.dataLayer||[];
dataLayer.push({
event: "favorito_adicionado",
post_id: postId,
post_title: postTitle
});
if(!localStorage.getItem("firstFavorite")){
alert("Postagem salva para ler mais tarde! Se você utilizar o mesmo dispositivo, poderá acessar os conteúdos marcados clicando no ícone de favoritos no menu do site.");
localStorage.setItem("firstFavorite", "true");
}else{
alert("Postagem salva para ler mais tarde.");
}}
setCookie(cookieName, ids.join(","), 3);
updateFavoritesBadge();
});
});
});
if(document.getElementById('toggleMode')||document.getElementById('themeToggle')||document.getElementById('themeToggle2')){
document.addEventListener("DOMContentLoaded", function (){
const toggleModeButton=document.getElementById("toggleMode");
const themeToggleCheckbox=document.getElementById("themeToggle");
const themeToggleCheckbox2=document.getElementById("themeToggle2");
const body=document.body;
const modeIcon=document.getElementById("modeIcon");
function getCookie(name){
const value=`; ${document.cookie}`;
const parts=value.split(`; ${name}=`);
if(parts.length===2) return parts.pop().split(';').shift();
return null;
}
function setCookie(name, value, days){
let expires="";
if(days){
const date=new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires="; expires=" + date.toUTCString();
}
document.cookie=`${name}=${value||""}${expires}; path=/`;
}
let savedTheme=getCookie("theme");
if(!savedTheme){
const prefersDark=window.matchMedia&&window.matchMedia('(prefers-color-scheme: dark)').matches;
savedTheme=prefersDark ? "dark":"light";
setCookie("theme", savedTheme, 365);
}
const isDarkMode=savedTheme==="dark";
body.classList.toggle("dark__mode", isDarkMode);
if(modeIcon){
modeIcon.src=isDarkMode
? "https://blog.nacionalinn.com.br//wp-content/themes/nacionalinn/img/icons/modo-leitura-noturno.svg"
: "https://blog.nacionalinn.com.br//wp-content/themes/nacionalinn/img/icons/modo-leitura.svg";
modeIcon.alt=isDarkMode ? "Modo Diurno":"Modo Noturno";
}
if(themeToggleCheckbox){
themeToggleCheckbox.checked=isDarkMode;
}
if(themeToggleCheckbox2){
themeToggleCheckbox2.checked=isDarkMode;
}
function toggleTheme(){
const isCurrentlyDark=body.classList.contains("dark__mode");
body.classList.toggle("dark__mode");
const newTheme=isCurrentlyDark ? "light":"dark";
setCookie("theme", newTheme, 365);
if(modeIcon){
modeIcon.src=newTheme==="dark"
? "https://blog.nacionalinn.com.br//wp-content/themes/nacionalinn/img/icons/modo-leitura-noturno.svg"
: "https://blog.nacionalinn.com.br//wp-content/themes/nacionalinn/img/icons/modo-leitura.svg";
modeIcon.alt=newTheme==="dark" ? "Modo Diurno":"Modo Noturno";
}
if(themeToggleCheckbox){
themeToggleCheckbox.checked=newTheme==="dark";
}
if(themeToggleCheckbox2){
themeToggleCheckbox2.checked=newTheme==="dark";
}}
if(toggleModeButton) toggleModeButton.addEventListener("click", toggleTheme);
if(themeToggleCheckbox) themeToggleCheckbox.addEventListener("change", toggleTheme);
if(themeToggleCheckbox2) themeToggleCheckbox2.addEventListener("change", toggleTheme);
});
};