if( CharacterMenu ) then

	if( CharacterMenu.TitleBar ) then

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

end

surface.CreateFont( "Nilland", 32, 500, true, false, "LargeAkbar" );
surface.CreateFont( "Nilland", 20, 500, true, false, "Akbar" );

local ChosenCharacterModel = "";

local FreeStatPoints = 30;
local MaxFreeStats = FreeStatPoints;

function CreateCharacterMenu( msg )

	gui.EnableScreenClicker( true )
	
	CharacterMenu = CreateBPanel( "Character Creation", 0, 0, ScrW(), ScrH() )
	CharacterMenu:SetBodyColor( Color( 0, 0, 0, 0 ) )
	CharacterMenu:CanClose( false )
	CharacterMenu:CanSeeTitle( false )
	
	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.ModelViewPanel = CreateBPanel( nil, 400, 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 ) );
	
	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
	 
	
	
	--[[CharacterMenu.ModelSelectPanel = vgui.Create( "DModelPanel", CharacterMenu )
	CharacterMenu.ModelSelectPanel:SetSize( 500, 500 )
	CharacterMenu.ModelSelectPanel:SetModel( TS.SelectablePlayerModels[1] )
	CharacterMenu.ModelSelectPanel:SetPos( 10, 110 )
	CharacterMenu.ModelSelectPanel:SetFOV( 100 );
	CharacterMenu.ModelSelectPanel:SetLookAt( Vector( 0, 0, 50 ) );
	CharacterMenu.ModelSelectPanel:SetCamPos( Vector( 30, -10, 55 ) );--]]
	
	--[[local n = 1
	
	local function NextMdl()
	
		n = n + 1;

		if( n > #TS.SelectablePlayerModels ) then
			n = 1;
		end
		
		CharacterMenu.ModelSelectPanel:SetModel( TS.SelectablePlayerModels[n] );
		
	end	
	
	local function PrevMdl()
	
		n = n - 1;
		
		if( n == 0 ) then
			n = #TS.SelectablePlayerModels
		end
		
		CharacterMenu.ModelSelectPanel:SetModel( TS.SelectablePlayerModels[n] );
		
	end	
	
	CharacterMenu:AddButton( "Next Model", 20, 490, function() NextMdl() end )
	CharacterMenu:AddButton( "PreviousModel", 20, 520, function() PrevMdl() 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, 20 )
	
		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 )
		
			
		end			
	
	end
	
	--name
	CharacterMenu.Link = CharacterMenu:AddLink( "Character Name: ", "LargeAkbar", 10, 10, Color( 255, 255, 255, 255 ), function() NamePanel() end )
	CharacterMenu.Link.HighlightColor = Color( 38, 172, 0, 255 )
		
	--age
	CharacterMenu.AgeLink = CharacterMenu:AddLink( "Character Age: ", "LargeAkbar", 10, 50, Color( 255, 255, 255, 255 ), function() AgePanel() end )
	CharacterMenu.AgeLink.HighlightColor = Color( 38, 172, 0, 255 )
	
	CharacterMenu:AddLabel( "Character Model:", "LargeAkbar", 10, 90 )
	
	CharacterMenu.ReadyLink = CharacterMenu:AddLink( "Create Character", "GiantChatFont", ScrW() - 400, 540, Color( 38, 172, 0, 255 ), function()
	
		RunConsoleCommand( "eng_createcharacter", "" );
		
	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 ), 190, 250 );
	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 )
	
	CharacterPicks = { }
	CharacterName = { }
	CharacterSpawnIcon = { }
	
	local y = 0
	
	ChooseMenu.PaintHook = function()
	
		for k, v in pairs( CharacterPicks ) do
		
			--[[CharacterSpawnIcon[v] = vgui.Create( "SpawnIcon", ChooseMenu )
			CharacterSpawnIcon[v]:SetSize( 32, 32 )
			CharacterSpawnIcon[v]:SetModel( v.Model )
			CharacterSpawnIcon[v]:SetPos( 5, y + 10 )
			
			CharacterName[v] = ChooseMenu:AddButton( v.RPName, 20, y + 10 )--]]
			
			CharacterName[v] = ChooseMenu:AddLink( v.RPName, "NewChatFont", 35, y + 30 )
			CharacterName[v].HighlightColor = Color( 0, 65, 160, 255 );
			CharacterName[v].Action = function()
			
				LocalPlayer():ConCommand( "eng_selectchar " .. UniqueID .. " " .. v.ID .. "\n" ) 
				RemoveCM()
				
			end	
				
			
			y = y + 15
			
			
			CharacterPicks = { }
			
		end

	end	
	
	ChooseMenu.CharCreateBut = ChooseMenu:AddButton( "Create Character", 30, 230 )
	ChooseMenu.CharCreateBut.Action = function()
	
		if( LocalPlayer():GetNWFloat( "charnum" ) >= MAX_CHARACTERS ) then
			CreateOkPanel( "No!", "You have too many characters" )
			return
		end	
	
		LocalPlayer():ConCommand( "eng_charcreate" )		
		RemoveCM()			
		CharacterPicks = nil
		
	end	
	
	gui.EnableScreenClicker( true )
		
	
