// ==PREPROCESSOR==
// @name "MainMenuManager All-In-One"
// @author "YBStone / T.P Wang / marc2003"
// @import "%fb2k_component_path%docs\flags.txt"
// @import "%fb2k_component_path%docs\helpers.txt"
// ==/PREPROCESSOR==

var tooltip = window.CreateTooltip();
// buttons is defined in docs\helpers.txt
var b = new buttons();

// button is defined in docs\helpers.txt
// arguments are x, y, w, h, {normal image, hover image is optional}, function, tooltip
b.buttons.menu = new button(5, 5, 32, 32, {normal : fb.ComponentPath + "samples\\basic\\images\\cog.png"}, function () { menu(); }, "Menu");

function on_paint(gr) {
	b.paint(gr);
}

function on_mouse_move(x, y) {
	b.move(x, y);
}

function on_mouse_lbtn_up(x, y) {
	b.lbtn_up(x, y);
}

function on_mouse_leave(x, y) {
	b.leave();
}

function menu() {
	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();
	
	// MF_STRING is defined in docs\flags.txt
	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");
	basemenu.AppendMenuSeparator();
	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 = basemenu.TrackPopupMenu(b.buttons.menu.x, b.buttons.menu.y);

	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();
}
