
---- Any pre/non-game stuff, main menu, intro etc. ----
-------------------------------------------------------------------------------

playedintro = false
function intro()
	if not playedintro then
		playedintro = true
		for i=0, 255, 8 do
			screen:fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, Color.new(0,0,0,i))
			screen:setTint(Color.new(255,255,255,i))
			screen:blit(32, 32, imgs.title)
			screen.flip()
		end
		screen:setTint()
		Fibers:pre({0,180}); Fibers:add(0, 180); Fibers:add(0, 180)
		for i=0, 320, 8 do
			screen:clear()
			Fibers:changeLast(i, 180)
			screen:blit(32, 32, imgs.title)
			Fibers:draw()
			screen.flip()
		end
		Fibers:add(320, 180)
		for i=180, 40, -8 do
			screen:clear()
			Fibers:changeLast(320, i)
			screen:blit(32, 32, imgs.title)
			Fibers:draw()
			screen.flip()
		end
		for i=0, 255, 12 do
			screen:clear()
			screen:setTint()
			screen:blit(32, 32, imgs.title)
			Fibers:draw()
			screen:setTint(Color.new(255,255,255,i))
			screen:blit(312, 32, imgs.alis)
			screen.flip()
		end
		screen:setTint()
		Fibers:stop()
	end
end


MainMenu = {
	options = {"Start\nGame", "Difficulty\n(Easy)", "High\nScores", "View\nCredits", "Quit\nCrystalise"},
	difficulties = {"Easy", "Medium", "Hard", "ZOMGWTF"}, difficulty = 1,
	funcs = {
		function(self)
			self.start = true
			self:printMessage("Starting Game...")
		end,
		function(self)
			self.difficulty = math.mod(self.difficulty, table.getn(self.difficulties))+1
			Difficulty = Difficulties[self.difficulty]
			self.options[2] = "Difficulty\n("..self.difficulties[self.difficulty]..")"
			self:printMessage("Set difficulty to "..self.difficulties[self.difficulty]..".")
		end,
		function(self)
			self:printMessage("Can you beat them?")
			Highscores:show() 
		end,
		function(self)
			self:printMessage("You actually selected that option. Wow.")
			Window:show(160, nil, self:generateCreditsWindowFgFunc())
		end,
		function(self)
			self.quit = true
			self:printMessage("You WILL be back.")
		end
	},
	start = false, quit = false,
	index = -1,
	x1=340, x2=460, y1=30, y2=260,
	cx = (340+460)/2, cy = (30+260)/2,
	drawcount = 0
}

function MainMenu:init()
	self.start, self.quit = false, false
	self.index = -1
	System.setClock(222)
	Fibers:pre({0,180}); 
	Fibers:add(0, 180); Fibers:add(320, 180); Fibers:add(320, 40)
end

function MainMenu:drawBG()
	screen:blit(32, 32, imgs.title)
	Fibers:draw()
	screen:blit(312, 32, imgs.alis)
end

function MainMenu:drawItems(col)
	self.drawcount = self.drawcount + 1
	local s = math.sin(self.drawcount/4)*105 + 150
	Vera:print("Main Menu", self.cx, 40, Font.CENTER, col or colors.white)
	for i,v in pairs(self.options) do
		Vera:print(v, self.cx, 38 + 36*i, Font.CENTER, 
			(i == self.index) and Color.new(255, s, s) or (col or colors.lightgray))
	end
	Vera:print("Use up/down to select an option,\npress circle to confirm.", 160, 194, Font.CENTER, col or colors.mediumgray)
end

function MainMenu:intro()
	for i=1, self.cy-self.y1, 4 do
		screen:clear()
		self:drawBG()
		Circle.points = { 
			{self.x1, self.cy-i}, {self.x2, self.cy-i}, 
			{self.x2, self.cy+i}, {self.x1, self.cy+i} }
		Circle:reallyDraw()
		screen.flip()
	end
	for i=0, 200, 12 do
		screen:clear()
		self:drawBG()
		Circle:reallyDraw()
		self:drawItems(Color.new(255,255,255,i))
		screen.flip()
	end
end

function MainMenu:drawNoFlip()
	screen:fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, colors.clear)
	screen:fillRect(0, 0, 310, 170, colors.black150)
	self:drawBG()
	Circle:draw()
	self:drawItems()
end

function MainMenu:draw()
	self:drawNoFlip()
	screen.flip()
end

function MainMenu:run()
	self:init()
	self:intro()
	self.index = 1
	
	local ctrl, oldctr
	repeat
		ctrl = Controls.read()
		if ctrl ~= oldctrl then
			if ctrl:down() and self.index < table.getn(self.options) then
				self.index = self.index+1
			elseif ctrl:up() and self.index > 1 then
				self.index = self.index-1
			end
			if ctrl:up() or ctrl:down() then
				screen:fillRect(self.x1, 34 + 36*self.index, 119, 34, colors.white200)
				sfx.menumove:play()
			end
			if ctrl:circle() then
				sfx.menuselect:play()
				self:selectedOption()
			end
		end
		oldctrl = ctrl
		self:draw()
	until self.start or self.quit or globalquit
	self:fadeOut()
end

function MainMenu:printMessage(msg)
	Vera:print(msg, 160, 232, Font.CENTER)
	screen.flip()
	Vera:print(msg, 160, 232, Font.CENTER)
end

function MainMenu:selectedOption()
	local f = self.funcs[self.index]
	if f then f(self)
	else self:printMessage("Not yet implemented!") end 
end

function MainMenu:fadeOut()
	for i=0, 90 do
		screen:fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, colors.clear)
		screen.flip()
	end
end


---- Credits Section ----

function MainMenu:generateCreditsWindowFgFunc()
	local credits = 
[[
Code, Graphics And Sfx
Peter Thoman

Music
Various artists 
(look in the files)

Beta Testing
Andreas Schnabl
Daniel Zemann

Thanks To
Shine and Nevyn for LuaPlayer
Everyone involved with the PSPsdk
jsgf for his contributions to pspGL

Greetings To
#luaplayer regulars
Fanjita 'cause he rocks 2.0
JOERG!


Their Satanic Majesties Request
Donate your soul!



D.U.R.A.N.T.E.
Deadly Unholy
Redhead-Abducting
Nightmare from the
Twisted Earth




So, if you're a (female!) redhead
and are in the area...
feel free to 
express your gratitude





By bringing me some 
homemade cookies of course!
(What were YOU thinking?)
]]
	local clines, length = {}, 0
	for line in string.gfind(credits, "[^\n]*") do
		length = length+1
		clines[length] = {60+12*length, line}
	end
	
	local i, sy = 0, 96
	local c = nil
	return function() 
		i, sinf = i+1, math.sin(i/6)
		c = Color.new(255,255,255,i*8)
		Crystal:print("Crystalise Credits", SCREEN_WIDTH2, 64, Font.CENTER, c)
		for j = 1, length do
			--Debug:print(tableString(clines[length]))
			if clines[j][1] < 90 and clines[j][1] > 0 then
				if clines[j][1] > 60 then c = Color.new(255,255,255,255-(clines[j][1]-60)*10) end
				if clines[j][1] < 30 then c = Color.new(255,255,255,(clines[j][1])*10) end
				Vera:print(clines[j][2], SCREEN_WIDTH2, sy+clines[j][1], Font.CENTER, c)
			end
			clines[j][1] = clines[j][1] - 0.25
		end
		if clines[length][1] <= 5 then Window:close() end
		
		Vera:print("Close with Cross or Circle.", SCREEN_WIDTH2, 198, Font.CENTER, Color.new(200,200,200,i*8))
	end
end
