$(document).ready(function() {

    $('.menu-icon').click(function(){
        $('.main-menu-inner nav').toggle();
        $(this).toggleClass('active');
    });

    /*
     * Resizes the TOC to take the whole content area's height.
     */
    function resizeTOC() {
        var tocTopMargin = parseInt($(".toc:first").css('margin-top')),
		tocHeader = $(".toc-menu").height(),
        tocHeight = tocTopMargin + tocHeader,
        contentAreaHeight = getContentAreaHeight(),
        newTOCHeight = contentAreaHeight - tocHeight - parseInt($("div.main-content").css("margin-bottom")) - $("div.button-bar").height();

        $(".toc:first").css('height', newTOCHeight);
        $(".mc-component").css('maxHeight', newTOCHeight);
    }

    /*
     * Resizes the TOC after 1 ms to give time margin to the footer to be rendered.
     */
    function resizeWithTimeout() {
        setTimeout(resizeTOC, 100);
    }

    /*
     * Resizing TOC after scrolling or resizing the window
     */
    $(window).on('scroll resize', resizeWithTimeout);

    /*
     * Call to adjust the TOC height from the beginning.
     */
    $(window).on('load', resizeTOC);

    // ToC
    $('.toc:first').on('click', 'li.has-children', function(e) {
        if (e.target === this) {
            var $element = $(this);
            rotateMenuArrow($element);
            $children = $element.find(' > ul');
            $children.slideToggle();
            $children.toggleClass('open');
        }
    });

    function rotateMenuArrow($element) {
        if ($element.hasClass('menuArrowRight')) {
            $element.removeClass('menuArrowRight');
            $element.addClass('menuArrowDown');

        } else if ($element.hasClass('menuArrowDown')) {
            $element.removeClass('menuArrowDown');
            $element.addClass('menuArrowRight');

        } else {
            $element.addClass('menuArrowRight');
        }
    }

    function setMenuPosition($page) {
        var $toc = $('div.toc');
        var $globalHeader = $('#header-placeholder');

        if ($toc.length && $globalHeader.length) {
            if ($($page).scrollTop() >
                ($globalHeader.outerHeight(true) - parseInt($toc.css('margin-top')))) {
                $toc.css('position', 'fixed');
                $toc.css('top', $globalHeader.outerHeight(true));
            } else {
                $toc.css('position', 'relative');
                $toc.css('top', 0);
            }
        }
    }

    function setBreadcrumbPosition($page) {
        var $breadcrumb = $('div.breadcrumbContainer');
        var $globalHeader = $('#header-placeholder');

        if ($breadcrumb.length && $globalHeader.length) {
            $globalHeader.css('margin-bottom', $breadcrumb.innerHeight());
            $breadcrumb.css({
                'position': 'fixed',
                'top': $globalHeader.outerHeight()
            });
        }
    }

    function hidePDFButton($page) {
        var $expand = $('button.btn-pdf');
        var $globalHeader = $('#header-placeholder');

        if ($expand.length) {
            if ($($page).scrollTop() > $globalHeader.outerHeight()) {
                $expand.css('display', 'none');
            } else {
                $expand.css('display', 'initial');
            }
        }
    }

    $(window).scroll(function (event) {
        setBreadcrumbPosition(this);
        setMenuPosition(this);
        hidePDFButton(this);
    });

});
