//21-06-22
/* ---------- Scroll Button C-scroll-button-v1 Scripts ---------- */

// Add sections to scroll to by adding the "scroll-section" class and an ID to the element

// Scrolls the page to the next scroll-section when scroll-button is clicked
let scrollID = 0;
let sectionIDs = [];
let scrollSections = $('.scroll-section');
const topBarHeightOffset = $('.top-bar').outerHeight();

function ScrollToId(elementId) {
  const scrollOptions = {
    top: $(elementId).attr('id') == 'top' ? $(elementId).offset().top : $(elementId).offset().top - topBarHeightOffset,
    behavior: 'smooth'
  };

  window.scrollTo(scrollOptions);
}

$(document).ready(function()
{
  $(scrollSections).each(function()
  {
    if (!$(this).hasClass('empty-element'))
    {
      sectionIDs.push($(this).attr('id'));
    }
  });

  let currentID = document.getElementById(sectionIDs[scrollID]);
  $('.scroll-button').click(function(event)
  {
    event.preventDefault();
    currentID = document.getElementById(sectionIDs[scrollID]);
    if (scrollID < sectionIDs.length)
    {
      // currentID.scrollIntoView({ behavior: 'smooth' });
      ScrollToId(currentID);
//       if (scrollID >= sectionIDs.length - 1 || $('.scroll-button').offset().top > $(sectionIDs[sectionIDs.length - 1]).offset().top)
//       {
//         $('.scroll-button > i').addClass('fa-flip-vertical');
//         $('.scroll-button > span').text('back to top');
//       }
    }
    else
    {
      scrollID = -1;
      $('.scroll-button > i').removeClass('fa-flip-vertical');
      $('.scroll-button > span').text('scroll');
      currentID = document.getElementById('top');
      // currentID.scrollIntoView({ behavior: 'smooth' });
      ScrollToId(currentID);
    }
  });

  let previousID = currentID;
  let currentIDTopOffset = $(currentID).offset().top;
  let currentScrollPosition = $(window).scrollTop();

  $(window).scroll(function()
  {
    try
    {
      let scrollButtonTopOffset = $('.scroll-button').offset().top;
      let lastSection = document.getElementById(sectionIDs[sectionIDs.length - 1]);
      let lastSectionTopOffset = Math.round($(lastSection).offset().top);

      if (scrollID <= sectionIDs.length)
      {
        previousID = currentID;
        if (scrollID === sectionIDs.length)
        {
          currentID = document.getElementById(sectionIDs[sectionIDs.length - 1]);
        }

        currentIDTopOffset = Math.floor($(currentID).offset().top);
        currentScrollPosition = $(window).scrollTop();

        if ((currentScrollPosition >= currentIDTopOffset - topBarHeightOffset && scrollID < sectionIDs.length) || scrollButtonTopOffset > lastSectionTopOffset)
        {
          currentID = document.getElementById(sectionIDs[scrollID + 1]);
          if (previousID !== currentID)
          {
            scrollID++;
          }
          if (scrollButtonTopOffset > lastSectionTopOffset)
          {
            scrollID = sectionIDs.length;
          }
        }
        else
        {
          if (scrollID !== 0)
          {
            let previousSection = document.getElementById(sectionIDs[scrollID - 1]);
            let previousSectionOffset = Math.round($(previousSection).offset().top);

            if ((currentScrollPosition < previousSectionOffset && scrollID !== 0) || scrollButtonTopOffset > lastSectionTopOffset)
            {
              currentID = document.getElementById(sectionIDs[scrollID - 1]);

              if (previousID === currentID)
              {
                scrollID--;
              }
            }
          }
        }

        if (scrollID >= sectionIDs.length)
        {
          $('.scroll-button > i').addClass('fa-flip-vertical');
          $('.scroll-button > span').text('Top');
        }
        else
        {
          $('.scroll-button > i').removeClass('fa-flip-vertical');
          $('.scroll-button > span').text('scroll');
        }
      }
    }
    catch (error)
    {
    // console.log('Error with the scroll script');
    }
  });

  for (let i = 0; i < sectionIDs.length - 1; i++)
  {
    let scrollSectionAbove = document.getElementById(sectionIDs[i]);
    let scrollSectionBelow = document.getElementById(sectionIDs[i + 1]);

    if (currentScrollPosition >= $(scrollSectionAbove).offset().top && currentScrollPosition < $(scrollSectionBelow).offset().top)
    {
      scrollID = i;
    }
  }

  let scrollSectionAbove = document.getElementById(sectionIDs[sectionIDs.length - 1]);

  currentScrollPosition = $(window).scrollTop();
  if (currentScrollPosition >= $(scrollSectionAbove).offset().top)
  {
    scrollID = sectionIDs.length;
  }
});
