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", "Nano in 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("applet", "The NanoGong Applet", prefix + "info_applet.html");
    node.addNode("features", "Feature List", prefix + "info_features.html");
    node.addNode("config", "Configure NanoGong", prefix + "info_config.html");
    node.addNode("script", "Control NanoGong Using Script", prefix + "info_script.html");
    node.addNode("infophp", "Using NanoGong with PHP", prefix + "info_php.html");
    node.addNode("result", "Results of NanoGong 4.1 Testing", prefix + "nanogong41_test.html");

    node = sitemap.findNode("moodle");
    node.addNode("moodleinfo", "Overview", prefix + "moodle.html");
    node.addNode("moodleinst", "Installation Instructions", prefix + "moodle_inst.html");
    node.addNode("moodleguide", "User Guide", prefix + "moodle_guide.html");

    node = sitemap.findNode("try");
    node.addNode("trynano", "Try NanoGong", prefix + "try.php");
    node.addNode("trymoodle", "Try NanoGong in Moodle", "../moodle");
    node.addNode("javatest", "Java Test for NanoGong", prefix + "try_javatest.html");
    node.addNode("tryreq", "Requirements", prefix + "try_req.html");

    node = sitemap.findNode("downloads");
    node.addNode("downloadsform", "Download Form", prefix + "downloads_form.html");
    node.addNode("policy", "Software Policy and License", prefix + "downloads_policy.html");

    node = sitemap.findNode("doc");
    node.addNode("faq", "Frequently Asked Questions (FAQ)", prefix + "doc_faq.html");
    node.addNode("download_stat", "Statistics", prefix + "info_stat.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;
}

