var nodes_array = new Array()
var current_document = -1
// Pre-load the images
var temp_image = new Image(22, 16)
temp_image.src = "/images/open.gif"
var temp_image = new Image(22, 16)
temp_image.src = "/images/closed.gif"
var temp_image = new Image(18, 13)
temp_image.src = "/images/plus.gif"
var temp_image = new Image(18, 13)
temp_image.src = "/images/minus.gif"
var temp_image = new Image(22, 16)
temp_image.src = "/images/open_page.gif"
var temp_image = new Image(22, 16)
temp_image.src = "/images/closed_page.gif"

function node_data (level, index, text, state, url) {
    this.level = level
    this.index = index
    this.text = text
    this.state = state
    this.url = url
}

function create_node(level, text, state, url) 
{
    var new_node = new Array()
    var nodes_index = nodes_array.length
    nodes_array[nodes_index] = new_node
    new_node[0] = new node_data(level, nodes_index, text, state, url)
    return new_node
}

function add_child(parent_node, child_node) 
{
    var next_index = parent_node.length
    parent_node[next_index] = child_node
    return child_node
}


function write_menu() 
{
    frames["tree_frame"].document.clear()
    frames["tree_frame"].document.writeln('<html>')
    frames["tree_frame"].document.writeln('<head>')
    frames["tree_frame"].document.writeln('<title>')
    frames["tree_frame"].document.writeln('Menu Frame')
    frames["tree_frame"].document.writeln('<\/title>')
    frames["tree_frame"].document.writeln('<style type="text/css">')
    frames["tree_frame"].document.writeln('<!--')
    frames["tree_frame"].document.writeln('td {font-size: 8pt; font-family: Arial, Helvetica}')
    frames["tree_frame"].document.writeln('-->')
    frames["tree_frame"].document.writeln('<\/style>')
    frames["tree_frame"].document.writeln('<\/head>')
    frames["tree_frame"].document.writeln('<body>')
    frames["tree_frame"].document.writeln('<font style="font-size: 12pt; font-family: Arial, Helvetica">Navigation Menu<\/font>')
    frames["tree_frame"].document.writeln('<hr>')
    frames["tree_frame"].document.writeln('<font style="font-size: 8pt; font-family: Arial, Helvetica">')
    frames["tree_frame"].document.writeln('<a href="javascript:parent.change_all(\'open\')"><img src="/images/plus.gif" border="0"></a>Expand All&nbsp;')
    frames["tree_frame"].document.writeln('<a href="javascript:parent.change_all(\'closed\')"><img src="/images/minus.gif" border="0"></a>Collapse All<p>')
    frames["tree_frame"].document.writeln('<\/font>')
    
    // Create a table for each node
    frames["tree_frame"].document.writeln('<table border="0" cellspacing="0" cellpadding="0">')
    
    // Write the main node
    frames["tree_frame"].document.writeln('<tr>')
    frames["tree_frame"].document.writeln('<td valign="top">')
    frames["tree_frame"].document.writeln('<a href="javascript:parent.toggle_state(\'' + main_node[0].index + '\')">')
    
    if (main_node[0].state == "closed") {
        frames["tree_frame"].document.writeln('<img src="/images/plus.gif" border="0"></a><img src="/images/closed.gif">')
    }
    else {
        frames["tree_frame"].document.writeln('<img src="/images/minus.gif" border="0"></a><img src="/images/open.gif">')
    }
    frames["tree_frame"].document.writeln('<\/td>')
    frames["tree_frame"].document.writeln('<td>')
    frames["tree_frame"].document.writeln(main_node[0].text)
    frames["tree_frame"].document.writeln('<\/td>')
    frames["tree_frame"].document.writeln('<\/tr>')
    frames["tree_frame"].document.writeln("<\/table>")

    // If the main node state is "open", write the child nodes
    if (main_node[0].state == "open") {
        write_children(main_node)
    }
   
    // Finish up
    frames["tree_frame"].document.writeln('<\/body>')
    frames["tree_frame"].document.writeln('<\/html>')
    frames["tree_frame"].document.close()

}

