function shuffle(atable)
	--will hold the elements in a new random order
	--debug("in function shuffle")
	local alocaltable={}
	local length=tableLength(atable)
	--debug("length="..length)
	local currentLocalIndex=1
	local candidat=0
	local i=0
	
	if length==0 then
		return alocaltable
	end
	
	--debug("nulling alocaltable")
	--might be not useful
	for i=1,length do
		alocaltable[i]=nil
	end
	--debug("done")
	
	--debug("getting random number")
	candidat=random(1, length)
	--debug("got "..candidat)
	while currentLocalIndex<=length do
		if atable[candidat]~=nil then --atable[candidat] is not null
			--debug("can swith")
			--switch
			alocaltable[currentLocalIndex]=atable[candidat]
			atable[candidat]=nil
			currentLocalIndex=currentLocalIndex+1
			--debug("switching done")
			candidat=math.random(1,length)
			--debug("new candidat is "..candidat)
		else --atable[candidat] is null, searching next candidat
			--debug("cannot switch, choosing next")
			if candidat==length then
				candidat=1
			else
				candidat=candidat+1
			end
			--debug("chose "..candidat)
		end
	end
	
	length=nil
	currentLocalIndex=nil
	candidat=nil
	return alocaltable
	
end

--load a java style .properties file
--problem:
--had to take string.len(line)-1 because the \n char is interpreted as a music note symbol
--so you HAVE TO put a new line after the last property in the file
function loadPropertiesFile(filename)
	local properties={}
	local indice=nil
	local key=nil
	local value=nil
	for line in io.lines(filename) do 
		if matchString(line, "^#")==nil then --don't take comments into account
			if string.len(line)>2 then --do not take emty lines into account (at least 3 chars "a=b")
 				indice=string.find(line, "=", 1, true)
 				key=string.sub(line, 1, indice-1)
 				value=string.sub(line, indice+1, string.len(line)-1)
 				if value~=nil and value~="" then
	 				value=replaceNewLineChars(value)
	 			end
 				--fdebug(key.." = "..value)
 				--be aware of the type if it is a boolean
 				if value=="true" then
 					value=true
 				elseif value=="false" then
 					value=false
 				else
	 				local number=tonumber(value)
	 				--be aware of the type if it represents a number
	 				if number~=nil then
	 					value=number
	 				elseif matchString(value, "^%d+%,%d+%,%d+%,%d+")~=nil or matchString(value, "^%d+%,%d+%,%d+")~=nil then 
		 				--be aware of the type if it represents a color (xx,xx,xx,xx or xx,xx,xx)
		 				--enableDebug()
		 				--debug("value = "..value)
		 				local i=string.find(value, ",", 1, true) --1st ","
		 				--debug("1st , = "..i)
		 				local R=tonumber(string.sub(value, 1, i-1))
		 				--debug("R = "..R)
		 				value=string.sub(value, i+1)
		 				--debug("value = "..value)
		 				i=string.find(value, ",", 1, true) --2nd ","
		 				--debug("2nd , = "..i)
		 				local G=tonumber(string.sub(value, 1, i-1))
		 				--debug("G = "..G)
		 				value=string.sub(value, i+1)
		 				--debug("value = "..value)
		 				if matchString(value, "^%d+%,%d+")~=nil then --alpha is provided
		 					--debug("alpha found")
			 				i=string.find(value, ",", 1, true) --3rd ","
			 				--debug("3rd , = "..i)
			 				local B=tonumber(string.sub(value, 1, i-1))
			 				--debug("B = "..B)
			 				local A=tonumber(string.sub(value, i+1))
			 				--debug("A = "..A)
		 					value=Color.new(R,G,B,A)
		 				else -- alpha is not provided
		 					--debug("no alpha found")
		 					local B=tonumber(value)
		 					--debug("B = "..B)
		 					value=Color.new(R,G,B)
		 				end
		 				--disableDebug()
	 				end
	 			end
	 			properties[key]=value
 			end
		end
	end
	return properties
end

