function Node(id, name, link, parent, target) {
    this.id = id;
    this.name = name;
    if (link) this.link = link;
    this.parent = parent;
    if (target) this.target = target;
    this.nodes = new Array(0);
}

Node.prototype.isRoot = function() {
    return (this.parent == null);
}

Node.prototype.addNode = function(id, name, link, target) {
    var node = new Node(id, name, link, this, target);
    this.nodes[this.nodes.length] = node;
    return node;
}

Node.prototype.findNode = function(id) {
    if (this.id == id) return this;
    for (var i = 0; i < this.nodes.length; i++) {
        var node = this.nodes[i];
        node = node.findNode(id);
        if (node != null) return node;
    }
    return null;
}

Node.prototype.findPrev = function(id) {
    for (var i = 0; i < this.nodes.length; i++) {
        var node = this.nodes[i];
        if (i > 0 && node.id == id) return this.nodes[i - 1];
        node = node.findPrev(id);
        if (node != null) return node;
    }
    return null;
}

Node.prototype.findNext = function(id) {
    for (var i = 0; i < this.nodes.length; i++) {
        var node = this.nodes[i];
        if (i < this.nodes.length - 1 && node.id == id) return this.nodes[i + 1];
        node = node.findNext(id);
        if (node != null) return node;
    }
    return null;
}

Node.prototype.writeShortCut = function() {
    if (this.parent != null && !this.parent.isRoot()) {
        this.parent.writeShortCut();
        document.write(" &raquo; ");
    }
    if (this.link) {
        document.write("<a alt=\"" + this.name + "\" href=\"" + this.link + "\">");
        document.write(this.name);
        document.write("</a>");
    }
    else {
        document.write(this.name + " ");
    }
}

Node.prototype.writeTree = function(cls) {
    document.write("<li>");
    if (this.link) {
        document.write("<a alt=\"" + this.name + "\" href=\"" + this.link + "\"");
        if (this.target) document.write(" target=\"blank\"");
        document.write(">");
        document.write(this.name);
        document.write("</a>");
    }
    else {
        document.write(this.name);
    }
    if (this.nodes.length > 0) {
        document.write("<ul class=\"" + cls + "\">");
        for (var i = 0; i < this.nodes.length; i++) {
            var node = this.nodes[i];
            node.writeTree(cls);
        }
        document.write("</ul>");
    }
    document.write("</li>");
}

Node.prototype.getTree = function(cls) {
    var output = "";
    output += "<li>";
    if (this.link) {
        output += "<a alt=\"" + this.name + "\" href=\"" + this.link + "\"";
        if (this.target) output += " target=\"blank\"";
        output += ">";
        output += this.name;
        output += "</a>";
    }
    else {
        output += this.name;
    }
    if (this.nodes.length > 0) {
        output += "\n<ul class=\"" + cls + "\">\n";
        for (var i = 0; i < this.nodes.length; i++) {
            var node = this.nodes[i];
            output += node.getTree(cls);
        }
        output += "</ul>\n";
    }
    output += "</li>\n";
    return output;
}

var sitemap;

