(function($){
function wireCarousel($root){
var $wrap = ($root && $root.find ? $root.find('.my-carousel') : $('.my-carousel')).first();
if(!$wrap.length) return;
function bindWhenReady(){
var el = $wrap.find('.elementor-main-swiper, .swiper, .swiper-container').get(0) || $wrap.get(0);
var sw = el && (el.swiper || $wrap.data('swiper') || $(el).data('swiper'));
if(!sw || !sw.on){ setTimeout(bindWhenReady, 150); return; } // صبر تا Swiper آماده شود
var $cards = $root && $root.find ? $root.find('[data-slide]') : $('[data-slide]');
function setActive(i){ $cards.removeClass('active').filter('[data-slide="'+i+'"]').addClass('active'); }
// کلیک روی کارت → رفتن به اسلاید
$cards.off('click.goto').on('click.goto', function(e){
e.preventDefault();
var i = parseInt($(this).attr('data-slide'),10) || 0;
if (typeof sw.slideToLoop === 'function') sw.slideToLoop(i); // برای لوپ
else if (typeof sw.slideTo === 'function') sw.slideTo(i);
setActive(i);
});
// همگامسازی با اتوپلی/دکمهها
sw.on('slideChange', function(){
var i = (typeof sw.realIndex !== 'undefined') ? sw.realIndex : sw.activeIndex;
setActive(i);
});
// وضعیت اولیه
var start = (typeof sw.realIndex !== 'undefined') ? sw.realIndex : sw.activeIndex;
setActive(start);
}
bindWhenReady();
}
// وقتی المنتور ویجتها رو رندر کرد + یکبار هم روی صفحهی آماده
$(window).on('elementor/frontend/init', function(){
elementorFrontend.hooks.addAction('frontend/element_ready/global', wireCarousel);
});
$(function(){ wireCarousel($(document)); });
})(jQuery);
// کلیک کارت → کلیک روی بولت مربوطه
$(document).on('click','[data-slide]',function(e){
e.preventDefault();
var i = +$(this).attr('data-slide')||0;
$('.my-carousel .swiper-pagination-bullet').eq(i).trigger('click');
});