public void TimerAdminMenu(int client)
{
	char buffer[16];
	Menu menu = new Menu(TimerAdminMenuHandler);
	menu.SetTitle("Timer Control Panel");
	if (g_iEditor != -1)
		menu.AddItem("zone", "Zones (Currently in use)", ITEMDRAW_DISABLED);
	else
		menu.AddItem("zone", "Zones");
	menu.AddItem("tier", "Set Map Tier");
	Format(buffer, sizeof(buffer), "%s map", g_bActive ? "Deactivate":"Activate");
	menu.AddItem("active", buffer);
	menu.ExitButton = true;
	menu.Display(client, MENU_TIME_FOREVER);
}

public int TimerAdminMenuHandler(Handle menu, MenuAction action, int client, int choice)
{
	switch(action)
	{
		case MenuAction_Select:
		{
			switch(choice)
			{
				case 0:
				{
					if (g_iEditor != -1)
					{
						PrintToChat(client, "Zone menu currently unavailable");
						TimerAdminMenu(client);
					}
					else
					{
						g_iEditor = client;
						g_iActivity[client] = -1;
						g_bEditorComesFromMenu = true;
						ZoneMenu(client, g_bEditorComesFromMenu);
					}
					
				}
				case 1:
				{
					TierMenu(client);
				}
				case 2:
				{
					g_bActive = !g_bActive;
					SetMapState(view_as<int>(g_bActive));
					PrintToChat(client, "%s the map...", g_bActive ? "Activating":"Deactivating");
					CS_TerminateRound(1.0, CSRoundEnd_Draw, true);
					TimerAdminMenu(client);
				}
			}
		}
		case MenuAction_End:
		{
			delete menu;
		}
	}
}

public void ZoneMenu(int client, bool comesFromMenu)
{
	Menu menu = new Menu(ZoneMenuHandler);
	menu.SetTitle("Zone Menu");
	menu.AddItem("start", "Set Start Zone");
	menu.AddItem("end", "Set End Zone");
	menu.AddItem("rr", "Respawn Zones");
	menu.AddItem("save", "Save Zones");
	menu.ExitButton = true;
	if (comesFromMenu)
		menu.ExitBackButton = true;
	menu.Display(client, MENU_TIME_FOREVER);
}

public int ZoneMenuHandler(Handle menu, MenuAction action, int client, int choice)
{
	switch(action)
	{
		case MenuAction_Select:
		{
			switch(choice)
			{
				case 0:
					CreateZoneMenu(client, STARTZONE, false);
					
				case 1:
					CreateZoneMenu(client, ENDZONE, false);
					
				case 2:
				{
					CS_TerminateRound(1.0, CSRoundEnd_Draw, true);
					ZoneMenu(client, g_bEditorComesFromMenu);
				}
				case 3:
				{
					SaveZones(client);
					ZoneMenu(client, g_bEditorComesFromMenu);
				}
			}
		}
		
		case MenuAction_Cancel:
		{
			g_iEditor = -1;
			g_bEditorComesFromMenu = false;
			if (choice == MenuCancel_ExitBack)
			{
				TimerAdminMenu(client);
			}
		}
		
		case MenuAction_End:
		{
			delete menu;
		}
	}
}

public void CreateZoneMenu(int client, int zone, bool point)
{
	char buffer[32], zonePoint[5];
	Menu menu = new Menu(CreateZoneMenuHandler);
	Format(buffer, sizeof(buffer), "Set the %s position", point ? "second":"first");
	//Format(zonePoint, sizeof(zonePoint), "%i|%s", zone, view_as<int>(point));
	Format(zonePoint, sizeof(zonePoint), "%i|%s", zone, point ? "1":"0");
	menu.AddItem(zonePoint, buffer);
	menu.ExitBackButton = false;
	menu.ExitButton = false;
	menu.Display(client, MENU_TIME_FOREVER);
}

