
init()
{
	suffix = undefined;
	allteamweapons = false;

	if(level.ex_all_weapons) // all weapons for all teams
	{
			game["menu_weapon_allies"] = "weapon_all";
			game["menu_weapon_axis"] = "weapon_all";

			if(level.ex_wepo_secondary)
			{
				game["menu_weapon_allies_sec"] = "weapon_all_sec";
				game["menu_weapon_axis_sec"] = "weapon_all_sec";

				[[level.ex_PrecacheMenuItem]](game["menu_weapon_allies_sec"]);
				[[level.ex_PrecacheMenuItem]](game["menu_weapon_axis_sec"]);
			}
	}
	else
	{
		switch(level.ex_wepo_class)
		{
			case 1: suffix = "pistol_only"; break; // pistol only
			case 2: suffix = "sniper_only"; break; // sniper only
			case 3: suffix = "mg_only"; break; // mg only
			case 4: suffix = "smg_only"; break; // smg only
			case 5: suffix = "rifle_only"; break; // rifle only
			case 6: suffix = "bolt_only"; break; // bolt action rifle only
			case 7: suffix = "shotgun_only"; break; // shotgun only
			default: allteamweapons = true; break; // stock menus
		}

		if(allteamweapons) // stock menus
		{
			game["menu_weapon_allies"] = "weapon_" + game["allies"];
			game["menu_weapon_axis"] = "weapon_" + game["axis"];
		}
		else // weapon class menus
		{
			// if team based, define allies and axis teams
			if(level.ex_wepo_team_only)
			{
				game["menu_weapon_allies"] = "weapon_" + game["allies"] + "_" + suffix;
				game["menu_weapon_axis"] = "weapon_" + game["axis"] + "_" + suffix;
			}
			else
			{
				game["menu_weapon_allies"] = "weapon_" + suffix;
				game["menu_weapon_axis"] = "weapon_" + suffix;
			}
		}

		if(level.ex_wepo_secondary)
		{
			if(!level.ex_wepo_sec_enemy)
			{
				game["menu_weapon_allies_sec"] = "weapon_" + game["allies"] + "_sec";
				game["menu_weapon_axis_sec"] = "weapon_" + game["axis"] + "_sec";
			}
			else
			{
				game["menu_weapon_axis_sec"] = "weapon_" + game["allies"] + "_sec";
				game["menu_weapon_allies_sec"] = "weapon_" + game["axis"] + "_sec";
			}

			[[level.ex_PrecacheMenuItem]](game["menu_weapon_allies_sec"]);
			[[level.ex_PrecacheMenuItem]](game["menu_weapon_axis_sec"]);
		}
	}

	game["menu_ingame"] = "ingame";
	game["menu_team"] = "team_" + game["allies"] + game["axis"];
	game["menu_callvote"] = "callvote";
	game["menu_muteplayer"] = "muteplayer";
	game["menu_serverinfo"] = "serverinfo_" + level.ex_currentgt;
	game["quickmessages"] = "quickmessages";

	[[level.ex_PrecacheMenuItem]](game["menu_ingame"]);
	[[level.ex_PrecacheMenuItem]](game["menu_team"]);
	[[level.ex_PrecacheMenuItem]](game["menu_weapon_allies"]);
	[[level.ex_PrecacheMenuItem]](game["menu_weapon_axis"]);
	[[level.ex_PrecacheMenuItem]](game["menu_callvote"]);
	[[level.ex_PrecacheMenuItem]](game["menu_muteplayer"]);
	[[level.ex_PrecacheMenuItem]](game["menu_serverinfo"]);
	[[level.ex_PrecacheMenuItem]](game["quickmessages"]);

	if(level.ex_rcon)
	{
		game["menu_rcon_main"] = "rcon_main";
		game["menu_rcon_mapctrl"] = "rcon_mapctrl";
		game["menu_rcon_playerctrl"] = "rcon_playerctrl";
		game["menu_rcon_scriptctrl"] = "rcon_scriptctrl";
		game["menu_rcon_msgcenter"] = "rcon_msgcenter";
		game["menu_rcon_setsvr"] = "rcon_setsvr";
		game["menu_rcon_setclnt"] = "rcon_setclnt";
		game["menu_rcon_setwpn"] = "rcon_setwpn";

		[[level.ex_PrecacheMenuItem]](game["menu_rcon_main"]);
		[[level.ex_PrecacheMenuItem]](game["menu_rcon_mapctrl"]);
		[[level.ex_PrecacheMenuItem]](game["menu_rcon_playerctrl"]);
		[[level.ex_PrecacheMenuItem]](game["menu_rcon_scriptctrl"]);
		[[level.ex_PrecacheMenuItem]](game["menu_rcon_msgcenter"]);
		[[level.ex_PrecacheMenuItem]](game["menu_rcon_setsvr"]);
		[[level.ex_PrecacheMenuItem]](game["menu_rcon_setclnt"]);
		[[level.ex_PrecacheMenuItem]](game["menu_rcon_setwpn"]);
	}

	level thread onPlayerConnect();
}

