(function ($){
'use strict';
function markReady(){
document.body.classList.add('ct-ready');
}
if(document.readyState==='complete'){
setTimeout(markReady, 80);
}else{
window.addEventListener('load', function (){
setTimeout(markReady, 120);
});
}
function getCookie(n){
var m=document.cookie.match(new RegExp('(?:^|;)' + n + '=([^;]*)'));
return m ? decodeURIComponent(m[1]):'';
}
function setCookie(n, v, days){
days=days===undefined ? 365:days;
var d=new Date();
d.setTime(d.getTime() + days * 864e5);
document.cookie=n + '=' + encodeURIComponent(v) + ';expires=' + d.toUTCString() + ';path=/';
}
var header=document.getElementById('ct-header');
if(header){
function onScroll(){
header.classList.toggle('scrolled', window.scrollY > 20);
}
window.addEventListener('scroll', onScroll, { passive: true });
onScroll();
}
var menuToggle=document.getElementById('ct-menu-toggle');
var nav=document.getElementById('ct-nav');
if(menuToggle&&nav){
menuToggle.addEventListener('click', function (){
nav.classList.toggle('open');
});
document.addEventListener('click', function (e){
if(!nav.contains(e.target)&&e.target!==menuToggle){
nav.classList.remove('open');
}});
}
var heroBg=document.querySelector('.ct-hero-bg');
if(heroBg){
var bgDelay=document.readyState==='complete' ? 200:350;
window.addEventListener('load', function (){
setTimeout(function (){ heroBg.classList.add('loaded'); }, bgDelay);
});
if(document.readyState==='complete'){
setTimeout(function (){ heroBg.classList.add('loaded'); }, bgDelay);
}}
function initCardReveal(){
var cards=document.querySelectorAll('.ct-grid .ct-card, .ct-similar-grid .ct-card');
if(!cards.length) return;
cards.forEach(function (card, i){
card.dataset.i=i % 8;
});
function revealCard(card){
var delay=parseInt(card.dataset.i, 10) * 90;
setTimeout(function (){
card.classList.add('ct-visible');
}, delay);
}
function startReveal(){
if(!('IntersectionObserver' in window)){
cards.forEach(revealCard);
return;
}
var io=new IntersectionObserver(function (entries){
entries.forEach(function (entry){
if(entry.isIntersecting){
io.unobserve(entry.target);
revealCard(entry.target);
}});
}, { threshold: 0.01, rootMargin: '0px 0px 80px 0px' });
cards.forEach(function (card){ io.observe(card); });
}
if(document.body.classList.contains('ct-ready')){
startReveal();
}else{
var poll=setInterval(function (){
if(document.body.classList.contains('ct-ready')){
clearInterval(poll);
startReveal();
}}, 40);
}}
var audio=document.getElementById('ct-audio');
var playBtn=document.getElementById('ct-play-btn');
if(!audio||!playBtn){ initCardReveal(); return; }
var prevBtn=document.getElementById('ct-prev');
var nextBtn=document.getElementById('ct-next');
var shuffBtn=document.getElementById('ct-shuffle');
var volSlide=document.getElementById('ct-volume');
var savedVol=parseFloat(getCookie('ct_volume'))||0.28;
audio.volume=Math.min(1, Math.max(0, savedVol));
if(volSlide){
volSlide.value=Math.round(audio.volume * 100);
volSlide.addEventListener('input', function (){
audio.volume=this.value / 100;
setCookie('ct_volume', audio.volume);
});
}
var shuffle=getCookie('ct_shuffle')==='on';
audio.loop  = !shuffle;
if(shuffBtn){
shuffBtn.classList.toggle('active', shuffle);
shuffBtn.addEventListener('click', function (){
shuffle    = !shuffle;
audio.loop = !shuffle;
setCookie('ct_shuffle', shuffle ? 'on':'off');
this.classList.toggle('active', shuffle);
});
}
var hasStarted=false;
function setLoading(on){ playBtn.classList.toggle('loading', on); }
function setPlaying(on){
playBtn.classList.remove('loading');
playBtn.classList.toggle('playing', on);
}
function doPlay(){
setLoading(true);
if(audio.readyState===0){
audio.load();
}
var p=audio.play();
if(p&&p.then){
p.then(function (){
hasStarted=true;
setPlaying(true);
showTipSequence(['For better experience', 'Use headphones']);
var cycleDelay=(window.CTF_Settings&&CTF_Settings.title_cycle_secs) ? parseInt(CTF_Settings.title_cycle_secs,10)*1000:34000;
setTimeout(startTitleCycle, cycleDelay);
}).catch(function (){ setLoading(false); });
}else{
hasStarted=true; setPlaying(true);
}}
if(playBtn){
playBtn.addEventListener('mouseenter', function (){
if(audio.readyState===0){
audio.preload='metadata';
audio.load();
}}, { once: true, passive: true });
playBtn.addEventListener('touchstart', function (){
if(audio.readyState===0){
audio.preload='metadata';
audio.load();
}}, { once: true, passive: true });
}
function togglePlay(){
if(!hasStarted)    doPlay();
else if(audio.paused){ audio.play(); setPlaying(true); }else{ audio.pause(); setPlaying(false); }}
playBtn.addEventListener('click', togglePlay);
document.addEventListener('keydown', function (e){
if(e.code==='Space'&&e.target.tagName!=='INPUT'&&e.target.tagName!=='TEXTAREA'){
e.preventDefault(); togglePlay();
}
if([123, 85, 73, 89].includes(e.keyCode)) e.preventDefault();
});
document.addEventListener('contextmenu', function (e){ e.preventDefault(); });
if(getCookie('ct_np_click')==='on'){
setCookie('ct_np_click', 'off', 1);
setTimeout(doPlay, 500);
}else if(shuffle&&window.CT&&!CT.is_ios){
setTimeout(doPlay, 1200);
}
[prevBtn, nextBtn].forEach(function (btn){
if(btn) btn.addEventListener('click', function (){
if(this.getAttribute('href')==='#') return;
if(hasStarted&&!audio.paused){
setCookie('ct_np_click', 'on', 1);
}else{
setCookie('ct_np_click', 'off', 1);
}});
});
audio.addEventListener('ended', function (){
if(!audio.loop&&nextBtn&&nextBtn.getAttribute('href')!=='#') nextBtn.click();
});
var msgEl=document.getElementById('ct-message');
function showMsg(text, cb){
if(!msgEl){ cb&&cb(); return; }
msgEl.style.transition='opacity 1.6s ease';
msgEl.style.opacity='0';
setTimeout(function (){
msgEl.textContent=text;
msgEl.style.opacity='1';
setTimeout(cb||function(){}, 1600);
}, 1600);
}
function hideMsg(cb){
if(!msgEl){ cb&&cb(); return; }
msgEl.style.transition='opacity 1.2s ease';
msgEl.style.opacity='0';
setTimeout(function (){
msgEl.textContent='\u00a0';
cb&&cb();
}, 1200);
}
function showTipSequence(tips, cb){
if(!tips.length){ hideMsg(cb); return; }
showMsg(tips[0], function (){
setTimeout(function (){
hideMsg(function (){
setTimeout(function (){ showTipSequence(tips.slice(1), cb); }, 400);
});
}, 3800);
});
}
function startTitleCycle(){
var messages=['CalmTweets', 'Calm Your Soul'];
var i=0;
function nextMsg(){
if(i >=messages.length){
hideMsg(function (){ setTimeout(startTitleCycle, 34000); });
return;
}
showMsg(messages[i++], function (){
setTimeout(nextMsg, 4000);
});
}
nextMsg();
}
initCardReveal();
var copyBtn=document.getElementById('ct-copy-link');
if(copyBtn){
copyBtn.addEventListener('click', function(){
var label=document.getElementById('ct-copy-label');
navigator.clipboard.writeText(window.location.href).then(function(){
copyBtn.classList.add('copied');
if(label) label.textContent='Copied!';
setTimeout(function(){
copyBtn.classList.remove('copied');
if(label) label.textContent='Copy Link';
}, 2500);
}).catch(function(){
var ta=document.createElement('textarea');
ta.value=window.location.href;
document.body.appendChild(ta);
ta.select();
document.execCommand ('copy');
document.body.removeChild(ta);
if(label){ label.textContent='Copied!'; setTimeout(function(){ label.textContent='Copy Link'; }, 2500); }});
});
}
var progressWrap=document.getElementById('ct-progress-wrap');
var progressBar=document.getElementById('ct-progress-bar');
if(audio){
audio.addEventListener('timeupdate', function(){
if(!audio.duration) return;
var pct=(audio.currentTime / audio.duration) * 100;
if(progressBar) progressBar.style.width=pct + '%';
if(progressWrap) progressWrap.setAttribute('aria-valuenow', Math.round(pct));
});
if(progressWrap){
progressWrap.addEventListener('click', function(e){
if(!audio.duration) return;
var rect=progressWrap.getBoundingClientRect();
var pct=(e.clientX - rect.left) / rect.width;
audio.currentTime=pct * audio.duration;
});
}}
}(jQuery));