InventoryTable = {}

function AddItem(data)
	local itemdata = {}
	itemdata.Name = data:ReadString();
	itemdata.Class = data:ReadString();
	itemdata.Description = data:ReadString();
	itemdata.Model = data:ReadString();
	
	table.insert(InventoryTable, itemdata);
end
usermessage.Hook("addinventory", AddItem);

function ClearItems()
	
	InventoryTable = {}
	
end
usermessage.Hook("clearinventory", ClearItems);

BusinessTable = {};

function AddBusinessItem(data)
	local itemdata = {}
	itemdata.Name = data:ReadString();
	itemdata.Class = data:ReadString();
	itemdata.Description = data:ReadString();
	itemdata.Model = data:ReadString();
	itemdata.Price = data:ReadLong();
	
	table.insert(BusinessTable, itemdata);
end
usermessage.Hook("addbusiness", AddBusinessItem);

function ClearBusinessItems()
	
	BusinessTable = {}
	
end
usermessage.Hook("clearbusiness", ClearBusinessItems);

function InitHiddenButton()
	CAKE.HiddenButton = vgui.Create("DButton") -- HOLY SHIT WHAT A HACKY METHOD FO SHO
	CAKE.HiddenButton:SetSize(ScrW(), ScrH());
	CAKE.HiddenButton:SetText("");
	CAKE.HiddenButton:SetDrawBackground(false);
	CAKE.HiddenButton:SetDrawBorder(false);
	CAKE.HiddenButton.DoRightClick = function()
		local Vect = gui.ScreenToVector(gui.MouseX(), gui.MouseY());
		local tracedata = {};
		tracedata.start = LocalPlayer():GetShootPos();
		tracedata.endpos = LocalPlayer():GetShootPos() + (Vect * 100);
		tracedata.filter = LocalPlayer();
		local trace = util.TraceLine(tracedata);
		
		if(trace.HitNonWorld) then
			local target = trace.Entity;
			
			local ContextMenu = DermaMenu()
				if(CAKE.IsDoor(target)) then
					ContextMenu:AddOption("Rent/Unrent Door", function() LocalPlayer():ConCommand("rp_purchasedoor " .. target:EntIndex()) end);
					if( target:GetNWString("owner") == LocalPlayer():GetNWString("steamid") ) then
						ContextMenu:AddOption("Lock", function() LocalPlayer():ConCommand("rp_lockdoor " .. target:EntIndex()) end);
						ContextMenu:AddOption("Unlock", function() LocalPlayer():ConCommand("rp_unlockdoor " .. target:EntIndex()) end);
					end
				elseif(target:GetClass() == "item_prop") then
					ContextMenu:AddOption("Pick Up", function() LocalPlayer():ConCommand("rp_pickup " .. target:EntIndex()) end);
					ContextMenu:AddOption("Use", function() LocalPlayer():ConCommand("rp_useitem " .. target:EntIndex()) end);
				elseif(target:IsPlayer()) then
					local function PopupCredits()
						local CreditPanel = vgui.Create( "DFrame" );
						CreditPanel:SetPos(gui.MouseX(), gui.MouseY());
						CreditPanel:SetSize( 200, 175 )
						CreditPanel:SetTitle( "Give " .. target:Nick() .. " Credits");
						CreditPanel:SetVisible(true);
						CreditPanel:SetDraggable(true);
						CreditPanel:ShowCloseButton(true);
						CreditPanel:MakePopup();
						
						local Credits = vgui.Create( "DNumSlider", CreditPanel );
						Credits:SetPos( 25, 50 );
						Credits:SetWide(150);
						Credits:SetText("Credits to Give");
						Credits:SetMin( 0 );
						Credits:SetMax( tonumber(LocalPlayer():GetNWString("money")) );
						Credits:SetDecimals( 0 );
						
						local Give = vgui.Create( "DButton", CreditPanel );
						Give:SetText("Give");
						Give:SetPos( 25, 125 );
						Give:SetSize( 150, 25 );
						Give.DoClick = function()
							LocalPlayer():ConCommand("rp_givemoney " .. target:EntIndex() .. " " .. Credits:GetValue());
							CreditPanel:Remove();
							CreditPanel = nil;
						end
					end
					
					ContextMenu:AddOption("Give Credits", PopupCredits);
				elseif( target:GetClass() == "prop_physics" ) then
					ContextMenu:AddOption("Search", function() LocalPlayer():ConCommand("rp_search " .. target:EntIndex()) end);
				end
				
			ContextMenu:Open();
		end
	end
end

