////////////////////////////////////////////////////////////////////////
//
// Filename: menu.js
// Purpose : Handle the menu
// Method  : xhtml
//
// Author  : J.van.der.Steen@gobase.org
// Date    : 2010-03-07
//
////////////////////////////////////////////////////////////////////////

var currentMenuItem  = null;
var currentMenuTopic = null;

/**
 * Initialize the menu
 */
function menuInit(menu, parent)
{
    for (var n = 0; n < menu.length; n++) {
        var item = menu[n];

        if (item.parent === undefined) {
            item.parent = parent;
        }
        if (item.restricted === undefined) {
            item.restricted = false;
        }
        if (item.guid === undefined) {
            item.guid = config.lookupTable.add(item);
        }
        if (item.tipover === undefined) {
            item.tipover = '';
        }
        if (item.topics !== undefined) {
            menuInit(item.topics, item);
        }
    }
}

/**
 * Display supplied menu inside element with supplied ID
 */
function menuLoad(menu, id)
{
    var html = menuToHTML(menu);

    switch (id) {

        case 'menumain':
            Element.update('menutopics', '');
            break;

        case 'menutopics':
            var style = 'text-align: right; margin: 0px 4px 4px 0px;';
            html = "<h2 style='" + style + "'>vanaf deze pagina</h2>\n" + html;
            break;

    }

    Element.update(id, html);
}


/**
 * Render supplied menu definition
 */
function menuToHTML(menu)
{
    var html = '';

    for (var n = 0; n < menu.length; n++) {
        var item = menu[n];

        if (config.user.group === 'visitor' && item.restricted === true) {
            continue;
        }
        html += "<DIV"
              + " class='item'"
              + " title='"   + item.tipover + "'"
              + " id='menu-" + item.guid    + "'"
              + " onclick='contentLoad(\"" + item.guid + "\");'"
              + ">";
        html += item.title;
        html += "</DIV>\n";
    }

    return html;
}

function menuSelect(guid)
{
    var item = config.lookupTable.get(guid);

    if (item === null) {
        return;
    }

    var elem = getE('menu-' + item.guid);

    if (elem !== null) {
        removeClassName(elem, 'item');
           addClassName(elem, 'itemselected');
    }
}

function menuDeselect(guid)
{
    var item = config.lookupTable.get(guid);

    if (item === null) {
        return;
    }

    var elem = getE('menu-' + item.guid);

    if (elem !== null) {
        removeClassName(elem, 'itemselected');
           addClassName(elem, 'item');
    }
}

/**
 * Print the menu
 */
function menuPrint(menu, parent)
{
    var html = '';

    html += "<DL>\n";
    for (var n = 0; n < menu.length; n++) {
        var item = menu[n];

        html += "<DT>";
        html += "<DIV"
              + " class='item'"
              + " title='"         + item.tipover + "'"
              + " id='menu-print-" + item.guid    + "'"
              + " onclick='contentLoad(\"" + item.guid + "\");'"
              + ">";
        html += item.title;
        html += "</DIV>";
        html += "</DT>";
        if (item.topics !== undefined) {
            html += "<DD>\n";
            html += menuPrint(item.topics, item);
            html += "</DD>\n";
        }
    }
    html += "</DL>\n";

    return html;
}

/**
 * Supplied item is selected, make sure the menu reflects this selection.
 */
function menuReflectSelection(guid)
{
    var item = config.lookupTable.get(guid);

    if (item === null) {
        return null;
    }

    var newMenuItem;
    var newMenuTopic;

    if (item.parent === null) {
        newMenuItem  = guid;
        newMenuTopic = null;
    } else {
        newMenuItem  = item.parent.guid;
        newMenuTopic = guid;
    }

    if (currentMenuItem != newMenuItem) {
        menuDeselect(currentMenuItem);
        currentMenuItem = newMenuItem;
        menuSelect(currentMenuItem);

        //
        // Display new topics
        //
        var root = config.lookupTable.get(currentMenuItem);
        if (root.topics !== undefined) {
            menuLoad(root.topics, 'menutopics');
        } else {
            Element.update('menutopics', '');
        }
    }
    if (currentMenuTopic != newMenuTopic) {
        menuDeselect(currentMenuTopic);
        currentMenuTopic = newMenuTopic;
        menuSelect(currentMenuTopic);
    }

    return item;
}

/**
 * Generate and emit the sitemap
 */
function emitSitemap()
{
    var elem = null;

    if ((elem = getE('content')) !== null) {
        var html = menuPrint(menu, null);
        html = "<center style='position: relative; top: -20px;>\n"      // was: -220px
             + "<div style='font-size:0.8em;'>\n"
             + "<table summary='sitemap'>"
             + "<tr><td style='padding: 16px;'>"
             + html
             + "</td></tr>"
             + "</table>\n"
             + "</div>\n"
             + "</center>\n"
             ;
        Element.update('content', html);
    }
}


