(function(){
var tabWrap=document.querySelector('.reviews-tabs');
if(tabWrap){
tabWrap.addEventListener('click',function(e){
var btn=e.target.closest('.reviews-tab');
if(!btn)return;
var tabs=document.querySelectorAll('.reviews-tab');
for(var i=0;i<tabs.length;i++)tabs[i].classList.remove('active');
btn.classList.add('active');
var target=btn.getAttribute('data-target');
var sections=document.querySelectorAll('.reviews-section');
for(var j=0;j<sections.length;j++)sections[j].classList.remove('active');
document.getElementById(target).classList.add('active');
});
}
var ratingSelector=document.getElementById('rating-selector');
if(ratingSelector){
ratingSelector.addEventListener('click',function(e){
var star=e.target.closest('[data-rating]');
if(!star)return;
var val=parseInt(star.getAttribute('data-rating'));
currentRating=val;
document.getElementById('review-rating').value=val;
var stars=document.querySelectorAll('#rating-selector .rv-star');
for(var i=0;i<stars.length;i++){
stars[i].classList.toggle('filled',(i+1)<=val);
}
});
}
document.addEventListener('click',function(e){
var star=e.target.closest('.rv-vote-stars [data-star]');
if(!star)return;
var wrap=star.closest('.rv-vote-stars');
if(!wrap||wrap.classList.contains('voted'))return;
var rid=wrap.getAttribute('data-review-id');
var val=parseInt(star.getAttribute('data-star'));
if(!rid||isNaN(val))return;
wrap.classList.add('voted');
var stars=wrap.querySelectorAll('.rv-star');
for(var i=0;i<stars.length;i++){
stars[i].classList.toggle('filled',(i+1)<=val);
}
var badge=document.querySelector('.rv-vote-count-badge[data-review-id="'+rid+'"]');
if(badge){
var m=badge.textContent.match(/\d+/);
var c=m?parseInt(m[0]):0;
var n=c+1;
badge.textContent='('+n+' vote'+(n!==1?'s':'')+')';
}
if(typeof Bridge!=='undefined'){
Bridge.post('/api/router.php',{action:'vote_review',review_id:rid,vote_value:1}).catch(function(){});
}
});
})();
