function skinMenu(originalBGImage)
	local ovImg=Image.load("images/menus/skin_menu.png")
	
	local ITTHEMES=1
	local ITCARDS=2
	local ITCARPETS=3
	local ITCURSORS=4
	local ITAVATARS=5
	local ITP1P2P3P4=6
	local ITSPEECHBUBBLES=7
	local ITICONS=8
	local ITSTATESNUMSRANKS=9
	
	local skinMenuItems={}
	skinMenuItems[ITTHEMES]="Themes"
	skinMenuItems[ITCARDS]="Cards"
	skinMenuItems[ITCARPETS]="Carpets"
	skinMenuItems[ITCURSORS]="Cursors"
	skinMenuItems[ITAVATARS]="Avatars"
	skinMenuItems[ITP1P2P3P4]="  P1  P2  P3  P4"
	skinMenuItems[ITSPEECHBUBBLES]="Speech Bubbles"
	skinMenuItems[ITICONS]="Icons"
	skinMenuItems[ITSTATESNUMSRANKS]="  States  Nums  Ranks"
	skinMenuItems["unavailable"]={}
	
	--local iconsItems={"states", "nums", "ranks"}
		
	local nbOptions=tableLength(skinMenuItems)
	local currentSelection=1
	local x=138+10
	local y=62+10
	
	local playerSelection=1
	local iconsSelection=1
	
	while true do
		screen:blit(0, 0, originalBGImage, true)
		screen:blit(0, 0, ovImg, true)
		updateInputs(Controls.read())
		
		if inputFirstPress("down") or inputIsRepeating("down") then
			if currentSelection==ITP1P2P3P4-1 then
				currentSelection=ITP1P2P3P4+1
			elseif
				currentSelection==ITICONS then
				currentSelection=ITTHEMES
			else
				currentSelection=math.mod(currentSelection, nbOptions)+1
			end
		elseif inputFirstPress("up") or inputIsRepeating("up") then
			if currentSelection==ITP1P2P3P4+1 then
				currentSelection=ITP1P2P3P4-1
			elseif currentSelection==ITTHEMES then
				currentSelection=ITICONS
			else
				currentSelection=currentSelection-1
				if currentSelection==0 then
					currentSelection=nbOptions
				end
			end
		elseif inputFirstPress("left") or inputIsRepeating("left") then
			if currentSelection==ITAVATARS then
				if playerSelection>1 then
					playerSelection=playerSelection-1
				end
			elseif currentSelection==ITICONS then
				if iconsSelection>1 then
					iconsSelection=iconsSelection-1
				end
			end
		elseif inputFirstPress("right") or inputIsRepeating("right") then
			if currentSelection==ITAVATARS then
				if playerSelection<4 then
					playerSelection=playerSelection+1
				end
			elseif currentSelection==ITICONS then
				if iconsSelection<3 then
					iconsSelection=iconsSelection+1
				end
			end
		end
		
		if inputFirstPress("r") then
			currentSelection=nbOptions
		end
		if inputFirstPress("l") then
			currentSelection=1
		end
		
		--selection of an item
		if inputFirstPress("cross") then
			if environment=="pc" then --because it goes too fast it's annoying
				screen.waitVblankStart(20)
			end
			if currentSelection==ITCARPETS then
				FONTS=loadFonts() --hot installation of skins
				local carpetchoice=chooseCarpetMenu(originalBGImage) --quitMenu cancel changed
				if carpetchoice=="quitMenu" then
					freeSkinItems()
					return "quitMenu"
				elseif carpetchoice=="changed" then
					savePreferences()
					alert("background changed!", 30)
					prepareGameBGBuffer()
				end
			elseif currentSelection==ITAVATARS then
				local avatarchoice=chooseAvatarMenu(originalBGImage, playerSelection) --quitMenu cancel changed
				if avatarchoice=="quitMenu" then
					freeSkinItems()
					return "quitMenu"
				elseif avatarchoice=="changed" then
					savePreferences()
					alert("avatar for player "..playerSelection.." changed!", 30)
					prepareGameBGBuffer()
				end
			elseif currentSelection==ITCARDS then
				local cardsChoice=chooseCardsMenu(originalBGImage) --quitMenu cancel changed
				if cardsChoice=="quitMenu" then
					freeSkinItems()
					return "quitMenu"
				elseif cardsChoice=="changed" then
					savePreferences()
					alert("cards changed!", 30)
					--prepareGameBGBuffer()
				end
			elseif currentSelection==ITCURSORS then
				local cursorsChoice=chooseCursorMenu(originalBGImage) --quitMenu cancel changed
				if cursorsChoice=="quitMenu" then
					freeSkinItems()
					return "quitMenu"
				elseif cursorsChoice=="changed" then
					savePreferences()
					alert("cursor changed!", 30)
					--prepareGameBGBuffer()
				end
			elseif currentSelection==ITSPEECHBUBBLES then
				local bubbleschoice=chooseBubblesMenu(originalBGImage) --quitMenu cancel changed
				if bubbleschoice=="quitMenu" then
					freeSkinItems()
					return "quitMenu"
				elseif bubbleschoice=="changed" then
					savePreferences()
					alert("speech bubbles changed!", 30)
					--prepareGameBGBuffer()
				end
			elseif currentSelection==ITTHEMES then
				FONTS=loadFonts() --hot installation of skins
				local themechoice=chooseThemeMenu(originalBGImage) --quitMenu cancel changed
				if themechoice=="quitMenu" then
					freeSkinItems()
					return "quitMenu"
				elseif themechoice=="changed" then
					savePreferences()
					alert("theme loaded!", 30)
					prepareGameBGBuffer()
				end
			elseif currentSelection==ITICONS then
				if iconsSelection==1 then --states icons
					local statesChoice=chooseStatesIconsMenu(originalBGImage) --quitMenu cancel changed
					if statesChoice=="quitMenu" then
						freeSkinItems()
						return "quitMenu"
					elseif statesChoice=="changed" then
						savePreferences()
						alert("state icons changed!", 30)
						--prepareGameBGBuffer()
					end
				elseif iconsSelection==2 then --playersNum icons
					local playersNumChoice=choosePlayersNumIconsMenu(originalBGImage) --quitMenu cancel changed
					if playersNumChoice=="quitMenu" then
						freeSkinItems()
						return "quitMenu"
					elseif playersNumChoice=="changed" then
						savePreferences()
						alert("players number icons changed!", 30)
						--prepareGameBGBuffer()
					end
				elseif iconsSelection==3 then --ranks icons
					local ranksChoice=chooseRanksIconsMenu(originalBGImage) --quitMenu cancel changed
					if ranksChoice=="quitMenu" then
						freeSkinItems()
						return "quitMenu"
					elseif ranksChoice=="changed" then
						savePreferences()
						alert("rank icons changed!", 30)
						--prepareGameBGBuffer()
					end
				end
			end
		end
		
		
		
		
		----drawing---
		local i=0
		for i=1,nbOptions do
			if i==currentSelection then
				if i==ITAVATARS then --on the "P1 P2.." line main_selector_title
					screen:blit(x-10, y-1+(i-1)*15, menuImages["main_selector"], true)
					if playerSelection==1 then
						screen:print(x+8, y+i*15, "[  ]", red)
					elseif playerSelection==2 then
						screen:print(x+40, y+i*15, "[  ]", red)
					elseif playerSelection==3 then
						screen:print(x+72, y+i*15, "[  ]", red)
					elseif playerSelection==4 then
						screen:print(x+104, y+i*15, "[  ]", red)
					end
				elseif i==ITICONS then --on the "states nums"
					screen:blit(x-10, y-1+(i-1)*15, menuImages["main_selector"], true)
					if iconsSelection==1 then
						screen:print(x+8, y+i*15, "[      ]", red)
					elseif iconsSelection==2 then
						screen:print(x+72, y+i*15, "[    ]", red)
					elseif iconsSelection==3 then
						screen:print(x+120, y+i*15, "[     ]", red)
					end
				else
					screen:blit(x-10, y-1+(i-1)*15, menuImages["main_selector"], true)
				end
			end
			
			
			
			
			
			if skinMenuItems["unavailable"][i] then
				screen:print(x, y+(i-1)*15, skinMenuItems[i], grey)
			else
				screen:print(x, y+(i-1)*15, skinMenuItems[i], white)
			end
		end
		
		if inputFirstPress("start") then
			if not modal then
				freeSkinItems()
				return "quitMenu"
			end
		end
		
		if inputFirstPress("square") then
			if not modal then
				freeSkinItems()
				return "cancel"
			end
		end
		
		screen.flip()
		screen.waitVblankStart()
	end
end

function freeSkinItems()
	carpetPreviews=nil
	avatarPreviews=nil
	cardsPreviews=nil
	bubblesPreviews=nil
	cursorsPreviews=nil
	playersNumPreviews=nil
	statesPreviews=nil
	ranksPreviews=nil
	themePreviews=nil
	GC()
end




		