end
usermessage.Hook( "PCM", CharacterChoosePanel )	

--I need some better ideas <_<
--[[function CharacterChoosePanel()
	
	LocalPlayer():ConCommand( "eng_charnumber" )
	LocalPlayer():ConCommand( "eng_getchars " .. UniqueID .. "\n" );
	
	local function RemoveCM()
	
		if( ChooseMenu ) then
			ChooseMenu:Remove()
			ChooseMenu = nil
		end
		
	end	
	
	CharacterPicks = { }
	CharacterModel = { }
	CharacterName = { }
	CharacterTitle = { }
	CharacterTitle2 = { }
	CharacterPickButton = { }
	CharacterDelButton = { }
	
	ChooseMenu = CreateBPanel( "You have  " .. LocalPlayer():GetNWFloat( "charnum" ) .. "/" .. MAX_CHARACTERS .. " chars", ScrW() / 2 - 300, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 282, 440 )
	ChooseMenu:EnableScrolling( true )
	--ChooseMenu:SetBodyColor( Color( 30, 30, 30, 240 ) )
	ChooseMenu:SetBodyColor( Color( 50, 50, 50, 100 ) )
	ChooseMenu:CanSeeTitle( false )
	ChooseMenu:CanClose( false )
	ChooseMenu:CanDrag( true )
	
	ChooseMenu.CreateBut = ChooseMenu:AddButton( "Create new character", 25, 4 )
	ChooseMenu.CreateBut.Action = function()
	
		if( LocalPlayer():GetNWFloat( "charnum" ) >= MAX_CHARACTERS ) then
			CreateOkPanel( "No!", "You have too many characters" )
			return
		end	
		
		LocalPlayer():ConCommand( "eng_charcreate" )
		
		RemoveCM()	
		
		CharacterPicks = nil
		
	end
	
	ChooseMenu.GoBack = ChooseMenu:AddButton( "Go Back", 180, 4 );
	ChooseMenu.GoBack.Action = function()
	
		RunConsoleCommand( "eng_mainmenu" )
		RemoveCM()
	end	
	
	y = 0
	
	ChooseMenu.PaintHook = function()
	
		for k, v in pairs( CharacterPicks ) do
		
		CharacterModel[v] = vgui.Create( "SpawnIcon", ChooseMenu )
		CharacterModel[v]:SetModel( v.Model )
		CharacterModel[v]:SetSize( 16, 16 )
		CharacterModel[v]:SetPos( 5, y + 26 )
		
		CharacterName[v] = ChooseMenu:AddLabel( v.RPName, "ChatFontSub", 70, y + 26, Color( 255, 255, 255, 255 ) )
		CharacterTitle[v] = ChooseMenu:AddLabel( v.RPTitle, "ChatFontSub", 70, y + 40, Color( 255, 255, 255, 255 ) )
		CharacterTitle2[v] = ChooseMenu:AddLabel( v.RPSTitle, "ChatFontSub", 70, y + 54, Color( 255, 255, 255, 255 ) )
		
		CharacterPickButton[v] = vgui.Create( "DImageButton", ChooseMenu )
		CharacterPickButton[v]:SetImage( "gui/silkicons/check_on.vtf" )
		CharacterPickButton[v]:SetPos( 70, y + 68 )
		CharacterPickButton[v]:SetSize( 16, 16 )
		CharacterPickButton[v].DoClick = function()
		
			LocalPlayer():ConCommand( "eng_selectchar " .. UniqueID .. " " .. v.ID .. "\n" ) 
			RemoveCM()
		
		end
			
		CharacterDelButton[v] = vgui.Create( "DImageButton", ChooseMenu )
		CharacterDelButton[v]:SetImage( "gui/silkicons/check_off.vtf" )
		CharacterDelButton[v]:SetPos( 90, y + 68 )
		CharacterDelButton[v]:SetSize( 16, 16 )
		CharacterDelButton[v].DoClick = 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	
			
		CharacterPicks = { }
		
		ChooseMenu:AddObject( CharacterModel[v] )
		
		y = y + 68
		
		ChooseMenu:CalculateScrollBar()
		
		
	end	
		
	end
	
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 )	