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

// ==PREPROCESSOR==
// @name "Menu Sample (Advance)"
// @author "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;

// custom popup menu should always reponse to left-click
// This sample is more complex, because use fb2k's context menu manager to display system context menu

function on_mouse_lbtn_down(x, y) {
    var _context = fb.CreateContextMenuManager();
    var _basemenu = window.CreatePopupMenu();
    var _child = window.CreatePopupMenu();
    var ret;

    // Please start index at 1, NOT 0
    _basemenu.AppendMenuItem(MF_STRING, 1, "item1");
    _basemenu.AppendMenuItem(MF_STRING, 2, "item2");
    _child.AppendTo(_basemenu, MF_STRING, "Now Playing");

    _context.InitNowPlaying();
    _context.BuildMenu(_child, 3, -1);

    ret = _basemenu.TrackPopupMenu(x, y);

    // Here is why start index should be 0
    if (ret == 0) return;

    // Execute!
    switch (ret) {
    case 1:
        fb.trace("Mmmm, you have choose item1, of course");
        break; // break is important, or it will exucte the following...
    case 2:
        fb.trace("Ok, item2 is clicked");
        break;

    default:
        _context.ExecuteByID(ret - 3);
        fb.trace(ret.toString());
        break;
    }

    _basemenu.Dispose();
}
