function getOffsetLeft(element) {
    var left = element.offsetLeft;
    while (element.offsetParent) {
        left += element.offsetParent.offsetLeft;
        element = element.offsetParent;
    }
    return left;
}

function getOffsetTop(element) {
    var top = element.offsetTop;
    while (element.offsetParent) {
        top += element.offsetParent.offsetTop;
        element = element.offsetParent;
    }
    return top;
}

function showMenu(id) {
    var menu = document.getElementById(id);

    var parent = menu.parentNode.parentNode;
    if (parent.id != "navmenu") showMenu(parent.id);

    menu.style.visibility = 'visible';
}

function hideMenu(id) {
    var menu = document.getElementById(id);

    var parent = menu.parentNode.parentNode;
    if (parent.id != "navmenu") hideMenu(parent.id);

    menu.style.visibility = 'hidden';
}

function Menu(id, cls, left, top, width) {
    this.id = id;
    this.cls = cls;
    this.left = left;
    this.top = top;
    this.width = width;
    this.items = new Array(0);
}

Menu.prototype.addItem = function(id, cls, label, desc, link, target, italic) {
    var item = new Item(id, cls, label, desc, link, target, italic);
    item.menu = this;
    this.items[this.items.length] = item;
    return item;
}

Menu.prototype.findItem = function(id) {
    for (var i = 0; i < this.items.length; i++) {
        var item = this.items[i];
        if (item.id == id) return item;
    }
    return null;
}

Menu.prototype.writeln = function() {
    document.write("<div id='" + this.id + "' class='" + this.cls + "'");
    if (isNaN(this.width))
        document.write(" style='left: " + this.left + "px; top: " + this.top + "px'");
    else
        document.write(" style='left: " + this.left + "px; top: " + this.top + "px; width: " + this.width + "px'");
    if (this.cls != "navmenu") {
        document.write(" onmouseover=\"showMenu('" + this.id + "')\"");
        document.write(" onmouseout=\"hideMenu('" + this.id + "')\"");
    }
    document.write(">"); 

    for (var i = 0; i < this.items.length; i++) this.items[i].writeln(this.width);
    document.writeln("</div>");
}

function Item(id, cls, label, desc, link, target, italic) {
    this.id = id;
    this.cls = cls;
    this.label = label;
    this.desc = desc;
    this.link = (link)? link : "#";
    if (target) this.target = target;
    if (italic) this.italic = italic;
}

Item.prototype.setSubMenu = function(subMenu) {
    this.subMenu = subMenu;

    subMenu.parent = this;
}

Item.prototype.writeln = function(width) {
    document.write("<div id='" + this.id + "' class='" + this.cls + "'>");
    document.write("<a alt='" + this.desc + "' href='" + this.link + "'");
    if (!isNaN(width)) document.write(" style='width: " + (width - 10) + "px'");
    if (this.subMenu) {
        document.write(" onmouseover=\"showMenu('" + this.subMenu.id + "')\"");
        document.write(" onmouseout=\"hideMenu('" + this.subMenu.id + "')\"");
    }
    if (this.target) document.write(" target='blank'");
    document.write(">");
    if (this.italic)
        document.write("<i>" + this.label + "</i>");
    else
        document.write(this.label);
    if (this.subMenu) document.write(" <span class=\"next_icon\">&gt;</span>");
    document.write("</a>");
    if (this.subMenu) this.subMenu.writeln();
    document.writeln("</div>");
}

