var _MaxLineWidth = 0;
var NameOfCookie = "FFMENU";

collpth = "/images/";

img_sid_on = "minus.gif";
img_sid_off = "plus.gif";

img_men_old = "application_home.png";
img_men_new = "application_go.png";

img_sid_left = "arrow_right.gif";
img_sid_right = "arrow_left.gif";

img_sid_fwd = "arrow_down.png";
img_sid_bck = "arrow_up.png";

img_sid_sync = "arrow_refresh.png";

enableAdmin = true;

function go(pg) {
    location.href = pg;
}
var _CookiesOn = false;
var _DebugCookies = false;
var _Cookie = new Array();
var _CookieDuration = 10; /* number of Days; 0: Session-length */
var _ContentSingleLine = true;
var _ContentShorten = true;
var _AutoHeight = true;
var _MenueFixed = true;
var _Paging = false;
var _AutoWidth = true;
var _AutoWidthMax = 300;
var _UseSideBarMenuLayout = false;
var _MenuWidth = new Array(200, 300, 400, 500);
var _ContentWidth = new Array(20, 30, 40, 50);
var _MenuHeight = new Array(10, 20, 50, 100, 200);
var _MenuTopPx = 87;
var _SidebarContent = ""; // Content of the manually edited sidebar menu
var FirstCall = true;
var imgSort = new Array();
imgSort.push(new Array("icons/kalender1.gif", "sort by date created", "show date created"));
imgSort.push(new Array("icons/calendar.gif", "sort by date last modified", "show date last modified"));
imgSort.push(new Array("icons/blase.gif", "sort by title", "show title"));
imgSort.push(new Array("icons/asc.gif", "sort by name", "show name"));
var pagesIncluded = new Array();

var state=Array(10);
    state["currSort"] = -1;
    state["currPaging"] = -1;
    state["currCategory"] = -1;
    state["currWidthIndex"] = -1;
    state["currHeightIndex"] = -1;
    state["currMenuVisibility"] = -1;
    state["currMenuPosition"] = -1;
    state["currShowDate"] = -1;
    state["currShowText"] = -1;
    state["currMenuSidebar"] = -1;
function stateReset() {
    state["currSort"] = 0;
    state["currPaging"] = 0;
    state["currCategory"] = 0;
    state["currWidthIndex"] = 0;
    state["currHeightIndex"] = 0;
    state["currMenuVisibility"] = 0;
    state["currMenuPosition"] = 0;
    state["currShowDate"] = 0;
    state["currShowText"] = 0;
    state["currMenuSidebar"] = 0;
}
var currPage = "";
var currPage0 = "";
var currPagePrev = "";
var currPageNext = "";