onPlayerConnect()
{
	for(;;)
	{
		level waittill("connecting", player);
		player thread onMenuResponse();
	}
}

onMenuResponse()
{
	self endon("disconnect");

	for(;;)
	{
		self waittill("menuresponse", menu, response);

		//logprint("DEBUG: menu " + menu + ":" + response + "\n");

		if(response == "menuopen")
		{
			if(menu == game["menu_serverinfo"])
			  self notify("start_motd_rotation");

			self.ex_inmenu = true;
			continue;
		}

		if(response == "menuclose")
		{
			self.ex_inmenu = false;
			continue;
		}

		if(menu == game["menu_serverinfo"])
		{
		  self notify("stop_motd_rotation");

			if(response == "team" && isDefined(self.pers["team"]) && self.pers["team"] == "spectator")
			{
				self closeMenu();
				self openMenu(game["menu_team"]);
			}
			else
			{
				self closeMenu();
				self closeInGameMenu();
			}
		}

		if(response == "open" || response == "close") continue;

		if(response == "back")
		{
			self closeMenu();
			self closeInGameMenu();

			if(menu == game["menu_team"])
			{
				self openMenu(game["menu_ingame"]);
			}
			else if(menu == game["menu_weapon_allies"] || menu == game["menu_weapon_axis"])
				self openMenu(game["menu_team"]);
				
			continue;
		}

		if(response == "endgame") continue;

		if(menu == game["menu_ingame"] || (level.splitscreen && (menu == game["menu_ingame_onteam"] || menu == game["menu_ingame_spectator"])))
		{
			switch(response)
			{
			case "changeweapon":
				self closeMenu();
				self closeInGameMenu();

				// do not show current weapons in use!
				self extreme\_ex_weapons::setWeaponClientStatus(false);

				if(level.ex_wepo_secondary)
				{
					self setClientCvar("ui_allow_primary", "1");
					self setClientCvar("ui_allow_secondary", "1");
				}
				else
				{
					self setClientCvar("ui_allow_primary", "0");
					self setClientCvar("ui_allow_secondary", "0");
				}

				if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies"]);
					else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis"]);
				break;	

			case "changeteam":
				self closeMenu();
				self closeInGameMenu();
				self openMenu(game["menu_team"]);
				break;

			case "muteplayer":
				self closeMenu();
				self closeInGameMenu();
				self openMenu(game["menu_muteplayer"]);
				break;

			case "callvote":
				if(level.ex_clanvoting)
				{
					if(isDefined(self.ex_name))
					{
						if(level.ex_clvote[self.ex_clid])
						{
							self closeMenu();
							self closeInGameMenu();
							self openMenu(game["menu_callvote"]);
						}
					}
				}
				else
				{
					self closeMenu();
					self closeInGameMenu();
					self openMenu(game["menu_callvote"]);
				}
				break;
				
			case "serverinfo":
				self closeMenu();
				self closeInGameMenu();
				self notify("start_motd_rotation");
				self openMenu(game["menu_serverinfo"]);
				break;

			case "rcon_main":
				self closeMenu();
				self closeInGameMenu();
				self openMenu(game["menu_rcon_main"]);
				break;
			}
		}
		else if(menu == game["menu_team"])
		{
			switch(response)
			{
			case "allies":
				if(!level.ex_forceauto)
				{
					self closeMenu();
					self closeInGameMenu();
					self [[level.allies]]();
				}
				break;

			case "axis":
				if(!level.ex_forceauto)
				{
					self closeMenu();
					self closeInGameMenu();
					self [[level.axis]]();
				}
				break;

			case "autoassign":
				self closeMenu();
				self closeInGameMenu();
				self [[level.autoassign]]();
				break;

			case "spectator":
				if(level.ex_specmusic && !self.pers["spec_on"])
				{
					self playLocalSound("spec_music_null");
					self.pers["spec_on"] = true;
					self playLocalSound("spec_music");
					self thread spec_monitor();
				}
				self closeMenu();
				self closeInGameMenu();
				self [[level.spectator]]();
				break;

			case "serverinfo":
				self closeMenu();
				self closeInGameMenu();
				self notify("start_motd_rotation");
				self openMenu(game["menu_serverinfo"]);
				break;
			}
		}
		else if(menu == game["menu_weapon_allies"] || menu == game["menu_weapon_axis"])
		{
			if(response == "teamchange")
			{
				self closeMenu();
				self closeInGameMenu();
				self openMenu(game["menu_team"]);
			}
			else if(response == "secondary" && isDefined(self.pers["weapon2"]))
			{
				self closeMenu();
				self closeInGameMenu();

				if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies_sec"]);
				else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis_sec"]);
			}
			else if(response != "secondary")
			{
				self closeMenu();
				self closeInGameMenu();
				self [[level.weapon]](response);
			}
		}
		else if(level.ex_wepo_secondary && (menu == game["menu_weapon_allies_sec"] || menu == game["menu_weapon_axis_sec"]))
		{
			if(response == "primary" && isDefined(self.pers["weapon1"]))
			{
				self closeMenu();
				self closeInGameMenu();

				if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies"]);
				else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis"]);
			}
			else
			{
				self closeMenu();
				self closeInGameMenu();
				self extreme\_ex_clientcontrol::menuSecWeapon(response);
			}
		}
		else if((level.ex_taunts == 1 || level.ex_taunts == 3) && isDefined(game["menu_quicktaunts"]) && menu == game["menu_quicktaunts"])
		{
			maps\mp\gametypes\_quickmessages::quicktaunts(response);
		}
		else if((level.ex_taunts == 1 || level.ex_taunts == 3) && isDefined(game["menu_quicktauntsb"]) && menu == game["menu_quicktauntsb"])
		{
			maps\mp\gametypes\_quickmessages::quicktauntsb(response);
		}
		else if(isDefined(game["menu_quickresponseslib"]) && menu == game["menu_quickresponseslib"])
		{
			maps\mp\gametypes\_quickmessages::quickresponseslib(response);
		}
		else
		{
			if(menu == game["menu_quickcommands"]) self thread maps\mp\gametypes\_quickmessages::quickcommands(response);
			else if(menu == game["menu_quickstatements"]) self thread maps\mp\gametypes\_quickmessages::quickstatements(response);
			else if(menu == game["menu_quickresponses"]) self thread maps\mp\gametypes\_quickmessages::quickresponses(response);
			else if(menu == game["menu_quickrequests"]) self thread maps\mp\gametypes\_quickmessages::quickrequests(response);
			else if(level.ex_rcon)
			{
				if(menu == game["menu_rcon_main"]) self thread extreme\_ex_rcon::rconMain(response);
				else if(menu == game["menu_rcon_mapctrl"]) self thread extreme\_ex_rcon::rconMapCtrl(response);
				else if(menu == game["menu_rcon_playerctrl"]) self thread extreme\_ex_rcon::rconPlayerCtrl(response);
				else if(menu == game["menu_rcon_scriptctrl"]) self thread extreme\_ex_rcon::rconScriptCtrl(response);
				else if(menu == game["menu_rcon_msgcenter"]) self thread extreme\_ex_rcon::rconMsgCenter(response);
				else if(menu == game["menu_rcon_setsvr"]) self thread extreme\_ex_rcon::rconSetSvr(response);
				else if(menu == game["menu_rcon_setclnt"]) self thread extreme\_ex_rcon::rconSetClnt(response);
				else if(menu == game["menu_rcon_setwpn"]) self thread extreme\_ex_rcon::rconSetWpn(response);
			}
		}
	}
}