function createMenu(prefix) {
    var width = 110, height = 20, submenu_height = 30;

    if (prefix == null) prefix = "";

    navmenu = new Menu("navmenu", "navmenu", 0, 0);
    navmenu.addItem("home", "navitem", "HOME", "The Homepage of the Gong Project", prefix + "index.html");
    navmenu.addItem("info", "navitem", "Information", "Information");
    navmenu.addItem("moodle", "navitem", "Gong &amp; Moodle", "Gong and Moodle");
    navmenu.addItem("try", "navitem", "Try It!", "Try It!");
    navmenu.addItem("downloads", "navitem", "Downloads", "Downloads");
    navmenu.addItem("doc", "navitem", "Documentation", "Related Documentations");
    navmenu.addItem("aboutus", "navitem", "About Us", "About Us", prefix + "aboutus.html");
    
    info = new Menu("info_menu", "navsubmenu", width, height, 240);
    info.addItem("basic", "navsubitem", "Basic Operations", "Basic Operations", prefix + "features_basic.html");
    info.addItem("flist", "navsubitem", "Feature List", "Feature List", prefix + "features_list.html");
    info.addItem("desc", "navsubitem", "Description of Features", "Description of Features", prefix + "features_desc.html");
    info.addItem("standalone", "navsubitem", "Gong as a Standalone System", "Gong as a Standalone System");
    info.addItem("integrate", "navsubitem", "Gong Integrated with Other Systems", "Gong Integrated with Other Systems");
    info.addItem("app", "navsubitem", "Gong Applet vs Gong Application", "Gong Applet vs Gong Application", prefix + "features_app.html");
    info.addItem("nano", "navsubitem", "NanoGong", "NanoGong", prefix + "features_nano.html");
    navmenu.findItem("info").setSubMenu(info);

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

    integrate = new Menu("integrate_menu", "navsubmenu", 240 - 1, 4 * submenu_height - 14, 200);
    integrate.addItem("auth", "navsubitem", "Authentication", "Authentication", prefix + "features_auth.html");
    integrate.addItem("param", "navsubitem", "Startup Parameters", "Startup Parameters", prefix + "features_param.html");
    integrate.addItem("paramex", "navsubitem", "Startup Parameters Examples", "Startup Parameters Examples", prefix + "features_param_example.html");
    integrate.addItem("script", "navsubitem", "Real-time Control", "Real-time Control", prefix + "features_script.html");
    integrate.addItem("scriptex", "navsubitem", "Real-time Control Examples", "Real-time Control Examples", prefix + "features_script_example.html");
    info.findItem("integrate").setSubMenu(integrate);
    
    moodle = new Menu("moodle_menu", "navsubmenu", (width + 1) * 2 - 1, height, 300);
    moodle.addItem("module", "navsubitem", "Gong Moodle Module", "Gong Moodle Module", prefix + "moodle_module.html");
    moodle.addItem("moodlefeatures", "navsubitem", "Features Supported by Gong as a Moodle Module", "Features Supported by Gong as a Moodle Module");
    moodle.addItem("moodleinst", "navsubitem", "Setup Instructions", "Setup Instructions", prefix + "moodle_inst.html");
    navmenu.findItem("moodle").setSubMenu(moodle);

    moodlefeatures = new Menu("moodlefeatures_menu", "navsubmenu", 300 - 1, submenu_height - 2, 200);
    moodlefeatures.addItem("webmenu", "navsubitem", "Web Resource Menu", "Web Resource Menu", prefix + "features_webmenu.html");
    moodlefeatures.addItem("mui", "navsubitem", "Multilingual Interface", "Multilingual Interface", prefix + "features_mui.html");
    moodlefeatures.addItem("stream", "navsubitem", "Streamed Playback", "Streamed Playback", prefix + "features_stream.html");
    moodlefeatures.addItem("unicode", "navsubitem", "Support for Multiple Languages", "Support for Multiple Languages", prefix + "features_unicode.html");
    moodlefeatures.addItem("html", "navsubitem", "Styled Text Editing", "Styled Text Editing", prefix + "features_html.html");
    moodlefeatures.addItem("voiceedit", "navsubitem", "Voice Editing", "Voice Editing", prefix + "features_voice.html");
    moodlefeatures.addItem("selectplay", "navsubitem", "Selective Word/Phrase Playback", "Selective Word/Phrase Playback", prefix + "features_selectplay.html");
    moodlefeatures.addItem("speed", "navsubitem", "Voice Speed Up/Slow Down", "Voice Speed Up/Slow Down", prefix + "features_speed.html");
    moodlefeatures.addItem("analysis", "navsubitem", "Voice Analysis", "Voice Analysis", prefix + "features_analysis.html");
    moodlefeatures.addItem("pinyin", "navsubitem", "Mandarin Pinyin Support", "Mandarin Pinyin Support", prefix + "features_pinyin.html");
    moodlefeatures.addItem("yale", "navsubitem", "Cantonese Yale Support", "Cantonese Yale Support", prefix + "features_yale.html");
    moodle.findItem("moodlefeatures").setSubMenu(moodlefeatures);
    
    tryit = new Menu("try_menu", "navsubmenu", (width + 1) * 3 - 1, height, 210);
    tryit.addItem("trystandalone", "navsubitem", "Try Gong as a Standalone System", "Try Gong as a Standalone System");
    tryit.addItem("trymoodle", "navsubitem", "Try Gong as a Moodle Module", "Try Gong as a Moodle Module", "/moodle/");
    tryit.addItem("javatest", "navsubitem", "Java Test for Gong Applet", "Java Test for Gong Applet", prefix + "try_javatest.html");
    tryit.addItem("tryreq", "navsubitem", "Requirements", "Requirements", prefix + "try_req.html");
    navmenu.findItem("try").setSubMenu(tryit);

    trystandalone = new Menu("trystandalone_menu", "navsubmenu", 210 - 1, 2, 170);
    trystandalone.addItem("tryapp", "navsubitem", "Try the Gong Application", "Try the Gong Application", prefix + "try.php?version=5.0.6CA");
    trystandalone.addItem("tryapplet", "navsubitem", "Try the Gong Applet", "Try the Gong Applet", prefix + "try.php?version=5.0.6C");
    trystandalone.addItem("trymini", "navsubitem", "Try the MiniGong Applet", "Try the MiniGong Applet", prefix + "try.php?version=5.0.6CM");
    tryit.findItem("trystandalone").setSubMenu(trystandalone);

    downloads = new Menu("downloads_menu", "navsubmenu", (width + 1) * 4 - 1, height, 170);
    downloads.addItem("downloadsform", "navsubitem", "Download Form", "Download Form", prefix + "downloads_form.html");
    downloads.addItem("setup", "navsubitem", "Setting up a Gong System", "Setting up a Gong System");
    downloads.addItem("policy", "navsubitem", "Software Policy &amp; License", "Software Policy and License", prefix + "downloads_policy.html");
    downloads.addItem("futuredev", "navsubitem", "Future Developments", "Future Developments", prefix + "futuredev_form.html");
    navmenu.findItem("downloads").setSubMenu(downloads);

    setup = new Menu("setup_menu", "navsubmenu", 170 - 1, submenu_height - 2, 200);
    setup.addItem("downloadsinst", "navsubitem", "Setup Instructions", "Setup Instructions", prefix + "downloads_instructions.html");
    setup.addItem("downloadsinstaller", "navsubitem", "Creating Gong Client Installer", "Creating Gong Client Installer", prefix + "downloads_installer_form.html");
    setup.addItem("downloadsserver", "navsubitem", "Generating Gong Server INI File", "Generating Gong Server INI File", prefix + "downloads_server_wizard.php");
    downloads.findItem("setup").setSubMenu(setup);

    doc = new Menu("doc_menu", "navsubmenu", (width + 1) * 5 - 1, height, 210);
    doc.addItem("faq", "navsubitem", "Frequently Asked Questions (FAQ)", "Frequently Asked Questions (FAQ)", prefix + "doc_faq.html");
    doc.addItem("userguide", "navsubitem", "User Guide", "User Guide", prefix + "doc_userguide.html");
    doc.addItem("api", "navsubitem", "Gong Scripting API Reference", "Gong Scripting API Reference", prefix + "doc_api.html");
    doc.addItem("publication", "navsubitem", "Related Publications", "Related Publications", prefix + "doc_publish.html");
    doc.addItem("usage", "navsubitem", "Usage &amp; Statistics", "Usage and Statistics", prefix + "info_usage.html");
    navmenu.findItem("doc").setSubMenu(doc);

    navmenu.writeln();
    document.writeln("<div class='spacer' style='height: " + height + "px'>&nbsp;</div>");
}

