jQuery( document ).ready(function($) {
  function loadScript(url, callback) {
    // Adding the script tag to the head as suggested before
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    // Then bind the event to the callback function.
    // There are several events for cross browser compatibility.
    script.onreadystatechange = callback;
    script.onload = callback;

    // Fire the loading
    head.appendChild(script);
  }

  $("link[media=\"1\"]").attr("media", "all"); // load css async

  if(typeof $().Stickyfill == "undefined") {
    loadScript("/wp-content/themes/ams_2016/dist/stickyfill/stickyfill.min.js", function() {
      $('#nav').Stickyfill(); // NAV
    });
  } else {
    $('#nav').Stickyfill(); // NAV
  }

  $(document).on("click", "nav.navbar #ams-topnav[aria-expanded=\"true\"] a", function() {
    $('.navbar-toggler').click();
  });

  $(document).on('click', 'a.scroll-link[href^="#"]', function(e){
    e.preventDefault();

    $('html, body').animate({
      scrollTop: $( $.attr(this, 'href') ).offset().top
    }, 500);
  });

  /** TdA Carousel **/

  if(typeof $().slick == "undefined") {
    loadScript("/wp-content/themes/ams_2016/dist/slick/slick.min.js", function() {
      $('.owl-carousel').slick({
          dots: false,
          infinite: true,
          variableWidth: true,
          autoplay: true,
          lazyLoad: 'progressive',
          swipeToSlide: true,
          waitForAnimate: false,
          nextArrow: '<span class="icon-next" aria-hidden="true"><i class="fa fa-chevron-right" aria-hidden="true"></i></span>',
          prevArrow: '<span class="icon-prev" aria-hidden="true"><i class="fa fa-chevron-left" aria-hidden="true"></i></span>',
      });
    });
  } else {
    $('.owl-carousel').slick({
        dots: false,
        infinite: true,
        variableWidth: true,
        autoplay: true,
        lazyLoad: 'progressive',
        swipeToSlide: true,
        waitForAnimate: false,
        nextArrow: '<span class="icon-next" aria-hidden="true"><i class="fa fa-chevron-right" aria-hidden="true"></i></span>',
        prevArrow: '<span class="icon-prev" aria-hidden="true"><i class="fa fa-chevron-left" aria-hidden="true"></i></span>',
    });
  }
});