/*******************************************************
 *             ADMIN/reseller MENY			           *
 ******************************************************/

	/**
	 *	Functions to handle the menu logic
	 */


    function menuSwitch(level, menuId, parentDisplay) {
        var counter = 1;
        var display = true;
        //alert(level + " - " + menuId);
        if(getById(menuId + "_1") == null) {
            return; // the menu wasnt found
         }
        if(getById(menuId + "_1").style.display == "") {
            display = false
        }

        // set/reset the style and save a cookie
        if(display) {
            getById(menuId).className = "menu2Common menu2Level" + level + " menu2Level" + level + "Selected";
            setCookie(menuId, true, null, "/","","");
        } else {
            getById(menuId).className = "menu2Common menu2Level" + level;

            // if level just above is the main level (parentDisplay == null), allow to setCookie to false
            if(parentDisplay == null || parentDisplay != null && parentDisplay == true)      // do not set false in cookie if we hide (false) on a higher level. We need to remember what should be expanded, if we collapse on a higher level and then expand again
            {
                setCookie(menuId, false, null, "/","","");
            }
        }

        // Loop through all sublevels below menuId
        while(getById(menuId + "_" + counter) != null) {
            if(display) {
                getById(menuId + "_" + counter).style.display = "";
            } else {
                getById(menuId + "_" + counter).style.display = "none";
            }

            if(getById(menuId + "_" + counter + "_1") != null) {    // is it a sub menu
                // ahh... a submenu!
//                menuSwitch(level+1, menuId + "_" + counter);

                // This works except that if sublevels are expanded and if clicked on a leaf, then the sublevels are collapsed while they should stay untouched expanded.
//                if(!display)     // only recurse down if display == false. That is, only collapse submenues if we click on a higher menu. But do not expand submenues further down which would cause to open the whole tree
                if(getCookie(menuId + "_" + counter) == "true")
                {
//                    menuSwitch(level+1, menuId + "_" + counter, display);  // pass on this display value so we can look on parent's value when we are recursing down to fix collapse problem.
                    cookieValue   = getCookie(menuId);
                    currLevelDisplay    = false;
                    if(cookieValue == "true")
                    {
                        currLevelDisplay    = true;
                    }
//                    menuSwitch(level+1, menuId + "_" + counter, display);  // pass on this display value so we can look on parent's value when we are recursing down to fix collapse problem.
                    menuSwitch(level+1, menuId + "_" + counter, currLevelDisplay);  // pass on this display value so we can look on parent's value when we are recursing down to fix collapse problem.
                }
            }

            counter++;
        }
    }

    function initMenu() {
        var counter = 1;
        var statusString = "";

        // init the last clicked sub item
        if(getCookie("lastMenuId") != null) {

            var lastMenuIdLevel = getCookie("lastMenuIdLevel")
            //alert("lastMenuIdLevel = " + lastMenuIdLevel);
            try {
                getById(getCookie("lastMenuId")).className = "menu2Common menu2Level" + lastMenuIdLevel + " menu2Level" + lastMenuIdLevel +"Selected";
            }
            catch (e) {
                //ignore error
            }
        }

        // Loop thru all menu items; stop when the next menu wasnt found
        // bugfix: loop at least 10 times...
        while(getById("sub" + counter) != null || counter < 10) {
            statusString += "sub" + counter + "=" + getCookie("sub" + counter) +",";
            //alert(statusString);
            if(getCookie("sub" + counter) == "true") {
                menuSwitch(1, "sub" + counter); // Note! Obs! Hardcoded because we only need one level (to fix: add the level value to a cookie)
            }

            counter++;
        }
    }

    function menuClick(level, menuId) {
        // clear the last clicked item
        if(getCookie("lastMenuId") != null) {
            try {
                getById(getCookie("lastMenuId")).className = "menu2Common menu2Level" + level;
            }
            catch (e) {
                //ignore error
            }
        }

        // save which item the user clicked on as a cookie
        setCookie("lastMenuId", menuId, null, "/","","");
        setCookie("lastMenuIdLevel", level, null, "/","","");

        // set the new style
        getById(menuId).className = "menu2Common menu2Level" + level + " menu2Level" + level + "Selected";
    }

    function resetMenuCookies() {
        // assume max 10 top folders
        for(i = 1; i < 10; i++) {
            deleteCookie("sub" + i, "/","");
        }
        deleteCookie("lastMenuId", "/","");
        deleteCookie("lastMenuIdLevel", "/","");
    }



	/**
	 *	Functions to handle the creation of the meny in javascript
	 */

    var currentLevel = 1;
    var levelCounter = new Array();
    var lastFolderLabel = "";

    levelCounter[currentLevel] = 0;

    function newFolder(label) {
        lastFolderLabel = label;
        levelCounter[currentLevel]++;
        currentLevel++;
        levelCounter[currentLevel] = 0;

        if(levelCounter[currentLevel] == 0) {
            // create new folder?
            //alert("creating folder");
            newFolderCreate(lastFolderLabel);
        }

    }

    function newFolderCreate(label ) {
        //document.write("folder: " + currentLevel + " - " + label +" (" + levelCounter[currentLevel-1] + "." + levelCounter[currentLevel] + ")<br>");
        var nodeName = getNodeName(currentLevel-1);
        if(nodeName == "subundefined") {
            return; // Undefined... we dont want this menu?!?!
        }

        var style = "";
        if(currentLevel > 2) {
            // An menuitem on the first level, not a folder
            style = "display:none";
        }

        document.write("<div id=\"" + nodeName + "\" class=\"menu2Common menu2Level" + (currentLevel-1) + "\" style=\"" + style + "\">");


        //document.write("<div id='" + nodeName + "' class='menu2Common menu2Level" + (currentLevel-1) + "'>");
        document.write("<a href='#' onClick=\"menuSwitch(" + (currentLevel-1) + ", '" + nodeName + "'); return false;\">");
                document.write("<div>" + label + "</div>");
            document.write("</a>");
        document.writeln("</div>");
    }

    function closeFolder() {
        /*alert(lastFolderLabel + " - " + levelCounter[currentLevel]);
        if(levelCounter[currentLevel] == 0) {
            if(getById(lastFolderLabel)) {
                getById(lastFolderLabel).style.display = "none";
            }
        }
        */

        currentLevel--;
    }

	 function newNode(label, url)
	 {
	 	newNode(label,url,null);

	 }

	function popupWindow(url,name, specs) {
		if(typeof(specs) !== 'undefined' && specs != null) {
			var faqWindow = window.open(url,name,specs);
		}else{
			var faqWindow = window.open(url,name,"width=850,height=690,scrollbars=yes,resizable=yes");
		}
		faqWindow.focus();
	}

    function newNode(label, url, target, specs) {
        if(url == "" || url == null) {
            // no url, no node!
            return;
        }
/*
        if(levelCounter[currentLevel] == 0) {
            // create new folder?
            //alert("creating folder");
            newFolderCreate(lastFolderLabel);
        }
*/
        levelCounter[currentLevel]++;
        //document.write("Node: " + currentLevel + " - " + label +"  url:" + url + "  (" + levelCounter[currentLevel] + ") <br>");
        var nodeName = getNodeName(currentLevel);
        var style = "display:none";
        if(currentLevel == 1) {
            // An menuitem on the first level, not a folder
            style = "";
        }

        var str = "";

        str += "<div id=\"" + nodeName + "\" class=\"menu2Common menu2Level" + currentLevel + "\" style=\"" + style + "\">";
        if(target){
            // var linkUrl = "popupFAQ(" + url +'\",\"'+ name +'\")"    ";
            if(typeof(specs) !== 'undefined' && specs != null) {
                str += '<a href="javascript:popupWindow(\''+url+'\',\''+target+'\',\'' + specs + '\')">';
            }else{
                str += '<a href="javascript:popupWindow(\''+url+'\',\''+target+'\')">';
            }
        }else{
            str += '<a href="' + url + '" onClick="menuClick(\'' + currentLevel + "', '" + nodeName + '\');" >';
        }
       	str += "<div>" + label + "</div>";
       	str += "</a>";
       	str += "</div>";

        document.write(str);
    }





    function getNodeName(currentLevel) {
        if(currentLevel == 1) {
            return "sub" + levelCounter[currentLevel];
        } else {
            var str = "sub";
            level = 1;
            while(level < currentLevel) {
                str += levelCounter[level] + "_";
                level ++;
            }
            str += levelCounter[currentLevel];
            return str;

            //return "sub" + levelCounter[currentLevel-1] + "_" + levelCounter[currentLevel];
        }
    }












