math.randomseed(os.time())
--hex color
alpha = color.new(0,0,0,0)
blue = color.new(0,0,255)
green = color.new(0,255,0)
red = color.new(255,0,0)
white = color.new(255,255,255)
yellow = color.new(155,155,0)
pink = color.new(255,192,203)
black = color.new(0,0,0)
gold= color.new(255,215,0)
orange = color.new(255,160,0)
alphawhite = color.new(255,255,255,124)

colorstab={pink,blue,green,red,white,yellow,black,gold,alphawhite,orange}
languagestab={spanish,catalan,english,french}
-------
tiempo = timer.new()

colortext=white
colorselec=blue
colormainbar=alphawhite
colorscrollbar=gold
textpos=5
selecpos=2
mainbarpos=9
scrollbarpos=8

spacing=12
size=0.6

mainscrollbar={x=445,y=20,w=9,h=232,h1=nil,pos=nil,posfiles=nil,color1=colormainbar,color2=colorscrollbar}
freescrollbar={x=445,y=20,w=9,h=232,h1=nil,pos=nil,posfiles=nil,color1=colormainbar,color2=colorscrollbar}
getdirscrollbar={x=445,y=20,w=9,h=232,h1=nil,pos=nil,posfiles=nil,color1=colormainbar,color2=colorscrollbar}
dirto={extract=nil,move=nil,copy=nil}
filesfreedir=nil
filescdir= nil
freecdir=nil
freedir=nil
currentdir = "ms0:/"

cross={
currentdir=10,
getdirdir=10,
freedir=10,
equalizer=10
}

options={
{textcolor,seleccolor,mainbarcolor,scrollbarcolor,text=changecolors},
{french,spanish,catalan,english,text=languages},
{"Default equalizer", text= "Default equalizer"},
{text=leave}
}

EQdefault={
{{0,0,480,272,green},
{resize,move,"color"},"wave"
},
{{0,0,480,272,red},
{resize,move,"colorline"},"waveline"
},
{{0,0,504,272,blue},
{resize,move,"color"},"spectrum_lines"
},
{{0,0,504,272,yellow,blue,red,green},
{resize,move,"color","colordown","colorup","colortop"},"spectrum_gradlines"
},
{{0,0,536,272,blue,green,yellow,white,red,6},
{resize,move,"colorbackup","colorbackdown","colorupbar","colordownbar","colortop","blocksize"},"spectrum_bars"
}
}

EQ={
{{0,0,480,272,green},
{resize,move,"color"},"wave"
},
{{0,0,480,272,red},
{resize,move,"colorline"},"waveline"
},
{{0,0,504,272,blue},
{resize,move,"color"},"spectrum_lines"
},
{{0,0,504,272,yellow,blue,red,green},
{resize,move,"color","colordown","colorup","colortop"},"spectrum_gradlines"
},
{{0,0,536,272,blue,green,yellow,white,red,6},
{resize,move,"colorbackup","colorbackdown","colorupbar","colordownbar","colortop","blocksize"},"spectrum_bars"
}
}

function songtime(time,duration)
local string=""
if math.floor((time/1000)/3600) <10 then
	string="0"..math.floor((time/1000)/3600)..":"
else string=math.floor((time/1000)/3600)..":"
end

if math.floor((time/1000)/60)-(math.floor((time/1000)/3600)*60) <10 then
	string=string.."0"..math.floor((time/1000)/60)-(math.floor((time/1000)/3600)*60)..":"
else string=string..math.floor((time/1000)/60)-(math.floor((time/1000)/3600)*60)..":"
end

if math.floor(time/1000)-(math.floor((time/1000)/60)-math.floor((time/1000)/3600))*60  <10 then
string=string.."0"..math.floor(time/1000)-(math.floor((time/1000)/60)-math.floor((time/1000)/3600))*60
else string=string..math.floor(time/1000)-(math.floor((time/1000)/60)-math.floor((time/1000)/3600))*60
end
if duration then
if math.floor((duration/1000)/3600) <10 then
	string=string.."/0"..math.floor((duration/1000)/3600)..":"