function write_children(parent_node) {

    var child_node
    var indent_width
        
    // Run through all of the parent's child nodes
    // parent_node[0] refers to the parent node itself, so start at 1
    for (var counter = 1; counter < parent_node.length; counter++) {
    
        // Store the child node
        child_node = parent_node[counter]
        
        // First check to see if this is a folder or a document
        var its_a_folder = true
        if (child_node.length == 1) {
            its_a_folder = false
        }
        frames["tree_frame"].document.writeln('<table border="0" cellspacing="0" cellpadding="0">')
        frames["tree_frame"].document.writeln('<tr>')
        frames["tree_frame"].document.writeln('<td>')
        indent_width = child_node[0].level * 20
        if (!its_a_folder) {
            indent_width += 20
        }
        frames["tree_frame"].document.writeln('<td>')
        frames["tree_frame"].document.writeln('<img src="/images/invisible.gif" width="' + indent_width + '" height="10">')
        frames["tree_frame"].document.writeln('<\/td>')
        frames["tree_frame"].document.writeln('<td valign="top">')
        if (its_a_folder) {
            frames["tree_frame"].document.writeln('<a href="javascript:parent.toggle_state(\'' + child_node[0].index + '\')">')
            if (child_node[0].state == "closed") {
                 frames["tree_frame"].document.write('<img src="/images/plus.gif" border="0"></a><img src="/images/closed.gif">')
            }
            else {
                frames["tree_frame"].document.write('<img src="/images/minus.gif" border="0"></a><img src="/images/open.gif">')
            }
            frames["tree_frame"].document.writeln('<\/td>')
            frames["tree_frame"].document.writeln('<td>')
            frames["tree_frame"].document.writeln(child_node[0].text)
            
            frames["tree_frame"].document.writeln('<\/td>')
            frames["tree_frame"].document.writeln('<\/tr>')
            frames["tree_frame"].document.writeln('<\/table>')
            if (child_node[0].state == "open") {
                write_children(child_node)
            }
        }
        else {       
           if (child_node[0].index == current_document) {
                frames["tree_frame"].document.writeln('<img src="/images/open_page.gif">')
                frames["tree_frame"].document.writeln('<\/td>')
                frames["tree_frame"].document.writeln('<td>')
                frames["tree_frame"].document.writeln(child_node[0].text)
            }
            else {
                frames["tree_frame"].document.writeln('<a href="javascript:parent.toggle_state(\'' + child_node[0].index + '\')">' + 
                '<img src="/images/closed_page.gif" border="0">')
                frames["tree_frame"].document.writeln('<\/td>')
                frames["tree_frame"].document.writeln('<td>')
                frames["tree_frame"].document.writeln('<a href="javascript:parent.toggle_state(\'' + child_node[0].index + '\')">') 
                frames["tree_frame"].document.writeln(child_node[0].text + '</a>')
            }                        
            frames["tree_frame"].document.writeln('<\/td>')
            frames["tree_frame"].document.writeln('<\/tr>')
            frames["tree_frame"].document.writeln('<\/table>')
        }

        frames["tree_frame"].document.writeln('<\/td>')
        frames["tree_frame"].document.writeln('<\/tr>')
    }
}

function toggle_state(node_index) {
    var current_node = nodes_array[node_index]
    current_state = current_node[0].state
    if (current_state == "open") {
        current_node[0].state = "closed"
    }
    else {
        current_node[0].state = "open"
    }
    if (current_node.length == 1) {
        if (current_document != -1) {
            nodes_array[current_document].state = "closed"
        }
        current_document = current_node[0].index
        frames["content_frame"].location = current_node[0].url
    }
    timeout_id = setTimeout("write_menu()", 50)
}

function change_all(new_state) 
{
    var current_node
    // Run through the global nodes_array
    for (counter = 0; counter < nodes_array.length; counter++) {
        current_node = nodes_array[counter]       
        if (current_node.length > 1) {
            current_node[0].state = new_state
        }
    }
   timeout_id = setTimeout("write_menu()", 50)
}

