local bedrockPath='/' if OneOS then OneOS.LoadAPI('/System/API/Bedrock.lua', false)elseif fs.exists(bedrockPath..'/Bedrock')then os.loadAPI(bedrockPath..'/Bedrock')else if http then print('Downloading Bedrock...')local h=http.get('http://pastebin.com/raw.php?i=0MgKNqpN')if h then local f=fs.open(bedrockPath..'/Bedrock','w')f.write(h.readAll())f.close()h.close()os.loadAPI(bedrockPath..'/Bedrock')else error('Failed to download Bedrock. Is your internet working?') end else error('This program needs to download Bedrock to work. Please enable HTTP.') end end if Bedrock then Bedrock.BasePath = bedrockPath Bedrock.ProgramPath = shell.getRunningProgram() end


local program = Bedrock:Initialise()

program.AppStoreURL = 'http://appstore.olivercooper.me/'

program:RegisterKeyboardShortcut({'\\'}, function()os.reboot()end)

program.OnKeyChar = function(self, event, keychar)
	if (keychar == keys.up or keychar == keys.down) then --and not program:GetActiveObject() then
		local currentPage = self:GetObject('CurrentPage')
		if currentPage then
			currentPage:OnScroll('mouse_scroll', (keychar == keys.up) and -1 or 1, 1, 1)
		end
	end
end

program.OpenPage = function(self, pageType, values)
	program:SetActiveObject()

	local currentPage = self:GetObject('CurrentPage')
	local previousPage = self:GetObject('PreviousPage')

	if currentPage then
		currentPage.Name = 'PreviousPage'
		currentPage.Visible = false
	end

	if previousPage then
		self:RemoveObject(previousPage)
	end

	if not values then
		values = {}
	end
	values.Type = pageType
	values.Name = 'CurrentPage'
	values.X = 1
	values.Y = 1
	values.Width = '100%'
	values.Height = '100%'
	self:GetObject('ContentScrollView'):AddObject(values)
end

program.OpenPreviousPage = function(self)
	program:SetActiveObject()
	
	local currentPage = self:GetObject('CurrentPage')
	local previousPage = self:GetObject('PreviousPage')
	if previousPage then
		previousPage.Name = 'CurrentPage'
		previousPage.Visible = true
		
		if currentPage then
			self:RemoveObject(currentPage)
		end
	else
		self:OpenPage('HomePageView')
	end
end

program:Run(function()
	program:RegisterEvent('http_success', function(self, event, url, h)
		local currentPage = self:GetObject('CurrentPage')
		if currentPage and not currentPage.Failed and not currentPage.Success and currentPage.RequestURL == url then
			currentPage.Success = true
			currentPage:OnDataLoad(url, h.readAll())
			h.close()
		end
	end)

	program:RegisterEvent('http_failure', function(self, event, url)
		local currentPage = self:GetObject('CurrentPage')
		if currentPage and not currentPage.Failed and not currentPage.Success and currentPage.RequestURL == url then
			currentPage.Failed = true
			currentPage:OnDataFailed(url, 'Unknown error')
		end
	end)

	program:GetObject('HomeLabel').OnClick = function(self)
		program:OpenPage('HomePageView')
	end

	program:GetObject('CategoriesLabel').OnClick = function(self)
		program:OpenPage('CategoriesPageView')
	end

	program:GetObject('SearchLabel').OnClick = function(self)
		program:OpenPage('SearchPageView')
	end

	if OneOS then
		program:GetObject('QuitLabel').Visible = false
	else
		program:GetObject('QuitLabel').OnClick = function()
			term.setCursorPos(1, 1)
			term.setBackgroundColour(colors.black)
			term.setTextColor(colours.white)
			term.clear()
			print('Thanks for using the App Store!')
			print('(c) 2013 - 2015 oeed')
			program:Quit()
		end
	end

end)