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

  function initInteractiveLinks($wrapper){
    var $items = $wrapper.find('.sifency-il-item');
    if(!$items.length) return;

    if($wrapper.hasClass('sifency-il-layout-split')){
      var firstImg = $items.first().data('image');
      if (firstImg) {
        $wrapper.find('.sifency-il-split-target').attr('src', firstImg);
      }
    }

    if($wrapper.hasClass('sifency-il-layout-center')){
      if(!$wrapper.find('.sifency-il-abs-img.active').length){
        $wrapper.find('.sifency-il-abs-img[data-index="0"]').addClass('active');
        $items.first().addClass('active');
      }
    }

    if($wrapper.hasClass('sifency-il-layout-background')){
      if(!$wrapper.find('.sifency-il-bg-item.active').length){
        $wrapper.find('.sifency-il-bg-item[data-index="0"]').addClass('active');
        $items.first().addClass('active');
      }
    }

    $items.off('mouseenter.sifencyIL').on('mouseenter.sifencyIL', function(){
      var $this = $(this);
      var index = $this.data('index');

      $items.removeClass('active');
      $this.addClass('active');

      if($wrapper.hasClass('sifency-il-layout-center')){
        $wrapper.find('.sifency-il-abs-img').removeClass('active');
        $wrapper.find('.sifency-il-abs-img[data-index="'+index+'"]').addClass('active');
      }

      if($wrapper.hasClass('sifency-il-layout-background')){
        $wrapper.find('.sifency-il-bg-item').removeClass('active');
        $wrapper.find('.sifency-il-bg-item[data-index="'+index+'"]').addClass('active');
      }

      if($wrapper.hasClass('sifency-il-layout-split')){
        var newImg = $this.data('image');
        var $target = $wrapper.find('.sifency-il-split-target');
        $target.css('opacity',0);
        setTimeout(function(){
          $target.attr('src', newImg);
          $target.css('opacity',1);
        },200);
      }
    });
  }

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

  $('.sifency-il-wrapper').each(function(){
    initInteractiveLinks($(this));
  });

});
