
ext_suport={
"rar",
"zip",
"txt",
"ini",
"lua",
"png",
"jpg",
"gif",
"bmp",
"mp3",
"wav",
"at3",
"bgm",
"pbp",
"iso",
"cso",
"psx",
"html",
"swf",
}


function abrir_barra()
	pos_tarea=posicion
	tareas_e=true
	for i=1,9 do
		if p.x<300 then
			tareas_c[i].x=p.x+10
		else
			tareas_c[i].x=300
		end
		if p.y<125 then
			tareas_c[i].y=p.y+i*15
		else
			tareas_c[i].y=125+i*15
		end
	end
end

function vac(mini,ruta)
img_anterior=screen.toimage()
actual=1
while true do
	controls.read()
	img_anterior:blend(0,0,100)
	draw.fillrect(0,0,480,272,blanco_t)
	
	if controls.press("down") then actual=actual+1 elseif controls.press("up") then actual=actual-1 end
	if actual>6 then actual=1 elseif actual<1 then actual=6 end
	
	screen.print(240,60,"El directorio está vacío.\n¿Qué deseas hacer?",0.8,negro,0,"center")
	
	if actual<6 then
		draw.fillrect(0,80+actual*20,480,15,azul)
	else
		draw.fillrect(0,210,480,15,azul)
	end
	screen.print(240,100,"Crear una carpeta",0.6,negro,0,"center")
	screen.print(240,120,"Crear un archivo de Texto",0.6,negro,0,"center")
	screen.print(240,140,"Crear un archivo de Configuración",0.6,negro,0,"center")
	screen.print(240,160,"Crear un script",0.6,negro,0,"center")
	screen.print(240,180,"Crear una imagen",0.6,negro,0,"center")
	screen.print(240,210,"Volver atrás",0.6,negro,0,"center")
	
	if controls.press("cross") then
		if actual<6 then
			if ruta!=nil then 
				if actual==1 then
					files.mkdir(ruta.."/".."Nueva Carpeta")
				elseif actual==2 then
					io.open(ruta.."/".."Nuevo archivo de texto.txt","w+")
				elseif actual==3 then
					io.open(ruta.."/".."Nuevo archivo de Configuracion.ini","w+")
				elseif actual==4 then
					io.open(ruta.."/".."Nuevo Script.lua","w+")
				elseif actual==5 then
					nuevaimg=image.create(480,272,blanco)
					nuevaimg:save(ruta.."/".."Nueva imagen.png")
					nuevaimg:free()
					nuevaimg=nil
				end
				workDir=files.list(ruta.."/")
			else
				if actual==1 then
					files.mkdir(workDir[posicion].name.."/".."Nueva Carpeta")
				elseif actual==2 then
					io.open(workDir[posicion].name.."/".."Nuevo archivo de texto.txt","w+")
				elseif actual==3 then
					io.open(workDir[posicion].name.."/".."Nuevo archivo de Configuracion.ini","w+")
				elseif actual==4 then
					io.open(workDir[posicion].name.."/".."Nuevo Script.lua","w+")
				elseif actual==5 then
					nuevaimg=image.create(480,272,blanco)
					nuevaimg:save(workDir[posicion].name.."/".."Nueva imagen.png")
					nuevaimg:free()
					nuevaimg=nil
				end
				workDir=files.list(workDir[posicion].name)
			end
		else
			if ruta!=nil then 
				workDir=files.list(files.nofile(ruta))
			else
				workDir=files.list(files.nofile(workDir[posicion].name))
			end
		end
	break
	end
		
	screen.flip()
end
if not mini then explorador() else posicion=1 end
end

function adelante(mini)
if #files.list(workDir[posicion].name)>0 then
	workDir=files.list(workDir[posicion].name)
	if not mini then explorador() else posicion=1 end
else
	if mini then return end
	vac()
end
end

function explorador_zip(mini)
workDir=zip.list(workDir[posicion].name)
if not mini then
	explorador()
else
	posicion=1
end
end

