ItemData = {  };

function LoadItem( schema, filename )

	local path = "schemas/" .. schema .. "/items/" .. filename;
	
	ITEM = {  };
	
	include( path );
	
	ItemData[ ITEM.Class ] = ITEM;
	
end

function CreateItem( class, pos, ang )

	if( ItemData[ class ] == nil ) then return; end
	
	local itemtable = ItemData[ class ];
	
	local item = ents.Create( "item_prop" );
	
	item:SetModel( itemtable.Model );
	item:SetAngles( ang );
	item:SetPos( pos );
	
	for k, v in pairs( itemtable ) do
		item[ k ] = v;
		if( type( v ) == "string" ) then
			item:SetNWString( k, v );
		end
	end
	
	item:Spawn( );
	item:Activate( );
	
end

function ccCreateItem( ply, cmd, args )

	if( ply:IsAdmin( ) or ply:IsSuperAdmin( ) ) then
	
		-- Drop the item 80 units infront of him.
		CreateItem( args[ 1 ], ply:CalcDrop( ), Angle( 0,0,0 ) );
		
	end
	
end
concommand.Add( "rp_createitem", ccCreateItem );

function ccDropItem( ply, cmd, args )

	local inv = GetCharField( ply, "inventory" );
	for k, v in pairs( inv ) do
		if( v == args[ 1 ] ) then
			CreateItem( args[ 1 ], ply:CalcDrop( ), Angle( 0,0,0 ) );
			ply:TakeItem( args[ 1 ] );
			return;
		end
	end
	
end
concommand.Add( "rp_dropitem", ccDropItem );

function ccBuyItem( ply, cmd, args )
	
	if( ItemData[ args[ 1 ] ] != nil ) then
		if( Teams[ ply:Team( ) ][ "business" ] ) then
		
			if(table.HasValue(Teams[ ply:Team( ) ][ "item_groups" ], ItemData[ args[ 1 ] ].ItemGroup)) then
		
				if( ItemData[ args[ 1 ] ].Purchaseable and tonumber(GetCharField(ply, "money" )) >= ItemData[ args[ 1 ] ].Price ) then
				
					ChangeMoney( ply, 0 - ItemData[ args[ 1 ] ].Price );
					CreateItem( args[ 1 ], ply:CalcDrop( ), Angle( 0,0,0 ) );
			
				else
					
					SendChat( ply, "You do not have enough money to purchase this item!" );
					
				end
				
			else
			
				SendChat( ply, "You cannot purchase this item!" );
				
			end
			
		else
		
			SendChat( ply, "You do not have access to Business!" );
			
		end
	end
	
end
concommand.Add( "rp_buyitem", ccBuyItem );