PlayerMenuVisible = false;
RadioMenuVisible = false;

PlayerMenuIntro = 0;

function event.PlayerMenuOff()

	if( PlayerMenuVisible ) then
	
		TogglePlayerMenu();
	
	end

end

function TogglePlayerMenu()

	PlayerMenuVisible = !PlayerMenuVisible;
	
	StorageItemID = nil;
	
	if( MEPropertiesWindow and MEPropertiesWindow:IsValid() ) then
	
		MEPropertiesWindow:SetVisible( PlayerMenuVisible );
		MEPropertiesWindow.Menu:SetVisible( PlayerMenuVisible );
		
		if( MEPropertiesWindow.TitleBar and MEPropertiesWindow.TitleBar:IsValid() ) then	
			MEPropertiesWindow.TitleBar:SetVisible( PlayerMenuVisible );
			MEPropertiesWindow.Menu.TitleBar:SetVisible( PlayerMenuVisible );
		end
		
	end

	for k, v in pairs( PlayerMenuLinks ) do

		v:SetVisible( PlayerMenuVisible );
		
		local x, y = v:GetPos();
		
		v:SetPos( -1000, y );

	end
	
	if( PlayerInfoPanel and PlayerInfoPanel:IsValid() ) then
	
		if( PlayerInfoPanel.TitleBar and PlayerInfoPanel.TitleBar:IsValid() ) then
			PlayerInfoPanel.TitleBar:SetVisible( PlayerMenuVisible );
		end
		
		PlayerInfoPanel:SetVisible( PlayerMenuVisible );
	
	end
	
	
	if( PlayerMenuVisible ) then
	
		gui.EnableScreenClicker( true );
		
		--PlayerMenuIDCard:SetVisible( true );
		
		InventoryMenu:SetVisible( true );
		InventoryMenu.TitleBar:SetVisible( true );
		
		PlayerMenuIntro = 0;
		
		RemoveItemButtons();
		
	else
	
		if( StorageMenu ) then
			StorageMenu:Remove();
			StorageMenu = nil;
		end
	
		--PlayerMenuIDCard:SetVisible( false );
		
		InventoryMenu:SetVisible( false );
		InventoryMenu.TitleBar:SetVisible( false );
		
		HideMouse();
	
	
	end
	
end
usermessage.Hook( "TPM", TogglePlayerMenu );

function DrawPlayerMenu()

	for k, v in pairs( PlayerMenuLinks ) do
	
		local x, y = v:GetPos();
	
		v:SetPos( ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ), y );
	
	end

	local sw = 125;
	
	HealthAlpha = 255;
	SprintAlpha = 255;
	
	local plur = "s";
	
	if( ClientVars["Rubles"] == 1 ) then
		plur = "";
	end
	
	draw.DrawText( "You have $" .. ClientVars["Rubles"] .. " USD" .. plur .. "\n", "NewChatFont", ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ) + 2, ScrH() - sw - 10, Color( 255, 255, 255, 255 ) );

	for n = 1, #TS.PlayerStats do
	
		local perc = ClientVars[TS.PlayerStats[n]] / 100;
	
		draw.RoundedBox( 2, ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ), ScrH() - sw + ( 20 * n ), ( ScrW() * .15 - 2 ), 14, Color( 0, 0, 0, 255 ) );
		local w = ( ScrW() * .15 - 2 );
		
		if( perc > 0 ) then
			draw.RoundedBox( 2, ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ) + 2, ScrH() - sw + ( 20 * n ) + 2, ( ScrW() * .15 - 6 ) * perc, 10, Color( 0, 128, 0, 255 ) );
			draw.RoundedBox( 2, ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ) + 2, ScrH() - sw + ( 20 * n ) + 2, ( ScrW() * .15 - 6 ) * perc, 4, Color( 255, 255, 255, 30 ) );
		end
		
		draw.DrawText( TS.PlayerStats[n] .. " - " .. ClientVars[TS.PlayerStats[n]], "SmallChatFont", ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ) + 2, ScrH() - sw + ( 20 * n ) + 2, Color( 255, 255, 255, 255 ) );
		
	end
	
	

