
init()
{
	suffix = undefined;
	allweapons = false;
	allies = "";
	axis = "";

	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:	allweapons = true;			break; // all weapons
	}

	if(allweapons) // 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;

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

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

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

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

		if(response == "menuopen") self.ex_inmenu = true;
		else if(response == "menuclose") self.ex_inmenu = false;
	}
}

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

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

		if(response == "menuopen" || response == "menuclose") continue;

		if(menu == game["menu_serverinfo"])
		{
			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 openMenu(game["menu_serverinfo"]);
					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 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(menu == game["menu_quickcommands"]) maps\mp\gametypes\_quickmessages::quickcommands(response);
			else if(menu == game["menu_quickstatements"]) maps\mp\gametypes\_quickmessages::quickstatements(response);
			else if(menu == game["menu_quickresponses"]) maps\mp\gametypes\_quickmessages::quickresponses(response);
			else if(menu == game["menu_quickrequests"]) maps\mp\gametypes\_quickmessages::quickrequests(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"])    
		{
			self.music_chg destroy();
			break;
		}
		wait .001;
	}
}
