function chooseCarpetMenu(originalBGImage)
	local ovImg=Image.load("images/menus/skin_choose_carpet.png")
	local nbCarpets=nil
	local i=0
	local prop=nil
	local currentSelection=1
	local names=nil
	
	if carpetPreviews==nil then
		-- loading
		screen:blit(0, 0, originalBGImage, true)
		alert("loading")
		--screen.waitVblankStart()
		--screen.flip()
		
		carpetPreviews={}
		names=getFolderList("images/carpets")
		nbCarpets=tableLength(names)
		for i=1,nbCarpets do
			carpetPreviews[i]={}
			carpetPreviews[i]["num"]=i
			--fdebug("loading image for carpetPreviews["..i.."][\"previewImage\"]")
			carpetPreviews[i]["previewImage"]=Image.load("images/carpets/"..names[i].."/preview.png")
			--fdebug("image is images/carpets/"..names[i].."/preview.png")
			prop=loadPropertiesFile("images/carpets/"..names[i].."/carpet.properties")
			carpetPreviews[i]["author"]=prop["author"]
			carpetPreviews[i]["name"]=prop["name"]
			carpetPreviews[i]["description"]=prop["description"]
			carpetPreviews[i]["folderName"]=names[i]
			carpetPreviews[i]["previewBGColor"]=prop["previewBGColor"]
			carpetPreviews[i]["namesColorIn"]=prop["namesColorIn"]
			carpetPreviews[i]["namesColorOut"]=prop["namesColorOut"]
			carpetPreviews[i]["namesBicolor"]=prop["namesBicolor"]
			carpetPreviews[i]["namesFontName"]=prop["namesFontName"]
			carpetPreviews[i]["namesFontW"]=prop["namesFontW"]*1.5
			carpetPreviews[i]["namesFontH"]=prop["namesFontH"]*1.5
			carpetPreviews[i]["namesFontSpacing"]=prop["namesFontSpacing"]*1.5
			carpetPreviews[i]["previewText"]=prop["previewText"]
			carpetPreviews[i]["previewTextPlacementX"]=prop["previewTextPlacementX"]
			carpetPreviews[i]["previewTextPlacementY"]=prop["previewTextPlacementY"]
			carpetPreviews[i]["copyright"]=prop["copyright"]
			if USERPREF["carpet"]==names[i] then --this is what the user is using
				currentSelection=i
			end
		end
	else
		nbCarpets=tableLength(carpetPreviews)
		for i=1,nbCarpets do
			if USERPREF["carpet"]==carpetPreviews[i]["folderName"] then --this is what the user is using
				currentSelection=i
			end
		end
	end
	
	drawCarpetMenu(originalBGImage, currentSelection, nbCarpets, ovImg)
	
	
	--main loop to get the input
	while true do
		updateInputs(Controls.read())
		

		--specific inputs
		if inputFirstPress("left") or inputIsRepeating("left") then
			if currentSelection>1 then
				currentSelection=currentSelection-1
			else
				currentSelection=nbCarpets
			end
			drawCarpetMenu(originalBGImage, currentSelection, nbCarpets, ovImg)
		elseif inputFirstPress("right") or inputIsRepeating("right") then
			if currentSelection<nbCarpets then
				currentSelection=currentSelection+1
			else
				currentSelection=1
			end
			drawCarpetMenu(originalBGImage, currentSelection, nbCarpets, ovImg)
		elseif inputFirstPress("l") or inputIsRepeating("l") then
			if currentSelection>3 then --1+4
				currentSelection=currentSelection-3
			else
				currentSelection=1
			end
			drawCarpetMenu(originalBGImage, currentSelection, nbCarpets, ovImg)
		elseif inputFirstPress("r") or inputIsRepeating("r") then
			if currentSelection<nbCarpets-2 then
				currentSelection=currentSelection+3
			else
				currentSelection=nbCarpets
			end
			drawCarpetMenu(originalBGImage, currentSelection, nbCarpets, ovImg)
		end
		
		--selection
		if inputFirstPress("cross") or inputFirstPress("circle") then
			alert("loading...")
			selectCarpet(carpetPreviews[currentSelection]["folderName"])
			return "changed"
		end
		
			
		--usual inputs
		if inputFirstPress("start") then
			return "quitMenu"
		end
		
		if inputFirstPress("square") then
			return "cancel"
		end
		
		
	end
end

function drawCarpetMenu(originalBGImage, currentSelection, nbCarpets, ovImg)
	local xref=90
	local yref=45
	screen:blit(0, 0, originalBGImage, true)
	screen:blit(0, 0, ovImg, true)
	
	screen:fillRect(xref-9, yref+13, 318, 100, carpetPreviews[currentSelection]["previewBGColor"])
	screen:blit(xref, yref+20, carpetPreviews[currentSelection]["previewImage"], true)
	printBicolor(screen, xref+273, yref, currentSelection.."/"..nbCarpets, black, white)
	printBicolor(screen, xref, yref, carpetPreviews[currentSelection]["name"].."   by "..carpetPreviews[currentSelection]["author"], black, white)
	
	if carpetPreviews[currentSelection]["namesBicolor"] then
		printU(screen, carpetPreviews[currentSelection]["previewTextPlacementX"], carpetPreviews[currentSelection]["previewTextPlacementY"], carpetPreviews[currentSelection]["previewText"], carpetPreviews[currentSelection]["namesColorIn"], true, carpetPreviews[currentSelection]["namesFontName"], carpetPreviews[currentSelection]["namesFontW"], carpetPreviews[currentSelection]["namesFontH"], carpetPreviews[currentSelection]["namesFontSpacing"], carpetPreviews[currentSelection]["namesColorOut"])
	else
		printU(screen, carpetPreviews[currentSelection]["previewTextPlacementX"], carpetPreviews[currentSelection]["previewTextPlacementY"], carpetPreviews[currentSelection]["previewText"], carpetPreviews[currentSelection]["namesColorIn"], true, carpetPreviews[currentSelection]["namesFontName"], carpetPreviews[currentSelection]["namesFontW"], carpetPreviews[currentSelection]["namesFontH"], carpetPreviews[currentSelection]["namesFontSpacing"])
	end	
	
	printU(screen, xref, yref+185, carpetPreviews[currentSelection]["copyright"], white, false, "default", 8, 8, 8, black)
	printWithCR(screen, xref, yref+121, carpetPreviews[currentSelection]["description"], white, 8, black)
	
	
	screen.flip()
	screen.waitVblankStart()
end