function Highlight(id) {
    $(id).css("opacity", "1");
    $(id).css("filter", "alpha(opacity=100)");
}
function Lowlight(id) {
    $(id).css("opacity", "0.4");
    $(id).css("filter", "alpha(opacity=40)");
}
function getImgLink(src, id, alt, href) {
    return "<a title='" + alt + "' href='javascript:" + href + "'>"
        + "<img id='" + id + "' alt='" + alt + "' src='" + collpth + src + "' />"
        + "</a>\r\n";
}
function SyncPage() {
    var PageFound = false; var i = 0;
    for (i = 0; i < pagesIncluded.length; i++) {
        if (pag[pagesIncluded[i]][0][0] == currPage0) {
            PageFound = true;
            break;
        }
    }
    if (PageFound) {
        state["currPaging"] = Math.floor(i / _MenuHeight[state["currHeightIndex"]]);
        createMenu();
    }
}
function JumpToPage(GoPage) {
    if (GoPage != '') {
        location.href = (currNS == "" ? "" : currNS + ".") + GoPage + ".ashx";
        createMenu();
    }
}
function getSyncButton() {
    return "<a href='javascript:SyncPage();'>"
+ "<img id='ff_pgsync' src='" + collpth + img_sid_sync + "' title='sync current page' alt='sync current page'"
+ "/></a>";
}
function getBckButton() {
    return "<a href=\"javascript:JumpToPage('" + currPagePrev + "')\">"
+ "<img id='ff_pgbck' src='" + collpth + img_sid_bck + "' title='previous page' alt='previous page'"
+ "/></a>";
}
function getFwdButton() {
    return "<a href=\"javascript:JumpToPage('" + currPageNext + "')\">"
+ "<img id='ff_pgfwd' src='" + collpth + img_sid_fwd + "' title='next page' alt='next page'"
+ "/></a>";
}
function getSortImg(id, src, alt, sel) {
    return "<img src='" + collpth + src + "' title='" + alt + "' alt='" + alt + "'"
+ " class=\"" + ((id == state["currSort"]) ? "nav_sortimagesel" : "nav_sortimagenonsel") + "\""
+ " onclick='state[\"currSort\"]=" + id + ";createMenu()'"
+ "/>";
}
function getDummyImg(src, alt, sel, attributes) {
    if (attributes == undefined) attributes = "";
    return "<img class=\"" + (sel ? "nav_dummyimagesel" : "nav_dummyimagenonsel") + "\" src='" + collpth + src + "' title='" + alt + "' alt='" + alt + "'"
+ " " + attributes
+ "/>";
}
function getSortImgs() {
    var s = "<table id=\"nav_sorttable\"><tr>"; var i;
    for (i = 0; i < imgSort.length; i++) {
        var s_class = (i == state["currSort"] ? "sortselected" : "sort");
        if ((enableAdmin & isAdmin) || ((i == 0) || (i == 2)))
            s += "<td class=\"" + s_class + "\">" + getSortImg(i, imgSort[i][0], imgSort[i][1]) + "</td>";
    }
    if (enableAdmin & isAdmin) {
        s += "<tr>";
        for (i = 0; i < 2; i++) {
            s_class = (i == state["currShowDate"] ? "selected" : "");
            s += "<td class=\"showdate " + s_class + "\">"
    + getDummyImg("void.png", imgSort[i][2], (i == state["currShowDate"]), " onclick='state[\"currShowDate\"]=(state[\"currShowDate\"]==" + i + "?2:" + i + ");createMenu()'")
    + "</td>";
        }
        for (i = 0; i < 2; i++) {
            s_class = (i == state["currShowText"] ? "selected" : "");
            s += "<td class=\"showtext " + s_class + "\">"
    + getDummyImg("void.png", imgSort[i + 2][2], (i == state["currShowText"]), " onclick='state[\"currShowText\"]=" + i + ";createMenu()'")
    + "</td>";
        }
        s += "</tr>";
    }
    s += "</table>";
    return s;
}
function getOption(val, txt, select) { return "<option value='" + val + "'" + (select ? " selected='selected'" : "") + ">" + txt + "</option>"; }
function getCategories() {
    var s = getOption("-", "all pages (" + pag.length + ")", (state["currCategory"] == 0 ? true : false));
    var i; for (i = 0; i < cat.length; i++) s += getOption(cat[i][0], cat[i][0] + " (" + cat[i][1] + ")", (state["currCategory"] == i + 1 ? true : false));
    return "<select id='ff_catsel' title='select category or &hArr;navigation path or -special' onchange='state[\"currPaging\"]=0;state[\"currCategory\"]=this.selectedIndex;createMenu();'>" + s + "</select>";
}
function getWidth() {
    var i; var s = ""; for (i = 0; i < _MenuWidth.length; i++) s += getOption(i, "&hArr;&nbsp;" + _MenuWidth[i], (state["currWidthIndex"] == i ? true : false));
    return "<select id='ff_width' title='menu width (px)' onchange='state[\"currWidthIndex\"]=this.selectedIndex;createMenu();'>" + s + "</select>";
}
function getHeight() {
    var i; var s = ""; for (i = 0; i < _MenuHeight.length; i++) s += getOption(i, "&#8661;&nbsp;" + _MenuHeight[i], (state["currHeightIndex"] == i ? true : false));
    return "<select id='ff_height' title='number of pages displayed' onchange='state[\"currHeightIndex\"]=this.selectedIndex;SyncPage();'>" + s + "</select>";
}
function format(digit) {
    if (digit > 9) return digit; else return "0" + digit;
}
function getDate(d) {
    return new Date(d.substr(0, 4), d.substr(5, 2), d.substr(8, 2), d.substr(11, 2), d.substr(14, 2), d.substr(17, 2));
}
function FormatDate(d) {
    return d.getFullYear() + "-" + format(d.getMonth()) + "-" + format(d.getDate());
}
function getVar(arrVar) {
    return (arrVar == undefined ? "error" : arrVar);
}
function getPage(index) {
    var Page = pag[pagesIncluded[index]];
    var dcreate = getDate(Page[0][2]);
    var dmod = getDate(Page[0][3]);
    var actual = currPage0 == Page[0][0];
    var s = "<tr>";
    var alt = "Created: " + FormatDate(dcreate) + " Modified: " + FormatDate(dmod) + " Name: " + Page[0][0] + " Size: " + Page[0][4] + " Categories: ";
    var i; var cati = "";
    if (cat.length != Page[1].length) {
        Page[0][1] = "* " + Page[0][1];
        for (i = 0; i < Page[1].length; i++) Page[0][1] += getVar(Page[1][i]) + ",";
        // alert("cat-length:" + Page[0][0] + " cat:" + cat.length + " pag:" + Page[1].length);
    }
    for (i = 0; i < cat.length; i++)
        cati += (getVar(Page[1][i]) == 0 ? "" : (cati == "" ? "" : ",") + getVar(cat[i][0]));
    alt += cati;
    if (enableAdmin & isAdmin) {
        switch (state["currShowDate"]) {
            case 0:
                s = "<td><span class='nav_date'>" + FormatDate(dcreate) + "</span></td>";
                break;
            case 1:
                s = "<td><span class='nav_date'>" + FormatDate(dmod) + "</span></td>";
                break;
            default:
            case 2:
                s = "";
                break;
        }
    }
    var text = "";
    if (enableAdmin & isAdmin)
        switch (state["currShowText"]) {
        default:
        case 0:
            text = Page[0][1];
            break;
        case 1:
            text = Page[0][0];
            break;
    }
    else {
        text = Page[0][1];
    }
    if (actual) {
        //text = (actual ? "<b>" + text + "</b>" : text)
        Highlight("#ff_pgsync");
    }
    // var i, c; for (i = 0; i < text.length; i++) c += text.charCodeAt(i) + " ";
    // alert(c);
    if (_AutoWidth) {
        text = text.replace(/ /gi, "&nbsp;");
    }
    else {
        if (_ContentShorten)
            if (text.length > _ContentWidth[state["currWidthIndex"]])
            text = text.substr(0, _ContentWidth[state["currWidthIndex"]]) + "&hellip;";
        if (_ContentSingleLine) text = text.replace(/ /gi, "&nbsp;");
    }

    s += "<td class=\"" + (actual ? "nav_pageselected" : "nav_page") + "\"><a href=\"javascript:"
        + "currPage='" + text + "';"
        + "location.href='" + (currNS == "" ? "" : currNS + ".") + Page[0][0] + ".ashx';"
        + "createMenu();\""
        + " title='" + alt + "'>" + text + "</a>";
    s += "</td>";
    s += "</tr>";
    return s;
}
function getPageSize() { return _MenuHeight[state["currHeightIndex"]]; }
function getPages() {
    Lowlight("#ff_pgsync");
    var s = "";
    var from = (_Paging ? (state["currPaging"] * getPageSize()) : 0);
    var to = (_Paging ? Math.min(pagesIncluded.length, (state["currPaging"] + 1) * getPageSize()) : pagesIncluded.length);
    s = "<table class=\"nav_table\">";
    for (i = from; i < to; i++) s += getPage(i);
    s += "</table>";
    return s;
}
var order = 0;
function sort(x, y, order) {
    return ((x < y) ? (order == 0 ? -1 : 1) : ((x > y) ? (order == 0 ? 1 : -1) : 0));
}
function sortTitle(a, b) {
    return sort(a[0][1].toLowerCase(), b[0][1].toLowerCase(), order);
}
function sortName(a, b) {
    return sort(a[0][0].toLowerCase(), b[0][0].toLowerCase(), order);
}
function sortCreate(a, b) {
    return sort(getDate(a[0][2]), getDate(b[0][2]), order);
}
function sortModified(a, b) {
    return sort(getDate(a[0][3]), getDate(b[0][3]), order);
}
function sortPages() {
    switch (state["currSort"]) {
        case 0: // Create
            order = 1;
            pag.sort(sortCreate);
            break;
        case 1: // Modified
            order = 1;
            pag.sort(sortModified);
            break;
        case 2: // alpha
            order = 0;
            pag.sort(sortTitle);
            break;
        case 3: // name
            order = 0;
            pag.sort(sortName);
            break;
    }
}
function setPagesIncluded() {
    var pgcount = 0, i;
    for (i = 0; i < pag.length; i++) {
        var PageIncluded = false;
        if (state["currCategory"] == 0) PageIncluded = true;
        else if (pag[i][1][state["currCategory"] - 1]) PageIncluded = true;
        if (PageIncluded) {
            pgcount = pagesIncluded.push(i);
        }
    }
    for (i = 0; i < pagesIncluded.length; i++) {
        if (currPage0 == pag[pagesIncluded[i]][0][0]) {
            if (i > 0) {
                currPagePrev = pag[pagesIncluded[i - 1]][0][0];
            } else {
                currPagePrev = "";
            }
            if (i < pagesIncluded.length - 1) {
                currPageNext = pag[pagesIncluded[i + 1]][0][0];
            } else {
                currPageNext = "";
            }
            break;
        }
    }
}
function getPaging() {
    var PagingLetters = "";
    if (pagesIncluded.length > getPageSize()) {
        var j; var pg = 0; for (j = 0; j < pagesIncluded.length; j += getPageSize()) {
            var nextPageNr = j;
            if (pg == state["currPaging"])
                nextPageNr = "<span class='nav_pagingsel'>" + nextPageNr + "</span> ";
            else
                nextPageNr = "<a href='#' onclick='state[\"currPaging\"]=" + pg + ";createMenu()'>" + nextPageNr + "</a> ";
            nextPageNr = "<tr><td>" + nextPageNr + "</td></tr>";
            PagingLetters += nextPageNr + " ";
            pg++;
        }
    }
    return "<table id='nav_pagingtable'>" + PagingLetters + "</table>";
}
function getCookieVal(c) {
    c = parseInt(c); 
    c = (isNaN(c) ? 0 : c);
    return (c==-1 ? 0: c); // automatically reset invalid state
}
function CreateCookie() {
    var localNS = (currNS == "" ? "-" : currNS);
    var Cookie = localNS + "," + state["currSort"] + "," + state["currPaging"] + "," + state["currCategory"] + "," + state["currWidthIndex"] + "," + state["currHeightIndex"] + "," + state["currMenuVisibility"] + "," + state["currMenuPosition"] + "," + state["currShowDate"] + "," + state["currShowText"] + "," + state["currMenuSidebar"];
    if (_Cookie.length == 0) {
        _Cookie = new Array();
    } else {
        if (_Cookie[0].substr(0, localNS.length) == localNS)
            _Cookie.shift();
    }
    _Cookie.unshift(Cookie);
    var cookie = ""; var i = 0;
    for (i = 0; i < _Cookie.length; i++) {
        cookie += (cookie == "" ? "" : "#") + _Cookie[i];
    }
    __CreateCookie(NameOfCookie, cookie, _CookieDuration)
    if (_DebugCookies) $("#ff_Cookie_wr").html(cookie.replace("#", " "));
}
var CookieRead = "";
function ReadCookie() {
    // initial settings
    stateReset();
    CookieRead = "";
    var localNS = (currNS == "" ? "-" : currNS);
    var Cookie = __ReadCookie(NameOfCookie);
    CookieRead = Cookie.replace("#", " ");
    var cookie;
    _Cookie = new Array();
    if ((Cookie == null) || (Cookie == undefined)) {
    }
    else {
        var CookieNS = Cookie.split("#");
        var i; for (i = 0; i < CookieNS.length; i++) {
            cookie = CookieNS[i].split(",");
            if (cookie[0] == localNS) {
                state["currSort"] = getCookieVal(cookie[1]);
                state["currPaging"] = getCookieVal(cookie[2]);
                state["currCategory"] = getCookieVal(cookie[3]);
                state["currWidthIndex"] = getCookieVal(cookie[4]);
                state["currHeightIndex"] = getCookieVal(cookie[5]);
                state["currMenuVisibility"] = getCookieVal(cookie[6]);
                state["currMenuPosition"] = getCookieVal(cookie[7]);
                state["currShowDate"] = getCookieVal(cookie[8]); 
                state["currShowText"] = getCookieVal(cookie[9]);
                state["currMenuSidebar"] = getCookieVal(cookie[10]);
            }
            else _Cookie.push(CookieNS[i]);
        }
    }
    /*
    if (state0["currSort"] == undefined) state["currSort"] = 0; else state["currSort"] = state0["currSort"];
    if (state0["currPaging"] == undefined) state["currPaging"] = 0; else state["currPaging"] = state0["currPaging"];
    if (state0["currCategory"] == undefined) state["currCategory"] = 0; else state["currCategory"] = state0["currCategory"];
    if (state0["currWidthIndex"] == undefined) state["currWidthIndex"] = 0; else state["currWidthIndex"] = state0["currWidthIndex"];
    if (state0["currHeightIndex"] == undefined) state["currHeightIndex"] = 0; else state["currHeightIndex"] = state0["currHeightIndex"];
    if (state0["currMenuVisibility"] == undefined) state["currMenuVisibility"] = 0; else state["currMenuVisibility"] = state0["currMenuVisibility"];
    if (state0["currMenuPosition"] == undefined) state["currMenuPosition"] = 0; else state["currMenuPosition"] = state0["currMenuPosition"];
    if (state0["currShowDate"] == undefined) state["currShowDate"] = 0; else state["currShowDate"] = state0["currShowDate"]; 
    if (state0["currShowText"] == undefined) state["currShowText"] = 0; else state["currShowText"] = state0["currShowText"];
    if (state0["currMenuSidebar"] == undefined) state["currMenuSidebar"] = 0; else state["currMenuSidebar"] = state0["currMenuSidebar"];
    */
}
function createMenu() {
    if ((state["currSort"] == -1) && (state["currPaging"] == -1) && (state["currCategory"] == -1) && (state["currWidthIndex"] == -1) && (state["currHeightIndex"] == -1) && (state["currMenuVisibility"] == -1) && (state["currMenuPosition"] == -1) && (state["currShowDate"] == -1) && (state["currShowText"] == -1) && (state["currMenuSidebar"] == -1)) {
        if (_CookiesOn) ReadCookie(); else stateReset();
    }
    if (FirstCall) {
        _SidebarContent = $("#SidebarContentDiv").html();
        FirstCall = false;
    }
    // if (enableAdmin & isAdmin) {
    //     state["currShowDate"] = 0;
    //     state["currShowText"] = 0;
    // }
    var html_img_Menu = getImgLink(state["currMenuSidebar"] ? img_men_new : img_men_old, 'img_menutype', 'menu stdold/new', 'ff_sid_Sidebar();');
    var lcurrPage = "";
    var host = "http://" + window.location.host + "/";
    lcurrPage = window.location.href.substr(host.length);
    if (currNS != "") lcurrPage = lcurrPage.substr(currNS.length + 1);
    var ptIndex = lcurrPage.indexOf(".");
    if (ptIndex > -1) lcurrPage = lcurrPage.substr(0, ptIndex);
    lcurrPage = lcurrPage.toLowerCase();
    if ((lcurrPage == "") || (lcurrPage == "default")) lcurrPage = currDefaultPage;
    currPage0 = lcurrPage;
    currPage = (currNS == "" ? "" : currNS + ".") + lcurrPage
    sortPages();
    pagesIncluded = new Array();
    setPagesIncluded();
    // create menu
    var html_img_Visibility = getImgLink(state["currMenuVisibility"] ? img_sid_off : img_sid_on, 'img_visible', 'turn on/off', 'ff_sid_Visibility();');
    var html_img_Position = getImgLink(state["currMenuPosition"] ? img_sid_left : img_sid_right, 'img_sidpos', 'switch left/right', 'ff_sid_Position();');
    var html_select_Categories = getCategories();
    var html_table_Sort = getSortImgs();
    var html_img_Bck = getBckButton();
    var html_img_Fwd = getFwdButton();
    var html_img_Sync = getSyncButton();
    $("#SidebarContentDiv").html(
        "<div id=\"SidebarMenuDebug\"></div>\r\n"
        + "<table id=\"SidebarMenu\"><tr><td id=\"SidebarMenuLayout\"></td>\r\n"
        + "<td id=\"SidebarMenuControls\">\r\n"
        + "</td></tr><tr><td id=\"SidebarMenuPaging\">\r\n"
        + "</td><td id=\"SidebarMenuContent\">\r\n"
        + "</td></tr></table>\r\n");
    if (_DebugCookies)
        $("#SidebarMenuDebug").html(
        "<table><tr><td>rd</td><td width='100px'><span style='font-size:x-small;'>"+CookieRead+"</span></td></tr>"
        + "<tr><td>wr</td><td><span id='ff_Cookie_wr' style='font-size:x-small;'></span></td></tr></table>"
    );
    if (_UseSideBarMenuLayout)
        $("#SidebarMenuLayout").html(
        "<table><tr><td align='center'>\r\n"
        + (SwitchMenu ? html_img_Menu + "</td></tr><tr><td valign='top' align='center'>\r\n" : "")
        + html_img_Visibility
        + "</td></tr><tr><td valign='top' align='center'>\r\n"
        + html_img_Position
        + "</td></tr></table>\r\n");
    if (SwitchMenu) {
        switch (state["currMenuSidebar"]) { // current state 0 (new) and 1 (old)
            case 1:  // wiki sidebar
                break;
            case 0: default:
                $("#SidebarMenuControls").html(
                    "<table><tr><td>\r\n"
                    + html_select_Categories
                    + "</tr><tr><td>\r\n"
                    + "<table><tr><td valign=\"top\">"
                    + html_table_Sort + "</td><td valign=\"top\">"
                    + "<table><tr><td valign=\"top\">"
                    + html_img_Bck
                    + "</td><td valign=\"top\">"
                    + html_img_Fwd
                    + (_Paging ? "<td valign=\"top\">" + html_img_Sync + "</td>" : "")
                    + "</td></tr></table>"
                    + "</td></tr></table>"
                    + "</td></tr><tr><td>\r\n"
                    + (_AutoWidth ? "" : getWidth() + "&nbsp;") + (_Paging ? getHeight() + "&nbsp;" : "")
                    + "</td></tr></table>");
                break;
        }
    }
    if (_Paging) $("#SidebarMenuPaging").html(getPaging()); else $("#SidebarMenuPaging").html("");
    if (SwitchMenu) {
        switch (state["currMenuSidebar"]) { // current state 0 (old) and 1 (new)
            case 1: 
                $("#SidebarMenuContent").html(_SidebarContent);
                break;
            case 0: default:
                $("#SidebarMenuContent").html(getPages());
                break;
        }
    }
    //$("#dialog").html("Select menu width<br/>"+getWidth()+"<br/><a href='#' class='close'>Close it</a>");
    if (currPageNext == "") Lowlight("#ff_pgfwd"); else Highlight("#ff_pgfwd");
    if (currPagePrev == "") Lowlight("#ff_pgbck"); else Highlight("#ff_pgbck");
    ff_sid_updatelayout();
    if (_AutoHeight) {
        $(document).ready(function() {
            var doc_height = $('html').height();
            var men_height = $('#SidebarDiv').height();
            var win_height = $(window).height() - $("#HeaderDiv").height();
            // alert("win:" + win_height + " body:" + doc_height + " menu:" + men_height + " " + cheight);
            $('#SidebarDiv').height(win_height);
            $(window).resize(function() {
                changepush();
            });
            function changepush() {
                var win_height = $(window).height() - $("#HeaderDiv").height();
                // var sidebarHeight = $('#SidebarDiv').height();
                // $('#SidebarDiv').height(sidebarHeight < win_height ? sidebarHeight : win_height);
                $('#SidebarDiv').height(win_height);
            }
        });
    }
}
function ff_sid_Position() {
    switch (state["currMenuPosition"]) {
        default:
        case 0: //right
            $("#img_sidpos").attr('src', collpth + img_sid_left);
            state["currMenuPosition"] = 1;
            break;
        case 1: //left
            $("#img_sidpos").attr('src', collpth + img_sid_right);
            state["currMenuPosition"] = 0;
            break;
    }
    ff_sid_updatelayout();
}
function ff_sid_Visibility() {
    switch (state["currMenuVisibility"]) {
        case 0: // visible
            state["currMenuVisibility"] = 1;
            $("#img_visible").attr('src', collpth + img_sid_off);
            break;
        case 1: // hidden
            state["currMenuVisibility"] = 0;
            $("#img_visible").attr('src', collpth + img_sid_on);
            break;
    }
    ff_sid_updatelayout();
}
function ff_sid_Sidebar() {
    switch (state["currMenuSidebar"]) {
        case 1: // old
            state["currMenuSidebar"] = 0;
            $("#img_men").attr('src', collpth + img_men_new);
            break;
        case 0: // new
            state["currMenuSidebar"] = 1;
            $("#img_men").attr('src', collpth + img_men_old);
            break;
    }
    createMenu();
}
function ff_sid_updatelayout() {
    var heightheader = $("#nav_pane_header").height();
    var heightpanorama = $("#imgpanorama").height(); // new_panorama
    var heightpanorama1 = $("#panoimg1").height(); // old panorama
    heightpanorama = (heightpanorama > heightpanorama1 ? heightpanorama : heightpanorama1);
    var currHeight = _MenuWidth[state["currHeightIndex"]];
    var sidebarWidth =  $("#SidebarDiv").width();
    $("#HeaderDiv").height(heightheader > heightpanorama ? heightheader : heightpanorama);
    $("#SidebarDiv").css({
        visibility: "visible",
        position: (_MenueFixed ? "fixed" : "absolute"),
    });
    switch (state["currMenuSidebar"]) {
        case 1: default: // wiki
            $("#SidebarMenu").css({ visibility: "visible" });
            $("#SidebarMenuControls").css({ visibility: "hidden", height: "" });
            $("#SidebarMenuPaging").css({ visibility: "hidden" });
            $("#SidebarMenuLayout").css({ visibility: "hidden", width: "0", height: "0" });
            break;
        case 0: // files
            // $("#SidebarDiv").width(_AutoWidth ? _AutoWidthMax : _MenuWidth[state["currWidthIndex"]]) + "px");
            $("#SidebarMenu").css({ visibility: "visible", width: (sidebarWidth > _AutoWidthMax ? _AutoWidthMax : sidebarWidth) + "px" });
            $("#SidebarMenuControls").css({ visibility: "visible", height: "" });
            $("#SidebarMenuPaging").css({ visibility: "visible" });
            $("#SidebarMenuLayout").css({ visibility: "visible", width: "0", height: "0" });
            break;
    }
    switch (state["currMenuPosition"]) {
        default:
        case 0: //right
            $("#SidebarDiv").css({ right: "0px", left: "", float: "right" });
            $("#MainDiv").css({ margin: "0px " + ($("#SidebarDiv").width() + 7) + "px 0px 0px" });
            $("#FooterDiv").css({ margin: "4px " + ($("#SidebarDiv").width() + 7) + "px 4px 4px" });
            break;
        case 1: //left
            $("#SidebarDiv").css({ right: "", left: "0px", float: "left" });
            $("#MainDiv").css({ margin: "0px 0px 0px " + ($("#SidebarDiv").width() + 7) + "px" });
            $("#FooterDiv").css({ margin: "4px 4px 4px " + ($("#SidebarDiv").width() + 7) + "px" });
            break;
    }
    if (state["currMenuVisibility"]==1) {
        $("#SidebarDiv").css({ visibility: "hidden" });
        $("#SidebarMenuControls").css({ visibility: "hidden" }); // 
        $("#SidebarMenuPaging").css({ visibility: "hidden" });
        $("#SidebarMenuLayout").css({ visibility: "hidden" });
        $("#SidebarMenuContent").css({ visibility: "hidden" }); // , width: "0px"
        $("#MainDiv").css({ margin: "0px" });
        $("#FooterDiv").css({ margin: "4px" });
    }
    if (_CookiesOn) CreateCookie();
}
DivBehaviour = "inline"; //"inline" or "block" (starts a new line)
function n_hide(ch, element) {
    if (element) {
        sid = document.getElementById(element);
        sid.style.visibility = "hidden";
        sid.style.width = "0";
    }
    pan_i = document.getElementById("n_i" + ch);
    pan_e = document.getElementById("n_e" + ch);
    pan_i.style.display = DivBehaviour;
    pan_e.style.display = "none";

    pan_i = document.getElementById("n_i" + ch);
}
function n_show(ch, element) {
    if (element) {
        sid = document.getElementById(element);
        sid.style.visibility = "visible";
        sid.style.width = "170";
    }
    pan_i = document.getElementById("n_i" + ch);
    pan_e = document.getElementById("n_e" + ch);
    pan_i.style.display = "none";
    pan_e.style.display = DivBehaviour;
}