public int CreateZoneMenuHandler(Menu menu, MenuAction action, int client, int choice)
{
	if (action == MenuAction_Select)
	{
		char buffer[5], array[2][2];
		int zone, point;
		menu.GetItem(0, buffer, sizeof(buffer));	
		ExplodeString(buffer, "|", array, 2, 2);
		zone = StringToInt(array[0]);
		point = StringToInt(array[1]);
		//PrintToChatAll("%s - %s %s - %i %i", buffer, array[0], array[1], zone, point);
		
		if (point == 0)
		{
			GetClientAbsOrigin(client, g_fStartOrigins[zone]);
			g_iSnapToClient = zone;
			CreateZoneMenu(client, zone, true);
		}
		if (point == 1)
		{
			GetClientAbsOrigin(client, g_fEndOrigins[zone]);
			g_iSnapToClient = -1;
			CreateTrigger(zone);
			ZoneMenu(client, g_bEditorComesFromMenu);
		}
	}
	if (action == MenuAction_End)
	{
		delete menu;
	}
}

public void TierMenu(int client)
{
	Menu menu = new Menu(TierMenuHandler);
	menu.SetTitle("Choose a tier");
	menu.AddItem("1", "Tier 1");
	menu.AddItem("2", "Tier 2");
	menu.AddItem("3", "Tier 3");
	menu.AddItem("4", "Tier 4");
	menu.AddItem("5", "Tier 5");
	menu.AddItem("6", "Tier 6");
	menu.ExitButton = true;
	menu.ExitBackButton = true;
	menu.Display(client, MENU_TIME_FOREVER);
}

public int TierMenuHandler(Handle menu, MenuAction action, int client, int choice)
{
	switch(action)
	{
		case MenuAction_Select:
		{
			g_iTier = choice + 1;
			SetMapTier(g_iTier);
			PrintToChat(client, "Setting the maps tier to %i", g_iTier);
			TimerAdminMenu(client);
		}	
		case MenuAction_Cancel:
		{
			if (choice == MenuCancel_ExitBack)
			{
				TimerAdminMenu(client);
			}
		}
		
		case MenuAction_End:
		{
			delete menu;
		}
	}
}

public void SQL_RequestTop(Database db, int userid, int numQueries, DBResultSet[] results, any[] queryData)
{
	if (db == null)
	{
		SetFailState("Lost connection to the database, will attempt to reconnect on map change");
		return;
	}
	
	int client = GetClientOfUserId(userid);
	
	if (!isValidClient(client))
		return;
	
	if (results[0].RowCount == 0)
	{	
		TimerPrintToChat(client, false, "%T", "MapNotFound", LANG_SERVER);
		return;
	}
	
	char cMap[64];
	results[0].FetchRow();
	results[0].FetchString(0, cMap, sizeof(cMap));
	
	if (results[1].RowCount == 0)
	{	
		TimerPrintToChat(client, false, "%T", "TimesNotFound", LANG_SERVER, cMap);
		return;
	}
	
	char cBuffer[128], cTime[16], cTimeToWR[16], cName[64];
	float fTime, fTimeToWR, fWR;
	int timescompleted;
	
	Menu menu = new Menu(TopTimeMenuHandler);
	
	Format(cBuffer, sizeof(cBuffer), "Top Times for %s", cMap);
	menu.SetTitle(cBuffer);
	
	results[1].FetchRow();
	results[1].FetchString(0, cName, sizeof(cName));
	fTime = fWR = results[1].FetchFloat(1);
	timescompleted = results[1].FetchInt(2);
	
	TimerFormat(fTime, cTime, sizeof(cTime), true, false);
	Format(cBuffer, sizeof(cBuffer), "%s\n%s (+00:00.00) (%i)", cName, cTime, timescompleted);
	
	menu.AddItem("", cBuffer);
	
	while (results[1].FetchRow())
	{
		results[1].FetchString(0, cName, sizeof(cName));
		fTime = results[1].FetchFloat(1);
		fTimeToWR = fTime - fWR;
		timescompleted = results[1].FetchInt(2);
		
		TimerFormat(fTime, cTime, sizeof(cTime), true, false);
		TimerFormat(fTimeToWR, cTimeToWR, sizeof(cTimeToWR), true, true);
		Format(cBuffer, sizeof(cBuffer), "%s\n%s (%s) (%i)", cName, cTime, cTimeToWR, timescompleted);
		
		menu.AddItem("", cBuffer);
	}
	menu.ExitButton = true;
	menu.Display(client, MENU_TIME_FOREVER);
	
}

public int TopTimeMenuHandler(Handle menu, MenuAction action, int client, int choice)
{
	switch(action)
	{
		case MenuAction_End:
		{
			delete menu;
		}
	}
}