/**
 * @file
 * A JavaScript file for the section menu component.
 */
(function ($, Drupal, window, document, undefined) {
    Drupal.behaviors.healthTopicsSidenav = {
        attach: function (context) {
            $('<div class="subnav-overlay"></div>').insertAfter('#main');

            //Disable sidenav when there's only one menu item
           // $('.view-content').find('.subnav-link').size() <= 1? $('body').toggleClass('hide-sidenav' +
           //     ' overlay'): $('body').toggleClass('overlay');

            $('#component-submenu-toggle').on('click', function(e) {
                $('body').toggleClass('overlay-active');
                //Adding Elements to overlay
                //$('#toolbar-administration').length? $('#toolbar-administration').clone().appendTo($('.subnav-overlay')): '';
                $('#header').length? $('#header').clone().appendTo($('.subnav-overlay')): '';
                $('.messages.messages--status').length? $('.messages.messages--status').clone().appendTo($('.subnav-overlay')): '';
                $('.block-extra-field-blocknodehealth-topicsworkbench-moderation-control').length? $('.block-extra-field-blocknodehealth-topicsworkbench-moderation-control').clone().appendTo($('.subnav-overlay')): '';
                $('.component-health-topics-menu').clone().appendTo($('.subnav-overlay'));
                $('.component-more-information').clone().appendTo($('.subnav-overlay'));
                $('.subnav-overlay > #toolbar-administration').css("margin-top", $('.adminimal-admin-toolbar').css("padding-top") );
                $('.subnav-overlay > #toolbar-administration').css("padding-top", "1px");
                $('.subnav-overlay > .component-health-topics-menu').toggleClass('subnav-open');
                $submenuToggleBtn=$('.subnav-overlay').find('#component-submenu-toggle');

                $overlay_height = $(document).height();
                $('.subnav-overlay').height($overlay_height);

                $(document).scroll(function() {
                  $current_page_height = $(document).height();

                  //Use overlay height only if overlay is active on scroll.
                  if ($('body').hasClass('overlay-active')) {
                    //If we are logged in user using the admint toolbar, remove admin toolbar elements height from the total overlay height.
                    if ($('body').hasClass('adminimal-admin-toolbar')) {
                      if ($current_page_height > $overlay_height) {
                        $overlay_height = $(document).height() - 45;
                      }
                      else {
                        $overlay_height = $overlay_height;
                      }
                    }
                    else {
                      if ($current_page_height > $overlay_height) {
                        $overlay_height = $current_page_height;
                      }
                      else {
                        $overlay_height = $(document).height();
                      }
                    }
                    $('.subnav-overlay').height($overlay_height);
                  }
                  else {
                    $overlay_height = 0;
                    $('.subnav-overlay').height($overlay_height);
                  }
                });

                $submenuToggleBtn.children('.subnav-inner-text').text("CLOSE");
                $submenuToggleBtn.on('click', function(e) {
                    $('.subnav-overlay').empty();
                    $(this).children('.subnav-inner-text').text("MENU");
                    $('body').toggleClass('overlay-active');
                    $('.subnav-overlay > .component-health-topics-menu').toggleClass('subnav-open');
                    $('.subnav-overlay').height(0);
                });

                $('.section-menu-dropdown > a').on('click', function(e) {
                    $(this).parent('.section-menu-dropdown').toggleClass('expanded');
                    e.preventDefault();
                    return false;
                });

                e.preventDefault();
                return false;
            });

            $('.section-menu-dropdown > a').on('click', function(e) {
                $(this).parent('.section-menu-dropdown').toggleClass('expanded');
                e.preventDefault();
                return false;
            });

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

            if($(window).width() <= 768 ) {
            // Show/hide View More/View Less button on click on mobile.
              $('#component-page-links-more-button').click(function(e){
                $(this).addClass('hidden');
                $(this).removeClass('button-visible');
                $(this).closest('#component-page-links-holder').find('.component-page-links-less-button').removeClass('hidden');
                $(this).closest('#component-page-links-holder').find('#component-page-links-ul .component-page-links-li').addClass('button-visible');
                refreshFilterWindow();
                e.preventDefault();
                return false;
              });
              $('#component-page-links-less-button').click(function(e){
                $(this).addClass('hidden');
                $(this).removeClass('button-visible');
                $(this).closest('#component-page-links-holder').find('.component-page-links-more-button').removeClass('hidden');
                $(this).closest('#component-page-links-holder').find('.component-page-links-more-button').addClass('button-visible');
                $(this).closest('#component-page-links-holder').find('#component-page-links-ul .component-page-links-li').removeClass('button-visible');
                e.preventDefault();
                return false;
              });
            }

        }
    };

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