// ==PREPROCESSOR==
// @name "DarkOne4Mod - Menu Panel"
// @version "1.0 build20161002"
// @author "tedGo, based on a sample by T.P Wang"
// @import "%fb2k_path%themes\DarkOne4Mod\Others\JScripts\DarkOne4Mod - Global Script.js"
// @import "%fb2k_path%themes\DarkOne4Mod\Others\JScripts\DarkOne4Mod - Global Drawbutton Object.js"
// ==/PREPROCESSOR==

// To enter commands for custom buttons please scroll down to line 42 and overwrite "type command here"
// with your desired command. You see some examples here in the pre-defined buttons from line 35 to 40.

var p_backcol = RGBA(31, 31, 31, 255);
var bbw, bbh, bsh, f_size = 0;
var b_font;

// ----- CREATE BUTTONS -----
var c = {normal: 0, hover: RGBA(255, 255, 255, 16), down: RGBA(255, 255, 255, 8)};

var tbtnl_b = window.GetProperty("2. Button top left", false);
if (tbtnl_b) var tbtnl_d = window.GetProperty("2. Button top left description (up to 10 letters)", "BTN. 1");
else window.SetProperty("2. Button top left description (up to 10 letters)", null);

var tbtnr_b = window.GetProperty("3. Button top right", false);
if (tbtnr_b) var tbtnr_d = window.GetProperty("3. Button top right description (up to 10 letters)", "BTN. 2");
else window.SetProperty("3. Button top right description (up to 10 letters)", null);

var bbtnl_b = window.GetProperty("4. Button bottom left", false);
if (bbtnl_b) var bbtnl_d = window.GetProperty("4. Button bottom left description (up to 10 letters)", "BTN. 3");
else window.SetProperty("4. Button bottom left description (up to 10 letters)", null);

var bbtnr_b = window.GetProperty("5. Button bottom right", false);
if (bbtnr_b) var bbtnr_d = window.GetProperty("5. Button bottom right description (up to 10 letters)", "BTN. 4");
else window.SetProperty("5. Button bottom right description (up to 10 letters)", null);

Buttons.exit = new Button(0, 0, bbw, bbh, c, function(){fb.Exit();});
Buttons.MEN = new Button(ww - ww / 8 - 1, 0, bbw, bbh, c, getMainMenu);
Buttons.console = new Button(0, Math.floor(wh / 7 * 6) - 1, bbw, bsh, c, function(){fb.ShowConsole();});
Buttons.statuspane = new Button(ww / 16 * 3, Math.floor(wh / 7 * 6) - 1, bbw, bsh, c, function(){fb.RunMainMenuCommand("Show Status pane");});
Buttons.statusbar = new Button(ww / 16 * 5, Math.floor(wh / 7 * 6) - 1, bbw, bsh, c, function(){fb.RunMainMenuCommand("Show Status bar");});
Buttons.REM = new Button(ww - ww / 8 - 1, Math.floor(wh / 7 * 6) - 1, bbw, bsh, c, function () {window.SetProperty("1. Remain Time", t_r ? false : true); TimeOpt();});

if (tbtnl_b) Buttons.top_l = new Button(ww / 16 * 9, Math.floor(wh / 7 * 2), bbw, bsh, c, function(){"type command here"});
if (tbtnr_b) Buttons.top_r = new Button(ww / 16 * 11, Math.floor(wh / 7 * 2), bbw, bsh, c, function(){"type command here"});
if (bbtnl_b) Buttons.bottom_l = new Button(ww / 16 * 9, Math.floor(wh / 7 * 6) - 1, bbw, bsh, c, function(){"type command here"});
if (bbtnr_b) Buttons.bottom_r = new Button(ww / 16 * 11, Math.floor(wh / 7 * 6) - 1, bbw, bsh, c, function(){"type command here"});

