
jQuery( document ).ready(function() {
  AOS.init();

  /**
   * Navigation
   * @type {boolean}
   */
  var open = false,
    subNavContainer = document.querySelector('.sub-navigation-container'),

    listElement = Array.prototype.slice.call(document.querySelectorAll('ul.sub-navigation > li')),
    sortedMobile = false;

  document.querySelector('.hamburger-menu').addEventListener('click', function (e) {
    open = !open;
    var ulSubNav = document.querySelector('ul.sub-navigation');
    subNavContainer.classList.toggle("height-auto--force");

    if(ulSubNav.classList.contains('open')) {
      subNavContainer.style.height = "40px";
      setTimeout(function () {
        ulSubNav.classList.remove('open');
      }, 800)
    } else {
      ulSubNav.classList.add('open');
    }

    if (open) {
      var height = listElement.reduce(function (sum, element) {
        return sum + element.offsetHeight + 6;
      }, 0);

      subNavContainer.style.height = height + (listElement[0].offsetHeight * 1.5) + "px";
      return;
    }
    subNavContainer.style.height = "40px";
  });


  window.addEventListener('resize', function (e) {
    var ulSubNav = document.querySelector('ul.sub-navigation');

    if (window.outerWidth > 768) {
      subNavContainer.style.height = "40px";
      ulSubNav.classList.remove('open');
      sortedMobile = false;
      open = false;

      sortList(document.querySelector('ul.sub-navigation'), 'orderDesktop');

      if (ulSubNav.classList.value.indexOf('center-vertically') === -1) {
        ulSubNav.classList.add('center-vertically');
      }

      sortList(document.querySelector('ul.sub-navigation'), 'orderDesktop')
    }

    if (window.outerWidth <= 767 && !sortedMobile) {
      sortMobile();
    }
  });

  function sortMobile() {
    sortedMobile = true;
    sortList(document.querySelector('ul.sub-navigation'), 'orderMobile')
  }

  if (window.outerWidth <= 720) {
    sortMobile();
  }

  Array.prototype.slice.call(document.querySelectorAll('.timeline-section')).forEach(function(section){
    if (!section){
      return;
    }
    var lineOne  = document.createElement('div');
    var lineTwo = document.createElement('div');
    var circle = document.createElement('div');
    lineOne.classList = 'line top';
    lineTwo.classList = 'line bottom';
    circle.className = 'center-vertically timeline-dot';
    section.appendChild(circle);


    lineOne.style.height = (section.offsetHeight/2) - (circle.offsetHeight / 2);
    lineTwo.style.height = (section.offsetHeight/2) - (circle.offsetHeight / 2);



    section.appendChild(lineOne);
    section.appendChild(lineTwo);

  });

  //


  function scrollToTop(scrollDuration) {
    const scrollHeight = window.scrollY,
      scrollStep = Math.PI / ( scrollDuration / 15 ),
      cosParameter = scrollHeight / 2;
    var scrollCount = 0,
      scrollMargin,
      scrollInterval = setInterval(function () {
        if (window.scrollY != 0) {
          scrollCount = scrollCount + 1;
          scrollMargin = cosParameter - cosParameter * Math.cos(scrollCount * scrollStep);
          window.scrollTo(0, ( scrollHeight - scrollMargin ));
        }
        else clearInterval(scrollInterval);
      }, 15);
  }

  function sortList(ul, type) {
    var new_ul = ul.cloneNode(false);

    // Add all lis to an array
    var lis = [];
    for (var i = ul.childNodes.length; i--;) {
      if (ul.childNodes[i].nodeName === 'LI')
        lis.push(ul.childNodes[i]);
    }

    // Sort the lis in descending order
    lis.sort(function (a, b) {
      console.log(a.dataset[type]);
      return a.dataset[type] - b.dataset[type]
    });

    // Add them into the ul in order
    for (var i = 0; i < lis.length; i++) {
      new_ul.appendChild(lis[i]);
    }

    ul.parentNode.replaceChild(new_ul, ul);
  }

  function imageReplace(e) {
    var width = e.target.outerWidth,
      image = document.querySelector('.backdrop-img');

    if (!width) {
      width = window.outerWidth;
    }

    if (width <= 692 && image.src !== 'https://s3.eu-west-2.amazonaws.com/ga-transformation/images/images/banner-strip.jpg') {
      image.src = 'https://s3.eu-west-2.amazonaws.com/ga-transformation/images/images/banner-strip-mobile.jpg';
      return;
    }

    if (image.src !== 'https://s3.eu-west-2.amazonaws.com/ga-transformation/images/images/banner-strip.jpg') {
      image.src = 'https://s3.eu-west-2.amazonaws.com/ga-transformation/images/images/banner-strip-mobile.jpg';
    }
  }


  window.onload = function (e) {
    imageReplace(e);
  };

  window.addEventListener('resize', function (e) {
    imageReplace(e)
  });
});
