ProgressBars = {};

--[[
Read in this order:
String: Title
Short: Time -- really an int
Short: X pos -- if -1 placed in middle of screen 
Short: Y pos -- if -1 set placed in middle of screen
Short: Color R -- valid 0 - 255
Short: Color G -- valid 0 - 255
Short: Color B -- valid 0 - 255
String: ConCommand
]]--
function MakeProgressBar(data)

	local pb = { }
	
	pb.Title = data:ReadString();
	pb.Time = data:ReadShort();
	pb.x = data:ReadShort();
	pb.y = data:ReadShort();
	pb.r = data:ReadShort();
	pb.g = data:ReadShort();
	pb.b = data:ReadShort();
	pb.cc = data:ReadString();
	
	if( pb.x == -1 ) then
		pb.x = ScrW() / 2;
	end
	
	if( pb.y == -1 ) then
		pb.y = ScrH() / 2;	
	end
	
	if( pb.r == -1 or pb.r > 255) then
		pb.r = 60;
	end
	
	if( pb.g == -1 or pb.g > 255) then
		pb.g = 120;
	end
	
	if( pb.b == -1 or pb.b > 255) then
		pb.b = 240;
	end
	
	pb.Start = CurTime();
	pb.End = CurTime() + pb.time;
	pb.Process = 0;
	
	table.insert( ProcessBars, pb );

end
usermessage.Hook("CreateProgressBar", MakeProgressBar);

function KillProgressBar( data )

	title = data:ReadString();
	
	for k, v in pairs( ProgressBars ) do
	
		if( v.Title == title ) then
			ProgressBars[k] = nil;
		end
	
	end

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

function ProgressThink()
	if( ValidEntity( LocalPlayer() ) ) then
		for k, v in pairs( ProgressBars ) do
	
			v.Process = math.Clamp( ( ( CurTime() - v.Start ) / v.Time ), 0, 1 );
		
			if( v.Process >= 1 ) then
				LocalPlayer():ConCommand( v.endcmd );
				ProgressBars[k] = nil;
			else
		
				-- background bar
				draw.RoundedBox( 0, v.x, v.y, 210, 50, Color(0,0,0,255) );
				-- progress of the bar
				draw.RoundedBox( 0, v.x, v.y, 200 * v.Process , 40, Color(v.r, v.g, v.b) );
			end
		end
	end
end
hook.Add( "Think", "ProcessThink", ProgressThink );