function InitHUDMenu()

	InitHiddenButton();

	HUDMenu = vgui.Create( "DFrame" )
	HUDMenu:SetPos( ScrW() - 130 - 5, 5 )
	HUDMenu:SetSize( 128, 128 )       -- Changing size cause I think its too big
	HUDMenu:SetSize( 65, 75 )
	HUDMenu:SetTitle( "Player Information" )
	HUDMenu:SetVisible( true )
	HUDMenu:SetDraggable( false )
	HUDMenu:ShowCloseButton( false )
	
	local label = vgui.Create("DLabel", HUDMenu);
	label:SetWide(0);
	label:SetPos(5, 25);
	label:SetText("Name: " .. LocalPlayer():Nick());
	
	--local cid = LocalPlayer():GetNWString("info.cid")
	--cid = string.sub(cid, 1, 2) .. "-" .. string.sub(cid, 3, 6) .. "-" .. string.sub(cid, 7, 10)

	--local label2 = vgui.Create("DLabel", HUDMenu);
	--label2:SetWide(0);
	--label2:SetPos(5, 40);
	--label2:SetText("CID: " .. cid);
	
	local label3 = vgui.Create("DLabel", HUDMenu);
	label3:SetWide(0);
	label3:SetPos(5, 55);
	label3:SetText("Title: " .. LocalPlayer():GetNWString("info.job"));
	
	local label4 = vgui.Create("DLabel", HUDMenu);
	label4:SetWide(0);
	label4:SetPos(5, 70);
	label4:SetText("Assosciation: " .. team.GetName(LocalPlayer():Team()));

	local label5 = vgui.Create("DLabel", HUDMenu);
	label5:SetWide(0);
	label5:SetPos(5, 85);
	label5:SetText(LocalPlayer():GetNWString("money") .. " Credits");
	
	local spawnicon = vgui.Create( "SpawnIcon", HUDMenu);
	spawnicon:SetSize( 128, 128 );
	spawnicon:SetPos(1,21);
	spawnicon:SetIconSize( 128 )
	spawnicon:SetModel(LocalPlayer():GetModel());
--	spawnicon:SetToolTip("Open Player Menu");
	
	local FadeSize = 130;
	
	function UpdateGUIData()
		label:SetText("Name: " .. LocalPlayer():Nick());
		
		--local cid = LocalPlayer():GetNWString("info.cid")
		--cid = string.sub(cid, 1, 2) .. "-" .. string.sub(cid, 3, 6) .. "-" .. string.sub(cid, 7, 10)

		--label2:SetText("CID: " .. cid);
		
		label3:SetText("Title: " .. LocalPlayer():GetNWString("info.job"));
		
		label4:SetText("Assosciation: " .. team.GetName(LocalPlayer():Team()));

		label5:SetText(LocalPlayer():GetNWString("money") .. " Credits");

		spawnicon:SetModel(LocalPlayer():GetModel());
		spawnicon:SetToolTip(false);
	end
	
	spawnicon.PaintOver = function()
		spawnicon:SetPos(FadeSize - 129, 21);
		HUDMenu:SetSize(FadeSize, 150);
		HUDMenu:SetPos(ScrW() - FadeSize - 5, 5 );
		
		label:SetWide(FadeSize - 128);
		label2:SetWide(FadeSize - 128);
		label3:SetWide(FadeSize - 128);
		label4:SetWide(FadeSize - 128);
		label5:SetWide(FadeSize - 128);
		
		if(FadeSize > 130) then
			FadeSize = FadeSize - 5;
		end
		
		UpdateGUIData();
	end
	
	spawnicon.PaintOverHovered = function()
		
		spawnicon:SetPos(FadeSize - 129, 21);
		HUDMenu:SetSize(FadeSize, 150);
		HUDMenu:SetPos(ScrW() - FadeSize - 5, 5 );
		
		label:SetWide(FadeSize - 128);
		label2:SetWide(FadeSize - 128);
		label3:SetWide(FadeSize - 128);
		label4:SetWide(FadeSize - 128);
		label5:SetWide(FadeSize - 128);
		
		if(FadeSize < 320) then
			FadeSize = FadeSize + 5;
		end
		
		UpdateGUIData();
	end
end




function ModelSelectMenu(data)

	local Name = data:ReadString();
	
	if(PickMenu) then
		PickMenu:Remove();
		PickMenu = nil;
	end
	
	local PickMenu = vgui.Create( "DFrame" );
	PickMenu:SetPos( ScrW() / 2 - 320, ScrH() / 2 - 240 );
	PickMenu:SetSize( 640, 480 );
	PickMenu:SetTitle( "Choose your class. Close menu to ... not ... get ... a gun." );
	PickMenu:SetVisible( true );
	PickMenu:SetDraggable( true );
	PickMenu:ShowCloseButton( true );
	PickMenu:MakePopup();
	
	local PickList = vgui.Create("DListView", PickMenu );
	PickList:SetPos(10,30)
	PickList:SetSize( 620, 440 );
	PickList:AddColumn( "Class" );
	PickList:AddLine( "Mobile Infantry" )
	PickList:AddLine( "Mobile Heavy Weapons Personnel - MG Loadout" )
	PickList:AddLine( "Mobile Intelligence and Reconnaisance Personnel" )
	PickList:AddLine( "Mobile Medical Aid Personnel" )
	PickList:AddLine( "Mobile Special Operations Trooper" )
	PickList:AddLine( "Mobile Engineer" )
	
