// ==PREPROCESSOR==
// @name "Menu Sample"
// @author "T.P Wang / marc2003"
// @import "%fb2k_component_path%docs\flags.txt"
// ==/PREPROCESSOR==

function on_mouse_lbtn_down(x, y) {
	var _context = fb.CreateContextMenuManager();
	var _basemenu = window.CreatePopupMenu();
	var _child = window.CreatePopupMenu();
	var idx;
	
	// 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);
	/*
	base_id set to 3 so it won't conflict with manually added items above.
	max_id was made optional in v2.0.4 and is omitted in this example.
	*/
	
	idx = _basemenu.TrackPopupMenu(x, y);
	
	switch (idx) {
		case 0: //user dismissed menu by clicking elsewhere. that's why you can't use 0 when building menu items
			break;
		case 1:
			fb.ShowPopupMessage('You have chose item1.');
			break;
		case 2:
			fb.ShowPopupMessage('OK, item2 is clicked.');
			break;
		default:
			_context.ExecuteByID(idx - 3);
			break;
	}
	_basemenu.Dispose();
	_child.Dispose();
	_context.Dispose();
}
