jQuery(document).ready(function($){

  function initNewsTicker($wrapper){
    var $swiperContainer = $wrapper.find('.sifency-nt-swiper');
    var $closeBtn        = $wrapper.find('.sifency-nt-close');

    if(!$swiperContainer.length) return;

    var settings = { autoplay:'yes', speed:4000, effect:'slide' };
    try {
      var raw = $wrapper.data('nt-settings');
      if(raw){ $.extend(settings, raw); }
    } catch(err){}

    var swiperOptions = {
      direction: 'vertical',
      loop: true,
      slidesPerView: 1,
      spaceBetween: 0,
      effect: settings.effect,
      autoplay: settings.autoplay === 'yes' ? {
        delay: settings.speed,
        disableOnInteraction: false,
        pauseOnMouseEnter: true
      } : false,
      navigation: {
        nextEl: $wrapper.find('.sifency-nt-next')[0],
        prevEl: $wrapper.find('.sifency-nt-prev')[0],
      }
    };

    if(settings.effect === 'fade'){
      swiperOptions.fadeEffect = { crossFade:true };
    }

    new Swiper($swiperContainer[0], swiperOptions);

    if($closeBtn.length){
      $closeBtn.off('click.sifencyNT').on('click.sifencyNT', function(){
        $wrapper.slideUp(300, function(){ $(this).remove(); });
      });
    }
  }

  $(document).on('mouseenter', '.sifency-nt-wrapper', function(){
    initNewsTicker($(this));
  });

  $('.sifency-nt-wrapper').each(function(){
    initNewsTicker($(this));
  });

});