function PickList:OnRowSelected(LineID, Line)
PickMenu:SetVisible( false )
		local loadout = Line:GetColumnText(1)
		if( loadout == "Mobile Infantry" ) then
		LocalPlayer():ConCommand( "rp_loadout Assault" )
		end
		if( loadout == "Mobile Heavy Weapons Personnel - MG Loadout" ) then
		LocalPlayer():ConCommand( "rp_loadout Support" )
		end
		if( loadout == "Mobile Intelligence and Reconnaisance Personnel" ) then
		LocalPlayer():ConCommand( "rp_loadout Reconnaissance" )
		end
		if( loadout == "Mobile Medical Aid Personnel" ) then
		LocalPlayer():ConCommand( "rp_loadout Medic" )
		end
		if( loadout == "Mobile Special Operations Trooper" ) then
		LocalPlayer():ConCommand( "rp_loadout Mspec" )
		end	
		if( loadout == "Mobile Engineer" ) then
		LocalPlayer():ConCommand( "rp_loadout Engineer" )
		end
end
end
usermessage.Hook("ModelSelect", ModelSelectMenu);

function ModelSelectMenuTwo(data)

	local Name = data:ReadString();
	
	if(PickMenu) then
		PickMenu:Remove();
		PickMenu = nil;
	end
	
	local PickMenu = vgui.Create( "DFrame" );
	PickMenu:SetPos( ScrW() / 2 - 320, ScrH() / 2 - 240 );
	PickMenu:SetSize( 640, 480 );
	PickMenu:SetTitle( "Choose your model. Close menu to keep current model." );
	PickMenu:SetVisible( true );
	PickMenu:SetDraggable( true );
	PickMenu:ShowCloseButton( true );
	PickMenu:MakePopup();
	
	local PickList = vgui.Create("DPanelList", PickMenu );
	PickList:SetPos(10,30)
	PickList:SetSize( 620, 440 );
	PickList:SetSpacing( 5 );
	PickList:SetPadding( 5 );
	PickList:EnableHorizontal( true );
	PickList:EnableVerticalScrollbar( true );
	
	local modelList = {};
	if( Name == "NSRF" ) then
	
		modelList = {
		"models/humans/group03/male_04.mdl",
		"models/humans/group03/male_07.mdl"
		};
		
	end
	
	if( Name == "NRD" ) then
	
		modelList = {
		"models/nrd_SOTAS_specops.mdl",
		"models/nrd_sas_infantry1.mdl",
		"models/characters/nrd_comm01.mdl",
		};
		
	end
	
	x = 10;
	y = 30;
	
	for k,v in pairs( modelList ) do

		if( x > 500 ) then -- past 4 icons, next row

			x = 10;
			y = y + 128 + 10;

		end
		
		local spawnicon = vgui.Create( "spawnicon" );
		PickList:AddItem( spawnicon );
		
		spawnicon:SetPos( x, y );
		x = x + 128 + 10;
		spawnicon:SetModel( v );
		spawnicon:SetSize( 128, 128 );
		spawnicon:SetMouseInputEnabled( true )
		spawnicon.DoClick = 
			function() 
				PickMenu:SetVisible( false ); -- hide this menu upon selection

				-- Call your set model function here and any other needed functions.
				LocalPlayer():ConCommand("rp_forcemodel " .. spawnicon:GetModel()); 
				--LocalPlayer():ConCommand("rp_flag " .. Name );

			end
		--spawnicon:SetToolTip( k );
		spawnicon:SetToolTip( false );
	
	end
	
end
usermessage.Hook("ModelSelectTwo", ModelSelectMenuTwo);


