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

  function animateBars($wrapper){
    var $bars = $wrapper.find('.sifency-vprog-fill');
    $bars.each(function(){
      var $bar = $(this);
      var h = $bar.data('height');
      $bar[0].offsetHeight;
      $bar.css('height', h + '%');
      $bar.closest('.sifency-vprog-item').addClass('is-animated');
    });
  }

  function initVerticalProgress($wrapper){
    if(!$wrapper.length) return;

    var observer = new IntersectionObserver(function(entries){
      entries.forEach(function(entry){
        if(entry.isIntersecting){
          animateBars($wrapper);
          observer.disconnect();
        }
      });
    }, { threshold: 0.2 });

    observer.observe($wrapper[0]);
  }

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

  $('.sifency-vprog-wrapper').each(function(){
    initVerticalProgress($(this));
  });

});
