/*
JS/jQUERY FOR LAYOUT 420
Steve @ Build Your Firm
4/20/2018
*/

// I. SHOW THE OVERLAY MENU AND POSITION THE CLOSE BUTTON
$('#toggle-open').click(function (e) {
  e.preventDefault();

  // 1. set container height
  var $containerHeight = $('#header-logo').outerHeight();
  $('#overlay-menu > .container').css({"height": $containerHeight + "px"});

  // 2. position the close button
  var $scrollOffset = $(window).scrollTop();
  var $toggleOpenOffset = $('#toggle-open i').offset().top;
  var $toggleCloseOffset = ($toggleOpenOffset - $scrollOffset);
  $('#toggle-close i').css({'top': $toggleCloseOffset});

  // 3. show the overlay menu
  $("#overlay-menu").css({'opacity': '1', 'visibility': 'visible'});
});


// II. HIDE THE OVERLAY MENU
$('#toggle-close').click(function (e) {
  e.preventDefault();
  $("#overlay-menu").css({'opacity': '0', 'visibility': 'hidden'});
});


// III. PARALLAX BACKGROUND SCROLL
$(function() {

  // 1. function to set parallax background position
  function setParallax() {
    var offset;
    var scroll = $(document).scrollTop();
    $('.parallax-bg').each(function() {
      if($(this).data("offset")) {
        offset = $(this).data("offset");
      } else {
        offset = 0;
      }
      var scrollOffset = $(this).offset().top;
      var yScroll = .2*(scroll - scrollOffset) - offset + "px";
      $(this).css({'background-position':'50% ' + yScroll});
    });
  }

  // 2. set parallax background position on page load
  setParallax();

  // 3. set parallax background position on page scroll
  $(window).on('scroll', function () {
    setParallax();
  });
});


// IV. FIXED NAVBAR ON SCROLL
$(function() {
  var navpos = $('#header-logo').offset();
  // 1. function to set fixed nav
  function fixNav() {
    if ($(window).scrollTop() > navpos.top) {
      $('#header-logo').addClass('fixed-top');
      var $headerHeight = $('#header-logo').outerHeight() + "px";
      $('header').css({'paddingBottom': $headerHeight});
    } else {
      $('#header-logo').removeClass('fixed-top');
      $('header').css({'paddingBottom': 0});
    }
  }
  // 2. set fixed nav on page load
  fixNav();
  // 3. set fixed nav on page scroll
  $(window).bind('scroll', function() {
    fixNav();
  });
});


// V. CAROUSEL SLIDER SPEED
$(function() {
  $("#myCarousel").carousel({
    interval: 4000
  });
});


// VI. SCROLL TO TOP
$(window).scroll(function() {
  if ($(this).scrollTop() > 150 ) {
    $("#scroll-icon").css({opacity: "1", visibility: "visible"});
  } else {
    $("#scroll-icon").css({opacity: "0", visibility: "hidden"});
  }
});

$("#scroll-icon .fa").click(function() {
  $("html,body").animate({scrollTop: 0 }, "1000");
  return false;
});


// VII. HEADLINER
$(function() {

  var $isIndex;
  if ( window.location.pathname == '/' || window.location.pathname.split('/').pop() == 'index.htm') {
    $isIndex = true;
  }

  if(!$isIndex) {
    var $heading = $("h1:first-of-type").text();
    if ($heading) {
      var $parentDiv = $("h1:first-of-type").closest(".container");
      $("h1:first-of-type").remove();
      $("<div id = 'headline'><div class = 'container'><h1>" + $heading + "</h1></div></div>").insertBefore($parentDiv);
    }
  }
});