function alert(message, timeInVBlanks)
	local width=stringSize(message)*8
	--local height=8
	local fontposx=240 - width/2
	local fontposy=132
	if timeInVBlanks==nil then
		timeInVBlanks=GAMEPREF["alertMessagesTime"]
	end

	--TODO: doesn't work on the psp, find how to do it
	--screenBuffer=screen --Image.createEmpty(480, 272)
	screen.waitVblankStart()
	screen.flip()
	--screen:blit(0, 0, screenBuffer, false)
	
	

	--we don't like odd (=impair??) numbers
	--width=width+math.mod(width,2)
	--height=height-math.mod(height,2)-2
	
	--screen=screenBuffer
	
	--draw a white box around the text, 5px at each side of the text
	screen:fillRect(fontposx - 5, fontposy - 5, width+10, 18, white)
	screen:fillRect(fontposx - 4, fontposy - 4, width+8, 16, black)
	screen:print(fontposx, fontposy, message, white)
	screen.flip()
	screen.waitVblankStart(timeInVBlanks) --show it at least that long

end

function capitalize(aString)
	return string.upper( string.sub( aString, 1, 1 ) )..string.sub( aString, 2 )
end

function replaceCRsWithSpaces(str)
	local i=1
	local num=0
	local ret=""
 	while true do
 		num=string.find(str, "\n")
 		if num~=nil then --found
 			ret=ret.." "..string.sub (str, 1, num-1)
 			str=string.sub(str, num+1)
 			i=i+1
 		else
 			ret=ret.." "..str
 			break
 		end
 	end
 	return string.sub(ret, 2)--removes the first space
end

function printBicolor(target, x, y, message, colorOut, colorIn)
	target:print(x+1, y, message, colorOut)
	target:print(x-1, y, message, colorOut)
	target:print(x, y+1, message, colorOut)
	target:print(x, y-1, message, colorOut)
	target:print(x, y, message, colorIn)
end

--bug: wrong behaviour with str containing "\n\n"
function printWithCR(target, x, y, str, color, space, colorOut)
	local i=1
	local tmp=nil
	--for sub in string.gmatch(str, "[^\n]*") do
	--	if math.mod(i, 2)==1 then
   	--		screen:print(x, y+(math.floor((i-1)/2)*space), "- "..sub, white)
   	--	end
   	--	i=i+1
 	--end
 	while true do
 		num=string.find(str, "\n")
 		if num~=nil then --found
 			tmp=string.sub (str, 1, num-1)
 			if colorOut~=nil then
 				target:print(x+1, y+(i-1)*space, tmp, colorOut)
 				target:print(x-1, y+(i-1)*space, tmp, colorOut)
 				target:print(x, y+(i-1)*space+1, tmp, colorOut)
 				target:print(x, y+(i-1)*space-1, tmp, colorOut)
 			end
 			target:print(x, y+(i-1)*space, tmp, color)
 			str=string.sub(str, num+1)
 			i=i+1
 		else
 			if colorOut~=nil then
 				target:print(x+1, y+(i-1)*space, str, colorOut)
 				target:print(x-1, y+(i-1)*space, str, colorOut)
 				target:print(x, y+(i-1)*space+1, str, colorOut)
 				target:print(x, y+(i-1)*space-1, str, colorOut)
 			end
 			screen:print(x, y+(i-1)*space, str, color)
 			break
 		end
 	end
end

--fontname and following params can be null
--withCR: takes carriage returns into account
--TODO: withCR param
--fontname has to be lowercase, and for that, file extension must be lowercase (ttf, not TTF)
--takes high ressources if colorOut~=nil
function printU(target, x, y, str, color, withCR, fontName, fontW, fontH, lineSpacing, colorOut)
	if fontName==nil then
		fontName="default"
		fontW=8
		fontH=8
		lineSpacing=8
	else
		--fontName=string.lower(fontName)
	end
	--enableFDebug()
	--initFDebug("debug.log")
	--fdebug("fontName is "..fontName)
	--fdebug("W="..fontW..", H="..fontH)
	
	--for key,value in pairs(FONTS) do
	--	fdebug("FONTS[\""..key.."\"]")
	--end
	
	--prepare font
	--fdebug("font called is "..fontName)
	FONTS[fontName]:setPixelSizes(fontW,fontH)
		
	local i=1
	local num=0
 	while true do
 		num=string.find(str, "\n")
 		if num~=nil then --found
 			local line=string.sub (str, 1, num-1)
 			if colorOut~=nil then
 				target:fontPrint(FONTS[fontName], x+1, y+(i-1)*lineSpacing, line, colorOut)
 				target:fontPrint(FONTS[fontName], x-1, y+(i-1)*lineSpacing, line, colorOut)
 				target:fontPrint(FONTS[fontName], x, y+1+(i-1)*lineSpacing, line, colorOut)
 				target:fontPrint(FONTS[fontName], x, y-1+(i-1)*lineSpacing, line, colorOut)
 			end
 			target:fontPrint(FONTS[fontName], x, y+(i-1)*lineSpacing, line, color)
 			str=string.sub(str, num+1)
 			i=i+1
 		else
 			if colorOut~=nil then
 				target:fontPrint(FONTS[fontName], x+1, y+(i-1)*lineSpacing, str, colorOut)
 				target:fontPrint(FONTS[fontName], x-1, y+(i-1)*lineSpacing, str, colorOut)
 				target:fontPrint(FONTS[fontName], x, y+1+(i-1)*lineSpacing, str, colorOut)
 				target:fontPrint(FONTS[fontName], x, y-1+(i-1)*lineSpacing, str, colorOut)
 			end
 			target:fontPrint(FONTS[fontName], x, y+(i-1)*lineSpacing, str, color)
 			break
 		end
 	end