function iconset(i)

	local com=files.ext(workDir[i].name)
	
	tables[i]={x=xx,y=yy,w=55,h=55}
	
	if workDir[i].directory then
		tables[i].i=2
	elseif com=="ini" then
		tables[i].i=3
	elseif com=="lua" then
		tables[i].i=4 
	elseif com=="mp3" then
		tables[i].i=5
	elseif com=="pbp" then
		tables[i].i=extractpbp(workDir[i].name)
		if tables[i].i!=6 then tables[i].y=tables[i].y+10 end
	elseif com=="txt" then
		tables[i].i=7
	elseif com=="bmp" then 
		tables[i].i=8
	elseif com=="png" then
		tables[i].i=9
	elseif com=="gif" then
		tables[i].i=10
	elseif com=="jpg" then 
		tables[i].i=11
	elseif com=="zip" then 
		tables[i].i=12
	elseif com=="rar" then 
		tables[i].i=13 
	elseif com=="iso" or com=="cso" then 
		tables[i].i=14 
	elseif com=="satd" then 
		tables[i].i=15 
	elseif com=="html" then 
		tables[i].i=16
	else	
		tables[i].i=1
	end
end


function actualizar_m()
xx=120
yy=20
cont=0

tareas_e=false
opndcon=false

tables={}

if #icons>16 then
	for i=#icons,17,-1 do
		icons[i]:free()
		icons[i]=nil
	end
end

if #workDir>0 then
	for i=1,#workDir do
		iconset(i)
		cont=cont+1
		xx=xx+130
		if cont>2 then yy=yy+90 xx=120 cont=0 end 
	end
end
end

function actualizar_e()
xx=40
yy=20
cont=-1
workDir=files.list("ms0:/")

tareas_e=false
opndcon=false

tables={}

if #icons>16 then
	for i=#icons,17,-1 do
		icons[i]:free()
		icons[i]=nil
	end
end