spec_monitor() //spectator music switch
{
	self endon("disconnect");

	mt = undefined;
	
	self.music_chg = newClientHudElem(self);	
	self.music_chg.x = 322;
	self.music_chg.y = 462;
	self.music_chg.alpha = 1;
	self.music_chg.fontscale = 1;
	self.music_chg.alignX = "center";
	self.music_chg.alignY = "middle";
	self.music_chg setText(&"MISC_MELEE_CHANGE_MUSIC");
	
	for(;;)
	{
		if(self meleeButtonPressed())
		{
			self.music_chg fadeovertime(.2);
			self.music_chg.alpha = 0;
			self playLocalSound("spec_music_null");
			self playLocalSound("spec_music_stop");
			wait .2;
			self.music_chg setText(&"MISC_MUSIC_CHNG");
			self.music_chg fadeovertime(.2);
			self.music_chg.alpha = 1;
			self playLocalSound("spec_music");
			mt = 30;
		}
		
		if(isdefined(mt) && mt <= 0)
		{
			mt = undefined;
			self.music_chg fadeovertime(.2);
			self.music_chg.alpha = 0;
			wait .2;
			self.music_chg setText(&"MISC_MELEE_CHANGE_MUSIC");
			self.music_chg fadeovertime(.2);
			self.music_chg.alpha = 1;
		}
		
		if(isdefined(mt) && mt > 0) mt--;
		
		if(!self.pers["spec_on"] || level.ex_gameover == true)
		{
			self.music_chg destroy();
			break;
		}
		wait .001;
	}
}
