﻿var sHeading = "doc_heading_";
var sSection = "doc_section_";
var pY = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;        

$(document).ready(function() {
    
    function PrintMenu(parentId)
    {
        var menuHtml = "<table>";

        // Get all headers
        var count = 1;
        $("h2").each(function () {
            if (count == 1) menuHtml += "<tr>";
        
            menuHtml += "<td class=\"tzt\" gid=\"" + $(this).attr("id") + "\"><ul><li>" + $(this).text() + "</li></ul></td>";
            
            if (count == 3) menuHtml += "</tr>";
            
            ((count + 1) > 3) ? count = 1 : count++;
        });
        menuHtml += "<td class=\"tzt\" gid=\"toppen\"><ul><li>Til toppen af artiklen</li></ul></td></table>";
        
        // "Print menu"
        $("#fw_contentmenu").html(menuHtml);

        // Add events to generated menu
        $("#fw_contentmenu td")
            .unbind('click')
            .bind('click', function() {
            
                // Reset colors to all links in content-menu
                $("#fw_contentmenu td").each(function() {
                    $(this).css("color", "#000");
                });
                
                // Set current content menu-link to red "active color"
                $(this).css("color", "#AF1014");
            
                // Menu height including padding!!!
                var menuHeight = $("#fw_contentmenuWrapper").height() + 10;
                
                // If user clicked "Til toppen", scroll to top of document
                if ($(this).attr("gid") == "toppen") 
                {
                    window.scrollTo(0,0);
                }
                else
                {
                    // Target offset (document height in pixels to target element)
                    var targetOffset = $("#"+$(this).attr("gid")).offset().top;
                
                    // Scroll down to target(heading) offset 
                    window.scrollTo(0, targetOffset - menuHeight);
                }

                // Set Y-coord for menu when navigating
                $("#fw_contentmenuWrapper").css("top", pY);
                
                // Debuginfo
                //$("#debug").text(" ($(this).attr(\"gid\"): " + $(this).attr("gid") + ")");
        });        
        
    }

    // Show wrapper (Icon for accessing contentmenu)
    $("#fw_contentmenuWrapper").show();

    // Update menu position when scrolling
    $(window).scroll(function () { 
    
        pY = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
        
        // Adjust pY according to fw_contentmenuAnchor, do not start scrolling until page has passed this anchor
        pY = (pY < $("#fw_contentmenuAnchor").offset().top) ? 0 : pY - $("#fw_contentmenuAnchor").offset().top;
        
        // Move menu to adjusted pY
        $("#fw_contentmenuWrapper").css("top", pY); 
        
        // Debuginfo
        //$("#debug").text(" (pY = " + pY + ", height: " + $("#fw_contentmenuWrapper").height() + ")");
    });
    
    // Update menu position when refreshing page
    $(window).ready(function () { 
        var pY = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
        $("#fw_contentmenuWrapper").css("top", pY); 
    });
    
    // Hide menu the first time
    PrintMenu(0);
    $("#fw_contentmenu").hide();
    
    // Fade out menu
    $("#fw_contentmenuHeader").click (
        function() {

            if ( $(this).next().css("display") == "none")
            {
                $("#fw_contentmenuWrapper div img").attr("src", "/image/down.jpg");
                $("#fw_contentmenuWrapper div label").text("Skjul indholdsfortegnelse");
            }
            else
            {
                $("#fw_contentmenuWrapper div img").attr("src", "/image/right.jpg");
                $("#fw_contentmenuWrapper div label").text("Vis indholdsfortegnelse");
            }
                
            $(this).next().slideToggle('normal');

            return false;
        }
    );
    
});