for i=1,math.min(12,#workDir) do
	iconset(i)	
	cont=cont+1
	xx=xx+110
	if cont>3 then yy=yy+70 xx=40 cont=0 end 
end

end

function abrir_con()
if opndcon then

	draw.fillrect(tareas_c[1].x-10,tareas_c[1].y-10,130,40,blanco)

	if col(p,tareas_c[1]) then
		draw.fillrect(tareas_c[1].x-10,tareas_c[1].y,130,10,verde)
		with=1
	else
		ext=files.ext(workDir[pos_tarea].name)
		if ext=="zip" or ext=="jpg" or ext=="png" or ext=="mp3" or ext=="gif" or ext=="bmp"
		or ext=="at3" or ext=="wav" or ext=="bgm" or ext=="html" then
			if col(p,tareas_c[2]) then
				draw.fillrect(tareas_c[2].x-10,tareas_c[2].y,130,10,verde)
				with=2
			end
		ext=nil
		end
	end

	if files.ext(workDir[pos_tarea].name)=="rar" then
		if with!=1 then with=1 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"Extractor",0.6,negro,0)
		if controls.press("cross") then
			ini.write("APP/extractor/RUTAS.txt","tipo","'rar'")
			ini.write("APP/extractor/RUTAS.txt","ruta","'"..workDir[pos_tarea].name.."'")
			ini.write("APP/extractor/RUTAS.txt","descompresion","'"..files.nofile(workDir[pos_tarea].name).."'")
			ini.write("SYSTEM/salida.ini","user",user)
			os.runeboot("APP/extractor/EBOOT.PBP")
		end
	elseif files.ext(workDir[pos_tarea].name)=="zip" then
		if with>2 then with=1 end
		if with<1 then with=2 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"ZipExplorator",0.6,negro,0)
		screen.print(tareas_c[2].x,tareas_c[2].y,"Extractor",0.6,negro,0)
		if controls.press("cross") then
			if with==1 then zipexplorator() end
			if with==2 then
				ini.write("extractor/RUTAS.txt","tipo","'zip'")
				ini.write("APP/extractor/RUTAS.txt","ruta","'"..workDir[pos_tarea].name.."'")
				ini.write("APP/extractor/RUTAS.txt","descompresion","'"..files.nofile(workDir[pos_tarea].name).."'")
				ini.write("SYSTEM/salida.ini","user",user)
				os.runeboot("APP/extractor/EBOOT.PBP")
			end
		end
	elseif files.ext(workDir[pos_tarea].name)=="txt" or files.ext(workDir[pos_tarea].name)=="ini" or files.ext(workDir[pos_tarea].name)=="lua" then
		if with!=1 then with=1 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"NotepadCode",0.6,negro,0)
		if controls.press("cross") then	
			new_corutina("NotepadCode") editor()
		end
	elseif files.ext(workDir[pos_tarea].name)=="png" or files.ext(workDir[pos_tarea].name)=="jpg" or files.ext(workDir[pos_tarea].name)=="gif" or files.ext(workDir[pos_tarea].name)=="bmp" then
		if with>2 then with=1 end
		if with<1 then with=2 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"Visor",0.6,negro,0)
		screen.print(tareas_c[2].x,tareas_c[2].y,"PlutuX",0.6,negro,0)
		if controls.press("cross") then
			if with==1 then new_corutina("Visor de imágenes") visor() else new_corutina("PlutuX") imgini=image.load(workDir[pos_tarea].name) trabajo=files.nofile(workDir[pos_tarea].name) plutuX() end
		end
	elseif files.ext(workDir[pos_tarea].name)=="mp3" or files.ext(workDir[pos_tarea].name)=="wav" or 
		files.ext(workDir[pos_tarea].name)=="at3" or files.ext(workDir[pos_tarea].name)=="bgm" then
		if with>2 then with=1 end
		if with<1 then with=2 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"Reproducir",0.5,negro,0)
		screen.print(tareas_c[2].x,tareas_c[2].y,"Lista de reproducción",0.5,negro,0)
		if controls.press("cross") then
			if with==1 then 
				new_corutina("Reproductor") reproductor() 
			else 
				if #song<13 then
					new_corutina("PlayList")
					table.insert(song,workDir[pos_tarea].name) 
					minimizado=true
					if type(playlist)=="userdata" then 
						if not playlist:playing() then playlist:play(4) end 
					else 
						playlist=sound.load(song[song_a]) 
					end
					opndcon=false
					tareas_e=false
				else
					os.message("Lista de reproducción completa")
				end
			end
		end
	elseif files.ext(workDir[pos_tarea].name)=="pbp" then
		if with!=1 then with=1 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"PSP Game",0.6,negro,0)
		if controls.press("cross") then	
			os.runeboot(workDir[pos_tarea].name)
		end
	elseif files.ext(workDir[pos_tarea].name)=="iso" or files.ext(workDir[pos_tarea].name)=="cso" then
		if with!=1 then with=1 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"ISO/CSO Game",0.6,negro,0)
		if controls.press("cross") then	
			os.runiso(workDir[pos_tarea].name)
		end
	elseif files.ext(workDir[pos_tarea].name)=="psx" then
		if with!=1 then with=1 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"PSX Launcher",0.6,negro,0)
		if controls.press("cross") then	
			os.startPSX(workDir[pos_tarea].name)
		end
	elseif files.ext(workDir[pos_tarea].name)=="html" then
		if with>2 then with=1 end
		if with<1 then with=2 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"PSP HTML",0.6,negro,0)
		screen.print(tareas_c[2].x,tareas_c[2].y,"NotepadCode",0.6,negro,0)
		if controls.press("cross") then
			if with==1 then
				a=io.open("www/busqueda.txt","w+")
				a:write(string.gsub(workDir[pos_tarea].name,"ms0:/","file:/"))
				a:close()
				ini.write("SYSTEM/salida.ini","user",user)
				os.runeboot("ms0:/PSP/GAME/PSPEXPLORER v2/WWW/eboot.pbp")
			else
				new_corutina("NotepadCode") editor()
			end
		end
	elseif files.ext(workDir[pos_tarea].name)=="swf" then
		if with!=1 then with=1 end
		screen.print(tareas_c[1].x,tareas_c[1].y,"PSP HTML",0.6,negro,0)
		if controls.press("cross") then
			files.copy(workDir[pos_tarea].name,"ms0:/index.swf")
			a=io.open("ms0:/index.html","w+")
			a:write('<html>\n<head>\n<title>Juegos</title>\n</head>\n<br>\n<a href="file:/index.swf" mce_href="file:/index.swf">Iniciar Juego</a>\n</body>\n</html>')
			a:close()

			a=io.open("www/busqueda.txt","w+")
			a:write("file:/index.html")
			a:close()
			ini.write("SYSTEM/salida.ini","user",user)
			os.runeboot("ms0:/PSP/GAME/PSPEXPLORER v2/WWW/eboot.pbp")
		end
	else
		opndcon=false
	end


	draw.rect(tareas_c[1].x-10,tareas_c[1].y-10,130,40,negro)
