﻿function setCustomMenuDisplay(elem, topNode) {
    if (elem.hasClass("static") && elem.hasClass("selected")) {
        var selectedNode = elem;
        var foundSelectedNode = false;
        var sublevelsToDisplay = 1;
        var sublevelCounter = 0;
        topNode.find("li.selected").each(function () {			
			$(this).show();
			$(this).find("li").each( function(){
            //if ($(this).parent().parent().html().indexOf("<span class=\"ms-hidden\">Currently selected</span>") != -1) { 
            //if ($(this).parent().parent().html().indexOf("Currently selected") != -1) { 
                if (sublevelCounter <= sublevelsToDisplay) {
                    if ($(this).children().hasClass("selected")) {
                        foundSelectedNode = true;
                    }
                    else if ($(this).parent().hasClass("selected")) {
                        foundSelectedNode = true;
                        sublevelCounter++;
                    }                    
                    $(this).parent().addClass("displayoverride"); 
                }
                if (foundSelectedNode) {
                    sublevelCounter++;
                }				
			});
            //}
        });
        // parrent selected out comment following
        if (foundSelectedNode == true) {
            if (!topNode.hasClass("selected")) {
                topNode.addClass("selected");
                topNode.addClass("selectedparent"); //CGR har sat denne på for at få hvid baggrund på selected parentnode

            }
        }
    }
}

function initiateCustomMenuDisplay($) {
    var rootNodeCSSClass = "celfmenu";
    $("div." + rootNodeCSSClass).find("ul.root").each(function () {
        $(this).children("li").each(function () {
            var topNode = $(this);
            var foundSelected = false;
            setCustomMenuDisplay($(this), topNode);
            $(this).find("li").each(function () {
                setCustomMenuDisplay($(this), topNode);
            });
            //hide all listitems without the class 'displayoverride'
            $(this).find("ul").each(function () {
                $(this).hide();
                if ($(this).hasClass('displayoverride')) {
                    $(this).show();
                }
            });
			//show all childsites too
			$(this).children("ul").show();
			$(this).find("li.selected").parent().show();
        });
    });
}

function removeImgtopnav($) {
    var selectedTopElement = $("div.topnavmenu").find("li.selected");
    selectedTopElement.next().css("background-image", "url('')");
    selectedTopElement.css("background-image" , "url('')");
    if(selectedTopElement.html() != $("div.topnavmenu").find("li:first").html()
        && selectedTopElement.html() != $("div.topnavmenu").find("li:last").html()){
        selectedTopElement.css({ "border-left": "4px solid #fff", "border-right": "4px solid #fff" });
        
             
    }
    if (selectedTopElement.html() == $("div.topnavmenu").find("li:last").html()) {
        selectedTopElement.css({ "border-left": "4px solid #fff" });
     /*   $("div.topnavmenu").css("width", "696px");       */
    }

 }


 jQuery(document).ready(function ($) {
     initiateCustomMenuDisplay($);
     $("div.topnavmenu").find("li:last").css({ "border-right": "4px solid #fff" });
     $("div.topnavmenu").find("li:first").css({ "border-left": "4px solid #fff" });
 });

 jQuery(".topnavmenu").ready(function () {
     removeImgtopnav($);
     
 });
 jQuery(document).ready(function ($) {
    if ($(window).width() < 1000) $('html').css({ 'overflow-x': 'auto' });
});


