-- Specifies mode (window) which we should create during the iteration
-- of the main loop.
--
-- DO NOT CHANGE NAME OF THIS VARIABLE

main_loop_target_mode = "main_menu"

-- Selects screen corresponding to the target mode.

function single_iteration()
    --local selected_screen_name = SelectedScreen()

    if main_loop_target_mode == "intro" then
        main_loop_target_mode = "time_machine"
        DoMainWindow("cutscenes/intro.lua")
        return
    end

    if main_loop_target_mode == "outro" then
        main_loop_target_mode = "main_menu"
        DoMainWindow("cutscenes/outro.lua")
        return
    end

      if main_loop_target_mode == "cutscene" then
        main_loop_target_mode = "main_menu"
        
        local cutsceneIndex = GetCutsceneIndex();
        
        if( cutsceneIndex == 2 ) then
			DoMainWindow("cutscenes/cutscene1.lua")
		end

        if( cutsceneIndex == 3 ) then
			DoMainWindow("cutscenes/cutscene2.lua")
		end

        if( cutsceneIndex == 4 ) then
			DoMainWindow("cutscenes/cutscene3.lua")
		end

        if( cutsceneIndex == 5 ) then
			DoMainWindow("cutscenes/cutscene4.lua")
		end

        if( cutsceneIndex == 6 ) then
			DoMainWindow("cutscenes/cutscene5.lua")
		end
	if( cutsceneIndex == 7 ) then
			DoMainWindow("cutscenes/cutscene6.lua")
		end

        return
    end
    -- If nothing was specified, launch the main menu.

    if main_loop_target_mode == "main_menu" then
        DoMainWindow("map_menu/show_map_menu.lua")
        return
    end

    -- Launch the time machine if desired.

    if main_loop_target_mode == "time_machine" then

        -- If the machine is complete, traverse to the outro screen instead.

        if IsGameComplete() then
           main_loop_target_mode = "outro"
           return
        end

        DoMainWindow("time_machine/show_time_machine.lua")
        return
    end

    -- If we are launching a game screen, ensure that
    -- the return will be back to the time machine.

    if main_loop_target_mode == "game_screen" then
        main_loop_target_mode = "main_menu"
        DoMainWindow("scripts/show_standard_screen.lua")
    end
end

-- Main game loop
function Main()

    -- Show individual splash screens.

	
    if exists_file( "logos/snap1.png" ) then
        DisplaySplash("", "logos/snap1.png",4000);
    end
    
    --DoMainWindow("cutscenes/intro.lua");

    if exists_file( "logos/snap2.png" ) then
        DisplaySplash("", "logos/snap2.png",2000);
    end

    if exists_file( "logos/snap3.png" ) then
        DisplaySplash("", "logos/snap3.png",2000);
    end

    -- Activate the intro menu.
	--[[
	DisplaySplash(
	"cutscenes/intro_animation_with_long_name.swf",
	"cutscenes/intro_backup.jpg",4000
	);
    --]]
    DoMainWindow("cutscenes/intro.lua");
    --DoModal("cutscenes/intro.lua")

--[[
    if IsNoProfile() then
       DoModal("scripts/profiles.lua");
    end

	DoModal("scripts/profiles.lua");
--]]
--	DoMainWindow("map_menu/show_map_menu.lua");

	--InitializeGameState()	

    -- Infinite loop for screen switching.

    while true do
        single_iteration()
    end
end


-- Return a function to be executed in a thread
return Main