else string=string.."/"..math.floor((duration/1000)/3600)..":"
end

if math.floor((duration/1000)/60)-(math.floor((duration/1000)/3600)*60) <10 then
	string=string.."0"..math.floor((duration/1000)/60)-(math.floor((duration/1000)/3600)*60)..":"
else string=string..math.floor((duration/1000)/60)-(math.floor((duration/1000)/3600)*60)..":"
end

if math.floor(duration/1000)-(math.floor((duration/1000)/60)-math.floor((duration/1000)/3600))*60  <10 then
string=string.."0"..math.floor(duration/1000)-(math.floor((duration/1000)/60)-math.floor((duration/1000)/3600))*60
else string=string..math.floor(duration/1000)-(math.floor((duration/1000)/60)-math.floor((duration/1000)/3600))*60
end

end
return string
end

function batteryandtime()

if batt.exists() then
	if batt.charging() 	and tiempo:time() >= 500 and tiempo:time() <= 1000 then
		if batt.percent() >0 and batt.percent() <=25 then
		draw.fillrect(300+115, 2,math.ceil((batt.percent()*14)/100), 9,red)
		elseif batt.percent() >25 and batt.percent() <=50 then
		draw.fillrect(300+115, 2,math.ceil((batt.percent()*14)/100), 9,orange)
		elseif batt.percent() >50 and batt.percent() <=75 then
		draw.fillrect(300+115, 2,math.ceil((batt.percent()*14)/100), 9,yellow)
		elseif batt.percent() >75 and batt.percent() <=100 then
		draw.fillrect(300+115, 3,14,7,green)
		end
		draw.line(300+114, 2, 315+114, 2,white)
		draw.line(300+114, 10, 315+114, 10,white)
		draw.line(300+114, 2, 300+114, 10,white)
		draw.line(316+114, 4, 316+114, 8,white)
		draw.line(315+114, 2, 315+114, 11,white)
		draw.line(315+114, 4, 317+114, 4,white)
		draw.line(315+114, 8, 317+114, 8,white)
		draw.line(317+114, 4, 317+114, 9,white)	
	elseif batt.charging()==false then
		if batt.percent() >0 and batt.percent() <=25 then
		draw.fillrect(300+115, 2,math.ceil(math.ceil(batt.percent()/14)), 9,red)
		elseif batt.percent() >25 and batt.percent() <=50 then
		draw.fillrect(300+115, 2,math.ceil((batt.percent()*14)/100), 9,orange)
		elseif batt.percent() >50 and batt.percent() <=75 then
		draw.fillrect(300+115, 2,math.ceil((batt.percent()*14)/100), 9,yellow)
		elseif batt.percent() >75 and batt.percent() <=100 then
		draw.fillrect(300+115, 3,14,7,green)
		end
		draw.line(300+114, 2, 315+114, 2,white)
		draw.line(300+114, 10, 315+114, 10,white)
		draw.line(300+114, 2, 300+114, 10,white)
		draw.line(316+114, 4, 316+114, 8,white)
		draw.line(315+114, 2, 315+114, 11,white)
		draw.line(315+114, 4, 317+114, 4,white)
		draw.line(315+114, 8, 317+114, 8,white)
		draw.line(317+114, 4, 317+114, 9,white)	
end
else 	
		draw.fillrect(300+115, 3,14,7,blue)
		draw.line(300+114, 2, 315+114, 2,white)
		draw.line(300+114, 10, 315+114, 10,white)
		draw.line(300+114, 2, 300+114, 10,white)
		draw.line(316+114, 4, 316+114, 8,white)
		draw.line(315+114, 2, 315+114, 11,white)
		draw.line(315+114, 4, 317+114, 4,white)
		draw.line(315+114, 8, 317+114, 8,white)
		draw.line(317+114, 4, 317+114, 9,white)	
end

if batt.exists() then 	
screen.print(319+115,2,batt.percent().."%",size,white,alpha)
end