function CreatePlayerMenu()
	if(CAKE.PlayerMenu) then
		CAKE.PlayerMenu:Remove();
		CAKE.PlayerMenu = nil;
	end
	
	CAKE.PlayerMenu = vgui.Create( "DFrame" )
	CAKE.PlayerMenu:SetPos( ScrW() / 2 - 320, ScrH() / 2 - 240 )
	CAKE.PlayerMenu:SetSize( 640, 480 )
	CAKE.PlayerMenu:SetTitle( "Player Menu" )
	CAKE.PlayerMenu:SetVisible( true )
	CAKE.PlayerMenu:SetDraggable( true )
	CAKE.PlayerMenu:ShowCloseButton( true )
	CAKE.PlayerMenu:SetDeleteOnClose( true )
	-- CAKE.PlayerMenu:SetBackgroundBlur( true )
	CAKE.PlayerMenu:MakePopup()
	
	CAKE.PropertySheet = vgui.Create( "DPropertySheet" )
	CAKE.PropertySheet:SetParent(CAKE.PlayerMenu)
	CAKE.PropertySheet:SetPos( 2, 30 )
	CAKE.PropertySheet:SetSize( 636, 448 )
	
	local PlayerInfo = vgui.Create( "DPanelList" )
	PlayerInfo:SetPadding(20);
	PlayerInfo:SetSpacing(20);
	PlayerInfo:EnableHorizontal(false);
	
	local icdata = vgui.Create( "DForm" );
	icdata:SetPadding(4);
	icdata:SetName(LocalPlayer():Nick() or "");
	
	local FullData = vgui.Create("DPanelList");
	FullData:SetSize(0, 84);
	FullData:SetPadding(10);
	
	local DataList = vgui.Create("DPanelList");
	DataList:SetSize(0, 64);
	
	local spawnicon = vgui.Create( "SpawnIcon");
	spawnicon:SetModel(LocalPlayer():GetModel());
	spawnicon:SetSize( 64, 64 );
	DataList:AddItem(spawnicon);
	
	local DataList2 = vgui.Create( "DPanelList" )
	
	--local cid = LocalPlayer():GetNWString("info.cid")
	--cid = string.sub(cid, 1, 2) .. "-" .. string.sub(cid, 3, 6) .. "-" .. string.sub(cid, 7, 10)

	--local label = vgui.Create("DLabel");
	--label:SetText("CID: " .. cid);
	--DataList2:AddItem(label);
	
	local label2 = vgui.Create("DLabel");
	label2:SetText("Title: " .. LocalPlayer():GetNWString("info.job"));
	DataList2:AddItem(label2);
	
	local label3 = vgui.Create("DLabel");
	label3:SetText("Assosciation: " .. team.GetName(LocalPlayer():Team()));
	DataList2:AddItem(label3);

	local Divider = vgui.Create("DHorizontalDivider");
	Divider:SetLeft(spawnicon);
	Divider:SetRight(DataList2);
	Divider:SetLeftWidth(64);
	Divider:SetHeight(64);
	
	DataList:AddItem(spawnicon);
	DataList:AddItem(DataList2);
	DataList:AddItem(Divider);

	FullData:AddItem(DataList)
	
	icdata:AddItem(FullData)
	
	local vitals = vgui.Create( "DForm" );
	vitals:SetPadding(4);
	vitals:SetName("Vital Signs");
	
	local VitalData = vgui.Create("DPanelList");
	VitalData:SetAutoSize(true)
	VitalData:SetPadding(10);
	vitals:AddItem(VitalData);
	
	local healthstatus = ""
	local hp = LocalPlayer():Health();
	
	if(!LocalPlayer():Alive()) then healthstatus = "Dead";
	elseif(hp > 95) then healthstatus = "Healthy";
	elseif(hp > 50 and hp < 95) then healthstatus = "OK";
	elseif(hp > 30 and hp < 50) then healthstatus = "Near Death";
	elseif(hp > 1 and hp < 30) then healthstatus = "Death Imminent"; end
	
	local health = vgui.Create("DLabel");
	health:SetText("Vitals: " .. healthstatus);
	VitalData:AddItem(health);
	
	PlayerInfo:AddItem(icdata)
	PlayerInfo:AddItem(vitals)
	
	CharPanel = vgui.Create( "DPanelList" )
	CharPanel:SetPadding(20);
	CharPanel:SetSpacing(20);
	CharPanel:EnableHorizontal(false);
	
	local newcharform = vgui.Create( "DForm" );
	newcharform:SetPadding(4);
	newcharform:SetName("New Character");
	newcharform:SetAutoSize(true);
	
	local CharMenu = vgui.Create( "DPanelList" )
	newcharform:AddItem(CharMenu);
	CharMenu:SetSize( 316, 386 )
	CharMenu:SetPadding(10);
	CharMenu:SetSpacing(20);
	CharMenu:EnableVerticalScrollbar();
	CharMenu:EnableHorizontal(false);


	
	local label = vgui.Create("DLabel");
	CharMenu:AddItem(label);
	label:SetSize(400, 25);
	label:SetPos(5, 25);
	label:SetText("Please Pick a Serious RP Name.");

	local info = vgui.Create( "DForm" );
	info:SetName("Personal Information");
	CharMenu:AddItem(info);

	local label = vgui.Create("DLabel");
	info:AddItem(label);
	label:SetSize(30,25);
	label:SetPos(150, 50);
	label:SetText("First: ");

	local firstname = vgui.Create("DTextEntry");
	info:AddItem(firstname);
	firstname:SetSize(100,25);
	firstname:SetPos(185, 50);
	firstname:SetText("");

	local label = vgui.Create("DLabel");
	info:AddItem(label);
	label:SetSize(30,25);
	label:SetPos(5, 50);
	label:SetText("Last: ");

	local lastname = vgui.Create("DTextEntry");
	info:AddItem(lastname);
	lastname:SetSize(100,25);
	lastname:SetPos(40, 50);
	lastname:SetText("");

	local label = vgui.Create("DLabel");
	info:AddItem(label);
	label:SetSize(100,25);
	label:SetPos(5, 80);
	label:SetText("Title: ");

	local occupation = vgui.Create("DTextEntry");
	info:AddItem(occupation);
	occupation:SetSize(205, 25);
	occupation:SetPos(80, 80);
	occupation:SetText("Unemployed");

	local spawnicon = nil;

	local modelform = vgui.Create( "DForm" );
	modelform:SetPadding(4);
	modelform:SetName("Appearance");
	CharMenu:AddItem(modelform);

	local modellist = vgui.Create( "DPanelList" );
	modellist:EnableHorizontal(true);
	modellist:EnableVerticalScrollbar();
	modellist:SetSize(256, 128);
	modelform:AddItem(modellist);

	for n = 1, table.getn(models) do -- Yes, this is Rick's code.
		
		local spawnicon = vgui.Create( "SpawnIcon");
		spawnicon:SetPos( x, y );
		spawnicon:SetSize( 64, 64 );
		spawnicon:SetModel( models[n] );
		--spawnicon:SetToolTip(false);
		spawnicon:SetToolTip( models[n] );

		spawnicon.DoClick = function() SetChosenModel( models[n] ) end
		
		modellist:AddItem(spawnicon);
		
	end

	local apply = vgui.Create("DButton");
	apply:SetSize(100, 25);
	apply:SetText("Apply");
	apply.DoClick = function ( btn )

		
		if(firstname:GetValue() == "" or lastname:GetValue() == "") then
			LocalPlayer():PrintMessage(3, "You must enter a first and last name!");
			return;
		end
		
		if(!table.HasValue(models, ChosenModel)) then
			LocalPlayer():PrintMessage(3, ChosenModel .. " is not a valid model!");
			return;
		end
		
		LocalPlayer():ConCommand("rp_startcreate");
		LocalPlayer():ConCommand("rp_setmodel " .. ChosenModel);
		LocalPlayer():ConCommand("rp_defaultmodel");
		LocalPlayer():ConCommand("rp_setstats"); -- Set spawning stats
		LocalPlayer():ConCommand("rp_changename " .. firstname:GetValue() .. " " .. lastname:GetValue());
		LocalPlayer():ConCommand("rp_job " .. string.sub(occupation:GetValue(), 1, 32));
		LocalPlayer():PrintMessage(3, "Welcome home, " .. firstname:GetValue() .. " " .. lastname:GetValue());
		LocalPlayer().MyModel = ""
		LocalPlayer():ConCommand("rp_finishcreate");
		
		--DrawNewChat();
		
		CAKE.PlayerMenu:Remove();
		CAKE.PlayerMenu = nil;
		
	end
	CharMenu:AddItem(apply);

	local selectcharform = vgui.Create( "DForm" );
	selectcharform:SetPadding(4);
	selectcharform:SetName("Select Character");
	selectcharform:SetSize(316, 386);

	local charlist = vgui.Create( "DPanelList" );
	selectcharform:AddItem(charlist)
	charlist:SetSize( 316, 386 )
	charlist:SetPadding(10);
	charlist:SetSpacing(20);
	charlist:EnableVerticalScrollbar();
	charlist:EnableHorizontal(true);

	for n = 1, table.getn(ExistingChars) do

		local spawnicon = vgui.Create( "SpawnIcon");
		spawnicon:SetSize( 128, 128 );
		spawnicon:SetIconSize( 128 )
		spawnicon:SetModel(ExistingChars[n]['model']);
		spawnicon:SetToolTip(false);
		spawnicon.DoClick = function ( btn )
		
			LocalPlayer():ConCommand("rp_selectchar " .. n);
			LocalPlayer().MyModel = ""
			
			DrawNewChat();
			
			CAKE.PlayerMenu:Remove();
			CAKE.PlayerMenu = nil;
			
		end
		charlist:AddItem(spawnicon);
		
		spawnicon.PaintOver = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("Trebuchet18");
			surface.SetTextPos(64 - surface.GetTextSize(ExistingChars[n]['name']) / 2, 5);
			surface.DrawText(ExistingChars[n]['name'])
		end
		
		spawnicon.PaintOverHovered = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("Trebuchet18");
			surface.SetTextPos(64 - surface.GetTextSize(ExistingChars[n]['name']) / 2, 5);
			surface.DrawText(ExistingChars[n]['name'])
		end
		
	end

	
	local divider = vgui.Create("DHorizontalDivider");
	divider:SetLeft(newcharform);
	divider:SetRight(selectcharform);
	divider:SetLeftWidth(316); 

	CharPanel:AddItem(newcharform);
	CharPanel:AddItem(selectcharform);
	CharPanel:AddItem(divider);
	
