(function(){
'use strict';
var userCountry='Unknown', userCountryCode='', userDevice='Desktop', userBrowser='Unknown', userOS='Unknown', userScreen='', userLang='', userReferrer='';
var trackingQueue=[], isProcessing=false;
(function detectTech(){
var ua=navigator.userAgent;
if(/Mobile|Android|iPhone|iPad/i.test(ua)) userDevice=/iPad/i.test(ua) ? 'Tablet':'Mobile';
if(ua.indexOf("Firefox") > -1) userBrowser='Firefox';
else if(ua.indexOf("Edg") > -1) userBrowser='Edge';
else if(ua.indexOf("Chrome") > -1) userBrowser='Chrome';
else if(ua.indexOf("Safari") > -1) userBrowser='Safari';
if(ua.indexOf("Win") > -1) userOS='Windows';
else if(ua.indexOf("Mac") > -1) userOS=ua.indexOf("like Mac") > -1 ? 'iOS':'macOS';
else if(ua.indexOf("Android") > -1) userOS='Android';
userScreen=window.screen.width + 'x' + window.screen.height;
userLang=navigator.language||'N/A';
if(document.referrer){ try { userReferrer=new URL(document.referrer).hostname; } catch(e){ userReferrer=document.referrer; }}else{ userReferrer='Direct'; }})();
if(typeof prbAnalyticsData!=='undefined'){
fetch('https://ipapi.co/json/', { method: 'GET', mode: 'cors', cache: 'force-cache' }).then(function(r){ return r.json(); }).then(function(d){ if(d){ userCountry=d.country_name||'Unknown'; userCountryCode=d.country_code||''; }}).catch(function(){});
}
function processQueue(){
if(isProcessing||trackingQueue.length===0) return;
isProcessing=true;
var batch=trackingQueue.splice(0, 5);
batch.forEach(function(item){
var fd=new FormData();
fd.append('action', 'prb_track_event'); fd.append('nonce', item.nonce); fd.append('eventType', item.type);
fd.append('blockId', item.blockId); fd.append('postId', item.postId);
fd.append('country', userCountry); fd.append('countryCode', userCountryCode);
fd.append('browser', userBrowser); fd.append('os', userOS);
fd.append('device', userDevice); fd.append('screenRes', userScreen); fd.append('language', userLang);
fd.append('referrer', userReferrer);
if(item.abVariant) fd.append('abVariant', item.abVariant);
if(item.type==='click'){ fd.append('buttonIndex', item.index); fd.append('storeName', item.store); }
if(navigator.sendBeacon) navigator.sendBeacon(item.ajaxUrl, fd);
});
isProcessing=false;
if(trackingQueue.length > 0) setTimeout(processQueue, 1000);
}
function prbTrackView(blockId, postId){
if(typeof prbAnalyticsData==='undefined') return;
if(prbAnalyticsData.excludeAdmin) return;
var k='prb_viewed_' + blockId;
if(sessionStorage.getItem(k)) return;
sessionStorage.setItem(k, '1');
trackingQueue.push({ type: 'view', blockId: blockId, postId: postId, ajaxUrl: prbAnalyticsData.ajaxUrl, nonce: prbAnalyticsData.nonce });
processQueue();
}
document.addEventListener('click', function(e){
var btn=e.target.closest('.prb-track-click');
if(!btn||typeof prbAnalyticsData==='undefined') return;
if(prbAnalyticsData.excludeAdmin) return;
if(btn.classList.contains('prb-btn-oos')){ e.preventDefault(); return; }
e.preventDefault();
var url=btn.getAttribute('href');
trackingQueue.push({ type: 'click', blockId: btn.getAttribute('data-block-id'), postId: btn.getAttribute('data-post'), index: btn.getAttribute('data-index'), store: btn.getAttribute('data-store'), ajaxUrl: prbAnalyticsData.ajaxUrl, nonce: prbAnalyticsData.nonce, abVariant: btn.getAttribute('data-ab')||'A' });
processQueue();
setTimeout(function(){ window.open(url, '_blank', 'noopener,noreferrer'); }, 150);
}, true);
function initCountdowns(){
var countdowns=document.querySelectorAll('.prb-countdown');
countdowns.forEach(function(cd){
var endDate=cd.getAttribute('data-end'); if(!endDate) return;
var end=new Date(endDate).getTime(); if(isNaN(end)) return;
function update(){
var now=Date.now(); var diff=end - now;
if(diff <=0){ cd.querySelector('.prb-countdown-timer').innerHTML='<div style="color: #ef4444; font-weight: 700;">Offer Expired!</div>'; return; }
var days=Math.floor(diff / (1000 * 60 * 60 * 24)); var hours=Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes=Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); var seconds=Math.floor((diff % (1000 * 60)) / 1000);
var units=cd.querySelectorAll('.prb-countdown-num');
units.forEach(function(u){ var unit=u.getAttribute('data-unit'); if(unit==='days') u.textContent=String(days).padStart(2, '0'); else if(unit==='hours') u.textContent=String(hours).padStart(2, '0'); else if(unit==='minutes') u.textContent=String(minutes).padStart(2, '0'); else if(unit==='seconds') u.textContent=String(seconds).padStart(2, '0'); });
}
update(); setInterval(update, 1000);
});
}
function initStickyButtons(){
var stickyBtns=document.querySelectorAll('.prb-sticky-mobile-btn'); if(stickyBtns.length===0) return; if(window.innerWidth > 768) return;
stickyBtns.forEach(function(btn){
var card=btn.closest('.prb-card'); if(!card) return; btn.style.display='none';
function checkScroll(){ var rect=card.getBoundingClientRect(); btn.style.display=rect.bottom < 0 ? 'block':'none'; }
window.addEventListener('scroll', checkScroll, { passive: true });checkScroll();
});
}
function initEmailForms(){
var forms=document.querySelectorAll('.prb-email-form');
forms.forEach(function(form){
form.addEventListener('submit', function(e){
e.preventDefault();
var email=form.querySelector('input[name="email"]').value;
var targetPrice=form.querySelector('input[name="target_price"]').value;
var blockId=form.getAttribute('data-block-id');
var postId=form.getAttribute('data-post-id');
var msgDiv=form.querySelector('.prb-email-message');
var btn=form.querySelector('button[type="submit"]');
btn.disabled=true;
btn.textContent='Subscribing...';
msgDiv.textContent='';
msgDiv.className='prb-email-message';
var fd=new FormData();
fd.append('action', 'prb_subscribe_alert');
fd.append('nonce', prbAnalyticsData.subscribeNonce);
fd.append('email', email);
fd.append('blockId', blockId);
fd.append('postId', postId);
fd.append('targetPrice', targetPrice);
fetch(prbAnalyticsData.ajaxUrl, { method: 'POST', body: fd })
.then(function(r){ return r.json(); })
.then(function(res){
btn.disabled=false;
btn.textContent='Notify Me';
if(res.success){
msgDiv.textContent=res.data;
msgDiv.classList.add('prb-email-success');
form.reset();
}else{
msgDiv.textContent=res.data||'Error. Please try again.';
msgDiv.classList.add('prb-email-error');
}})
.catch(function(){
btn.disabled=false;
btn.textContent='Notify Me';
msgDiv.textContent='Network error.';
msgDiv.classList.add('prb-email-error');
});
});
});
}
if(document.readyState==='loading'){
document.addEventListener('DOMContentLoaded', function(){ initCountdowns(); initStickyButtons(); initEmailForms(); });
}else{
initCountdowns(); initStickyButtons(); initEmailForms();
}
window.prbTrackView=prbTrackView;
})();