--OSServices--

	updateFrequency = OSSettings['update_frequency']
	dragTimeout = 1
	commandTimeout = 1
	shouldHideAllMenus = false
	
	rescheduleTimer = function()
		OSUpdateTimer = os.startTimer(OSServices.updateFrequency) 
	end
	
	resetSleepTimer = function()
		if OSSettings['sleep_delay'] > 0 then
			OSSleepTimer = os.startTimer(OSSettings['sleep_delay'])
		end
	end
	
	shutdown = function()
		shell.run("System/Library/Computer/Shutdown")
	end
	
	restart = function()
		shell.run("System/Library/Computer/Restart")
	end
	
	switchScreen = function()
		shell.run("System/Library/Computer/SwitchScreen")
		OSServices.rescheduleTimer()
	end
	
	craftos = function()
		shell.run("System/Library/Computer/CraftOS")
	end
	
	sleep2 = function()
		term.setTextColour(colours.white)
		shell.run("System/Library/Computer/Sleep")
	end
	
	compactArray = function (table) --remove any array items set as nil
		local j=0
		for i=1, #table do
			if table[i]~=nil then
				j=j+1
				table[j]=table[i]
			end
		end
		for i=j+1, #table do
			table[i]=nil
		end
		return table
	end
	
	removeTableItem = function(_table, _item)
		for _,item in ipairs(_table) do
			if item == _item then
				table.remove(_table, _)
			end
		end
	end
	
	--copied from http://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value
	copyTable =function(t)
		local u = { }
		for k, v in pairs(t) do
			u[k] = v
		end
		return setmetatable(u, getmetatable(t))
	end
	
	longestStringInArray = function(input)
		local length = 0
		for i = 1, #input do
			--if the string length is larger then the set length reset it
			local titleLength = string.len(input[i].title)
			if titleLength > length then
				length = titleLength
			end
		end
		return length
	end
	
	availableScreenSize = function()
		local w, h = term.getSize()
		return w, h - 4
	end
	
	round = function(num, idp)
		local mult = 10^(idp or 0)
		return math.floor(num * mult + 0.5) / mult
	end
	
	lastID = 0	--the last used id number, this is used in generateID
	generateID = function()
		OSServices.lastID = OSServices.lastID + 1
		return OSServices.lastID
	end

	clickEntity = function(entity, x, y)
		--make sure the entity has an action
		if entity.action then
			--if the entity doen't have the 'enabled' key or enabled is true
			if entity.enabled == nil or entity.enabled then
				local entityRelativeX = x - entity.x
				local entityRelativeY = y - entity.y
				entity:action(entityRelativeX, entityRelativeY)
				entity.isSelected = true
				return true --prevent any other clicks from being registered in the same place
			end
		end
		return false
	end
	
	hideAllMenus = function ()
		for i = 1, #OSInterfaceEntities.list do	 					-- first delete all the list items
			if OSInterfaceEntities.list[i].type == "OSMenuItem" then
				OSInterfaceEntities.list[i]=nil
			end
		end
		OSServices.compactArray(OSInterfaceEntities.list)

		for i = 1, #OSInterfaceEntities.list do	 --then set all menus to hidden     removed -->(the menu that just had it's items removed set to hidden)
			if OSInterfaceEntities.list[i].type == "OSMenu" then
				OSInterfaceEntities.list[i].isHidden = true
				OSInterfaceEntities.list[i].isFirstDraw = true
			end
		end
	end
	
	pointOverlapsRect = function (point, rect)
		return point.x >= rect.x and point.x <=rect.x + rect.width - 1 and point.y >= rect.y and point.y <=rect.y + rect.height - 1
	end

	split = function(str, sep)
		local sep, fields = sep or ":", {}
		local pattern = string.format("([^%s]+)", sep)
		str:gsub(pattern, function(c) fields[#fields+1] = c end)
		return fields
	end