end

function PlayerMenuThink()

	if( PlayerMenuIntro < 100 ) then
		
		PlayerMenuIntro = math.Clamp( PlayerMenuIntro + 500 * FrameTime(), 0, 100 );
	
	end

end

if( PlayerInfoPanel ) then
	PlayerInfoPanel:Remove();
end
PlayerInfoPanel = nil;

function CreatePlayerInfo()

	PlayerInfoPanel = CreateBPanel( "Player Information", ScrW() / 2 - 200, ScrH() / 2 - 200, 430, 400 );
	
	PlayerInfoPanel.PaintHook = function()
	
		local h, m, s = GetHMS();
		local str;
		
		if( h == 1 ) then
			str = h .. " hour";
		else
			str = h .. " hours";
		end
		
		if( m == 1 ) then
			str = str .. ", " .. m .. " minute";
		else
			str = str .. ", " .. m .. " minutes";
		end
	
		if( s == 1 ) then
			str = str .. ", " .. s .. " second";
		else
			str = str .. ", " .. s .. " seconds";
		end
	
		draw.DrawText( "This session, you've played for " .. str, "NewChatFont", 7, 10, Color( 255, 255, 255, 255 ) );
		draw.DrawText( "You are currently on Character: " .. LocalPlayer():GetRPName(), "NewChatFont", 7, 25, Color( 255, 255, 255, 255 ) );
	
	end

end

if( PlayerMenuLinks ) then
	for k, v in pairs( PlayerMenuLinks ) do
		v:Remove();
	end
end
PlayerMenuLinks = { }

function CreatePlayerMenu()
	
	local function AddLink( name, y, func )
	
		local link = vgui.Create( "BLink" );
		link:SetPos( ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 8 ) * ( PlayerMenuIntro / 100 ) ), y );
		link:SetFont( "NewChatFont" );
		link:SetText( name );
		link:SizeToContents();
		link.HighlightColor = Color( 0, 65, 160, 255 );
		link.Action = func;
		
		table.insert( PlayerMenuLinks, link );
		
	end
	
	AddLink( "Trader Menu", ScrH() - 230, function() TogglePlayerMenu(); RunConsoleCommand( "eng_tradermenu", "" ); end );
	AddLink( "Character Creation", ScrH() - 210, function() TogglePlayerMenu(); RunConsoleCommand( "eng_loadcharacters" ) end );
	AddLink( "Set Radio Frequency", ScrH() - 190, function () RunConsoleCommand( "eng_promptradiomenu" ) end )


end

CreatePlayerMenu();

if( AccountMenu ) then

	if( AccountMenu.TitleBar ) then

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

end

function CreateAddDoorOwnerMenu( msg )
	
	AddDoorOwnerMenu = CreateBPanel( "Give out a spare set of keys.", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 );
	AddDoorOwnerMenu:CanClose( true );
	AddDoorOwnerMenu:CanDrag( false );
	AddDoorOwnerMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	AddDoorOwnerMenu:AddLabel( "Recipitant:", "NewChatFont", 13, 8, Color( 255, 255, 255, 255 ) );
	
	local x, y = AddDoorOwnerMenu:GetPos();


	AddDoorOwnerMenu.NameEntry = vgui.Create( "DTextEntry", AddDoorOwnerMenu );
	AddDoorOwnerMenu.NameEntry:SetPos( x + 80, y + 8 );
	AddDoorOwnerMenu.NameEntry:SetSize( 100, 15 );
	AddDoorOwnerMenu.NameEntry:MakePopup();
	
	ApplyMaxCharacters( AddDoorOwnerMenu.NameEntry, 20 );
	
	AddDoorOwnerMenu.SetOwner = AddDoorOwnerMenu:AddButton( "Give Keys", 60, 25);
	
	AddDoorOwnerMenu.SetOwner.Action = function()
		
		RunConsoleCommand( "eng_ado", AddDoorOwnerMenu.NameEntry:GetValue() );
	
	end
	
	gui.EnableScreenClicker( true );

