/*
JS/jQUERY FOR PAGE SCROLL ANIMATIONS
Steve @ Build Your Firm
3/7/2018
*/

// I. ANIMATE PAGE ELEMENTS ON SCROLL
$(function() {
  var $elements = $(".animate-left, .animate-right, .animate-up, .animate-down, .animate-down-left, .animate-down-right, .animate-up-left, .animate-up-right, .bounce, .bounceIn, .bounceInDown, .bounceInLeft, .bounceInRight, .bounceInUp, .bounceOut, .bounceOutDown, .bounceOutLeft, .bounceOutRight, .bounceOutUp, .fadeIn, .fadeInDown, .fadeInDownBig, .fadeInLeft, .fadeInLeftBig, .fadeInRight, .fadeInRightBig, .fadeInUp, .fadeInUpBig, .fadeOut, .fadeOutDown, .fadeOutDownBig, .fadeOutLeft, .fadeOutLeftBig, .fadeOutRight, .fadeOutRightBig, .fadeOutUp, .fadeOutUpBig, .flash, .flipInX, .flipInY, .flipOutX, .flipOutY, .headShake, .heartBeat, .hinge,.jackInTheBox,.jello,.lightSpeedIn,.lightSpeedOut,.pulse,.rollIn,.rollOut,.rotateIn,.rotateInDownLeft,.rotateInDownRight,.rotateInUpLeft,.rotateInUpRight,.rotateOut,.rotateOutDownLeft,.rotateOutDownRight,.rotateOutUpLeft,.rotateOutUpRight,.rubberBand,.shake,.slideInDown,.slideInLeft,.slideInRight,.slideInUp,.slideOutDown,.slideOutLeft,.slideOutRight,.slideOutUp,.swing,.tada,.wobble,.zoomIn,.zoomInDown,.zoomInLeft,.zoomInRight,.zoomInUp,.zoomOut,.zoomOutDown,.zoomOutLeft,.zoomOutRight,.zoomOutUp");
  var $window = $(window);

  // 1. function to animate the elements
  function animate() {
    $elements.each(function(i, elem) {
      if ($(this).hasClass('animated'))
      return;
      animateMe($(this));
    });
  }

  // 2. add "animated" class
  function animateMe(elem) {
    var winTop = $(window).scrollTop();
    var winBottom = winTop + $(window).height();
    var elemTop = $(elem).offset().top - 35;
    if ((elemTop <= winBottom) && (elemTop >= winTop)) {
      $(elem).addClass('animated');
    }
  }

  // 3. animate elements visible on page load
  animate();

  // 4. animate elements visible on scroll
  $window.on('scroll', function(e) {
    animate();
  });

});
