// vi:set ft=javascript ff=dos ts=4 sts=4 sw=4 et:

// ==PREPROCESSOR==
// @name "MainMenuManager All-In-One"
// @author "YBStone"
// Modded by T.P Wang
// ==/PREPROCESSOR==

// Flags, used by Menu
var MF_SEPARATOR = 0x00000800;
var MF_ENABLED = 0x00000000;
var MF_GRAYED = 0x00000001;
var MF_DISABLED = 0x00000002;
var MF_UNCHECKED = 0x00000000;
var MF_CHECKED = 0x00000008;
var MF_STRING = 0x00000000;
var MF_POPUP = 0x00000010;
var MF_RIGHTJUSTIFY = 0x00004000;

function on_mouse_lbtn_up(x, y) {

    var basemenu = window.CreatePopupMenu();
    var contextman = fb.CreateContextMenuManager();

    contextman.InitNowPlaying();

    var child1 = window.CreatePopupMenu(); //File
    var child2 = window.CreatePopupMenu(); //Edit
    var child3 = window.CreatePopupMenu(); //View
    var child4 = window.CreatePopupMenu(); //Playback
    var child5 = window.CreatePopupMenu(); //Library
    var child6 = window.CreatePopupMenu(); //Help
    var child7 = window.CreatePopupMenu(); //Now playing

    var menuman1 = fb.CreateMainMenuManager();
    var menuman2 = fb.CreateMainMenuManager();
    var menuman3 = fb.CreateMainMenuManager();
    var menuman4 = fb.CreateMainMenuManager();
    var menuman5 = fb.CreateMainMenuManager();
    var menuman6 = fb.CreateMainMenuManager();

    child1.AppendTo(basemenu, MF_STRING, "File");
    child2.AppendTo(basemenu, MF_STRING, "Edit");
    child3.AppendTo(basemenu, MF_STRING, "View");
    child4.AppendTo(basemenu, MF_STRING, "Playback");
    child5.AppendTo(basemenu, MF_STRING, "Library");
    child6.AppendTo(basemenu, MF_STRING, "Help");
    child7.AppendTo(basemenu, MF_STRING, "Now Playing");

    menuman1.Init("file");
    menuman2.Init("edit");
    menuman3.Init("View");
    menuman4.Init("playback");
    menuman5.Init("library");
    menuman6.Init("help");

    menuman1.BuildMenu(child1, 1, 200);
    menuman2.BuildMenu(child2, 201, 200);
    menuman3.BuildMenu(child3, 401, 200);
    menuman4.BuildMenu(child4, 601, 300);
    menuman5.BuildMenu(child5, 901, 300);
    menuman6.BuildMenu(child6, 1201, 100);

    contextman.InitNowPlaying();
    contextman.BuildMenu(child7, 1301, -1);
    ret = 0;

    ret = basemenu.TrackPopupMenu(x, y);

    // fb.trace(ret);
    switch (true) {
    case(ret >= 1 && ret < 201):
        menuman1.ExecuteByID(ret - 1);
        break;

    case (ret >= 201 && ret < 401):
        menuman2.ExecuteByID(ret - 201);
        break;

    case (ret >= 401 && ret < 601):
        menuman3.ExecuteByID(ret - 401);
        break;

    case (ret >= 601 && ret < 901):
        menuman4.ExecuteByID(ret - 601);
        break;

    case (ret >= 901 && ret < 1201):
        menuman5.ExecuteByID(ret - 901);
        break;

    case (ret >= 1201 && ret < 1301):
        menuman6.ExecuteByID(ret - 1201);
        break;

    case (ret >= 1301):
        contextman.ExecuteByID(ret - 1301);
        break;
    }

    basemenu.Dispose();
    contextman.Dispose();
    menuman1.Dispose();
    menuman2.Dispose();
    menuman3.Dispose();
    menuman4.Dispose();
    menuman5.Dispose();
    menuman6.Dispose();
}