end
	
	if controls.press("cross") and pos_tarea!=0 and tareas_e then
		if actual==1 then 
			if workDir[pos_tarea].directory then	
				tareas_e=false
				posicion=pos_tarea
				adelante()
			elseif files.ext(workDir[pos_tarea].name)=="satd" then 
				ruta=ini.read(workDir[pos_tarea].name,"Ruta","ms0:/")
				if files.exists(ruta) then
					if #files.list(ruta)>0 then
						workDir=files.list(ruta)
						explorador()
					else
						vac()
					end
				end
			elseif files.ext(workDir[pos_tarea].name)=="app" then 
				appa=tonumber(ini.read(workDir[pos_tarea].name,"app","1"))
				if appa!=nil then
					runapp(appa/8-5)
				end
			else
				for i=1,#ext_suport do
					if files.ext(workDir[pos_tarea].name)==ext_suport[i] then
						opndcon=true
						tareas_e=false
					end
				end
			end
		end
	end
end

function tareas(destino)
if tareas_e then

	if controls.press("cross") then
		if actual==2 then
			porta=workDir[pos_tarea].name
			tareas_e=false
		end
		if actual==3 then
			porta=workDir[pos_tarea].name
			kill=true
			tareas_e=false
		end
		if actual==4 then   
			newcopy(porta,files.nofile(workDir[1].name)..files.nopath(porta))
			workDir=files.list(lugar)
			if kill then
				files.remove(porta)
				kill=false
			end
			tareas_e=false 
			if destino==1 then
				sistema()
			else
				explorador()
			end 
		end
		if actual==5 then 
			files.copy("APP/explorador/archivo.txt",files.nofile(workDir[pos_tarea].name).."Acceso Directo a "..files.nopath(workDir[pos_tarea].name)..".SATD")
			if not workDir[pos_tarea].directory then ini.write(files.nofile(workDir[pos_tarea].name).."Acceso Directo a "..files.nopath(workDir[pos_tarea].name)..".SATD","Ruta",files.nofile(workDir[pos_tarea].name)) end 
			if workDir[pos_tarea].directory then ini.write(files.nofile(workDir[pos_tarea].name).."Acceso Directo a "..files.nopath(workDir[pos_tarea].name)..".SATD","Ruta",workDir[pos_tarea].name.."/") end 
			tareas_e=false
			workDir=files.list(lugar)
			if destino==1 then
				sistema()
			else
				explorador()
			end 
		end
		if actual==6 then 
			if workDir[pos_tarea].name:find("PSPEXPLORER")!=nil then
				if os.message("Eliminar archivos del sistema puede influir en el rendimiento del equipo. ¿Deseas continuar?",1)==1 then
					if workDir[pos_tarea].directory then   
						files.remove(workDir[pos_tarea].name,true) tareas_e=false workDir=files.list(lugar)
					else   
						files.remove(workDir[pos_tarea].name) tareas_e=false workDir=files.list(lugar) 
					end
					if #workDir==0 then workDir=files.list(files.nofile(lugar))
					if destino==1 then sistema() else explorador() end  end
					if destino==1 then sistema() else explorador() end 
				end
			else
				if workDir[pos_tarea].directory then   
					files.remove(workDir[pos_tarea].name,true) tareas_e=false workDir=files.list(lugar)
				else   
					files.remove(workDir[pos_tarea].name) tareas_e=false workDir=files.list(lugar) 
				end
				if #workDir==0 then workDir=files.list(files.nofile(lugar))
				if destino==1 then sistema() else explorador() end  end
				if destino==1 then sistema() else explorador() end
			end
		end
		if actual==7 and files.ext(workDir[pos_tarea].name)!="satd" then 
			if keyboard==1 then
				newname=teclado(1,"Nombre del archivo/carpeta",25,"")
			else newname = os.osk("Nombre del archvo/carpeta","",25,1,2)
		end	
			if newname!="" then 
				if workDir[pos_tarea].directory then 
					files.rename(workDir[pos_tarea].name,files.nofile(workDir[pos_tarea].name)..newname)
				else
					files.rename(workDir[pos_tarea].name,files.nofile(workDir[pos_tarea].name)..newname.."."..files.ext(workDir[pos_tarea].name))
				end
			end
			newname=nil
			tareas_e=false
			opndcon=false
			workDir=files.list(lugar)
			if destino==1 then
				sistema()
			else
				explorador()
			end 
		end
		if actual==8 then
			nuevo=true
			tareas_e=false
		end
		if actual==9 then 
			propiedad=files.list(lugar)
			ruta_completa=propiedad[pos_tarea].name 
			cdimenh=18
			if #ruta_completa>30 then
				cdimenh=40
				ruta_completa=ruta_completa:sub(1,30).."\n"..ruta_completa:sub(31,#ruta_completa)
			end
			propi=true
			tareas_e=false
		end
	end

	draw.fillrect(tareas_c[1].x-10,tareas_c[1].y-10,200,150,blanco)

	actual=0
	for i=1,9 do
		if col(p,tareas_c[i]) then
			draw.fillrect(tareas_c[i].x-10,tareas_c[i].y,200,10,azul)
			actual=i
		end
	end

	draw.rect(tareas_c[1].x-10,tareas_c[1].y-10,200,150,gris)

	if workDir[pos_tarea].directory or files.ext(workDir[pos_tarea].name)=="satd" or files.ext(workDir[pos_tarea].name)=="app" then
		screen.print(tareas_c[1].x,tareas_c[1].y,opnd,0.6,negro,0)
	else
		screen.print(tareas_c[1].x,tareas_c[1].y,opndwith,0.6,negro,0)
	end

	screen.print(tareas_c[2].x,tareas_c[2].y,copy,0.6,negro,0)
	screen.print(tareas_c[3].x,tareas_c[3].y,cut,0.6,negro,0)

	if porta!="" then
		screen.print(tareas_c[4].x,tareas_c[4].y,paste,0.6,negro,0)
	else
		screen.print(tareas_c[4].x,tareas_c[4].y,paste,0.6,gris,0)
	end

	screen.print(tareas_c[5].x,tareas_c[5].y,accese,0.6,negro,0)
	screen.print(tareas_c[6].x,tareas_c[6].y,remo,0.6,negro,0)
	screen.print(tareas_c[7].x,tareas_c[7].y,rena,0.6,negro,0)
	screen.print(tareas_c[8].x,tareas_c[8].y,newe,0.6,negro,0)
	screen.print(tareas_c[9].x,tareas_c[9].y,dades,0.6,negro,0)

end
end

function barra_ab()
	barralateral:blit(440,24)
	if #workDir>3 then
		altura_barra=math.ceil(#workDir/3)
		for i=0,altura_barra do
			if tables[1].y==20-90*i then scroll=i end
		end

		barraexpimg:blit(460,(206/(altura_barra-1)*(scroll))+24)
	else
		barraexpimg:blit(460,24)
	end
end

function carpeta()

if files.exists(files.nofile(workDir[1].name).."Nueva Carpeta") then
	creada=1
	while files.exists(files.nofile(workDir[1].name).."Nueva Carpeta "..creada) do
		creada=creada+1
	end

	files.mkdir(files.nofile(workDir[1].name).."Nueva Carpeta "..creada)
else
	files.mkdir(files.nofile(workDir[1].name).."Nueva Carpeta")
end
end

function new_file(destino)
	if nuevo then
	
		draw.fillrect(tareas_c[1].x-10,tareas_c[1].y-10,200,90,blanco)

		elegida=0
		
		for i=1,5 do
			if col(p,tareas_c[i]) then
				elegida=i
				draw.fillrect(tareas_c[i].x-10,tareas_c[i].y,200,10,azul)
			end
		end
		
		draw.rect(tareas_c[1].x-10,tareas_c[1].y-10,200,90,gris)
		
		screen.print(tareas_c[1].x,tareas_c[1].y,fold,0.6,negro,0)
		screen.print(tareas_c[2].x,tareas_c[2].y,texte,0.6,negro,0)
		screen.print(tareas_c[3].x,tareas_c[3].y,inio,0.6,negro,0)
		screen.print(tareas_c[4].x,tareas_c[4].y,lua,0.6,negro,0) 
		screen.print(tareas_c[5].x,tareas_c[5].y,png,0.6,negro,0)

		if controls.press("cross") and not tareas_e then
			if elegida==1 then
				carpeta()
			elseif elegida==2 then
				io.open(files.nofile(workDir[1].name).."Nuevo archivo de texto.txt","w+")
			elseif elegida==3 then
				io.open(files.nofile(workDir[1].name).."Nuevo archivo de Configuracion.ini","w+")
			elseif elegida==4 then
				io.open(files.nofile(workDir[1].name).."Nuevo Script.lua","w+")
			elseif elegida==5 then
				nuevaimg=image.create(480,272,blanco)
				nuevaimg:save(files.nofile(workDir[1].name).."Nueva imagen.png")
				nuevaimg:free()
				nuevaimg=nil
			end
			
			nuevo=false
			workDir=files.list(lugar)
			if destino==1 then
				sistema()
			else
				explorador()
			end 
		end

	end
end

function propiedades(big)

	if propi then
		local size=propiedad[pos_tarea].size
		tareas_e=false
		nuevo=false
		if big then
			draw.fillrect(0,0,480,272,blanco_t)
			screen.print(240,20,"Propiedades",0.8,negro,0,"center")
			
			screen.print(240,50,"Nombre:",0.6,negro,0,"center")
			screen.print(240,65,files.nopath(propiedad[pos_tarea].name),0.6,negro,0,"center")
			
			screen.print(240,100,"Nombre:",0.6,negro,0,"center")
			if files.ext(propiedad[pos_tarea].name)!=nil then
				if files.ext(propiedad[pos_tarea].name)!="satd" then
					screen.print(240,115,tostring(files.ext(propiedad[pos_tarea].name)),0.6,negro,0,"center")
				else
					screen.print(240,115,accese,0.6,negro,0,"center")
				end
			elseif propiedad[pos_tarea].directory then 
				screen.print(240,115,fold,0.6,negro,0,"center")
			end
			
			screen.print(240,150,"Tamaño:",0.6,negro,0,"center")
			screen.print(240,165,math.ceil(size/10500)/100 .." MB",0.6,negro,0,"center")
			
			screen.print(240,200,"Ruta:",0.6,negro,0,"center")
			screen.print(240,215,ruta_completa,0.6,negro,0,"center") 
		else
			draw.fillrect(112,24,342,243,blanco_t)
			screen.print(320,50,"Propiedades",0.8,negro,0,"center")
			screen.print(160,80,"Nombre: "..files.nopath(propiedad[pos_tarea].name),0.6,negro,0)
			if files.ext(propiedad[pos_tarea].name)!=nil then
				if files.ext(propiedad[pos_tarea].name)!="satd" then
					screen.print(160,120,"Tipo: "..tostring(files.ext(propiedad[pos_tarea].name)),0.6,negro,0)
				else
					screen.print(160,120,"Tipo: "..accese,0.6,negro,0)
				end
			elseif propiedad[pos_tarea].directory then 
				screen.print(160,120,"Tipo: "..fold,0.6,negro,0)
			end
			screen.print(160,160,"Tamaño: "..math.ceil(size/10500)/100 .." MB",0.6,negro,0)
			screen.print(160,200,"Ruta: "..ruta_completa,0.6,negro,0) 
		end 
	end
end

