-- Navigate SubPages (using NavigateButtons)
local function navigateNewPage(page, oldPos, newPos, direction, instantTween)
	local tweenTime
	if instantTween then
		tweenTime = 0
	else
		tweenTime = 0.3
	end
	local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	local subPageNames = subPageOrders[page.Name]
	local pageOut
	if tonumber(oldPos) then
		pageOut = page[subPageNames[oldPos]]
	end
	local pageIn = page[subPageNames[newPos]]
	local startX
	if pageOut and pageOut:IsA("ScrollingFrame") then
		pageOut.ScrollBarImageTransparency = 1
	end
	if pageIn:IsA("ScrollingFrame") then
		pageIn.ScrollBarImageTransparency = 0
	end
	local searchBar = page:FindFirstChild("SearchBar")
	if searchBar then
		if pageIn.Size.Y.Scale < 0.85 then
			searchBar.Visible = true
		else
			searchBar.Visible = false
		end
	end
	--
	for i,v in pairs(subPageNames) do
		local subPage = page[v]
		if subPage ~= pageIn and subPage ~= pageOut then
			subPage.Position = UDim2.new(-2,0,subPage.Position.Y.Scale,0)
		end
	end
	--
	if direction == "Right" then
		startX = 1
	elseif direction == "Left" then
		startX = -1
	end
	local endX = startX*-1
	page.NavigateButtons.TextLabel.Text = string.upper(pageIn.Name)
	pageIn.Position = UDim2.new(startX, 0, pageIn.Position.Y.Scale, pageIn.Position.Y.Offset)
	local tweenIn = main.tweenService:Create(pageIn, tweenInfo, {Position = UDim2.new(0,0,pageIn.Position.Y.Scale,0)})
	tweenIn:Play()
	if pageOut then
		local tweenOut = main.tweenService:Create(pageOut, tweenInfo, {Position = UDim2.new(endX,0,pageOut.Position.Y.Scale,0)})
		tweenOut:Play()
		spawn(function()
			tweenOut.Completed:Wait()
			if pageOut.Position.X.Scale == endX then
				pageOut.Position = UDim2.new(-2,0,pageOut.Position.Y.Scale,0)
			end
		end)
	end
end
for a,b in pairs(pages:GetChildren()) do
	if b:FindFirstChild("NavigateButtons") then
		local pos = 1
		local pageX = 0
		local subPages = subPageOrders[b.Name]
		for i,v in pairs(subPages) do
			local subPage = b[v]
			if i == 1 then
				b.NavigateButtons.TextLabel.Text = string.upper(v)
			else
				pageX = 1
			end
			subPage.Position = UDim2.new(pageX,0,subPage.Position.Y.Scale,0)
			subPage.Visible = true
		end
		for c,d in pairs(b.NavigateButtons:GetChildren()) do
			if d:IsA("TextButton") then
				d.MouseButton1Down:Connect(function()
					local oldPos = pos
					if d.Name == "Right" then
						pos = pos + 1
						if pos > #subPages then
							pos = 1
						end
					elseif d.Name == "Left" then
						pos = pos - 1
						if pos < 1 then
							pos = #subPages
						end
					end
					navigateNewPage(b, oldPos, pos, d.Name)
				end)
			end
		end
	end
end

for a,b in pairs(mainFrame.DragBar:GetChildren()) do
	if b:IsA("TextButton") then
		b.MouseButton1Down:Connect(function()
			if guiDe then
				guiDe = false
				if b.Name == "Minimise" then
					if b.TextLabel.Text == "-" then
						main.tweenService:Create(pages, TweenInfo.new(0.5), {Position = UDim2.new(0,0,-0.8,0)}):Play()
						b.TextLabel.Text = "+"
					else
						pages.Parent.Visible = true
						main.tweenService:Create(pages, TweenInfo.new(0.5), {Position = UDim2.new(0,0,0.1,0)}):Play()
						b.TextLabel.Text = "-"
					end
				elseif b.Name == "Close" then
					module:CloseMainFrame()
				elseif b.Name == "Back" then
					tweenPages(UDim2.new(1,0,0,0), pages.Home)
				end
				guiDe = true
			end
		end)
	end
end