screen.print(290,2,os.date("%x"),size,white,alpha)
screen.print(290+screen.textwidth(os.date("%x").."  "),2,os.date("%H"),size,white,alpha)
screen.print(290+screen.textwidth(os.date("%x").."  "..os.date("%H")..":"),2,os.date("%M"),size,white,alpha)
if tiempo:time() >= 500 and tiempo:time() <= 1000 then
	screen.print(290+screen.textwidth(os.date("%x").."  "..os.date("%H")),2,":",size,white,alpha)
elseif tiempo:time() >= 1000 then tiempo:reset()
end
end
EQtype = 1
equalizer={
list={},
id3={},
pos=1,
subpos=1,
xcent=1,
showmenu=false,
insubmenu=false,
showinformation = false ,
menu=function()
if equalizer.insubmenu == false then   --main menu
	EQtype = equalizer.pos
	if controls.press("down") then
		if equalizer.pos == #EQ then equalizer.pos = 1
		else equalizer.pos = equalizer.pos + 1 end
	end
	if controls.press("up") then
		if equalizer.pos == 1 then equalizer.pos = #EQ
		else equalizer.pos = equalizer.pos - 1 end
	end
elseif equalizer.insubmenu == true then     --submenu
	equalizer.showsubmenu()
end
if controls.press("cross") then
	equalizer.insubmenu = true	
end
for i =1, #EQ do
	screen.print(10,200+11*i,EQ[i][3],0.6,white,black)
end
draw.rect(5,200+10+(11*(equalizer.pos-1)),10+screen.textwidth(EQ[equalizer.pos][3]),size*2*10,blue)
end,
blit=function()
if song:percent() >= 99.9 then equalizer.changesong("up") end
screen.clear(black)
if EQtype == 1  then
	song:blit("wave",EQ[EQtype][1][1],EQ[EQtype][1][2],EQ[EQtype][1][3],EQ[EQtype][1][4],EQ[EQtype][1][5])
elseif EQtype == 2 then
	song:blit("waveline",EQ[EQtype][1][1],EQ[EQtype][1][2],EQ[EQtype][1][3],EQ[EQtype][1][4],EQ[EQtype][1][5])
elseif EQtype == 3 then
	song:blit("spectrum_lines",EQ[EQtype][1][1],EQ[EQtype][1][2],EQ[EQtype][1][3],EQ[EQtype][1][4],EQ[EQtype][1][5])
elseif EQtype == 4 then
	song:blit("spectrum_gradlines",EQ[EQtype][1][1],EQ[EQtype][1][2],EQ[EQtype][1][3],EQ[EQtype][1][4],EQ[EQtype][1][5],EQ[EQtype][1][6],EQ[EQtype][1][7],EQ[EQtype][1][8])
elseif EQtype == 5 then
	song:blit("spectrum_bars",EQ[EQtype][1][1],EQ[EQtype][1][2],EQ[EQtype][1][3],EQ[EQtype][1][4],EQ[EQtype][1][5],EQ[EQtype][1][6],EQ[EQtype][1][7],EQ[EQtype][1][8],EQ[EQtype][1][9],EQ[EQtype][1][10],EQ[EQtype][1][11])
end
end,
changesong=function(direction)
if controls.press("l") and song:percent() <1 or direction == "down" then
	if equalizer.songposition>1 then
		for i= equalizer.songposition-1,1,-1 do	
			if files.ext(equalizer.list[i].name) == "mp3" or files.ext(equalizer.list[i].name) == "at3" or 
			files.ext(equalizer.list[i].name) == "bgm" or files.ext(equalizer.list[i].name) == "wav" then
				equalizer.songposition= i
				song:stop();
				song:free();
				song=sound.load(equalizer.list[i].name); 
				equalizer.id3={}
				equalizer.id3=sound.id3(equalizer.list[i].name) 
				song:play();
				break
			end
		end
	end
end
if controls.press("r") or direction == "up" then
	if equalizer.songposition<#equalizer.list then
		for i= equalizer.songposition+1,#equalizer.list do	
			if files.ext(equalizer.list[i].name) == "mp3" or files.ext(equalizer.list[i].name) == "at3" or 
			files.ext(equalizer.list[i].name) == "bgm" or files.ext(equalizer.list[i].name) == "wav" then
				equalizer.songposition= i
				song:stop();
				song:free();
				song=sound.load(equalizer.list[i].name); 
				equalizer.id3={}
				equalizer.id3=sound.id3(equalizer.list[i].name) 
				song:play();
				break
			end
		end
	end
end
end,
showsubmenu=function()
if controls.press("down") then
		if equalizer.subpos == #EQ[equalizer.pos][2] then equalizer.subpos = 1
		else equalizer.subpos = equalizer.subpos + 1 end
	end
	if controls.press("up") then
		if equalizer.subpos == 1 then equalizer.subpos = #EQ[equalizer.pos][2]
		else equalizer.subpos = equalizer.subpos - 1 end
	end
	if controls.cross() and equalizer.subpos>2 then
		EQ[equalizer.pos][1][equalizer.subpos+2] = colorstab[math.random(1,9)]
	end
	if controls.cross() and equalizer.subpos>2 then
		EQ[equalizer.pos][1][equalizer.subpos+2] = colorstab[math.random(1,9)]
	end 
	if controls.press("right") and EQ[equalizer.pos][2][equalizer.subpos] == "blocksize" then
		EQ[5][1][10] = EQ[5][1][10] +1 
	end
	if controls.press("left") and EQ[equalizer.pos][2][equalizer.subpos] == "blocksize" then
		EQ[5][1][10] = EQ[5][1][10] -1 
	end
	
	if equalizer.subpos == 2 then
		draw.rect(EQ[equalizer.pos][1][1],EQ[equalizer.pos][1][2],EQ[equalizer.pos][1][3],EQ[equalizer.pos][1][4],white)
		if math.abs(controls.analogx())>20 then EQ[equalizer.pos][1][1] = EQ[equalizer.pos][1][1] + (controls.analogx()/200) end
		if math.abs(controls.analogy())>20 then EQ[equalizer.pos][1][2] = EQ[equalizer.pos][1][2] + (controls.analogy()/200) end
	elseif equalizer.subpos == 1 then
		draw.rect(EQ[equalizer.pos][1][1],EQ[equalizer.pos][1][2],EQ[equalizer.pos][1][3],EQ[equalizer.pos][1][4],white)
		if math.abs(controls.analogx())>20 then EQ[equalizer.pos][1][3] = EQ[equalizer.pos][1][3] + (controls.analogx()/200) end
		if math.abs(controls.analogy())>20 then EQ[equalizer.pos][1][4] = EQ[equalizer.pos][1][4] + (controls.analogy()/200) end
	end
	draw.rect(145,272-(11*#EQ[equalizer.pos][2])+11*equalizer.subpos-15,10+screen.textwidth(EQ[equalizer.pos][2][equalizer.subpos]),size*2*10,green)
	for j =1, #EQ[equalizer.pos][2] do
		if EQ[equalizer.pos][2][j] != resize and EQ[equalizer.pos][2][j] != move then
			screen.print(150,(272-(11*#EQ[equalizer.pos][2]))+11*j-15,EQ[equalizer.pos][2][j],0.6,EQ[equalizer.pos][1][j+2],white,black)
		elseif EQ[equalizer.pos][2][j] == "blocksize" then
			screen.print(150,(272-(11*#EQ[equalizer.pos][2]))+11*j-15,EQ[equalizer.pos][1][10]..EQ[equalizer.pos][2][8],0.6,white,black)
		elseif EQ[equalizer.pos][2][j] == resize then 
			screen.print(150,(272-(11*#EQ[equalizer.pos][2]))+11*j-15,EQ[equalizer.pos][2][j].."  ("..EQ[equalizer.pos][1][3]..","..EQ[equalizer.pos][1][4]..")",0.6,white,black)
		elseif EQ[equalizer.pos][2][j] == move then 
			screen.print(150,(272-(11*#EQ[equalizer.pos][2]))+11*j-15,EQ[equalizer.pos][2][j].."  ("..EQ[equalizer.pos][1][1]..","..EQ[equalizer.pos][1][2]..")",0.6,white,black) 
		end
	end
	if controls.press("circle") then
		equalizer.subpos = 1
		equalizer.insubmenu = false	
	end
end,
show=function()
for y = 280, 200,-4 do
	equalizer.blit()	
	screen.print(10,11*1+y,EQ[1][3],0.6,white,alpha)
	screen.print(10,11*2+y,EQ[2][3],0.6,white,alpha)
	screen.print(10,11*3+y,EQ[3][3],0.6,white,alpha)
	screen.print(10,11*4+y,EQ[4][3],0.6,white,alpha)
	screen.print(10,11*5+y,EQ[5][3],0.6,white,alpha)
	if equalizer.showinformation == true then equalizer.information()end
	screen.flip()
end
end,
hide=function()
for y = 1, 80,4 do
	equalizer.blit()	
	screen.print(10,200+(11*1+y),EQ[1][3],0.6,white,black)
	screen.print(10,200+(11*2+y),EQ[2][3],0.6,white,black)
	screen.print(10,200+(11*3+y),EQ[3][3],0.6,white,black)
	screen.print(10,200+(11*4+y),EQ[4][3],0.6,white,black)
	screen.print(10,200+(11*5+y),EQ[5][3],0.6,white,black)
	if equalizer.showinformation == true then equalizer.information()end
	screen.flip()
end
end,

information=function()
cross.equalizer = screen.print(cross.equalizer,2,title..equalizer.id3.title.."  "..artist..equalizer.id3.artist.."  "..album..equalizer.id3.album.."  "..year..equalizer.id3.year.."  "..genre..equalizer.id3.genre,0.7,white,black,"scroll_left",480)
screen.print(340,235,songtime(song:position(),song:duration()),size,white,black)
draw.fillrect(324,249,152,10,black)
draw.fillrect(325,250,150,8,white)
--draw.fillrect(325,250,math.ceil(song:percent()*1.5),8,blue)
draw.gradrect(325,250,math.ceil(song:percent()*1.5),8,color.new(0,0,255),color.new(0,255,0),color.new(0,0,155),color.new(0,155,0))
end,
showinf=function()
for y = 50,0,-5 do
	equalizer.blit()	
	screen.print(340,235+y,songtime(song:position()),size,white,black)
	draw.fillrect(324,249+y,152,10,black)
	draw.fillrect(325,250+y,150,8,white)
	draw.gradrect(325,250+y,math.ceil(song:percent()*1.5),8,color.new(0,0,255),color.new(0,255,0),color.new(0,0,155),color.new(0,155,0))
	screen.print(cross.equalizer,2-y*0.5,title..equalizer.id3.title.."  "..artist..equalizer.id3.artist.."  "..album..equalizer.id3.album.."  "..year..equalizer.id3.year.."  "..genre..equalizer.id3.genre,0.7,white,black,"scroll_left",480)
	if equalizer.showmenu then equalizer.menu() end
	if equalizer.insubmenu == true then equalizer.showsubmenu() end
	screen.flip()
end
cross.equalizer=10
end,
hideinf=function()
for y = 0, 50,5 do
	equalizer.blit()	
	screen.print(340,235+y,songtime(song:position()),size,white,black)
	draw.fillrect(324,249+y,152,10,black)
	draw.fillrect(325,250+y,150,8,white)
	draw.gradrect(325,250+y,math.ceil(song:percent()*1.5),8,color.new(0,0,255),color.new(0,255,0),color.new(0,0,155),color.new(0,155,0))
	screen.print(cross.equalizer,2-y*0.5,title..equalizer.id3.title.."  "..artist..equalizer.id3.artist.."  "..album..equalizer.id3.album.."  "..year..equalizer.id3.year.."  "..genre..equalizer.id3.genre,0.7,white,black,"scroll_left",480)
	if equalizer.showmenu then equalizer.menu() end
	if equalizer.insubmenu == true then equalizer.showsubmenu() end
	screen.flip()
end
cross.equalizer=10
end
}

tiempo:start()