titles = {
    pharao = nil,
    jailgames = nil,
    jgmummy = nil,
}

local sndLaugh, sndThunder
local musTitles

function titles.lightning_step1()
    pal.subpal(29,1)
    pal.subpal(4,1)
    pal.subpal(6,1)
    pal.subpal(4,1)
    pal.subpal(21,28)
    pal.subpal(22,28)
    pal.subpal(26,28)
    surf.source(titles.pharao)
    draw.surf(0,0,160,104,0,0)
    pal.subpal()

    surf.source(titles.jailgames)
    draw.surf(0,0,98,14,31,49)
end

function titles.lightning_step2()
    pal.subpal(29,28)
    pal.subpal(4,28)
    pal.subpal(6,28)
    pal.subpal(4,28)
    pal.subpal(1,28)
    pal.subpal(21,1)
    pal.subpal(22,1)
    pal.subpal(26,1)
    pal.subpal(28,1)
    surf.source(titles.pharao)
    draw.surf(0,0,160,104,0,0)
    surf.source(titles.jailgames)
    draw.surf(0,0,98,14,31,49)
    pal.subpal()
end


local dt = 0
function titles.enable()
    app.update = titles.update
    titles.pharao = surf.load("gfx/titles/pharao.gif")
    titles.jailgames = surf.load("gfx/titles/jailgames.gif")
    titles.jgmummy = surf.load("gfx/titles/jgmummy.gif")
    surf.target(surf.SCREEN)
    sndLaugh = sound.load("snd/laugh.wav")
    sndThunder = sound.load("snd/thunder.wav")
    music.play("mus/paku_title.ogg", 0)
    dt = sys.time()
end

function titles.update()
    surf.source(titles.jailgames)
    draw.surf(0,0,98,14,31,49)
    if sys.time()-dt > 4 then
        app.update = titles.step2
        dt = sys.time()
        sound.play(sndThunder)
        sound.play(sndLaugh)
    end
end

function titles.step2()
    titles.lightning_step1()
    if sys.time()-dt > 0.1 then
        app.update = titles.step3
        dt = sys.time()
    end
end

function titles.step3()
    surf.cls(28)
    if sys.time()-dt > 0.1 then
        app.update = titles.step4
        dt = sys.time()
    end
end

function titles.step4()
    titles.lightning_step2()
    if sys.time()-dt > 0.1 then
        app.update = titles.step5
        dt = sys.time()
    end
end

function titles.step5()
    surf.cls(28)
    if sys.time()-dt > 0.1 then
        app.update = titles.step6
        dt = sys.time()
    end
end

function titles.step6()
    surf.source(titles.pharao)
    draw.surf(0,0,160,104,0,0)
    surf.source(titles.jgmummy)
    draw.surf(0,0,112,22,24,45)
    --titles.lightning_step1()
    --app.update = titles.step7
end