end
usermessage.Hook( "PADO", CreateAddDoorOwnerMenu );

function RemoveAddDoorOwnerMenu( msg )
	
	if( AddDoorOwnerMenu and AddDoorOwnerMenu.TitleBar ) then
	
		AddDoorOwnerMenu.TitleBar:Remove();
		AddDoorOwnerMenu:Remove();

	end

end
usermessage.Hook( "RADO", RemoveAddDoorOwnerMenu );

function CreateRadioMenu( msg )
	
	RadioMenu = CreateBPanel( "Set your radio frequency", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 );
	RadioMenu:CanClose( true );
	RadioMenu:CanDrag( false );
	RadioMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	RadioMenu:AddLabel( "Frequency:", "NewChatFont", 13, 8, Color( 255, 255, 255, 255 ) );
	
	local x, y = RadioMenu:GetPos();


	RadioMenu.FreqEntry = vgui.Create( "DTextEntry", RadioMenu );
	RadioMenu.FreqEntry:SetPos( x + 80, y + 8 );
	RadioMenu.FreqEntry:SetSize( 100, 15 );
	RadioMenu.FreqEntry:MakePopup();
	
	ApplyMaxCharacters( RadioMenu.FreqEntry, 20 );
	
	RadioMenu.SetFreq = RadioMenu:AddButton( "Set Frequency", 60, 25);
	
	RadioMenu.SetFreq.Action = function()
	
		if( not tonumber( RadioMenu.FreqEntry:GetValue() ) ) then
		
			CreateOkPanel( "Invalid Frequency", "Invalid Number." );
			return;
		
		end
	
		if( tonumber( RadioMenu.FreqEntry:GetValue() ) < 0 or
			tonumber( RadioMenu.FreqEntry:GetValue() ) > 999.9 ) then
		
			CreateOkPanel( "Invalid Frequency", "Frenquency must be between 0 and 999.9" );
			return;		
		
		end
		
		RunConsoleCommand( "eng_changefrequency", RadioMenu.FreqEntry:GetValue() );
	
	end
	
	gui.EnableScreenClicker( true );

end
usermessage.Hook( "PRM", CreateRadioMenu );

function RemoveRadioMenu( msg )
	
	if( RadioMenu and RadioMenu.TitleBar ) then
	
		RadioMenu.TitleBar:Remove();
		RadioMenu:Remove();

	end

end
usermessage.Hook( "RRM", RemoveRadioMenu );

function NoRadio( msg )
	
CreateOkPanel( "Error", "You need a radio to do this." );
gui.EnableScreenClicker( true );

end
usermessage.Hook( "NR", NoRadio );


function LoanMenu()

	LoanMenu = CreateBPanel( "Pay/Take a loan", ( ScrW() * .5 ) - 125, ( ScrH() * .5 ) - 40, 250, 80 );
	LoanMenu:CanClose( true );
	LoanMenu:CanDrag( false );
	LoanMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	LoanMenu:AddLabel( "Take Loan:", "NewChatFont", 5, 8, Color( 255, 255, 255, 255 ) );
	LoanMenu:AddLabel( "Pay Loan:", "NewChatFont", 5, 25, Color( 255, 255, 255, 255 ) );
	
	local x, y = LoanMenu:GetPos();

	LoanMenu.LoanTakeEntry = vgui.Create( "DTextEntry", LoanMenu );
	LoanMenu.LoanTakeEntry:SetPos( x + 80, y + 8 );
	LoanMenu.LoanTakeEntry:SetSize( 163, 15 );
	LoanMenu.LoanTakeEntry:MakePopup();
	
	LoanMenu.PayLoanEntry = vgui.Create( "DTextEntry", LoanMenu );
	LoanMenu.PayLoanEntry:SetPos( x + 103, y + 25 );
	LoanMenu.PayLoanEntry:SetSize( 140, 15 );
	LoanMenu.PayLoanEntry:MakePopup();
	
	LoanMenu.TakeLoanButton = LoanMenu:AddButton( "Take Loan", 5, 50 );
	LoanMenu.PayLoan = LoanMenu:AddButton( "Pay Loan", 75, 50 );
	LoanMenu.GetLoan = LoanMenu:AddButton( "Loan Amount", 140, 50 );
	
	LoanMenu.TakeLoanButton.Action = function()
	
		RunConsoleCommand( "eng_takeloan", LoanMenu.LoanTakeEntry:GetValue() )
		
	end

	LoanMenu.PayLoan.Action = function()

		RunConsoleCommand( "eng_payloan", LoanMenu.PayLoanEntry:GetValue() )
		
	end	
	
	LoanMenu.GetLoan.Action = function()
	
		RunConsoleCommand( "eng_loanamount" )
		
	end	
		
	