function createSitemap(id, prefix) {
    if (prefix == null) prefix = "";

    sitemap = new Node("home", "HOME", prefix + "index.html", null);

    var node, subnode;

    sitemap.addNode("info", "Information");
    sitemap.addNode("moodle", "Gong and Moodle");
    sitemap.addNode("try", "Try It!");
    sitemap.addNode("downloads", "Downloads");
    sitemap.addNode("doc", "Related Documentations");
    sitemap.addNode("aboutus", "About Us", prefix + "aboutus.html");
    sitemap.addNode("sitemap", "Sitemap", prefix + "sitemap.html");

    node = sitemap.findNode("info");
    node.addNode("basic", "Basic Operations", prefix + "features_basic.html");
    node.addNode("flist", "Feature List", prefix + "features_list.html");
    node.addNode("desc", "Description of Features", prefix + "features_desc.html");
    node.addNode("standalone", "Gong as a Standalone System");
    node.addNode("integrate", "Gong Integrated with Other Systems");
    node.addNode("app", "Gong Applet vs Gong Application", prefix + "features_app.html");
    node.addNode("nano", "NanoGong", prefix + "features_nano.html");

    node = sitemap.findNode("standalone");
    node.addNode("webmenu", "Web Resource Menu", prefix + "features_webmenu.html");
    node.addNode("hiboard", "Hierarchical Board Organization", prefix + "features_hiboard.html");
    node.addNode("search", "Advanced Message Search", prefix + "features_search.html");
    node.addNode("chat", "Real-time Text/Voice Chat", prefix + "features_chat.html");
    node.addNode("pmessage", "Personal Messaging", prefix + "features_pmessage.html");
    node.addNode("mini", "Compact Version - MiniGong", prefix + "features_mini.html");
    node.addNode("mui", "Multilingual Interface", prefix + "features_mui.html");
    node.addNode("stream", "Streamed Playback", prefix + "features_stream.html");
    node.addNode("unicode", "Support for Multiple Languages", prefix + "features_unicode.html");
    node.addNode("html", "Styled Text Editing", prefix + "features_html.html");
    node.addNode("voiceedit", "Voice Editing", prefix + "features_voice.html");
    node.addNode("selectplay", "Selective Word/Phrase Playback", prefix + "features_selectplay.html");
    node.addNode("speed", "Voice Speed Up/Slow Down", prefix + "features_speed.html");
    node.addNode("analysis", "Voice Analysis", prefix + "features_analysis.html");
    node.addNode("pinyin", "Chinese Mandarin Pinyin Romanization System", prefix + "features_pinyin.html");
    node.addNode("yale", "Chinese Cantonese Yale Romanization System", prefix + "features_yale.html");

    node = sitemap.findNode("integrate");
    node.addNode("auth", "Authentication", prefix + "features_auth.html");
    node.addNode("param", "Startup Parameters", prefix + "features_param.html");
    node.addNode("paramex", "Startup Parameters Examples", prefix + "features_param_example.html");
    node.addNode("script", "Real-time Control", prefix + "features_script.html");
    node.addNode("scriptex", "Real-time Control Examples", prefix + "features_script_example.html");

    node = sitemap.findNode("moodle");
    node.addNode("module", "Gong Moodle Module", prefix + "moodle_module.html");
    node.addNode("moodlefeatures", "Features Supported by Gong as a Moodle Module");
    node.addNode("moodleinst", "Setup Instructions", prefix + "moodle_inst.html");

    node = sitemap.findNode("moodlefeatures");
    node.addNode("webmenu", "Web Resource Menu", prefix + "features_webmenu.html");
    node.addNode("mui", "Multilingual Interface", prefix + "features_mui.html");
    node.addNode("stream", "Streamed Playback", prefix + "features_stream.html");
    node.addNode("unicode", "Support for Multiple Languages", prefix + "features_unicode.html");
    node.addNode("html", "Styled Text Editing", prefix + "features_html.html");
    node.addNode("voiceedit", "Voice Editing", prefix + "features_voice.html");
    node.addNode("selectplay", "Selective Word/Phrase Playback", prefix + "features_selectplay.html");
    node.addNode("speed", "Voice Speed Up/Slow Down", prefix + "features_speed.html");
    node.addNode("analysis", "Voice Analysis", prefix + "features_analysis.html");
    node.addNode("pinyin", "Chinese Mandarin Pinyin Romanization System", prefix + "features_pinyin.html");
    node.addNode("yale", "Chinese Cantonese Yale Romanization System", prefix + "features_yale.html");

    node = sitemap.findNode("try");
    node.addNode("trystandalone", "Try Gong as a Standalone System");
    node.addNode("trymoodle", "Try Gong as a Moodle Module", "/moodle");
    node.addNode("javatest", "Java Test for Gong Applet", prefix + "try_javatest.html");
    node.addNode("tryreq", "Requirements", prefix + "try_req.html");

    node = sitemap.findNode("trystandalone");
    node.addNode("tryapp", "Try the Gong Application", prefix + "try.php?version=5.0.6CA");
    node.addNode("tryapplet", "Try the Gong Applet", prefix + "try.php?version=5.0.6C");
    node.addNode("trymini", "Try the MiniGong Applet", prefix + "try.php?version=5.0.6CM");

    node = sitemap.findNode("downloads");
    node.addNode("downloadsform", "Download Form", prefix + "downloads_form.html");
    node.addNode("setup", "Setting up a Gong System");
    node.addNode("policy", "Software Policy and License", prefix + "downloads_policy.html");
    node.addNode("futuredev", "Future Developments", prefix + "futuredev_form.html");

    node = sitemap.findNode("setup");
    node.addNode("downloadsinst", "Setup Instructions", prefix + "downloads_instructions.html");
    node.addNode("downloadsinstaller", "Creating Gong Client Installer", prefix + "downloads_installer_form.html");
    node.addNode("downloadsserver", "Generating Gong Server INI File", prefix + "downloads_server_wizard.php");

    node = sitemap.findNode("doc");
    node.addNode("faq", "Frequently Asked Questions (FAQ)", prefix + "doc_faq.html");
    node.addNode("userguide", "User Guide", prefix + "doc_userguide.html");
    node.addNode("api", "Gong Scripting API Reference", prefix + "doc_api.html");
    node.addNode("publication", "Related Publications", prefix + "doc_publish.html");
    node.addNode("usage", "Usage &amp; Statistics", prefix + "info_usage.html");

    writeShortCut(id);
}

function writeShortCut(id) {
    var node = sitemap.findNode(id);
    if (node != null && !node.isRoot()) {
        document.write("[");
        node.writeShortCut();
        document.write("]");
    }
}

function writeSitemap() {
    document.write("<ul class=\"sitemap_node\">");
    sitemap.writeTree("sitemap_node");
    document.writeln("</ul>");
}

function getSitemap() {
    var output = "";
    output += "<ul class=\"sitemap_node\">\n";
    output += sitemap.getTree("sitemap_node");
    output += "</ul>\n";
    return output;
}

