--OSInterfaceApplications--

	list = {}

	currentKey = ''

	previousKey = ''

	currentID = 0

	order = {} --as key based tables cant keep an order have a table with the order of the keys

	setCurrentKey = function(key)
		OSInterfaceApplications.previousKey = OSInterfaceApplications.currentKey
		OSInterfaceApplications.currentKey = key
		for i,v in ipairs(OSInterfaceApplications.order) do
			if v == key then
				table.remove(OSInterfaceApplications.order, i)
			end	
		end
		table.insert(OSInterfaceApplications.order,1,key)
	end

	current = function () --a helper function, returns the current application
		return OSInterfaceApplications.list[OSInterfaceApplications.currentKey]
	end

	remove = function(application)
		--prevent finder from being quit
		if application == Finder then
			return
		end
		for i,v in ipairs(OSInterfaceApplications.order) do
			if v == application.title then
				table.remove(OSInterfaceApplications.order, i)
				OSInterfaceApplications.list[application.title] = nil
			end	
		end
		OSInterfaceApplications.switchTo(OSInterfaceApplications.list[OSInterfaceApplications.order[1]])
	end

	switchTo = function(application)

		--remove the previous applications menu, if there was a previous application
		if OSInterfaceApplications.currentID > 0 then
			local previousID = OSInterfaceApplications.currentID
			--remove all menu items with the same tag as the previous application
			for i = 1, #OSInterfaceEntities.list do
				if OSInterfaceEntities.list[i] and OSInterfaceEntities.list[i].tag == previousID then
					OSInterfaceEntities.list[i]:cleanup()
					OSInterfaceEntities.list[i]=nil
				end
			end
			OSServices.compactArray(OSInterfaceEntities.list)
		end

		--switch over to the new application	
		OSInterfaceApplications.setCurrentKey(application.title)
		OSInterfaceApplications.currentID = application.id
		OSInterfaceApplications.list[OSInterfaceApplications.currentKey] = application	

		--draw the current applications menus
		cursorX = 4 --the first pixel after the P menu
		if not OSFirstRunMode then
			local appMenuItems = {
				OSMenuItem:new("About "..application.title, function () application.environment.about(application) end),
				OSMenuSplitter:new(),
				OSMenuItem:new("Preferences...", function () application:quit() end, ","),
				OSMenuSplitter:new(),
				OSMenuItem:new("Close Window", function () OSCurrentWindow:close() end, "W"),
				OSMenuItem:new("Quit "..application.title, function () application:quit() end, "Q")
			}

			if application.title == "Finder" then
				--give a special  menu for finder
				appMenuItems = {
				OSMenuItem:new("About "..application.title, function () application.environment.about(application) end),
				OSMenuSplitter:new(),
				OSMenuItem:new("Preferences...", nil),
				OSMenuSplitter:new(),
				OSMenuItem:new("Close Window", function () OSCurrentWindow:close() end, "W"),
			}
			end
			local appMenu = OSMenu:new(cursorX, 1, application.title, appMenuItems)
			appMenu.tag = application.id

			OSInterfaceEntities.add(appMenu)
		end
		cursorX = cursorX + string.len(application.title) + 2

		for i = 1, #application.menus do
			local menu = application.menus[i]
			menu.x = cursorX
			menu.tag = application.id
			OSInterfaceEntities.add(menu)
			cursorX = cursorX + menu.width
		end
	end