--	factionTab();
	
	/* NO MORE FLAG TABLE, IT WILL NOW BE A FACTION TABLE! MUAHAHAHAHA!!!! (cl_factionTab.lua)
	Commands = vgui.Create( "DPanelList" )
	Commands:SetPadding(20);
	Commands:SetSpacing(20);
	Commands:EnableHorizontal(true);
	Commands:EnableVerticalScrollbar(true);
	
	local Flags = vgui.Create("DListView");
	Flags:SetSize(600,446);
	Flags:SetMultiSelect(false)
	Flags:AddColumn("Flag Name");
	Flags:AddColumn("Salary");
	Flags:AddColumn("Business Access");
	Flags:AddColumn("Public Flag?");
	
	function Flags:DoDoubleClick(LineID, Line)
	
		LocalPlayer():ConCommand("rp_flag " .. TeamTable[LineID].flagkey);
		CAKE.PlayerMenu:Remove();
		CAKE.PlayerMenu = nil;
		
	end
	
	for k, v in pairs(TeamTable) do
		local yesno = "";
		if(v.public) then
			yesno = "Yes";
		elseif(!v.public) then
			yesno = "No";
		end
		
		local yesno2 = "";
		if(v.business) then
			yesno2 = "Yes";
		elseif(!v.business) then
			yesno2 = "No";
		end
		
		Flags:AddLine(v.name, tostring(v.salary), yesno2, yesno);
	end
	
	Commands:AddItem(Flags);
	*/
	
	--------------------OLD INV CODE
	/*
	Inventory = vgui.Create( "DPanelList" )
	Inventory:SetPadding(20);
	Inventory:SetSpacing(20);
	Inventory:EnableHorizontal(true);
	Inventory:EnableVerticalScrollbar(true);
	
	for k, v in pairs(InventoryTable) do
		local spawnicon = vgui.Create( "SpawnIcon");
		spawnicon:SetSize( 128, 128 );
		spawnicon:SetIconSize( 128 )
		spawnicon:SetModel(v.Model);
		spawnicon:SetToolTip(false);
		
		local function DeleteMyself()
			spawnicon:Remove()
		end
		
		spawnicon.DoClick = function ( btn )
		
			local ContextMenu = DermaMenu()
				ContextMenu:AddOption("Drop", function() LocalPlayer():ConCommand("rp_dropitem " .. v.Class); DeleteMyself(); end);
			ContextMenu:Open();
			
		end
		
		spawnicon.PaintOver = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("DefaultSmall");
			surface.SetTextPos(64 - surface.GetTextSize(v.Name) / 2, 5);
			surface.DrawText(v.Name)
		end
		
		spawnicon.PaintOverHovered = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("DefaultSmall");
			surface.SetTextPos(64 - surface.GetTextSize(v.Name) / 2, 5);
			surface.DrawText(v.Name)
		end
		
		Inventory:AddItem(spawnicon);
	end
	*/
	---------OLD INV CODE END
	
	----------NEW INV CODE START
	Inventory = vgui.Create( "DPanelList" )
	Inventory:SetPadding(20);
	Inventory:SetSpacing(20);
	Inventory:EnableHorizontal(true);
	Inventory:EnableVerticalScrollbar(true);
	
	for k, v in pairs(InventoryTable) do

	local spawnicon = vgui.Create( "DModelPanel" )
	spawnicon:SetSize( 128, 128 );
	spawnicon:SetModel( v.Model )
	spawnicon:SetAnimSpeed( 0.0 )
	spawnicon:SetAnimated( false )
	spawnicon:SetAmbientLight( Color( 50, 50, 50 ) )
	spawnicon:SetDirectionalLight( BOX_TOP, Color( 255, 255, 255 ) )
	spawnicon:SetDirectionalLight( BOX_FRONT, Color( 255, 255, 255 ) )
	spawnicon:SetCamPos( Vector( 0, 0, 40 ) )
	spawnicon:SetLookAt( Vector( 0, 0, 40 ) )
	spawnicon:SetFOV( 70 )
	spawnicon:SetToolTip(v.Description)
		
		local function DeleteMyself()
			spawnicon:Remove()
		end
		
		spawnicon.DoClick = function ( btn )
		if( LocalPlayer():GetNWInt( "tiedup" ) == 1 ) then return false; end
			local ContextMenu = DermaMenu()
			ContextMenu:AddOption("Drop", function() LocalPlayer():ConCommand("rp_dropitem " .. v.Class); DeleteMyself(); end);
			ContextMenu:AddOption("Use", function() LocalPlayer():ConCommand("rp_useinvitem ".. v.Class); DeleteMyself(); end);
			ContextMenu:Open();
			
		end
		
		spawnicon.PaintOver = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("DefaultSmall");
			surface.SetTextPos(64 - surface.GetTextSize(v.Name) / 2, 5);
			surface.DrawText(v.Name)
		end
		
		spawnicon.PaintOverHovered = function()
			surface.SetTextColor(Color(255,255,255,255));
			surface.SetFont("DefaultSmall");
			surface.SetTextPos(64 - surface.GetTextSize(v.Name) / 2, 5);
			surface.DrawText(v.Name)
		end
		
		spawnicon.LayoutEntity = function( self, ent )
		
			ent:SetAngles( Angle( 0, 90, 100 ) );
		
		end

		Inventory:AddItem(spawnicon);
	end
	-------------NEW INV CODE END
	
	
	Business = vgui.Create( "DPanelList" )
	Business:SetPadding(20);
	Business:SetSpacing(20);
	Business:EnableHorizontal(true);
	Business:EnableVerticalScrollbar(true);
	if(TeamTable[LocalPlayer():Team()] != nil) then
		if(TeamTable[LocalPlayer():Team()].business) then
			for k, v in pairs(BusinessTable) do
				local spawnicon = vgui.Create( "SpawnIcon");
				spawnicon:SetSize( 128, 128 );
				spawnicon:SetIconSize( 128 )
				spawnicon:SetModel(v.Model);
				spawnicon:SetToolTip(false);
				
				spawnicon.DoClick = function ( btn )
				
					local ContextMenu = DermaMenu()
						if(tonumber(LocalPlayer():GetNWString("money")) >= v.Price) then
							ContextMenu:AddOption("Purchase", function() LocalPlayer():ConCommand("rp_buyitem " .. v.Class); end);
						else
							ContextMenu:AddOption("Not Enough Tokens!");
						end
					ContextMenu:Open();
					
				end
				
				spawnicon.PaintOver = function()
					surface.SetTextColor(Color(255,255,255,255));
					surface.SetFont("DefaultSmall");
					surface.SetTextPos(64 - surface.GetTextSize(v.Name .. " (" .. v.Price .. ")") / 2, 5);
					surface.DrawText(v.Name .. " (" .. v.Price .. ")")
				end
				
				spawnicon.PaintOverHovered = function()
					surface.SetTextColor(Color(255,255,255,255));
					surface.SetFont("DefaultSmall");
					surface.SetTextPos(64 - surface.GetTextSize(v.Name .. " (" .. v.Price .. ")") / 2, 5);
					surface.DrawText(v.Name .. " (" .. v.Price .. ")")
				end
				
				Business:AddItem(spawnicon);
			end
		elseif(!TeamTable[LocalPlayer():Team()].business) then
			local label = vgui.Create("DLabel")
			label:SetText("You do not have access to the Business tab!");
			label:SetWide(400);
			
			Business:AddItem(label);
		end
	end
	
	Scoreboard = vgui.Create( "DPanelList" )
	Scoreboard:SetPadding(10); -- changed from 0 to 10
	Scoreboard:SetSpacing(10); -- changed from 0 to 10
	Scoreboard:EnableVerticalScrollbar();
	Scoreboard:EnableHorizontal(false);

	-- Let's draw the SCOREBOARD.
	
	for k, v in pairs(player.GetAll()) do
		local DataList = vgui.Create("DPanelList");
		--DataList:SetAutoSize( true )
		--DataList:SizeToContents( true );
		DataList:SetTall( 75 );
		
		local CollapsableCategory = vgui.Create("DCollapsibleCategory");
		CollapsableCategory:SetExpanded( 1 )
		CollapsableCategory:SetLabel( v:Nick() );
		Scoreboard:AddItem(CollapsableCategory);
		
		local spawnicon = vgui.Create( "SpawnIcon");
		spawnicon:SetModel(v:GetModel());
		spawnicon:SetSize( 64, 64 );
		spawnicon:SetMouseInputEnabled(false);
		DataList:AddItem(spawnicon);
		spawnicon:SetToolTip(false);
		
		local DataList2 = vgui.Create( "DPanelList" )
		DataList2:SetAutoSize( true )
		
		local label = vgui.Create("DLabel");
		label:SetText("OOC Name: " .. v:Name());
		DataList2:AddItem(label);
		
		local label2 = vgui.Create("DLabel");
		label2:SetText("Title: " .. v:GetNWString("info.job"));
		DataList2:AddItem(label2);
		
		local label3 = vgui.Create("DLabel");
		label3:SetText("Ping: " .. v:Ping());
		DataList2:AddItem(label3);
		
		local Divider = vgui.Create("DHorizontalDivider");
		Divider:SetLeft(spawnicon);
		Divider:SetRight(DataList2);
		Divider:SetLeftWidth(64);
		Divider:SetHeight(64);
		
		DataList:AddItem(spawnicon);
		DataList:AddItem(DataList2);
		DataList:AddItem(Divider);
		
		CollapsableCategory:SetContents(DataList);
	end

	local Help = vgui.Create( "DPanelList" )
	Help:SetPadding(20);
	Help:EnableHorizontal(false);
	Help:EnableVerticalScrollbar(true);
	
	local function AddHelpLine(text)
			local label = vgui.Create("DLabel");
			label:SetText(text);
			label:SizeToContents();
			Help:AddItem(label);
	end
	
	local lines = {
	"Welcome to the AftermathRP: Out of the Fire server.",
	"Visit our forums at http://www.aftermathrp.com",
	"",
	"                                                                                       Storyline:",
	"",
	"Civilization as we know it came to an end in the year 2009. A rain of nuclear warheads finished the job",
	"after grueling conventional warfare failed. Whichever side pushed the button first is no longer around",
	"to see the devastation they have unleashed. The surviving citizens, however, could care less. Plunged into",
	"a primitive, anarchic society, all that matters now is survival.",
	"",
	"You are currently wandering the outskirts of Darlington Nuclear Power Plant in southern Ontario, Canada.",
	"Conventional bombs ripped this area apart a few months before the nukes began to fly. Food and clean water are",
	"scarce, modern equipment even scarcer. The locals are scared, confused, but ready to do whatever it takes to",
	"live. Law no longer applies, with government eradicated. Gangs and factions, formed by the feral survivors, ",
	"have already begun to war for supplies and shelter.",
	"",
	"The only source of hope in this area are rumors of a surviving group of Canadian Armed Forces Reservists,",
	"searching the wasteland for survivors to bring to their post-apocalyptic paradise. Most dismiss this as an",
	"overly optimistic fairytale, but there may be some truth to the story. Nevertheless, the survivors in this",
	"region live every day of their life as if it were their last. And rightly so.",
	"",
	"",
	"                                                                               Basic Interaction:",
	"",
	"Nearly everything that you should be able to interact with can be interacted with by using TAB + Rightclick.",
	"In CakeScript, TAB enables your mouse clicker allowing you to do things like rightclick entities.",
	"",
	"Just hold tab, and hover your mouse over the entity you wish to interact with, then press your right mouse button.",
	"We call the menu the Context Menu. Valid entities at the moment to interact with are items, doors, and players.",
	"",
	"                                                                                     Economy:",
	"",
	"In AftermathRP, the economy is completely player driven. There is no salary, and the only ways money can get into the",
	"economy are rations or admins. To give a player money, you just have to open a context menu for them, click Give Money",
	"and specify the amount to give. That's it!",
	"",
	"",
	"                                                                                    Commands:",
	"",
	"Chat Commands",
	"/ooc or // - OOC Chat, this will send to all players on the server",
	".// or [[ - Local OOC Chat, this is considered OOC, but it will only go to players within speaking range.",
	"/y /yell - Yell something, larger radius than regular talking.",
	"/w /whisper - Whisper something, much smaller radius than regular talking.",
	"/me - Describe an action to the people around you.",
	"/it - Describe something in object form.",
	"/a /an /? - Say something anonymously.",
	"/pm name message - Send a private message to someone. Not to be used IC.",
	"/bc - Broadcast something, if your team is allowed to do as such.",
	"/passout /sleep - Have your character passout.",
	"/wakeup /getup - Have your character wakeup.",
	"/drop /dropgun /dropweapon - Drops the weapon you are holding.",
	"!ad !acceptdeath - to accept your death and respawn.",
	"/nsrf - Allows you to become NSRF if you are properly flagged.",
	"/nrd - Allows you to become NRD if you are properly flagged.",
	"/makensrf *NAME* - Gives the *NAME* person the nsrf flag.",
	"/makensrfl *NAME* - Gives the *NAME* person the nsrf leader flag.",
	"/makenrd *NAME* - Gives the *NAME* person the nrd flag.",
	"/makenrdl *NAME* - Gives the *NAME* person the nrd leader flag.",
	"",
	"Regular Commands",
	"Please note, all these commands can be said in chat as well by replacing rp_ with !",
	"rp_changename - Change your roleplay name",
	"rp_job - Set your occupation (This is not your job flag)",
	"rp_acceptdeath - If you died, this will allow you to skip the respawn timer.",
	"rp_flag flagkey - Set your current flag, this is already made into a menu though.",
	"rp_dropweapon - Drop the current weapon you are holding. Not all weapons can be dropped.",
	"rp_toggleholster - Holsters/unholsters your weapon.",
	"rp_equip - If you are missing some SWEPs, just type this to get them.",
	"",
	"Keys",
	"F1 - Player Menu",
	"TAB - Enable Mouse",
	"TAB + RightClick - Context Menu",
	"",
	"                                                                                    Player Menus:",
	"",
	"Character Menu-",
	"Select your character or create a new one. You see this when you first spawn.",
	"Flags Menu-",
	"Easily select your flags. Just doubleclick on the one you want to use. Public flags are available to everyone.",
	"Backpack-",
	"This is your inventory. Click on an item to bring up a menu with options.",
	"Business-",
	"This tab is only available if you are using a flag with BUSINESS ACCESS. Click on an item to bring up a menu with options.",
	"Scoreboard-",
	"This shows some basic player/character info for everyone. Click on one of the tabs to make it expand!",
	"",
	"",
	"Well, that's pretty much all you need to know, have fun!"
	}

	for k, v in pairs(lines) do
		AddHelpLine(v);
	end
	
	--PropertySheet:AddSheet( "Player Menu", PlayerInfo, "gui/silkicons/user", false, false, "General information.");
	--PropertySheet:AddSheet( "Character Menu", CharPanel, "gui/silkicons/group", false, false, "Switch to another character or create a new one.");
	--PropertySheet:AddSheet( "Commands/Flagging", Commands, "gui/silkicons/wrench", false, false, "Execute some common commands or set your flag.");
	--PropertySheet:AddSheet( "Backpack", Inventory, "gui/silkicons/box", false, false, "View your inventory.")
	--PropertySheet:AddSheet( "Business", Business, "gui/silkicons/box", false, false, "Purchase items.");
	--PropertySheet:AddSheet( "Scoreboard", Scoreboard, "gui/silkicons/application_view_detail", false, false, "View the scoreboard.");	
	--PropertySheet:AddSheet( "Help", Help, "gui/silkicons/magnifier", false, false, "Get some help with CakeScript!");
	CAKE.PropertySheet:AddSheet( "Player Menu", PlayerInfo, "gui/silkicons/user", false, false, false);
	CAKE.PropertySheet:AddSheet( "Character Menu", CharPanel, "gui/silkicons/group", false, false, false);
	--CAKE.PropertySheet:AddSheet( "Commands/Flagging", Commands, "gui/silkicons/wrench", false, false, false);
--	CAKE.PropertySheet:AddSheet( "Faction", CAKE.FactionTab, "gui/silkicons/wrench", false, false, false );
	CAKE.PropertySheet:AddSheet( "Backpack", Inventory, "gui/silkicons/box", false, false, false)
	CAKE.PropertySheet:AddSheet( "Business", Business, "gui/silkicons/box", false, false, false);
	CAKE.PropertySheet:AddSheet( "Scoreboard", Scoreboard, "gui/silkicons/application_view_detail", false, false, false);	
	CAKE.PropertySheet:AddSheet( "Help", Help, "gui/silkicons/magnifier", false, false, false);
	
	drawOptionsTab();
	
	-- Make it so that the default tab would be scoreboard. Overwritten when called by CharacterCreatePanel
	CAKE.PropertySheet:SetActiveTab( CAKE.PropertySheet.Items[5].Tab );
end
usermessage.Hook("playermenu", CreatePlayerMenu);

function OpenHelpMenu()

	CreatePlayerMenu()
	CAKE.PropertySheet:SetActiveTab( CAKE.PropertySheet.Items[6].Tab );
	
end
usermessage.Hook("openHelpMenu", OpenHelpMenu);