function menu_init()
print("MENU INIT")
    menu_sel=0
    menu_state=update_main_menu;
    abad.update = abad_nop
    game_update=update_menu
    menu_count=0
    menu_change_room=300
    menu_room=math.random(0,59)
    music.play(audio_main_song)
end

function menu_update()
    --game_update=update_game
    music.stop()
end

function draw_hab(hab,x,y)

end

function mapa_update(hab1, hab2)

end

function redraw_menu_hab()
    -- draw_hab(menu_room,16,41)
    -- draw.rectf(0,0,15*o2aX,97*o2aX,16)
    -- draw.rectf(114*o2aX,0,78*o2aX,97*o2aX,16)
    -- draw.rect(15*o2aX,40*o2aX,99*o2aX,50*o2aX,2)
    -- mapa_update(abad.hab,menu_room)

    -- for key,actor in pairs(actors) do
    --     actor:update()
    -- end
    -- imp.update()
    -- bambolla.update()
    -- cacau.update()
    -- fireball.update()
    -- switches.update()
end

function update_menu()
    menu_change_room=menu_change_room-1
    if menu_change_room==0 then
        menu_room = math.random(0,59)
        menu_change_room=300
    end

    surf.cls(16)
	draw_title(14*o2aX, 0)
    menu_state()
end

