if( CharacterMenu ) then

	if( CharacterMenu.TitleBar ) then

		CharacterMenu.TitleBar:Remove();
		CharacterMenu.TitleBar = nil;
		
	end
		
	CharacterMenu:Remove();
	CharacterMenu = nil;

end

if( NamePanel ) then

	NamePanel:Remove()
	NamePane = nil
	
end	

if( AgePanel ) then

	AgePanel:Remove()
	AgePanel = nil
	
end	

if( StatsWindow ) then

	StatsWindow:Remove()
	StatsWindow = nil
	
end	

surface.CreateFont( "akbar", 32, 500, true, false, "LargeAkbar" );
surface.CreateFont( "akbar", 20, 500, true, false, "Akbar" );
surface.CreateFont( "akbar", 16, 500, true, false, "SmallAkbar" );

local ChosenCharacterModel = "";

local FreeStatPoints = 30;
local MaxFreeStats = FreeStatPoints;

function CreateCharacterMenu()

	--what an eye sore
	if string.find( ClientVars["FactionFlags"], "A" ) then
	
		table.insert( TS.SelectablePlayerModels, "models/stalker_exo/ex_freedom.mdl" )
		
	elseif string.find( ClientVars["FactionFlags"], "B" ) then	
	
		table.insert( TS.SelectablePlayerModels, "models/stalker_hood_duty.mdl" )
		
	elseif string.find( ClientVars["FactionFlags"], "BA" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/stalker_exo/exosk_dolg.mdl" )
		
	elseif string.find( ClientVars["FactionFlags"], "C" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/stalker_bandit_veteran2.mdl" )
		
	elseif string.find( ClientVars["FactionFlags"], "D" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/stalker_bandit_veteran.mdl" )	
		
	elseif string.find( ClientVars["FactionFlags"], "E" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/stalker_hood_ecologist1.mdl" )		
		
	elseif string.find( ClientVars["FactionFlags"], "F" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/srp/stalker_antigas_killer_fr.mdl" )	
		
	elseif string.find( ClientVars["FactionFlags"], "H" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/srp/specnaz.mdl" )		
		
	elseif string.find( ClientVars["FactionFlags"], "Ha" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/srp/stalker_antigas_killer_uk.mdl" )		
		
	elseif string.find( ClientVars["FactionFlags"], "Hb" ) then		
	
		table.insert( TS.SelectablePlayerModels, "models/srp/stalker_antigas_killer.mdl" )		
		
	end	
		
		

	gui.EnableScreenClicker( true )

	CharacterMenu = CreateBPanel( "Character Creation", ScrW() * .2, ScrH() * .2, ScrW() * .6, ScrH() * .6 )
	CharacterMenu:CanClose( false );
	CharacterMenu:CanDrag( false );
	CharacterMenu:CanSeeTitle( false );
	CharacterMenu:SetBodyColor( Color( 30, 30, 30, 255 ) )
	CharacterMenu:SetOutline( true, 3 )
	
	local x, y = CharacterMenu:GetPos();
	
	CharacterMenu.ModelSelectPanel = CreateBPanel( nil, 10, 110, 216, 256 )
	CharacterMenu.ModelSelectPanel:SetParent( CharacterMenu );
	CharacterMenu.ModelSelectPanel:SetBodyColor( Color( 30, 30, 30, 255 ) );
	CharacterMenu.ModelSelectPanel:CanClose( false );
	CharacterMenu.ModelSelectPanel:CanDrag( false );
	CharacterMenu.ModelSelectPanel:CanSeeTitle( false );
	CharacterMenu.ModelSelectPanel:EnableScrolling( true );
	CharacterMenu.ModelSelectPanel:SetOutline( false, 0 )
	
	CharacterMenu.ModelViewPanel = CreateBPanel( nil, 300, 0, 190, 350 );
	CharacterMenu.ModelViewPanel:SetParent( CharacterMenu );
	CharacterMenu.ModelViewPanel:CanClose( false );
	CharacterMenu.ModelViewPanel:CanDrag( false );
	CharacterMenu.ModelViewPanel:CanSeeTitle( false );
	CharacterMenu.ModelViewPanel:EnableScrolling( true );
	CharacterMenu.ModelViewPanel:SetBodyColor( Color( 30, 30, 30, 0 ) );
	CharacterMenu.ModelViewPanel:SetOutline( false, 0 )
	
	local function SetPlayerViewModel( model )
	
		ChosenCharacterModel = model;
	
		if( not CharacterMenu.ModelViewPanel.Model ) then
		
			CharacterMenu.ModelViewPanel.Model = vgui.Create( "DModelPanel", CharacterMenu.ModelViewPanel );
			CharacterMenu.ModelViewPanel.Model:SetPos( -70, 0 );
			CharacterMenu.ModelViewPanel.Model:SetSize( 500, 500 );
			CharacterMenu.ModelViewPanel.Model:SetModel( model );
			
			CharacterMenu.ModelViewPanel.Model.LayoutEntity = function( self )
			    
			    self:RunAnimation();
			    
		 	end
		 	
			
			CharacterMenu.ModelViewPanel.Model:SetFOV( 100 );
			CharacterMenu.ModelViewPanel.Model:SetLookAt( Vector( 0, 0, 50 ) );
	 		CharacterMenu.ModelViewPanel.Model:SetCamPos( Vector( 30, -10, 55 ) );
			
		else
			
			CharacterMenu.ModelViewPanel.Model:SetModel( model );

		
		end
	
	end
	
	CharacterMenu.ModelSelectPanel.Models = { }
	local modelx = 0
	local modely = 0
	
	for k, v in pairs( TS.SelectablePlayerModels ) do
	
		CharacterMenu.ModelSelectPanel.Models[k] = vgui.Create( "SpawnIcon", CharacterMenu.ModelSelectPanel );
		CharacterMenu.ModelSelectPanel.Models[k]:SetModel( v );
		CharacterMenu.ModelSelectPanel.Models[k]:SetPos( modelx, modely );
		CharacterMenu.ModelSelectPanel.Models[k]:SetSize( 64, 64 );
		CharacterMenu.ModelSelectPanel.Models[k].LayoutEntity = function()
		    
	 	end
	 	
	 	CharacterMenu.ModelSelectPanel.Models[k].DoClick = function()
	 	
	 		SetPlayerViewModel( v );
	 	
	 	end
	 	
	 	CharacterMenu.ModelSelectPanel:AddObject( CharacterMenu.ModelSelectPanel.Models[k] );
	 	
	 	modelx = modelx + 64;
	 	
	 	if( modelx >= 192 ) then
	 	
	 		modely = modely + 64;
	 		modelx = 0;
	 	
	 	end
	 	
	 end	
	
	local function NamePanel()
	
		NamePanel = CreateBPanel( "Character Name", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 )
		NamePanel:CanClose( true )
		NamePanel:CanDrag( false )
		NamePanel:SetBodyColor( Color( 40, 40, 40, 220 ) )
		
		NamePanel:AddLabel( "Name:", "NewChatFont", 13, 8, Color( 255, 255, 255, 255 ) );
	
		local x, y = NamePanel:GetPos();
		
		NamePanel.NameEntry = vgui.Create( "DTextEntry", NamePanel )
		NamePanel.NameEntry:SetPos( x + 80, y + 8 )
		NamePanel.NameEntry:SetSize( 100, 15 )
		NamePanel.NameEntry:SetText( LocalPlayer():GetTable().CCName or "" )
		NamePanel.NameEntry:MakePopup()
	
		ApplyMaxCharacters( NamePanel.NameEntry, 20 )
	
		NamePanel.SetName = NamePanel:AddButton( "Set Name", 60, 25 )
		
		NamePanel.SetName.Action = function()
	
			if( string.len( NamePanel.NameEntry:GetValue() ) < 7 or
			string.len( NamePanel.NameEntry:GetValue() ) > 38 ) then
		
				CreateOkPanel( "Invalid name.", "Your name needs to be between 7 and 38 characters." );
				return;		
		
			end
	
			if( string.gsub( NamePanel.NameEntry:GetValue(), " ", "" ) == "" ) then
		
				CreateOkPanel( "Invalid name.", "Your name is blank, queer." );
				return;		
		
			end
			
			RunConsoleCommand( "eng_ccsetname", NamePanel.NameEntry:GetValue() )
					
			CharacterMenu.NameLabel = CharacterMenu:AddLabel( NamePanel.NameEntry:GetValue(), "Akbar", 240, 20 )
			
			if( NamePanel ) then
				NamePanel:Remove()
				NamePanel = nil
			end	
				
		end			
	
	end
	
	local function AgePanel()
	
		AgePanel = CreateBPanel( "Character Age", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 )
		AgePanel:CanClose( true )
		AgePanel:CanDrag( false )
		AgePanel:SetBodyColor( Color( 40, 40, 40, 220 ) )
		
		AgePanel:AddLabel( "Age:", "NewChatFont", 13, 8, Color( 255, 255, 255, 255 ) );
	
		local x, y = AgePanel:GetPos();
		
		AgePanel.AgeEntry = vgui.Create( "DTextEntry", AgePanel )
		AgePanel.AgeEntry:SetPos( x + 80, y + 8 )
		AgePanel.AgeEntry:SetSize( 100, 15 )
		AgePanel.AgeEntry:MakePopup()
	
		ApplyMaxCharacters( AgePanel.AgeEntry, 2 )
	
		AgePanel.SetAge = AgePanel:AddButton( "Set Age", 60, 25 )
		
		AgePanel.SetAge.Action = function()
	
			if( not tonumber( AgePanel.AgeEntry:GetValue() ) ) then
		
				CreateOkPanel( "Invalid number", "Invalid age." );
				return;
		
			end
		
			if( tonumber( AgePanel.AgeEntry:GetValue() ) < 18 or
				tonumber( AgePanel.AgeEntry:GetValue() ) > 80 ) then
			
					CreateOkPanel( "Invalid number", "Age must be between 18 and 80." );
			return;			
			
			end
			
			RunConsoleCommand( "eng_ccsetage", AgePanel.AgeEntry:GetValue() )
					
			AgePanel.AgeLabel = CharacterMenu:AddLabel( AgePanel.AgeEntry:GetValue(), "Akbar", 240, 50 )
		
		
			AgePanel:Remove()
			AgePanel = nil
			
		end			
	
	end
	
	local function StatsWindow()
	
		StatsPanel = CreateBPanel( "Set Stats", ScrW() / 2 - 150, ScrH() / 2 - 200, 247, 264 )
		StatsPanel:CanClose( true )
		StatsPanel:CanDrag( false )
		StatsPanel:CanSeeTitle( false )
		StatsPanel:SetBodyColor( Color( 40, 40, 40, 220 ) )
		
		StatsPanel.PaintHook = function()
		
			local strperc = ClientVars["Strength"] / 100;
			local spdperc = ClientVars["Speed"] / 100;
			local endperc = ClientVars["Endurance"] / 100;
			local aimperc = ClientVars["Aim"] / 100;
			local freeperc = FreeStatPoints / MaxFreeStats;
			
			draw.DrawText( "Strength - " .. ClientVars["Strength"], "SmallChatFont", 70, 50, Color( 255, 255, 255, 255 ) );
			draw.RoundedBox( 2, 70, 60, 100, 20, Color( 0, 0, 0, 255 ) );
			draw.RoundedBox( 2, 71, 61, 98 * strperc, 18, Color( 0, 128, 0, 255 ) );
			draw.RoundedBox( 2, 71, 64, 98 * strperc, 6, Color( 255, 255, 255, 20 ) );
			draw.DrawText( "Speed - " .. ClientVars["Speed"], "SmallChatFont", 70, 80, Color( 255, 255, 255, 255 ) );
			draw.RoundedBox( 2, 70, 90, 100, 20, Color( 0, 0, 0, 255 ) );
			draw.RoundedBox( 2, 71, 91, 98 * spdperc, 18, Color( 0, 128, 0, 255 ) );
			draw.RoundedBox( 2, 71, 94, 98 * spdperc, 6, Color( 255, 255, 255, 20 ) );
			draw.DrawText( "Endurance - " .. ClientVars["Endurance"], "SmallChatFont", 70, 110, Color( 255, 255, 255, 255 ) );
			draw.RoundedBox( 2, 70, 120, 100, 20, Color( 0, 0, 0, 255 ) );
			draw.RoundedBox( 2, 71, 121, 98 * endperc, 18, Color( 0, 128, 0, 255 ) );
			draw.RoundedBox( 2, 71, 124, 98 * endperc, 6, Color( 255, 255, 255, 20 ) );
			draw.DrawText( "Aim - " .. ClientVars["Aim"], "SmallChatFont", 70, 140, Color( 255, 255, 255, 255 ) );
			draw.RoundedBox( 2, 70, 150, 100, 20, Color( 0, 0, 0, 255 ) );
			draw.RoundedBox( 2, 71, 151, 98 * aimperc, 18, Color( 0, 128, 0, 255 ) );
			draw.RoundedBox( 2, 71, 154, 98 * aimperc, 6, Color( 255, 255, 255, 20 ) );
			draw.DrawText( "Free Stat Points - " .. FreeStatPoints, "SmallChatFont", 70, 170, Color( 255, 255, 255, 255 ) );
			draw.RoundedBox( 2, 70, 180, 100, 20, Color( 0, 0, 0, 255 ) )
			
			if( freeperc > 0 ) then
		
				draw.RoundedBox( 2, 70, 180, 98 * freeperc, 18, Color( 0, 0, 128, 255 ) );
				draw.RoundedBox( 2, 70, 180, 98 * freeperc, 6, Color( 255, 255, 255, 20 ) );

			end
			
		end
			
		local function Add( stat )
	
			if( FreeStatPoints > 0 ) then
	
				ClientVars[stat] = ClientVars[stat] + 1;
				FreeStatPoints = FreeStatPoints - 1;
			
			end
	
		end
	
		local function Subtract( stat )
	
			if( ClientVars[stat] > 10 ) then
	
				ClientVars[stat] = ClientVars[stat] - 1;
				FreeStatPoints = FreeStatPoints + 1;
			
			end		
	
		end
		
		StatsPanel:AddButton( "-", 50, 57, function() Subtract( "Strength" ) end );
		StatsPanel:AddButton( "-", 50, 87, function() Subtract( "Speed" ) end );
		StatsPanel:AddButton( "-", 50, 117, function() Subtract( "Endurance" ) end );
		StatsPanel:AddButton( "-", 50, 147, function() Subtract( "Aim" ) end );
		
		StatsPanel:AddButton( "+", 175, 57, function() Add( "Strength" ) end );
		StatsPanel:AddButton( "+", 175, 87, function() Add( "Speed" ) end );
		StatsPanel:AddButton( "+", 175, 117, function() Add( "Endurance" ) end );
		StatsPanel:AddButton( "+", 175, 147, function() Add( "Aim" ) end );
		
		StatsPanel:AddButton( "Set Stats", 70, 214, function() 
		
		RunConsoleCommand( "eng_ccsetstats", ClientVars["Strength"], ClientVars["Endurance"], ClientVars["Speed"], ClientVars["Aim"] ) 
		
		StatsPanel:Remove()
		StatsPanel = nil
		
		end )

	end	
	
	CharacterMenu.Link = CharacterMenu:AddLink( "Character Name: ", "LargeAkbar", 10, 10, Color( 255, 255, 255, 255 ), function() NamePanel() end )
	CharacterMenu.Link.HighlightColor = Color( 170, 149, 87, 255 )
	
	CharacterMenu.AgeLink = CharacterMenu:AddLink( "Character Age: ", "LargeAkbar", 10, 50, Color( 255, 255, 255, 255 ), function() AgePanel() end )
	CharacterMenu.AgeLink.HighlightColor = Color( 170, 149, 87, 255 )
	
	CharacterMenu.StatsLink = CharacterMenu:AddLink( "Character Stats", "LargeAkbar", 10, 90, Color( 255, 255, 255, 255 ), function() StatsWindow() end )
	CharacterMenu.StatsLink.HighlightColor = Color( 170, 149, 87, 255 )
	
	CharacterMenu.ReadyLink = CharacterMenu:AddLink( "Create Character", "GiantChatFont", 10, 420, Color( 170, 149, 87, 255 ), function()

		RunConsoleCommand( "eng_ccsetmodel", ChosenCharacterModel )
		RunConsoleCommand( "eng_createcharacter", "" );
		
	end )

	CharacterMenu.ClearLink = CharacterMenu:AddLink( "Clear Details", "GiantChatFont", 10, 450, Color( 255, 0, 0, 255 ), function()
	
		CharacterMenu:Remove()
		CharacterMenu = nil
		
		LocalPlayer():GetTable().CCAge = ""
		LocalPlayer():GetTable().CCName = ""
		
		CreateCharacterMenu()
		
		FreeStatPoints = 30
		
		LocalPlayer():GetTable().CCStrength = 10
		LocalPlayer():GetTable().CCEndurance = 10
		LocalPlayer():GetTable().CCSpeed = 10
		LocalPlayer():GetTable().CCAim = 10
		
		
	end )	
	
	CharacterMenu.GoBack = CharacterMenu:AddLink( "Go Back", "GiantChatFont", 10, 480, Color( 255, 0, 0, 255 ), function()
	
		CharacterMenu:Remove()
		CharacterMenu = nil
		
		LocalPlayer():GetTable().CCAge = ""
		LocalPlayer():GetTable().CCName = ""
	
		FreeStatPoints = 30
		
		LocalPlayer():GetTable().CCStrength = 10
		LocalPlayer():GetTable().CCEndurance = 10
		LocalPlayer():GetTable().CCSpeed = 10
		LocalPlayer():GetTable().CCAim = 10
		
		CharacterChoosePanel()
		
	end	)
	
end
usermessage.Hook( "CCM", CreateCharacterMenu );

local function addCharacter( msg )

	local name = msg:ReadString();
	local model = msg:ReadString();
	local id = msg:ReadShort();
	local title = msg:ReadString()
	local stitle = msg:ReadString()
	
	table.insert( CharacterPicks, { RPName = name, Model = model, ID = id, RPTitle = title, RPSTitle = stitle } );

end
usermessage.Hook( "addCharacter", addCharacter );

function CharacterChoosePanel()

	ChooseMenu = CreateBPanel( nil, ScrW() / 2 - 400, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 540, 180 );
	ChooseMenu:SetParent( CharacterMenu );
	ChooseMenu:CanClose( false );
	ChooseMenu:CanDrag( false );
	ChooseMenu:CanSeeTitle( false );
	ChooseMenu:EnableScrolling( true );
	ChooseMenu:SetBodyColor( Color( 30, 30, 30, 255 ) );
	
	local function RemoveCM()
	
		if( ChooseMenu ) then
			ChooseMenu:Remove()
			ChooseMenu = nil
		end
		
	end	
	
	LocalPlayer():ConCommand( "eng_getchars " .. UniqueID .. "\n" );
	LocalPlayer():ConCommand( "eng_gcm" )
	
	--ChooseMenu:AddLabel( LocalPlayer():GetNWFloat( "charnum" ) .. "/" .. MAX_CHARACTERS .. " characters available", "NewChatFont", 18, 5 )
	ChooseMenu.CreateLink = ChooseMenu:AddLink( "Create Character", "NewChatFont", 18, 5, Color( 255, 255, 255, 255 ), function()
	ChooseMenu.CreateLink.HighlightColor = Color( 170, 149, 87, 255 )
	
		if( LocalPlayer():GetNWFloat( "charnum" ) >= MAX_CHARACTERS ) then
			CreateOkPanel( "No!", "You have too many characters" )
			return
		end	
	
		LocalPlayer():ConCommand( "eng_charcreate" )		
		RemoveCM()			
		CharacterPicks = nil
		
	end )	
	
	CharacterPicks = { }
	CharacterNameButton = { }
	CharacterModel = { }
	
	local y = 0
	local x = 0
	
	ChooseMenu.PaintHook = function()
	
		for k, v in pairs( CharacterPicks ) do

			CharacterModel[v] = vgui.Create( "DModelPanel", ChooseMenu )
			CharacterModel[v]:SetSize( 128, 128 )
			CharacterModel[v]:SetModel( v.Model )
			CharacterModel[v]:SetPos( x + -5, y + 20 )
			CharacterModel[v]:SetCamPos( Vector( 50, 0, 50 ) )
			CharacterModel[v]:SetLookAt( Vector( 0, 0, 50 ) )
			CharacterModel[v]:SetFOV( 70 )
			
			CharacterModel[v].LayoutEntity = function( self, ent )
			
				ent:SetAngles( Angle( 0, 0, 0) )
			
			end
			
	 		CharacterModel[v]:SetToolTip( v.RPName )
			CharacterModel[v].DoClick = function()
			
				local OptionsMenu = DermaMenu()
			
				OptionsMenu:AddOption( "Choose " .. v.RPName, function () LocalPlayer():ConCommand( "eng_selectchar " .. UniqueID .. " " .. v.ID .. "\n" ) 
			
					ChooseMenu:Remove()
					ChooseMenu = nil
				
				end )
				
				OptionsMenu:AddOption( "Delete " .. v.RPName, function()
					
					DeleteFrame = CreateBPanel( "Delete Character", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 )
			DeleteFrame:AddLabel( "Do you want to delete " .. v.RPName .. "?", "NewChatFont", 8, 8, Color( 255, 255, 255, 255 ) )
			
			DeleteFrame.YesButton = DeleteFrame:AddButton( "Yes", 70, 25 )
			DeleteFrame.YesButton.Action = function()
				RunConsoleCommand( "eng_deletecharacer", UniqueID, v.ID )
				CharacterPicks[k] = nil;
				DeleteFrame:Remove()
				DeleteFrame = nil
				ChooseMenu:Remove()
				ChooseMenu = nil
				LocalPlayer():ConCommand( "eng_mainmenu" )
				LocalPlayer():ConCommand( "eng_charnumber" )
				
			end

			DeleteFrame.NoButton = DeleteFrame:AddButton( "No", 105, 25 )
			DeleteFrame.NoButton.Action = function()
				DeleteFrame:Remove()
				DeleteFrame = nil
			end			
			
			end )
			
			OptionsMenu:Open()
				
			end	
			
			CharacterModel[v].LayoutEntity = function( self )
			    
			    self:RunAnimation();
			    
		 	end
				
			x = x + 130


			CharacterPicks = { }
			
		end

	end	
	
	gui.EnableScreenClicker( true )
		
	
end
usermessage.Hook( "PCM", CharacterChoosePanel )	

function RemoveCharacterMenu()
	
	if( CharacterMenu and CharacterMenu.TitleBar ) then
	
		CharacterMenu.TitleBar:Remove();
		CharacterMenu:Remove();

		

	end
	
end
usermessage.Hook( "RCM", RemoveCharacterMenu );

function HasCharAlready()

	CreateOkPanel( "Sorry", "There is already a character named this" )
	
end
usermessage.Hook( "HasCharacterAlready", HasCharAlready ) 	

function TooManyChar()

	CreateOkPanel( "Sorry", "You have too many characters!" )
	
end
usermessage.Hook( "TMC", TooManyChar )	