/*
LAYOUT 510 - THE GRISWOLD
Steve @ Build Your Firm
10/12/2019
*/

$(document).ready(function() {

  // I. CHECK FOR MOBILE SIZE
  function isMobile() {
    return $("#mobile-indicator").is(":visible");
  }

  // II. FIXED NAVBAR
  var navpos = $('#nav-menu .navbar-nav').offset();
  if($('#myCarousel').length) {
    var hero = $('#myCarousel');
  } else {
    var hero = $('#interior-hero');
  }

  function fixNav() {
    if ($(window).scrollTop() >= 400) {
      var navHeight = $('#nav-menu').outerHeight() + "px";
      $('header').addClass('fixed');
      hero.css({'margin-top': navHeight});
    }
    if ($(window).scrollTop() < navpos.top || $(window).scrollTop() == 0) {
      $('header').removeClass('fixed');
      hero.css({'margin-top': "0"});
    }
  }
  fixNav();
  $(window).bind('scroll', function() {
    fixNav();
  });


  // III. PARALLAX BACKGROUND SCROLL
  function setParallax() {
    var scroll = $(window).scrollTop(); // top of the viewport
    $('.parallax-bg').each(function() {
      var theElement = $(this);
      var elementTop = theElement.offset().top;
      var elementBottom = theElement.offset().top + theElement.outerHeight();
      var viewportBottom = scroll + $(window).innerHeight();
      if ((viewportBottom > elementTop) && (scroll < elementBottom)){
        var yScroll = .12*(scroll - elementTop)  + "px";
        theElement.css({'background-position':'50% ' + 'calc(50% - ' + yScroll + ')' });
      }
    });
  }

  $(window).on('scroll', function () {
    if(!isMobile()) {
      setParallax();
    }
  }).scroll();


  // IV. CAROUSEL FUNCTIONS
  //
  // For smoother transition & animations, pause
  // carousels when they're scrolled out of view
  var carouselBottom, screenBottom, carouselTop, screenTop, carousels = $("#myCarousel, #myTestimonials")
  $(window).on('scroll', function () {
    carousels.each(function() {
      var theCarousel = $(this);
      if(theCarousel.length) {
        carouselTop = theCarousel.offset().top;
        carouselBottom = theCarousel.offset().top + theCarousel.outerHeight();
        screenBottom = $(window).scrollTop() + $(window).innerHeight();
        screenTop = $(window).scrollTop();
        if ((screenBottom > carouselTop) && (screenTop > carouselBottom)) {
          theCarousel.carousel('pause');
        } else {
          if($('.email-field').val().length === 0 && (!$('.email-field').is(":focus"))) {
            carousel.carousel('cycle');
            console.log("Scrolled back into view");
          }
        }
      }
    });
  });

  // Carousel Speed
  var carousel = $("#myCarousel");
  $("#myCarousel, #myTestimonials").carousel({
    interval: 3500
  });

  // Pause carousel when focused
  $('.email-field').focus(function () {
    carousel.carousel('pause');
  });

   // Restart carousel on hover out
  $("#myCarousel").bind("mouseout", function() {
    if($('.email-field').val() == "" && (!$('.email-field').is(":focus"))) {
      carousel.carousel('cycle');
    }
  });

});
