// Functions for the menu display.  This currently uses the globals onlineBase,
// offlineBase and the function convert_file_url from ../javascript.js.

var menu = new Object();
var errorDisplayed = false;
var urlMap = new Object();
var tree;
var baseUrl;
var locationUndetermined = false;
var initDone = false;

// Some initialization:
function init()
{
    // Determine if we're online or offline, and pull out the part of the URL
    // relative to the root:
    var myUrl = document.URL.replace(/\%20/g," ");
    var hashIndex = myUrl.indexOf(".htm#");
    if (hashIndex>-1)
    {
        hashIndex = hashIndex+4;
        myUrl = myUrl.substr(0,hashIndex);
    }
    if (myUrl.indexOf(onlineBase)==0)
    {
        var relUrl = myUrl.substr(onlineBase.length+1);
        baseUrl = onlineBase;
    }
    else
    {
        var thisUrl = myUrl;
        
        offlineBase = convert_file_url(offlineBase);
        var startIndex = thisUrl.indexOf(offlineBase);
        if (startIndex>=0)
        {
            var relUrl = thisUrl.substr(startIndex+offlineBase.length+1);
            relUrl = relUrl.replace(/\\/g,"/");
            baseUrl = offlineBase;
        }
        else
        {
            // alert("Illegal document url ("+thisUrl+") passed to "+
                // "show_menu().");
            locationUndetermined = true;
            return;
        }
    }

    if (locationUndetermined)
    {
        return;
    }
    tree = urlMap[relUrl];
    initDone = true;
}

// Call this function to show the menu:
function show_menu()
{
    if (!initDone)
    {
        init();
    }
    if (locationUndetermined)
    {
        return;
    }
    show_menu2(menu,0);
}

// Show a branch of the tree.  This assumes the global variable tree has been
// defined, with an array of page names.
function show_menu2(ptr,level)
{
    var i;
    if (ptr==menu)
    {
        // The top level.
        for (i=0; i<ptr["list"].length; i++)
        {
            show_menu2(ptr["list"][i],0);
        }
        return;
    }

    // Show current item in a table:
    font = "<font face=\"Arial, Helvetica, Geneva\" size=2>";
    document.write("<table border=0 cellpadding=1 cellspacing=1>\n");
    document.write("<tr>");
    for (i=0; i<level; i++)
    {
        if (i==(level-1))
        {
            document.write("<td valign=\"top\">"+font+"&nbsp;-</font></td>\n");
        }
        else
        {
            document.write("<td>&nbsp;&nbsp;</td>\n");
        }
    }
    if (level==tree.length-1 && tree[level]==ptr["name"])
    {
        // We're at the page currently being displayed.  Give this some 
        // special highlighting in the display.
        document.write("<td>"+font+"<b>"+ptr["name"]+"</b></font></td>\n");
    }
    else
    {
        document.write('<td>'+font+'<a class="tree_menu" href="'+
            convert_file_url(baseUrl+"/"+ptr["url"])+'">'+ptr["name"]+
            '</a></font></td>');
        // document.write("<td>"+font+ptr["name"].link(convert_file_url(baseUrl+
            // "/"+ptr["url"]))+"</font></td>");
    }
    document.write("</tr></table>\n");

    // Show all items underneath, if they exist and are part of the tree 
    // heirarchy we are in.
    if (tree[level] == ptr["name"])
    {
        if (ptr["list"])
        {
            for (i=0; i<ptr["list"].length; i++)
            {
                show_menu2(ptr["list"][i],level+1);
            }
        }
    }
}
        
// Add an item to the tree (ai = add item):
// Each node in the tree has the following attributes:
// url:  The URL to go to if clicked on (may be blank).
// name: The name to display.
// list: Points to an array of nodes within the current one.
function ai(list,url)
{
    // Store a mapping between the url and the heirarchy:
    urlMap[url] = list;

    // Descend the tree until we get to the item we want to add.
    var ptr = menu;
    var newName = list[list.length-1];
    var i,newIndex;
    for (i=0; i<list.length-1; i++)
    {
        ptr = find_node(ptr,list[i]);
        if (ptr==null)
        {
            if (!errorDisplayed)
            {
                alert("Cannot find menu item '"+list[i]+"'.");
                errorDisplayed = true;
            }
            return;
        }
    }

    if (!ptr["list"])
    {
        ptr["list"] = new Array();
    }
    newIndex = ptr["list"].length;
    ptr["list"][newIndex] = new Object;
    ptr["list"][newIndex]["url"] = url;
    ptr["list"][newIndex]["name"] = newName;
}

// Given a pointer to a node and a name, return a pointer to the node within
// which has that name.  Returns null if not found.
function find_node(ptr,name)
{
    var j;
    for (j=0; j<ptr["list"].length; j++)
    {
        if (ptr["list"][j]["name"] == name)
        {
            return ptr["list"][j];
        }
    }
    return null;
}

// Show the navigation bar:
function show_nav_bar()
{
    if (!initDone)
    {
        init();
    }

    if (locationUndetermined)
    {
        return;
    }

    // Display the page title and top level of the tree:
    var title = tree[tree.length-1];
    document.write('<table bgcolor=#000000 width="100%" cellpadding=3 '+
        'cellspacing=0>');
    document.write("<tr>");
    document.write('<td><font face="Arial,Helvetica,Geneva" size=4 '+
        'color=#FFB559><b>'+title+'</b></font><br>');
    document.write('<font size=2 face="Arial,Helvetica,Geneva" color=#FFFFFF>'+
        '<a class="nav_bar" href="/tutorials/index.htm">'+
        'Tutorials</a>');

    // Display the rest of the hierarchy:
    var ptr = menu;
    for (var i=0; i<tree.length; i++)
    {
        ptr = find_node(ptr,tree[i]);
        if (ptr==null)
        {
            if (!errorDisplayed)
            {
                alert("Cannot find menu item '"+list[i]+"'.");
                errorDisplayed = true;
            }
            return;
        }
        var convertedUrl = convert_file_url(baseUrl+"/"+ptr["url"]);
        document.write(' > <a class="nav_bar" href="'+convertedUrl+'">'+tree[i]+
            '</a>');
    }
    document.write('</td>');
    
    // Display links to the site map and search functions:
    document.write('<td align="right" width=100><font size=2 '+
        'face="Arial, Helvetica, Geneva">'+
        '<a class="nav_bar" href="'+convert_file_url(baseUrl+"/"+siteMapUrl)+
        '">Site Map</a></font></td>');
    document.write('</tr></table>');
}
