/**
 * @file
 * A JavaScript file for creating lab banner links to the headers on the Science pages
 */
(function ($, Drupal, window, document, cookies) {
    Drupal.behaviors.sciencePageLinks = {
        attach: function (context, settings) {

            $(".component-lab-banner-links").each(function() {

                //loop through all h2 headers
                $.each($('article.node--type-science h2'),function(){

                    //Retrieve H2 title and hyphenate it
                    var hyphenatedTitle = $(this).text().trim().replace(/\s/g,'-');

                    //Create and append the link to the lab banner component
                    $('.component-lab-banner-links ul').append( '<li>' + $(this).html() + '</li>' );

                    //Create anchor link for the Header by adding the id attribute
                    $(this).attr('id',hyphenatedTitle);
                });

            });
        }
    };
    Drupal.behaviors.HealthTopicsPageLinks = {
        attach: function (context) {

            $(".component-page-links").each(function() {
                //loop through all h2 headers
                $.each($('article.node--type-health-topics h2'),function(){
                    //Skip visually-hidden h2s
                    if (!$(this).hasClass('visually-hidden')) {
                        //Retrieve H2 title and hyphenate it
                        var hyphenatedTitle = $(this).text().replace(/\s/g,'-');
                        var shortTitle = $(this).next('.field--name-field-component-section-st').text();
                        if ($(this).next('.field--name-field-component-section-st').text()) {
                          $('.component-page-links ul').append( '<li class="component-page-links-li">'+ shortTitle +'</li>' );
                        }
                        else {
                          $('.component-page-links ul').append( '<li class="component-page-links-li">'+ $(this).html() +'</li>' );
                        }
                        //Create anchor link for the Header by adding the id attribute
                        $(this).attr('id',hyphenatedTitle);
                    }
                });
                // Check how many page links are generated. If more than 6, show View More/View Less button.
                $page_links_count = document.getElementById("component-page-links-ul").querySelectorAll("li").length;

                if ($page_links_count <= 6) {
                  if ($page_links_count = 6) {
                    $('#component-page-links-ul li:nth-child(6)').toggleClass('button-visible');
                  }
                  else {
                    return $page_links_count;
                  }
                  return $page_links_count;
                }
                else {
                  //Create and append the link to the lab banner component
                  $('.component-page-links-more-button').toggleClass('button-visible');
                }

            });
        }
    };
    Drupal.behaviors.ResearchTopicsPageLinks = {
        attach: function (context, settings) {

            $(".component-page-links").each(function() {
                //loop through all h2 headers
                $.each($('article.node--type-research-topics h2'),function(){
                    //Skip visually-hidden h2s
                    if (!$(this).hasClass('visually-hidden')) {
                        //Retrieve H2 title and hyphenate it
                        var hyphenatedTitle = $(this).text().replace(/\s/g,'-');
                        var shortTitle = $(this).next('.field--name-field-component-section-st').text();
                        if ($(this).next('.field--name-field-component-section-st').text()) {
                          $('.component-page-links ul').append( '<li class="component-page-links-li">'+ shortTitle +'</li>' );
                        }
                        else {
                          $('.component-page-links ul').append( '<li class="component-page-links-li">'+ $(this).html() +'</li>' );
                        }
                        //Create anchor link for the Header by adding the id attribute
                        $(this).attr('id',hyphenatedTitle);
                    }
                });
                // Check how many page links are generated. If more than 6, show View More/View Less button.
                $page_links_count = document.getElementById("component-page-links-ul").querySelectorAll("li").length;

                if ($page_links_count <= 6) {
                  if ($page_links_count = 6) {
                    $('#component-page-links-ul li:nth-child(6)').toggleClass('button-visible');
                  }
                  else {
                    return $page_links_count;
                  }
                  return $page_links_count;
                }
                else {
                  //Create and append the link to the lab banner component
                  $('.component-page-links-more-button').toggleClass('button-visible');
                }

            });
        }
    };
})(jQuery, Drupal, this, this.document);