document.addEventListener('DOMContentLoaded', function (){
function pad(n){
return n < 10 ? '0' + n:String(n);
}
function setupPlainSlider(rootSelector, slideSelector, prevSelector, nextSelector, counterSelector, intervalMs){
var root=document.querySelector(rootSelector);
if(!root) return;
var slides=Array.prototype.slice.call(root.querySelectorAll(slideSelector));
var prev=root.querySelector(prevSelector);
var next=root.querySelector(nextSelector);
var counters=Array.prototype.slice.call(root.querySelectorAll(counterSelector||''));
var active=0;
var autoTimer=null;
if(!slides.length) return;
function render(index){
active=index;
slides.forEach(function (slide, i){
slide.classList.toggle('is-active', i===index);
});
counters.forEach(function (counter){
counter.textContent=pad(index + 1);
});
}
function go(delta, manual){
render((active + delta + slides.length) % slides.length);
if(manual) restartAuto();
}
function startAuto(){
if(!intervalMs||slides.length < 2) return;
stopAuto();
autoTimer=setInterval(function (){ go(1, false); }, intervalMs);
}
function stopAuto(){
if(autoTimer) clearInterval(autoTimer);
autoTimer=null;
}
function restartAuto(){
stopAuto();
startAuto();
}
if(prev) prev.addEventListener('click', function (){ go(-1, true); });
if(next) next.addEventListener('click', function (){ go(1, true); });
root.addEventListener('mouseenter', stopAuto);
root.addEventListener('mouseleave', startAuto);
render(active);
startAuto();
}
function setupTypewriterTestimonials(){
var root=document.querySelector('[data-ramiro-book-testimonials]');
if(!root) return;
var slides=Array.prototype.slice.call(root.querySelectorAll('[data-ramiro-book-testimonial-slide]'));
var prev=root.querySelector('[data-ramiro-book-testimonials-prev]');
var next=root.querySelector('[data-ramiro-book-testimonials-next]');
var counters=Array.prototype.slice.call(root.querySelectorAll('[data-ramiro-book-testimonials-current]'));
var total=root.querySelector('[data-ramiro-book-testimonials-total]');
var active=0;
var timers=[];
var autoTimer=null;
var cursor=document.createElement('span');
cursor.className='ramiro-book-tw-cursor';
if(!slides.length) return;
if(total) total.textContent=pad(slides.length);
var data=slides.map(function (slide){
var textEl=slide.querySelector('[data-ramiro-book-testimonial-text]');
var authorEl=slide.querySelector('[data-ramiro-book-testimonial-author]');
return {
text: textEl ? textEl.textContent.trim():'',
author: authorEl ? authorEl.textContent.trim():''
};});
function clearTimers(){
timers.forEach(function (timer){ clearTimeout(timer); });
timers=[];
if(cursor.parentNode) cursor.parentNode.removeChild(cursor);
}
function stopAuto(){
if(autoTimer) clearTimeout(autoTimer);
autoTimer=null;
}
function scheduleAuto(){
stopAuto();
if(slides.length < 2) return;
autoTimer=setTimeout(function (){
render((active + 1) % slides.length, false);
}, 6500);
}
function typeInto(el, value, delay, done){
var i=0;
if(!el){
if(done) done();
return;
}
el.textContent='';
el.appendChild(cursor);
function tick(){
if(i <=value.length){
el.textContent=value.slice(0, i);
el.appendChild(cursor);
i++;
timers.push(setTimeout(tick, delay + Math.random() * 6));
}else{
if(cursor.parentNode===el) el.removeChild(cursor);
if(done) done();
}}
tick();
}
function render(index, manual){
clearTimers();
stopAuto();
active=index;
slides.forEach(function (slide, i){
slide.classList.toggle('is-active', i===index);
var textEl=slide.querySelector('[data-ramiro-book-testimonial-text]');
var authorEl=slide.querySelector('[data-ramiro-book-testimonial-author]');
if(textEl) textEl.textContent='';
if(authorEl) authorEl.textContent='';
});
counters.forEach(function (counter){
counter.textContent=pad(index + 1);
});
var currentSlide=slides[index];
var textEl=currentSlide.querySelector('[data-ramiro-book-testimonial-text]');
var authorEl=currentSlide.querySelector('[data-ramiro-book-testimonial-author]');
var item=data[index];
typeInto(textEl, item.text, 14, function (){
timers.push(setTimeout(function (){
typeInto(authorEl, item.author, 10, scheduleAuto);
}, 220));
});
}
function go(delta){
render((active + delta + slides.length) % slides.length, true);
}
if(prev) prev.addEventListener('click', function (){ go(-1); });
if(next) next.addEventListener('click', function (){ go(1); });
root.addEventListener('mouseenter', stopAuto);
root.addEventListener('mouseleave', scheduleAuto);
render(0, false);
}
setupTypewriterTestimonials();
setupPlainSlider(
'[data-ramiro-book-keypoints]',
'[data-ramiro-book-keypoint-slide]',
'[data-ramiro-book-keypoints-prev]',
'[data-ramiro-book-keypoints-next]',
'[data-ramiro-book-keypoints-current]',
7000
);
});