﻿$(document).ready(function() {
    var pY = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;        
   
    DocumentMenu();
    $("#fw-doctitle").css("margin-left", "190px");
    $("#doc_body").css("margin-left", "190px");

    /*** MENU SCROLL EVENTS **************************************************/
    // Show wrapper (Icon for accessing contentmenu)
    $("#fw-docmenu").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-content").offset().top) ? 
            68 : 
            pY - $("#fw-page").offset().top;
        
        // Move menu to adjusted Yp
        $("#fw-docmenu").css("top", pY); 
    });
    
    // Update menu position when refreshing page
    $(window).ready(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-content").offset().top) ? 
            68 : 
            pY - $("#fw-page").offset().top;
        
        // Move menu to adjusted pY
        $("#fw-docmenu").css("top", pY); 
    });
});

/*** DOCUMENT MENU HIDE/SHOW *************************************************/
/*function ShowMenu() { $('#content-menu').show(); }
function HideMenu() {}*/

/*** DOCUMENT MENU ***********************************************************/
function DocumentMenu() {
    var count = 0;
    var menuHtml = '<ul><li class="title">Indholdsfortegnelse</li>';

    // Get h2 and h3 headers
    $("h2").each(function () {
        count++;
        var headerY = $(this).offset().top;

        var id = $(this).attr("id");

        if (id != "" && $(this).parent().attr('id') != 'fw-contact') {
            menuHtml += '<li><a href="#' + id + '"' + SnipTitle($.trim($(this).text()), 25) + '>' + Snip($.trim($(this).text()), 25) + '</a>';

            var subMenu = "";
            $(this).next().find("h3").each(function () {
                headerY = $(this).offset().top;
                subMenu += "<li><a href=\"#" + $(this).attr('id') + "\">" + Snip($.trim($(this).text()), 25) + "</a>";
            });

            if (subMenu != "") {
                subMenu = "</a><ul class=\"level-3\">" + subMenu + "</ul>";
                menuHtml += subMenu;
            }

            menuHtml += "</li>";
        }
    });
    menuHtml += "<li id=\"document-top\"><a href=\"#\">Til toppen af dokumentet</a></li>";
    menuHtml += "</ul>";

    // "Print menu"
    //if (count > 0) { $("#content_menu_toggle").parent().append(menuHtml); }
    if (count > 0) { $("#fw-docmenu").append(menuHtml); }

    // Add events to generated menu
    $("#fw-docmenu li a").click(function() {
        // Hide all submenus
        $(this).parent().parent().find("ul").hide();

        // Show submenu for current h2
        $(this).parent().find("ul").show();

        // Apply style to current menuelement & clear others
        /*$(this).parent().parent().find("a").css("font-weight", "normal");
        $(this).css("font-weight", "bold");*/

        var url = $(this).attr("href");
        window.location = url;
        return false;
    });

} // End Function DocumentMenu
    
function GetHrefAnchorOffset()
{
    var arr = window.location.href.split('#');

    if (arr != null && arr.length > 1)
    {
        return $('div#doc-content a[name=' + arr[1] + ']').offset().top - 40;    
    }
    return 0;
}

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function Snip(str, len)
{    
    var cut = str.length;
    var append = "";
    if (str.length > (len - 3)) {
        cut = len;
        append = "...";
    }
        
    str = str.substr(0, cut) + append;
    
    return str;
}

function SnipTitle(str, len) {
    if (str.length > (len - 3)) {
        return ' title="' + str + '"';
    }
    return "";
}
