/*
JS/jQUERY FOR LAYOUT 460
Steve @ Build Your Firm
4/4/2019
*/


// I. 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();
  });
});


// II. 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();
  });
});


// III. CAROUSEL SLIDER SPEED
$(function() {
  $("#myCarousel, #myTestimonials").carousel({
    interval: 4000
  });
});


// IV. 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").click(function() {
  $("html,body").animate({scrollTop: 0 }, "1000");
  return false;
});


// V. NAVBAR SPY
$(".navbar-nav .nav-item, .navbar-nav .nav-item *").mouseover(function() {

  if($(this).is(".navbar-nav .nav-item")) {
    var currentLink = $(this);
  } else {
    var currentLink = $(this).closest("li");
  }

  // calculate offsets
  var leftOffset = currentLink.offset().left - $(".navbar-collapse").offset().left + 5;
  var rightOffset = $(".navbar-collapse").width() - (leftOffset + currentLink.width()) + 8;

  // remove if no highlight
  if(currentLink.is(':last-of-type')) {
    leftOffset = leftOffset + 80;
    rightOffset = rightOffset + 80;
  }

  // set position
  $("#navbar-spy").css({"left": leftOffset, "right": rightOffset, "bottom": 0});

});

$(".navbar-nav").mouseleave(function() {
  $("#navbar-spy").css({"bottom": "-6px"});
  setTimeout(function() {
    $("#navbar-spy").css({"left": "", "right": ""});
  }, 205);
});


// VI. SERVICE SECTION SPY
$("#services a").mouseover(function() {
  var currentLink = $(this);

  $("#service-spy").css({"opacity": 1});

  // calculate offsets
  var leftOffset = currentLink.offset().left - $("#service-track").offset().left;
  var rightOffset =  $("#service-track").width() - (leftOffset + currentLink.children(".service").outerWidth());
  var topOffset = currentLink.offset().top - $("#service-track").offset().top;

  // calculate height
  var spyHeight = currentLink.children(".service").outerHeight();
  // set position and height
  $("#service-spy").css({"left": leftOffset, "right": rightOffset, "top": topOffset, "height": spyHeight});
});

$("#services a").mouseleave(function() {
  $("#service-spy").css({"opacity": 0});
});


// VII. NORMALIZE TESTIMONIALS SLIDER HEIGHTS
$(window).on('load', function() {
  normalizeHeights();
});

$(window).on('resize orientationchange', function () {
  var items = $('#myTestimonials .carousel-item');
  items.each(function() {
    $(this).css('height', 'auto');
  });
  normalizeHeights();
});

function normalizeHeights() {
  var items = $('#myTestimonials .carousel-item'),	heights = [],	tallest;
  items.each(function() {
    heights.push($(this).height());
  });
  tallest = Math.max.apply(null, heights);
  items.each(function() {
    $(this).css('height',tallest + 'px');
  });
}

document.addEventListener( 'DOMContentLoaded', () => {

    // Handle high contrast toggle button click
    document.querySelector( '#hc-toggle' ).addEventListener( 'click', function() {
      if ( !document.body.classList.contains( 'hcm' ) ) {
        document.body.classList.add( 'hcm' );
        byf_set_cookie( 'byf_hc_mode', '1', 14 );
      } else {
        document.body.classList.remove( 'hcm' );
        byf_set_cookie( 'byf_hc_mode', '0', 14 );
      }
      this.blur();
    });
  
    // Set high contrast mode if set in cookie
    function byf_set_hc_mode() {
      let byf_hc_mode = byf_get_cookie( 'byf_hc_mode' );
      if ( byf_hc_mode == 1 ) {
        document.body.classList.add( 'hcm' );
      }
    }
  
    // Check for cookie on page load
    byf_set_hc_mode();
  
  
    // Generic get cookie
    function byf_get_cookie( cname ) {
      var name = cname + '=';
      var decodedCookie = decodeURIComponent(document.cookie);
      var ca = decodedCookie.split( ';' );
      for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
          c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length);
        }
      }
      return '';
    }
  
    // Generic set cookie
    function byf_set_cookie( cname, cvalue, exdays ) {
      var d = new Date();
      d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
      var expires = 'expires=' + d.toUTCString();
      document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
    }
  
  
    // Event listeners for updating 'aria-expanded' property for dropdowns when using keyboard navigation
    document.querySelectorAll( '.nav-item' ).forEach( navlink => {
  
      navlink.addEventListener( 'click', function(e) {
        if ( navlink.parentNode.classList.contains( 'show' ) ) {
          return;
        }
        byf_set_aria( navlink );
      });
  
      navlink.addEventListener( 'keyup', function(e) {
        if ( e.keyCode != 9 || navlink.getAttribute( 'aria-expanded' ) == 'true' ) {
          return;
        }
        byf_set_aria( navlink );
      });
  
    });
  
    // Helper function to update aria-expanded property
    function byf_set_aria( navlink ) {
      document.querySelectorAll( 'button.nav-link' ).forEach( link => {
        link.setAttribute( 'aria-expanded', 'false' );
      });
  
      if ( navlink.tagName == 'BUTTON' ) {
        navlink.setAttribute( 'aria-expanded', 'true' );
      }
    }
    
    
    // CAROUSEL SLIDE, FOCUS NEXT SLIDE
    let slides = document.querySelectorAll('.carousel-item');
    let slide_count = slides.length;
    let slide_index = 0;
    
    document.querySelectorAll('.carousel-control-next').forEach( next_btn => {
        next_btn.addEventListener( 'click', () => {
          slide_index = (slide_index + 1 == slide_count) ? 0 : slide_index + 1;
          focus_slide();
        });
    });
    
      document.querySelectorAll('.carousel-control-prev').forEach( prev_btn => {
        prev_btn.addEventListener( 'click', () => {
          slide_index = (slide_index - 1 < 0) ? (slide_count - 1) : (slide_index - 1); 
          focus_slide();
        });
    });
    
    function focus_slide() {
       // console.log(slide_index);
    }
    
  //   $("#myCarousel").on('slide.bs.carousel', function () {
  //     slides[slide_index].querySelector('h2').removeAttribute('tabindex');
  //     slide_index = (slide_index + 1 == slide_count) ? 0 : slide_index + 1;
  //     slides[slide_index].querySelector('h2').setAttribute('tabindex', '0');
  //     slides[slide_index].querySelector('h2').focus();
  //   });
    
    
  });
  