/* Menu controls */

$(document).ready(function() {
    ff_sid_updatelayout();
    $(".namespacedropdown").attr("title", "select namespace");
    $('.nav_admin').mouseover(function() {
        var position = $(this).position();
        $('#nav_admin_box').show();
        $('#nav_admin_box').css('top', position.top + 12);
        $('#nav_admin_box').css('left', position.left + 10);
        $('#nav_admin_box').css('visibility', 'visible');
    })
    $('.nav_box').click(function() {
        $(this).hide();
    });
    $('.nav_sidebar').mouseover(function() {
        var position = $(this).position();
        $('#nav_sidebar_box').show();
        $('#nav_sidebar_box').css('top', position.top + 12);
        $('#nav_sidebar_box').css('left', position.left + 10);
        $('#nav_sidebar_box').css('visibility', 'visible');
    })
    // $('#nav_sidebar_box').click(function() {
    //     $(this).hide();
    // });
    $("#nav_pane_header").hover(
        function() { $(this).removeClass('nav_pane_back').addClass('nav_pane_back_hoover'); },
        function() { $(this).removeClass('nav_pane_back_hoover').addClass('nav_pane_back'); }
    );
});
function _DoSearch(id, ns_all) {
    if ((id == null) || (id == undefined)) return;
    if ((ns_all == null) || (ns_all == undefined)) ns_all = 1;
    var inp = document.getElementById(id); if ((inp == null) || (inp == undefined)) return;
    var f = document.getElementsByTagName("select"); if ((f == null) || (f == undefined)) return;
    if (f.length > 0) f = f[0];
    var ns = f.options[f.selectedIndex].value;
    if (ns == "-") ns = ""; if (ns != "") ns += ".";
    document.location = ns + 'Search.aspx?AllNamespaces=' + ns_all + '&FilesAndAttachments=1&Query=' + encodeURI(inp.value);
}
function sidebar(mode,side) {
    switch (mode) {
        case 'hide':
            state["currMenuVisibility"] = 1;
            createMenu();
            break;
        case 'reset':
            _Cookie = "";
            stateReset();
            currPage = "";
            currPage0 = "";
            currPagePrev = "";
            currPageNext = "";
            if (_CookiesOn) CreateCookie(); 
            createMenu();
            break;
        case 'wiki':
            state["currMenuSidebar"] = 1;
            state["currMenuVisibility"] = 0;
            state["currMenuPosition"] = (side == 'left' ? 1 : 0);
            createMenu();
            break;
        case 'menu':
            state["currMenuSidebar"] = 0;
            state["currMenuVisibility"] = 0;
            state["currMenuPosition"] = (side == 'left' ? 1 : 0);
            createMenu();
            break;
    }
}