// ----- CREATE MENUS -----
function getMainMenu(x, y) {
	x = Buttons.MEN.left;
	y = Buttons.MEN.top;

	var a = window.CreatePopupMenu();
	var b = fb.CreateContextMenuManager();

	b.InitNowPlaying();

	var c1 = window.CreatePopupMenu();
	var c2 = window.CreatePopupMenu();
	var c3 = window.CreatePopupMenu();
	var c4 = window.CreatePopupMenu();
	var c5 = window.CreatePopupMenu();
	var c6 = window.CreatePopupMenu();
	var c7 = window.CreatePopupMenu();

	var d1 = fb.CreateMainMenuManager();
	var d2 = fb.CreateMainMenuManager();
	var d3 = fb.CreateMainMenuManager();
	var d4 = fb.CreateMainMenuManager();
	var d5 = fb.CreateMainMenuManager();
	var d6 = fb.CreateMainMenuManager();

	c1.AppendTo(a, 16, "File");
	c2.AppendTo(a, 16, "Edit");
	c3.AppendTo(a, 16, "View");
	c4.AppendTo(a, 16, "Playback");
	c5.AppendTo(a, 16, "Library");
	c6.AppendTo(a, 16, "Help");
	fb.IsPlaying && c7.AppendTo(a, 16, "Now Playing");

	d1.Init("file");
	d2.Init("edit");
	d3.Init("View");
	d4.Init("playback");
	d5.Init("library");
	d6.Init("help");

	d1.BuildMenu(c1, 1, 200);
	d2.BuildMenu(c2, 201, 200);
	d3.BuildMenu(c3, 401, 200);
	d4.BuildMenu(c4, 601, 300);
	d5.BuildMenu(c5, 901, 300);
	d6.BuildMenu(c6, 1201, 100);

	b.InitNowPlaying();
	b.BuildMenu(c7, 1301, -1);
	ret = 0;

	ret = a.TrackPopupMenu(x, y);

	switch (true) {
		case(ret >= 1 && ret < 201):
			d1.ExecuteByID(ret - 1);
			break;

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

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

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

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

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

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

	a.Dispose();
	b.Dispose();
	d1.Dispose();
	d2.Dispose();
	d3.Dispose();
	d4.Dispose();
	d5.Dispose();
	d6.Dispose();
}

// ----- CREATE TIMESWITCH OPTION -----
function TimeOpt() {
	t_r = window.GetProperty("1. Remain Time", false);
	window.NotifyOthers("remTime", t_r);
}

TimeOpt();

// ----- DRAW -----
function on_paint(gr) {
	!window.IsTransparent && gr.FillSolidRect(0, 0, ww, wh, p_backcol);

	gr.SetInterpolationMode(7);
	gr.SetSmoothingMode(4);

	g_signs && gr.DrawImage(g_signs, 0, 0, bbw, bbh, 1206, 396, 150, 72);

	b_font = gdi.Font("Arial Black", f_size);

	tbtnl_b && gr.GdiDrawText(tbtnl_d, b_font, ui_btntxtcol, ww / 16 * 9, 0, bbw, Math.floor(wh / 7 * 2), 33);
	tbtnr_b && gr.GdiDrawText(tbtnr_d, b_font, ui_btntxtcol, ww / 16 * 11, 0, bbw, Math.floor(wh / 7 * 2), 33);

	g_signs && gr.DrawImage(g_signs, ww - ww / 8 - 1, 0, bbw, bbh, 1674, 396, 150, 72);

	gr.GdiDrawText("CONSOLE", b_font, ui_btntxtcol, 0, Math.floor(wh / 7 * 4), bbw, Math.floor(wh / 7 * 2), 33);
	gr.GdiDrawText("PANE --- STATUS --- BAR", b_font, ui_btntxtcol, ww / 16 * 3, Math.floor(wh / 7 * 4), bbw * 2, Math.floor(wh / 7 * 2), 1);

	bbtnl_b && gr.GdiDrawText(bbtnl_d, b_font, ui_btntxtcol, ww / 16 * 9, Math.floor(wh / 7 * 4), bbw, Math.floor(wh / 7 * 2), 33);
	bbtnr_b && gr.GdiDrawText(bbtnr_d, b_font, ui_btntxtcol, ww / 16 * 11, Math.floor(wh / 7 * 4), bbw, Math.floor(wh / 7 * 2), 33);

	gr.GdiDrawText("TIME", b_font, ui_btntxtcol, ww - ww / 8 - 1, Math.floor(wh / 7 * 4), bbw, Math.floor(wh / 7 * 2), 33);

	buttonsDraw(gr);
}

// ----- EVENTS -----
function on_size() {
	ww = window.Width;
	wh = window.Height;
	bbw = Math.floor(ww / 8);
	bbh = Math.floor(bbw / 75 * 36);
	bsh = Math.floor(wh / 7);
	f_size = ww * 7 / 400;

	Buttons.exit.changePos(0, 0, bbw, bbh);
	Buttons.MEN.changePos(ww - ww / 8 - 1, 0, bbw, bbh);
	Buttons.console.changePos(0, Math.floor(wh / 7 * 6) - 1, bbw, bsh);
	Buttons.statuspane.changePos(ww / 16 * 3, Math.floor(wh / 7 * 6) - 1, bbw, bsh);
	Buttons.statusbar.changePos(ww / 16 * 5, Math.floor(wh / 7 * 6) - 1, bbw, bsh);
	Buttons.REM.changePos(ww - ww / 8 - 1, Math.floor(wh / 7 * 6) - 1, bbw, bsh);

	tbtnl_b && Buttons.top_l.changePos(ww / 16 * 9, Math.floor(wh / 7 * 2), bbw, bsh);
	tbtnr_b && Buttons.top_r.changePos(ww / 16 * 11, Math.floor(wh / 7 * 2), bbw, bsh);
	bbtnl_b && Buttons.bottom_l.changePos(ww / 16 * 9, Math.floor(wh / 7 * 6) - 1, bbw, bsh);
	bbtnr_b && Buttons.bottom_r.changePos(ww / 16 * 11, Math.floor(wh / 7 * 6) - 1, bbw, bsh);
}