

function CanItemFitInBlackmarket( id )

	local w = TS.ItemsData[id].Width;
	local h = TS.ItemsData[id].Height;

	local x, y = FindFreeSpaceInBlackmarket( w, h );
	
	if( x ) then return true; end
	
	
	return false;

end

function GiveBlackmarketItem( id )

	local invtbl;

	if( type( id ) == "table" ) then

		invtbl = { }
	
		for k, v in pairs( id ) do
		
			invtbl[k] = v;
		
		end
		
		id = invtbl.ID;
	
	end
	
	local delay = 0;
	
	for k, v in pairs( player.GetAll() ) do
		
		if( v and v:IsValid() and v:IsPlayer() and v:GetTable().BlackmarketMenu ) then
				
			if( not v:GetTable().ItemsDownloaded[id] ) then 
				v:SendItemData( id );
				delay = 1;
			end
				
		end
			
	end

	local w = TS.ItemsData[id].Width;
	local h = TS.ItemsData[id].Height;

	local x, y = FindFreeSpaceInBlackmarket( w, h );

	if( x ) then

		InsertIntoBlackmarket( x, y, w, h );
		
		if( invtbl ) then
			BlackmarketGrid[x][y].ItemData = invtbl;
		else
			BlackmarketGrid[x][y].ItemData = TS.ItemsData[id];
		end

		local isi = function()
		
		for k, v in pairs( player.GetAll() ) do
		
		if( v and v:IsValid() and v:IsPlayer() and v:GetTable().BlackmarketMenu ) then

			umsg.Start( "IBMI", v );
				umsg.String( id );
				umsg.Short( x );
				umsg.Short( y );
			umsg.End();
			end
		end

		end
		timer.Simple( delay, isi );
		
		return true;
		
	end
	

	
	return false;

end

function InsertIntoBlackmarket( x, y, w, h )

	for k = 0, h - 1 do

		for j = 0, w - 1 do
		
			if( BlackmarketGrid[x + j] and BlackmarketGrid[x + j][y + k] ) then
				BlackmarketGrid[x + j][y + k].Filled = true;
				BlackmarketGrid[x + j][y + k].SX = x;
				BlackmarketGrid[x + j][y + k].SY = y;

			end
			
		end
	
	end

end

function CanFitInBlackmarket( x, y, w, h )

	local iw = 14
	local ih = 8

	for k = 0, h - 1 do
	
		for j = 0, w - 1 do
		
			if( x + j >= iw or
				y + k >= ih ) then
				
				return false;
				
			end
		
			if( not BlackmarketGrid[x + j] or
				not BlackmarketGrid[x + j][y + k] ) then

				return false;
				
			end
		
			if( BlackmarketGrid[x + j][y + k].Filled ) then

				return false;
			
			end
		
		end
	
	end

	return true;
end

function FindFreeSpaceInBlackmarket( w, h )

	local iw = 14
	local ih = 8
	
	for y = 0, ih - 1 do
	
		for x = 0, iw - 1 do

			if( BlackmarketGrid[x] and BlackmarketGrid[x][y] and ( not BlackmarketGrid[x][y].Filled and CanFitInBlackmarket( x, y, w, h ) ) ) then
				return x, y;
			
			end
		
		end
		
	end

	return false;

end

function RemoveBlackmarketItemAt( x, y )

	for k, v in pairs( BlackmarketGrid ) do
	
		for n, m in pairs( v ) do

			if( m.SX == x and m.SY == y ) then
		
				BlackmarketGrid[k][n].Filled = false;
				BlackmarketGrid[k][n].SX = -1;
				BlackmarketGrid[k][n].SY = -1;
				BlackmarketGrid[k][n].ItemData = nil;
				
			end
		
		end
	
	end
	
		for k, v in pairs( player.GetAll() ) do
		
			if( v and v:IsValid() and v:IsPlayer() and v:GetTable().BlackmarketMenu ) then
				umsg.Start( "RBMI", v );
					umsg.Short( x );
					umsg.Short( y );
				umsg.End();
			end
			
		end


end

function HasBlackmarketItem( id )

	for i, v in pairs( BlackmarketGrid ) do
	
		for x, c in pairs( v ) do
		
			for y, u in pairs( c ) do
				
				if( BlackmarketGrid[x][y].ItemData ) then
				
					if( BlackmarketGrid[x][y].ItemData.ID == id ) then
						
						return true, x, y;
						
					end
			
				end
			
			end
		
		end
	
	end
	
	return false;

end