end	
usermessage.Hook( "OLM", LoanMenu ) 

function CreateNameMenu( msg )

	local propertyname = msg:ReadString();
	local doorname = msg:ReadString();
	
	NameMenu = CreateBPanel( "Set this door/property name", ( ScrW() * .5 ) - 125, ( ScrH() * .5 ) - 40, 250, 80 );
	NameMenu:CanClose( true );
	NameMenu:CanDrag( false );
	NameMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	NameMenu:AddLabel( "Door Name:", "NewChatFont", 5, 8, Color( 255, 255, 255, 255 ) );
	NameMenu:AddLabel( "Property Name:", "NewChatFont", 5, 25, Color( 255, 255, 255, 255 ) );
	
	local x, y = NameMenu:GetPos();

	NameMenu.DoorNameEntry = vgui.Create( "DTextEntry", NameMenu );
	NameMenu.DoorNameEntry:SetPos( x + 80, y + 8 );
	NameMenu.DoorNameEntry:SetSize( 163, 15 );
	NameMenu.DoorNameEntry:SetText( doorname );
	NameMenu.DoorNameEntry:MakePopup();
	
	ApplyMaxCharacters( NameMenu.DoorNameEntry, 20 );
	
	NameMenu.PropNameEntry = vgui.Create( "DTextEntry", NameMenu );
	NameMenu.PropNameEntry:SetPos( x + 103, y + 25 );
	NameMenu.PropNameEntry:SetSize( 140, 15 );
	NameMenu.PropNameEntry:SetText( propertyname );
	NameMenu.PropNameEntry:MakePopup();
	
	
	ApplyMaxCharacters( NameMenu.PropNameEntry, 20 );
	
	NameMenu.SetNames = NameMenu:AddButton( "Name Property", 75, 50);
	
	NameMenu.SetNames.Action = function()
	
		if( string.gsub( NameMenu.DoorNameEntry:GetValue(), " ", "" ) == "" ) then
		
			CreateOkPanel( "Invalid door name.", "Door name cannot be blank" );
			return;		
		
		end
		
		--[[if( string.gsub( NameMenu.PropNameEntry:GetValue(), " ", "" ) == "" ) then
		
			CreateOkPanel( "Invalid property name.", "Property name is blank, queer." );
			return;		
		
		end]]--
		
		RunConsoleCommand( "eng_setdoorname", NameMenu.DoorNameEntry:GetValue() );
		RunConsoleCommand( "eng_setpropertyname", NameMenu.PropNameEntry:GetValue() );
	
	end
	
	gui.EnableScreenClicker( true );

end
usermessage.Hook( "PNM", CreateNameMenu );

function RemoveNameMenu( msg )
	
	if( NameMenu and NameMenu.TitleBar ) then
	
		NameMenu.TitleBar:Remove();
		NameMenu:Remove();

	end

end
usermessage.Hook( "RNM", RemoveNameMenu );