function update_options_menu()
    menu_count=menu_count+1
    local parpadeig=false
    if menu_count>=20 then
        parpadeig=true
        if menu_count>40 then menu_count=0 end
    end
    draw.rect(28*o2aX,(28+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,14)
    if (not parpadeig) then draw.rect(28*o2aX,(28+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,13) end

	arc_text("MÚSICA", 30*o2aX, 30*o2aX, 14); arc_text(music.enabled() and "SI" or "NO",91*o2aX,30*o2aX,15)
    arc_text("SÓ", 30*o2aX, 40*o2aX, 14);     arc_text(sound.enabled() and "SI" or "NO",91*o2aX,40*o2aX,15)
    arc_text("REDEFINIR TECLAT", 30*o2aX, 50*o2aX, 14)
    arc_text("REDEFINIR GAMEPAD", 30*o2aX, 60*o2aX, 14)
    arc_text("TORNAR", 30*o2aX, 70*o2aX, 14)

    if key.press(keyShoot) or key.press(key.SPACE) or pad.press(pad.A) or pad.press(btnShoot) then
        if menu_sel==0 then
            music.enabled(not music.enabled())
            if music.enabled() then music.play(audio_main_song) end
        elseif menu_sel==1 then
            sound.enabled(not sound.enabled())
        elseif menu_sel==2 then
            menu_count=0
            menu_state=update_redefine_keys_menu
        elseif menu_sel==3 then
            menu_count=0
            menu_state=update_redefine_pad_menu
        else
            menu_count=0
            menu_sel=0
            menu_state=update_main_menu
        end
    elseif key.press(keyDown) or key.press(key.DOWN) or pad.press(pad.DOWN) or pad.press(btnDown) then
        menu_sel=menu_sel+1
        if menu_sel==5 then menu_sel=0 end
    elseif key.press(keyUp) or key.press(key.UP) or pad.press(pad.UP) or pad.press(btnUp) then
        menu_sel=menu_sel-1
        if menu_sel==-1 then menu_sel=4 end
    elseif key.press(key.ESCAPE) or pad.press(btnPause) then
        menu_count=0
        menu_sel=0
        menu_state=update_main_menu
    end
end

function update_main_menu()
    menu_count=menu_count+1
    local parpadeig=false
    if menu_count>=20 then
        parpadeig=true
        if menu_count>40 then menu_count=0 end
    end
    draw.rect(28*o2aX,(18+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,14)
    if (not parpadeig) then draw.rect(28*o2aX,(18+(10*menu_sel))*o2aX,73*o2aX,9*o2aX,13) end
    arc_text("JUGAR", 54*o2aX, 20*o2aX, 14)
    arc_text("OPCIONS", 50*o2aX, 30*o2aX, 14)
    --if (not parpadeig or menu_sel~=2) then text("REDEFINIR GAMEPAD", 30, 32, 13) end

    redraw_menu_hab()

    if key.press(keyShoot) or key.press(key.SPACE) or pad.press(pad.A) or pad.press(btnShoot) then
        if menu_sel==0 then
            game_exit()
            game_init()
            fade.fadeoutin()
        elseif menu_sel==1 then
            menu_count=0
            menu_sel=0
            menu_state=update_options_menu--update_redefine_keys_menu;
        else
            menu_count=0
            menu_state=update_redefine_pad_menu
        end
    elseif key.press(keyDown) or key.press(key.DOWN) or pad.press(pad.DOWN) or pad.press(btnDown) then
        menu_sel=menu_sel+1
        if menu_sel==2 then menu_sel=0 end
    elseif key.press(keyUp) or key.press(key.UP) or pad.press(pad.UP) or pad.press(btnUp) then
        menu_sel=menu_sel-1
        if menu_sel==-1 then menu_sel=1 end
    elseif key.press(key.ESCAPE) or pad.press(btnPause) then
        sys.quit()
    end
end

function update_redefine_keys_menu()
    if key.press(key.ESCAPE) then
        menu_count = 0
        menu_state = update_options_menu;
    end
    if menu_count==0 then
        arc_textB("PULSA TECLA PER A AMUNT", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 1
            keyUp=k
            config.key("keyup", keyUp)
        end
    elseif menu_count==1 then
        arc_textB("PULSA TECLA PER A AVALL", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 2
            keyDown=k
            config.key("keydown", keyDown)
        end
    elseif menu_count==2 then
        arc_textB("PULSA TECLA PER A ESQUERRA", 14*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 3
            keyLeft=k
            config.key("keleft", keyLeft)
        end
    elseif menu_count==3 then
        arc_textB("PULSA TECLA PER A DRETA", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 4
            keyRight=k
            config.key("keyright", keyRight)
        end
    elseif menu_count==4 then
        arc_textB("PULSA TECLA PER A BOTAR", 20*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 5
            keyJump=k
            config.key("keyjump", keyJump)
        end
    elseif menu_count==5 then
        arc_textB("PULSA TECLA PER A DISPAR", 18*o2aX, 24*o2aX, 13)
        local k = key.press();
        if k ~= 0 and k~=key.ESCAPE then
            menu_count = 0
            menu_state = update_options_menu;
            keyShoot=k
            config.key("keyshoot", keyShoot)
        end
    end
end

function update_redefine_pad_menu()
    if key.press(key.ESCAPE) then
        menu_count = 0
        menu_state = update_options_menu;
    end
    if menu_count==0 then
        arc_textB("PULSA BOTÓ PER A AMUNT", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 1
            btnUp=btn
            config.key("btnup", btnUp)
        end
    elseif menu_count==1 then
        arc_textB("PULSA BOTÓ PER A AVALL", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 2
            btnDown=btn
            config.key("btndown", btnDown)
        end
    elseif menu_count==2 then
        arc_textB("PULSA BOTÓ PER A ESQUERRA", 16*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 3
            btnLeft=btn
            config.key("keleft", btnLeft)
        end
    elseif menu_count==3 then
        arc_textB("PULSA BOTÓ PER A DRETA", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 4
            btnRight=btn
            config.key("btnright", btnRight)
        end
    elseif menu_count==4 then
        arc_textB("PULSA BOTÓ PER A BOTAR", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 5
            btnJump=btn
            config.key("btnjump", btnJump)
        end
    elseif menu_count==5 then
        arc_textB("PULSA BOTÓ PER A DISPAR", 20*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 6
            btnShoot=btn
            config.key("btnshoot", btnShoot)
        end
    elseif menu_count==6 then
        arc_textB("PULSA BOTÓ PER A GPS ARRERE", 14*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 7
            btnCycle1=btn
            config.key("btncycle1", btnCycle1)
        end
    elseif menu_count==7 then
        arc_textB("PULSA BOTÓ PER A GPS AVANT", 14*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 8
            btnCycle2=btn
            config.key("btncycle2", btnCycle2)
        end
    elseif menu_count==8 then
        arc_textB("PULSA BOTÓ PER A PAUSA", 22*o2aX, 24*o2aX, 13)
        local btn = pad.press();
        if btn ~= -1 then
            menu_count = 0
            menu_state = update_options_menu;
            btnPause=btn
            config.key("btnpause", btnPause)
        end
    end
end