end

function random(min, max)
	--not local, used to get different random numbers 
	--even if called during the same second
	if randomChanger==nil then
		randomChanger=333
	else
		randomChanger=randomChanger+1+max
		if randomChanger>3333 then
			randomChanger=math.floor(randomChanger/3)
		end
	end
	--on the psp, os.time() returns a table, and not a timestamp
	--so I had to do that ugly thing
	--behaviour is quite good
	local date=os.date("*t", os.time())
	local rseed=1000*(date.sec+60*(date.min+60*date.hour))+123456789
	math.randomseed(rseed+math.mod(rseed, randomChanger))
	return math.random(min, max)
end

function copyScreen()
	local x=0
	local y=0
	local img=Image.createEmpty(480,272)
	local color=nil
	for x=0,479 do
		for y=0,271 do
			color=screen:pixel(x, y)
			img:pixel(x, y, color)
		end
	end
	return img
end

-- takes a table with "holes" in it, i.e. nil values
-- maxHoleSize tells how far this function will look to find more
-- values after a hole. if its value is 10 for example, the table
-- will be allowed to have 10 successive nil values and then
-- other values. if it has 11 nils or more, the function will stop
-- looking and every following values will be dropped
function repackTable(aPerforatedTable, maxHoleSize)
	local packedTable={}
	local packedIndice=1 --pointer on the packed table
	local perforatedIndice=1 --pointer on the perforated table
	local holeSize=0 --current hole size (num of successive nils found)
	
	while holeSize<=maxHoleSize do
		if aPerforatedTable[perforatedIndice]~=nil then
			packedTable[packedIndice]=aPerforatedTable[perforatedIndice]
			packedIndice=packedIndice+1
			perforatedIndice=perforatedIndice+1
			holeSize=0
		else --nil value in aperf table
			perforatedIndice=perforatedIndice+1
			holeSize=holeSize+1
		end
	end	
	enableDebug()
	--debug("repack ok")
	disableDebug()
	return packedTable
end



function snapshot(img)
	--global, so that when snapshots are taken during the same second
	--there is no overwrite
	if snapshotCounter==nil then
		snapshotCounter=1
	else
		snapshotCounter=snapshotCounter+1
	end
	
	
--	screen.waitVblankStart()
--	screen.flip()
--	printBicolor(screen, 10, 260, "getting snapshot please wait...", black, white)

	
	local snap
	if img==nil then
		snap=copyScreen()
	else
		snap=img
	end
	System.createDirectory(getSnapshotFolder())
	--local photoContent=getFolderList(GAMEPREF["photoFolder"])
	local date=os.date("%Y%m%d%H%M%S")
	local filename="snapshot-"..date.."-"..snapshotCounter..".jpg"
	fdebug("saving snapshot to '"..getSnapshotFolder().."/"..filename.."'")
	
	snap:save(getSnapshotFolder().."/"..filename)
end

--replaces "\n" with the CR char in strings
function replaceNewLineChars(str)
	--fdebug("replaceNewLineChars(\""..str.."\")")
	local ret=""
	local i=1
	while true do
 		num=string.find(str, "\\n")
 		if num~=nil then --found
 			--target:print(x, y+(i-1)*space, string.sub (str, 1, num-1), color)
 			ret=ret..string.sub (str, 1, num-1).."\n"
 			str=string.sub(str, num+2)
 			i=i+1
 		else
 			ret=ret..str
 			break
 		end
 	end
 	--fdebug(
 	return ret
end


















