local order = {}
function OnSceneEnter()
	if s.s01_0409_tape_visit==0 then
		Talk("com_s01_0409_01_hero")
	end
    
   OldInterfaceMap.SetCuttentModule(s01_0409_tape)
   
   SetMinigameResolver(function()
        HideAllSprites()
        HideAllRegions()
        s.s01_0409_tape_solved = 1
		Talk("com_s01_0409_02_hero")
   end)    
   
   if s.s01_0409_tape_solved == 1 then
     HideAllSprites()
     HideAllRegions()
   else 
	 ShowOneSprite(24)
   end		
end

for i=0, 24 do
    order[i] = 24 - i  
end

function HideAllSprites()
    for i=0, 24 do
        states["spr_s01_0409_tape"..i] = 0
    end
end

function HideAllRegions()
    for i=0, 24 do
        states["reg_s01_0409_tape"..i] = 1
    end
end

function ShowOneRegion(endNumber)
    HideAllRegions()
    for i=0, endNumber do
        states["reg_s01_0409_tape"..i] = 0
    end
end

function ShowAll()
    for i=0, 24 do
        states["reg_s01_0409_tape"..i] = 0 
    end
    s.s01_0409_tape_last = 0
    ShowOneSprite(24)
end

function ShowOneSprite(number)
     HideAllSprites()
     states["spr_s01_0409_tape"..number] = 1
     ShowOneRegion(number)
end

function SolvedEvent()
    s.s01_0409_tape_solved = 1
	Talk("com_s01_0409_02_hero")
end

function CheckSolution()
    if s.s01_0409_tape_last>24 then
       SolvedEvent()
       return true
    end
end

function OnClickElement(elemName)
    if s.s01_0409_tape_solved == 1 then
        return
    end
    
    len = string.len(elemName)
    number = tonumber(string.sub(elemName, 18,len))
    if order[s.s01_0409_tape_last] == number then
       
        ShowOneSprite(number-1)
        s.s01_0409_tape_last = s.s01_0409_tape_last + 1

		
        PlaySound("snd_reg_s01_0409_tape_break")
        
        CheckSolution()
    else
         ShowAll()
         PlaySound("snd_reg_s01_0409_tape_reset")
		 Talk("com_s01_0409_03_hero")
    end
end