--------------------------------------------------------------------------------
dofile('data/Lua/Constants.lua')
dofile('data/Lua/Utilities.lua')
dofile('data/Lua/Helpers.lua')
dofile('data/Lua/LevelHelpers.lua')
dofile('data/Lua/FunctionDispatcher.lua')
dofile('data/Lua/LevelDefaultEventHandlers.lua')
dofile('data/Lua/ReferenceCounter.lua')
dofile('data/Lua/Comics.lua')
dofile('data/Lua/screens/inventory.lua')
dofile('data/Lua/screens/menu.lua')
dofile('data/Lua/screens/ScreenManager.lua')
dofile('data/Lua/Sound.lua')
dofile('data/Lua/InteractiveHelp.lua')
dofile('data/Lua/Console.lua')
--------------------------------------------------------------------------------
function sleep(aTime)

    local sleepStartTime = os.time();
    
    while (true) do
    
        if (os.time() - sleepStartTime > aTime)
        then
            break;
        end
        
    end
end
--------------------------------------------------------------------------------
ERS_LOGO_SCREEN         = "ERS Logo Screen"
DISTRIBUTOR_LOGO_SCREEN = "Distributor Logo Screen"
TITLE_MOVIE_SCREEN      = "Title Movie Screen"
LOADING_SCREEN          = "Loading Screen"
INTRO_ONE_MOVIE_SCREEN  = "Intro one screen"
SHOW_INTRO              = true;
EXTRAS_BUILD            = false;

SHOW_MOVIE_1            = true;
SHOW_MOVIE_2            = true;
SHOW_MOVIE_3            = true;

--------------------------------------------------------------------------------
levels      = {}
levels[0]   = ''
levels[01]  = 'data/Scenes/00_Intro_Main.lua'
levels[02]  = 'data/Scenes/00_Global_Map.lua'
levels[10]  = 'data/Scenes/01_Dupens_House_Entrance_Main.lua'
levels[11]  = 'data/Scenes/01_Dupens_House_Entrance_Door.lua'
levels[20]  = 'data/Scenes/02_Dupens_House_Main.lua'
levels[21]  = 'data/Scenes/02_Dupens_House_Fireplace_HO.lua'
levels[22]  = 'data/Scenes/02_Dupens_House_Room.lua';
levels[23]  = 'data/Scenes/Minigames/02_lighter.lua';
levels[24]  = 'data/Scenes/02_Dupens_House_Corner_HO.lua';
levels[25]  = 'data/Scenes/02_Dupens_House_Sideboard_HO.lua';
levels[30]  = 'data/Scenes/03_Dupens_House_Exit_Main.lua'
levels[31]  = 'data/Scenes/03_Dupens_House_Exit_Carriage.lua'
levels[32]  = 'data/Scenes/03_Dupens_House_Exit_Carriage_HO.lua'
levels[40]  = 'data/Scenes/04_Bridge_Main.lua';
levels[41]  = 'data/Scenes/04_Bridge_Bench.lua';
levels[42]  = 'data/Scenes/Minigames/04_lamp.lua';
levels[43]  = 'data/Scenes/Minigames/04_puzzle.lua';
levels[50]  = 'data/Scenes/05_Gendarmery_Main.lua';
levels[51]  = 'data/Scenes/05_Gandarmery_Wart.lua';
levels[52]  = 'data/Scenes/05_Gandarmery_Wart_Cell1.lua';
levels[53]  = 'data/Scenes/05_Gandarmery_Wart_Cell2.lua';
levels[54]  = 'data/Scenes/05_Gandarmery_Wart_Cell3.lua';
levels[55]  = 'data/Scenes/05_Gandarmery_Wart_Cell1_HO.lua';
levels[60]  = 'data/Scenes/06_Laundry_Main.lua';
levels[61]  = 'data/Scenes/06_Laundry_Table_HO.lua';
levels[62]  = 'data/Scenes/06_Laundry_Shelves_HO.lua';
levels[63]  = 'data/Scenes/06_Laundry_Utility_Room_HO.lua'
levels[64]  = 'data/Scenes/06_Laundry2_Main.lua'
levels[65]  = 'data/Scenes/06_Laundry2_Washer.lua';
levels[66]  = 'data/Scenes/06_Laundry2_HO.lua'
levels[67] = 'data/Scenes/Minigames/06_laundry.lua'
levels[70]  = 'data/Scenes/07_Atelier_Main.lua';
levels[71]  = 'data/Scenes/07_Atelier_Table_HO.lua';
levels[72]  = 'data/Scenes/07_Atelier_Chest_HO.lua';
levels[73]  = 'data/Scenes/MiniGames/07_lock.lua';
levels[51]  = 'data/Scenes/05_Gendarmery_Table_HO.lua';
levels[80]  = 'data/Scenes/08_Bank_Main.lua';
levels[81]  = 'data/Scenes/08_Bank_Door.lua';
levels[90]  = 'data/Scenes/09_Bank_Office_Main.lua';
levels[91]  = 'data/Scenes/09_Bank_Office_Cupboard_HO.lua';
levels[92]  = 'data/Scenes/09_Bank_Office_Depositary.lua';
levels[110] = 'data/Scenes/11_Restaurant_Main.lua'
levels[111] = 'data/Scenes/11_Restaurant_MG.lua'
levels[112] = 'data/Scenes/11_Restaurant_Bar_HO.lua'
levels[120] = 'data/Scenes/12_Restaurant_Kitchen_HO.lua'
levels[121] = 'data/Scenes/Minigames/11_wine.lua'
levels[300] = 'data/Scenes/MiniGames/06_owl_lock.lua'
levels[130] = 'data/Scenes/13_Crimeplace_Street.lua'
levels[131] = 'data/Scenes/13_Crimeplace_Main.lua'
levels[132] = 'data/Scenes/13_Crimeplace_Main_HO.lua'
levels[133] = 'data/Scenes/Minigames/13_lock.lua'
levels[140] = 'data/Scenes/14_Corridor_Main.lua'
levels[141] = 'data/Scenes/Minigames/14_puzzle.lua'
levels[150] = 'data/Scenes/15_Workshop_Main.lua'
levels[151] = 'data/Scenes/15_Workshop_Floor_HO.lua'
levels[152] = 'data/Scenes/15_Workshop_Cupboard_HO.lua'
levels[153] = 'data/Scenes/15_Workshop_Room_HO.lua'
levels[160] = 'data/Scenes/16_Doctor_Main.lua'
levels[161] = 'data/Scenes/16_Doctor_Table_HO.lua'
levels[162] = 'data/Scenes/Minigames/16_lock.lua'
levels[163] = 'data/Scenes/16_Doctor_Room_HO.lua'
levels[170] = 'data/Scenes/17_Bedroom_Main.lua'
levels[171] = 'data/Scenes/17_Bedroom_Mirror_HO.lua'
levels[172] = 'data/Scenes/17_Bedroom_Fireplace_HO.lua'
levels[173] = 'data/Scenes/17_Bedroom_Microscope.lua'
levels[180] = 'data/Scenes/18_Apartment_Main.lua'
levels[181] = 'data/Scenes/18_Apartment_HO.lua'
levels[182] = 'data/Scenes/Minigames/18_lock.lua'
levels[190] = 'data/Scenes/19_Mill_Outside_Main.lua'
levels[200] = 'data/Scenes/20_Mill_Inside_HO.lua'
levels[201] = 'data/Scenes/20_Mill_Inside_Main.lua'
levels[202] = 'data/Scenes/Minigames/20_lock.lua'
levels[210] = 'data/Scenes/21_Mill_Workshop_Main.lua'
levels[211] = 'data/Scenes/21_Mill_Workshop_HO.lua'
levels[220] = 'data/Scenes/22_RiverPort_Main.lua'
levels[221] = 'data/Scenes/22_RiverPort_Deck1.lua'
levels[222] = 'data/Scenes/22_RiverPort_Deck2.lua'
levels[230] = 'data/Scenes/23_Stateroom_Main.lua'
levels[231] = 'data/Scenes/23_Stateroom_Cage_HO.lua'
levels[232] = 'data/Scenes/Minigames/23_flock.lua'
levels[240] = 'data/Scenes/24_Hold_Main.lua'
levels[241] = 'data/Scenes/24_Hold_Steps_HO.lua'
levels[242] = 'data/Scenes/24_Hold_Floor_HO.lua'
levels[250] = 'data/Scenes/25_Credits.lua';
levels[251] = 'data/Scenes/25_CreditsEx.lua';

levels[600] = 'data/Scenes/30_Empty.lua';

levels[511] = 'data/Scenes/Extras/E_01_Enter_Main.lua'
levels[521] = 'data/Scenes/Extras/E_02_Corridor_Main.lua'
levels[522] = 'data/Scenes/Extras/E_02_Corridor_Bookcase_HO.lua'
levels[523] = 'data/Scenes/Extras/Minigames/E_02_hanoi_towers.lua'
levels[531] = 'data/Scenes/Extras/E_03_Living_Room_Main.lua'
levels[532] = 'data/Scenes/Extras/Minigames/E_03_puzzle_15.lua'
levels[541] = 'data/Scenes/Extras/E_04_Bedroom_Main.lua'
levels[542] = 'data/Scenes/Extras/E_04_Bedroom_HO.lua'
levels[543] = 'data/Scenes/Extras/Minigames/E_04_assoc.lua'
levels[551] = 'data/Scenes/Extras/E_05_Toilet_Main.lua'
--------------------------------------------------------------------------------

local date = os.date('*t')
math.randomseed(os.clock() + date.sec + date.min + date.hour + date.day + date.month + date.year)
--------------------------------------------------------------------------------
function PreloadSounds()

    LoadMusic("menu_music",             "data/Music/menu_music.ogg");
    
	LoadMusic("music3",                 "data/Music/fon_3.ogg");
	LoadMusic("music4",                 "data/Music/fon_4.ogg");
	LoadMusic("music5",                 "data/Music/fon_5.ogg");
	LoadMusic("music6",                 "data/Music/fon_6.ogg");
	
	LoadMusic("music_location1",        "data/Music/fon_location1.ogg");
	LoadMusic("fon_without_rain",       "data/Music/fon_without_rain.ogg");
	LoadMusic("loading",                "data/sound/Logo_loop.ogg");
	
	LoadMusic("011_InterNoise",              "data/Music/011_InterNoise.ogg");
	LoadMusic("03_InterNoise",               "data/Music/03_InterNoise.ogg");
	LoadMusic("04_InterNoise",               "data/Music/04_InterNoise.ogg");
	LoadMusic("19_InterNoise",               "data/Music/19_InterNoise.ogg");
	LoadMusic("22_RiverPortInterNoiseDeck1", "data/Music/22_RiverPortInterNoiseDeck1.ogg");
	LoadMusic("22_RiverPortInterNoiseDeck2", "data/Music/22_RiverPortInterNoiseDeck2.ogg");
	LoadMusic("22_RiverPortInterNoiseMain",  "data/Music/22_RiverPortInterNoiseMain.ogg");
	
	LoadSound("Menu_button_click",      "data/Sound/Menu_button_click.ogg");
	LoadSound("Window_open",            "data/Sound/Opening_window.ogg");

    LoadSound("Item_found",             "data/Sound/item_found.ogg");
    LoadSound("Wrong_click",            "data/Sound/Wrong_click.ogg");
    LoadSound("Cursor_change",          "data/Sound/Cursor_change.ogg");
	LoadSound("hint",                   "data/Sound/hint.ogg");
	LoadSound("Curtains_up",            "data/Sound/Curtains_up.ogg");
	LoadSound("Curtains_open",          "data/Sound/Curtains_open.ogg");
	LoadSound("ButtonFocused",          "data/Sound/ButtonFocused.ogg");
	
	-- Locations sounds
	LoadSound("Fireplace",              "data/Sound/Fireplace.ogg");
	
	LoadSound("01_LockFix",             "data/Sound/01_LockFix.ogg")
	LoadSound("01_OpenGryphon",         "data/Sound/01_OpenGryphon.ogg")
	LoadSound("01_OpenMailbox",         "data/Sound/01_OpenMailbox.ogg")
	LoadSound("01_StepBreak",           "data/Sound/01_StepBreak.ogg")
	LoadSound("01_UseKey",              "data/Sound/01_UseKey.ogg")
	LoadSound("01_UseGlue",             "data/Sound/01_UseGlue.ogg")
	LoadSound("011_InterNoise",         "data/Sound/011_InterNoise.ogg")
	
	LoadSound("02_Fireplace",           "data/Sound/02_Fireplace.ogg")
	LoadSound("02_OpenCupboard",        "data/Sound/02_OpenCupboard.ogg")
	LoadSound("02_CandleFire",          "data/Sound/02_CandleFire.ogg")
	LoadSound("02_Gramophone",          "data/Sound/02_Gramophone.ogg")
	LoadSound("02_Lever",               "data/Sound/02_Lever.ogg")
	LoadSound("02_MG_Gather_Lighter",   "data/Sound/02_MG_Gather_Lighter.ogg")
	LoadSound("02_MG_Mechan",           "data/Sound/02_MG_Mechan.ogg")
	LoadSound("02_OpenChest",           "data/Sound/02_OpenChest.ogg")
	LoadSound("02_OpenShutter",         "data/Sound/02_OpenShutter.ogg")
	LoadSound("02_SkeletonShow",        "data/Sound/02_SkeletonShow.ogg")
	LoadSound("02_OpenLumber",          "data/Sound/02_OpenLumber.ogg")
	LoadSound("02_PanCoverMove",        "data/Sound/02_PanCoverMove.ogg")
	LoadSound("02_TakeGold",            "data/Sound/02_TakeGold.ogg")
	LoadSound("02_MG_LockClick",        "data/Sound/02_MG_LockClick.ogg")
	LoadSound("02_OpenShelf",           "data/Sound/02_OpenShelf.ogg")
	LoadSound("02_OpenShelf_2",         "data/Sound/02_OpenShelf.ogg")
	LoadSound("02_CandlePlaced",        "data/Sound/02_CandlePlaced.ogg")
	LoadSound("02_StoneFly",            "data/Sound/02_StoneFly.ogg")
	LoadSound("02_Clock",               "data/Sound/02_Clock.ogg")
	LoadSound("02_Monkey",              "data/Sound/02_Monkey.ogg")
	
	LoadSound("02_Thunder",             "data/Sound/02_Thunder.ogg")
	LoadSound("02_Horror",              "data/Sound/02_Horror.ogg")
	
	LoadSound("02_ScorpionClick",       "data/Sound/02_ScorpionClick.ogg")
	LoadSound("02_ScorpionOpen",        "data/Sound/02_ScorpionOpen.ogg")
	LoadSound("02_ReversButton",        "data/Sound/02_ReversButton.ogg")
	LoadSound("09_KeyPlaced",           "data/Sound/09_KeyPlaced.ogg")
	
	LoadSound("03_OpenCarriageDoor",    "data/Sound/03_OpenCarriageDoor.ogg")
	LoadSound("03_OpenDivan",           "data/Sound/03_OpenDivan.ogg")
	LoadSound("03_InterNoise",          "data/Sound/03_InterNoise.ogg")
	
	LoadSound("04_BarrelFall",          "data/Sound/04_BarrelFall.ogg")
	LoadSound("04_BridgeFall",          "data/Sound/04_BridgeFall.ogg")
	LoadSound("04_CutoffRope",          "data/Sound/04_CutoffRope.ogg")
	LoadSound("04_MG_CutterGather",     "data/Sound/04_MG_CutterGather.ogg")
	LoadSound("04_MG_CutterScrew",      "data/Sound/04_MG_CutterScrew.ogg")
	LoadSound("04_PickBreak",           "data/Sound/04_PickBreak.ogg")
	LoadSound("04_PlaceScissorsPart",   "data/Sound/04_PlaceScissorsPart.ogg")
	LoadSound("04_Monkey",   			"data/Sound/04_Monkey.ogg")
	LoadSound("04_GorillaAndGate",      "data/Sound/04_GorillaAndGate.ogg")
	
	LoadSound("05_OpenSafe",            "data/Sound/05_OpenSafe.ogg")
	LoadSound("05_CutBread",            "data/Sound/05_CutBread.ogg")
	LoadSound("05_UseVise",             "data/Sound/05_UseVise.ogg")
	LoadSound("05_StoneUp",             "data/Sound/05_StoneUp.ogg")
	LoadSound("05_SawUse",              "data/Sound/05_SawUse.ogg")
	LoadSound("05_SkelClick",           "data/Sound/05_SkelClick.ogg")
	LoadSound("05_OpenSecondSafe",      "data/Sound/05_OpenSecondSafe.ogg")
	LoadSound("05_PlaceLockPart",       "data/Sound/05_PlaceLockPart.ogg")
	LoadSound("05_MG_FireLamp",         "data/Sound/05_MG_FireLamp.ogg")
	LoadSound("05_MG_FireMatch",        "data/Sound/05_MG_FireMatch.ogg")
	LoadSound("05_OpenSewage",          "data/Sound/05_OpenSewage.ogg")
	LoadSound("05_OpenLuk",             "data/Sound/05_OpenLuk.ogg")
	LoadSound("05_UseLockParts",        "data/Sound/05_UseLockParts.ogg")
	LoadSound("05_SafeValvePlaced",     "data/Sound/05_SafeValvePlaced.ogg")
	LoadSound("05_MovePan",             "data/Sound/05_MovePan.ogg")
	LoadSound("05_OpenBag",             "data/Sound/05_OpenBag.ogg")
	LoadSound("05_PlaceScrollRetort",   "data/Sound/05_PlaceScrollRetort.ogg")
	LoadSound("05_PaperFly",            "data/Sound/05_PaperFly.ogg")
	
	LoadSound("06_BreakPig",            "data/Sound/06_BreakPig.ogg")
	LoadSound("06_KeyRotate",           "data/Sound/06_KeyRotate.ogg")
	LoadSound("06_OpenLeftDoor",        "data/Sound/06_OpenLeftDoor.ogg")
	LoadSound("06_OpenRightDoor",       "data/Sound/06_OpenRightDoor.ogg")
	LoadSound("06_PressButtons",        "data/Sound/06_PressButtons.ogg")
	LoadSound("06_UseKey",              "data/Sound/06_UseKey.ogg")
	LoadSound("06_UseValve",            "data/Sound/06_UseValve.ogg")
	LoadSound("06_WaterFall",           "data/Sound/06_WaterFall.ogg")
	
	LoadSound("06_CloseMask",           "data/Sound/06_CloseMask.ogg")
	LoadSound("06_OpenLock",            "data/Sound/06_OpenLock.ogg")
	LoadSound("06_OpenMask",            "data/Sound/06_OpenMask.ogg")
	LoadSound("06_PlaceButton",         "data/Sound/06_PlaceButton.ogg")
	LoadSound("06_RotateEyes",          "data/Sound/06_RotateEyes.ogg")
	LoadSound("06_PlaceValve",          "data/Sound/06_PlaceValve.ogg")
	LoadSound("06_OpenCashRegister",    "data/Sound/06_OpenCashRegister.ogg")
	LoadSound("06_OpenShelf",           "data/Sound/06_OpenShelf.ogg")
	LoadSound("06_OpenShelfInHO",       "data/Sound/06_OpenShelfInHO.ogg")
	
	LoadSound("07_OpenShelf",           "data/Sound/07_OpenShelf.ogg")
	LoadSound("07_Wind",                "data/Sound/07_Wind.ogg")
	LoadSound("07_MoveCell",            "data/Sound/07_MoveCell.ogg")
	LoadSound("07_OpenDoor",            "data/Sound/07_OpenDoor.ogg")
	LoadSound("07_OpenLock",            "data/Sound/07_OpenLock.ogg")
	LoadSound("07_PlaceCell",           "data/Sound/07_PlaceCell.ogg")
	LoadSound("07_SelectCell",          "data/Sound/07_SelectCell.ogg")
	
	LoadSound("08_DoorknobPlaced",      "data/Sound/08_DoorknobPlaced.ogg")
	LoadSound("08_DoorknobUse",         "data/Sound/08_DoorknobUse.ogg")
	LoadSound("08_PlaceLever",          "data/Sound/08_PlaceLever.ogg")
	
	LoadSound("09_OpenChestLittle",     "data/Sound/09_OpenChestLittle.ogg")
	LoadSound("09_OpenCupboard",        "data/Sound/09_OpenCupboard.ogg")
	LoadSound("09_OpenCurtain",         "data/Sound/09_OpenCurtain.ogg")
	LoadSound("09_PullOutCupboard",     "data/Sound/09_PullOutCupboard.ogg")
	LoadSound("09_OpenSafe",            "data/Sound/09_OpenSafe.ogg")
	LoadSound("09_OpenCell",            "data/Sound/09_OpenCell.ogg")
	LoadSound("09_OpenGate",            "data/Sound/09_OpenGate.ogg")
	LoadSound("09_OpenLock",            "data/Sound/09_OpenLock.ogg")
	LoadSound("09_MG_MoveButton",       "data/Sound/09_MG_MoveButton.ogg")
	LoadSound("09_MG_RotateButton",     "data/Sound/09_MG_RotateButton.ogg")
	LoadSound("09_MG_Win",              "data/Sound/09_MG_Win.ogg")
	LoadSound("09_PlaceCell",           "data/Sound/09_PlaceCell.ogg")
	LoadSound("09_MoveCarpet",          "data/Sound/09_MoveCarpet.ogg")
	LoadSound("09_CleanUpGlass1",       "data/Sound/09_CleanUpGlass.ogg")
	LoadSound("09_CleanUpGlass2",       "data/Sound/09_CleanUpGlass.ogg")
	
	LoadSound("11_MG_OpenBottle",       "data/Sound/11_MG_OpenBottle.ogg")
	LoadSound("11_MG_PourToGlass",      "data/Sound/11_MG_PourToGlass.ogg")
	LoadSound("11_OpenPanCover",        "data/Sound/11_OpenPanCover.ogg")
	LoadSound("11_OpenShutter",         "data/Sound/11_OpenShutter.ogg")
	LoadSound("11_OpenShelf",           "data/Sound/11_OpenShelf.ogg")
	
	LoadSound("12_FishJump",            "data/Sound/12_FishJump.ogg")
	
	LoadSound("13_OpenDoor",            "data/Sound/13_OpenDoor.ogg")
	LoadSound("13_Painting_Fall",       "data/Sound/13_Painting_Fall.ogg")
	LoadSound("13_Pincers_Apply",       "data/Sound/13_Pincers_Apply.ogg")
	LoadSound("13_OpenShelf",           "data/Sound/13_OpenShelf.ogg")
	
	LoadSound("14_Bell",                "data/Sound/14_Bell.ogg")
	LoadSound("14_CutBag",              "data/Sound/14_CutBag.ogg")
	LoadSound("14_ChangePict",          "data/Sound/14_ChangePict.ogg")
	LoadSound("14_LockOpening",         "data/Sound/14_LockOpening.ogg")
	LoadSound("14_PlacePict",           "data/Sound/14_PlacePict.ogg")
	LoadSound("14_OpenBedroomDoor",     "data/Sound/14_OpenBedroomDoor.ogg")
	LoadSound("14_CutCarpet",           "data/Sound/14_CutCarpet.ogg")
	LoadSound("14_OpenCache",           "data/Sound/14_OpenCache.ogg")
	LoadSound("14_PlaceLadder",         "data/Sound/14_PlaceLadder.ogg")
	LoadSound("14_BrushApply1",         "data/Sound/14_BrushApply.ogg")
	LoadSound("14_BrushApply2",         "data/Sound/14_BrushApply.ogg")
	
	LoadSound("13_CellSelect",          "data/Sound/13_CellSelect.ogg")
	LoadSound("13_OpenLock",            "data/Sound/13_OpenLock.ogg")
	LoadSound("13_PlaceCell",           "data/Sound/13_PlaceCell.ogg")
	LoadSound("13_WorkshopOpen",        "data/Sound/13_WorkshopOpen.ogg")
	LoadSound("13_PipetteApply",        "data/Sound/13_PipetteApply.ogg")
	
	LoadSound("15_OpenBox",             "data/Sound/15_OpenBox.ogg")
	LoadSound("15_OpenDoor",            "data/Sound/15_OpenDoor.ogg")
	LoadSound("15_SmashGlass",          "data/Sound/15_SmashGlass.ogg")
	LoadSound("15_SmashGlass2",         "data/Sound/15_SmashGlass.ogg")
	LoadSound("15_Acid",                "data/Sound/15_Acid.ogg")
	LoadSound("15_ScrewdriverApply",    "data/Sound/15_ScrewdriverApply.ogg")
	LoadSound("15_WrenchApply",         "data/Sound/15_WrenchApply.ogg")
	LoadSound("15_WrenchApply2",        "data/Sound/15_WrenchApply.ogg")
	LoadSound("15_WrenchApply3",        "data/Sound/15_WrenchApply.ogg")
	LoadSound("15_DoorOpen",            "data/Sound/15_DoorOpen.ogg")
	LoadSound("15_Pinions",             "data/Sound/15_Pinions.ogg")
	
	LoadSound("16_MG_Drop",             "data/Sound/16_MG_Drop.ogg")
	LoadSound("16_MG_Get",              "data/Sound/16_MG_Get.ogg")
	LoadSound("16_MG_LockOpen",         "data/Sound/16_MG_LockOpen.ogg")
	LoadSound("16_Counterpane",         "data/Sound/16_Counterpane.ogg")
	LoadSound("16_MoveShelf",           "data/Sound/16_MoveShelf.ogg")
	LoadSound("16_MoveShelf2",          "data/Sound/16_MoveShelf.ogg")
	LoadSound("16_PlaceKey",            "data/Sound/16_PlaceKey.ogg")
	LoadSound("16_PlacePart",           "data/Sound/16_PlacePart.ogg")
	LoadSound("16_RotateKey",           "data/Sound/16_RotateKey.ogg")
	LoadSound("16_OpenDoor",            "data/Sound/16_OpenDoor.ogg")
	
	LoadSound("17_AcidShell",           "data/Sound/17_AcidShell.ogg")
	LoadSound("17_DripOnGlass",         "data/Sound/17_DripOnGlass.ogg")
	LoadSound("17_GlassApply",          "data/Sound/17_GlassApply.ogg")
	LoadSound("17_UsePipette",          "data/Sound/17_UsePipette.ogg")
	
	LoadSound("18_BallPlaced",          "data/Sound/18_BallPlaced.ogg")
	LoadSound("18_DoorOpen",            "data/Sound/18_DoorOpen.ogg")
	LoadSound("18_LockRotate",          "data/Sound/18_LockRotate.ogg")
	LoadSound("18_OpenShelfInHO",       "data/Sound/18_OpenShelfInHO.ogg")
	LoadSound("18_HammerApply",         "data/Sound/18_HammerApply.ogg")
	
	LoadSound("19_OpenDoor",            "data/Sound/19_OpenDoor.ogg")
	
	LoadSound("20_MoveBar",             "data/Sound/20_MoveBar.ogg")
	LoadSound("20_MoveParts",           "data/Sound/20_MoveParts.ogg")
	LoadSound("20_PlaceParts",          "data/Sound/20_PlaceParts.ogg")
	LoadSound("20_OpenDoorInside",      "data/Sound/20_OpenDoorInside.ogg")
	LoadSound("20_OpenShelf",           "data/Sound/20_OpenShelf.ogg")
	
	LoadSound("21_PitchforkApply",      "data/Sound/21_PitchforkApply.ogg")
	
	LoadSound("22_Effluent",            "data/Sound/22_Effluent.ogg")
	LoadSound("22_KettlePlaced",        "data/Sound/22_KettlePlaced.ogg")
	LoadSound("22_OpenDoor",            "data/Sound/22_OpenDoor.ogg")
	LoadSound("22_WrenchApply",         "data/Sound/22_WrenchApply.ogg")
	
	LoadSound("23_Gorilla",             "data/Sound/23_Gorilla.ogg")
	LoadSound("23_AxeApply",            "data/Sound/23_AxeApply.ogg")
	LoadSound("23_AxeApply2",           "data/Sound/23_AxeApply.ogg")
	LoadSound("23_HoopApply",           "data/Sound/23_HoopApply.ogg")
	LoadSound("23_HoopClick",           "data/Sound/23_HoopClick.ogg")
	LoadSound("23_KettleFullApply",     "data/Sound/23_KettleFullApply.ogg")
	LoadSound("23_OpenButterflyNuts1",  "data/Sound/23_OpenButterflyNuts.ogg")
	LoadSound("23_OpenButterflyNuts2",  "data/Sound/23_OpenButterflyNuts.ogg")
	LoadSound("23_OpenManhole",         "data/Sound/23_OpenManhole.ogg")
	LoadSound("23_PlaceButterflyNut",   "data/Sound/23_PlaceButterflyNut.ogg")
	LoadSound("23_StoveFire",           "data/Sound/23_StoveFire.ogg")
	LoadSound("23_GreenLight",          "data/Sound/23_GreenLight.ogg")
	LoadSound("23_PlacePicture",        "data/Sound/23_PlacePicture.ogg")
	
	LoadSound("24_CombinationPliersApply", "data/Sound/24_CombinationPliersApply.ogg")
	LoadSound("24_CrowbarApply",           "data/Sound/24_CrowbarApply.ogg")
	
	-- Map
	LoadSound("MapMove",                "data/Sound/MapMove.ogg")
	
	-- Common location sounds
	LoadSound("OpenDoor",               "data/Sound/OpenDoor.ogg")
	LoadSound("CloseDoor",              "data/Sound/CloseDoor.ogg")
	LoadSound("OpenNotebook",           "data/Sound/OpenNotebook.ogg")
	LoadSound("CloseNotebook",          "data/Sound/CloseNotebook.ogg")
	LoadSound("MousePunish",            "data/Sound/Mouse_Punish.ogg")
	
	if EXTRAS_BUILD then
	    
	    LoadSound("E_01_BoxDown",           "data/Sound/Extras/E_01_BoxDown.ogg")
	    LoadSound("E_01_DoorknobApply",     "data/Sound/Extras/E_01_DoorknobApply.ogg")
	    LoadSound("E_01_DoorOpen",          "data/Sound/Extras/E_01_DoorOpen.ogg")
	    LoadSound("E_01_MoveBox",           "data/Sound/Extras/E_01_MoveBox.ogg")
	    
	    LoadSound("E_02_OpenDrawer",        "data/Sound/Extras/E_02_OpenDrawer.ogg")
	    LoadSound("E_02_CarpetClick",       "data/Sound/Extras/E_02_CarpetClick.ogg")
	    LoadSound("E_02_PendulumApply",     "data/Sound/Extras/E_02_PendulumApply.ogg")
	    LoadSound("E_02_SignetRingApply",   "data/Sound/Extras/E_02_SignetRingApply.ogg")
	    LoadSound("E_02_SignetRingRotate",  "data/Sound/Extras/E_02_SignetRingApply.ogg")
	    LoadSound("E_02_StartClock",        "data/Sound/Extras/E_02_StartClock.ogg")
	    LoadSound("E_02_WeightApply",       "data/Sound/Extras/E_02_WeightApply.ogg")
	    LoadSound("E_02_ShutterOpen",       "data/Sound/Extras/E_02_ShutterOpen.ogg")
	    LoadSound("E_02_KeyRotate",         "data/Sound/Extras/E_02_KeyRotate.ogg")
	    LoadSound("E_02_DoorOpen",          "data/Sound/Extras/E_02_DoorOpen.ogg")
	    
	    LoadSound("E_03_FireplaceSound",    "data/Sound/Extras/E_03_FireplaceSound.ogg")
	    LoadSound("E_03_StatueApply",       "data/Sound/Extras/E_03_StatueApply.ogg")
	    LoadSound("E_03_ButtonPress",       "data/Sound/Extras/E_03_ButtonPress.ogg")
	    LoadSound("E_03_MG_StartDrag",      "data/Sound/Extras/E_03_MG_StartDrag.ogg")
	    LoadSound("E_03_MG_StopDrag",       "data/Sound/Extras/E_03_MG_StopDrag.ogg")
	    LoadSound("E_03_TileMove",          "data/Sound/Extras/E_03_TileMove.ogg")
	    LoadSound("E_03_DoorOpen",          "data/Sound/Extras/E_03_DoorOpen.ogg")
	    LoadSound("E_03_MiniGameOpen",      "data/Sound/Extras/E_03_MiniGameOpen.ogg")
	    LoadSound("E_03_ChestOpen",         "data/Sound/Extras/E_03_ChestOpen.ogg")
	    
	    LoadSound("E_04_DusterApply",       "data/Sound/Extras/E_04_DusterApply.ogg")
	    LoadSound("E_04_MagnetApply",       "data/Sound/Extras/E_04_MagnetApply.ogg")
	    LoadSound("E_04_PencilApply1",      "data/Sound/Extras/E_04_PencilApply.ogg")
	    LoadSound("E_04_PencilApply2",      "data/Sound/Extras/E_04_PencilApply.ogg")
	    LoadSound("E_04_SafeOpen",          "data/Sound/Extras/E_04_SafeOpen.ogg")
	    LoadSound("E_04_SafeValveMove",     "data/Sound/Extras/E_04_SafeValveMove.ogg")
	    LoadSound("E_04_PictureFall",       "data/Sound/Extras/E_04_PictureFall.ogg")
	    LoadSound("E_04_OpenDrawer",        "data/Sound/Extras/E_04_OpenDrawer.ogg")
	    LoadSound("E_04_MG_CircleClick",    "data/Sound/Extras/E_04_MG_CircleClick.ogg")
	    
	    LoadSound("E_05_StoolApply",        "data/Sound/Extras/E_05_StoolApply.ogg")
	    LoadSound("E_05_WaterDrain",        "data/Sound/Extras/E_05_WaterDrain.ogg")
	    LoadSound("E_05_KapelSound",        "data/Sound/Extras/E_05_KapelSound.ogg")
	end
end

--------------------------------------------------------------------------------
function PreloadIntroResources()
    BuildMainMenuComposition();    
end

--------------------------------------------------------------------------------
function PreloadPanelResources()

end

--------------------------------------------------------------------------------
function GetLevelToLoad()
    local state = GetGameManager():GetCurrPlayer().m_gameState
    
    if __EXTRAS_SCENE then
        if EXTRAS_BUILD and IsExtrasOpen() then
            return levels[511]
        else
            return levels[600]
        end
    end
    
    if state:HasState('LastVisitedScene') then
        return state:GetState('LastVisitedScene'):Get(0)
    else
        return levels[10];
    end
end

--------------------------------------------------------------------------------
function GetFirstLevelPath()
    return 'data/Scenes/01_Dupens_House_Entrance_Main.lua'
end

--------------------------------------------------------------------------------
function DestroyCurrentLevel()

    if (play_field ~= nil)
    then
        OnSceneDestroy(play_field);
        play_field:GetLevelScreen():DestroySelf();
        play_field = nil;
    end

end

--------------------------------------------------------------------------------
function StartGame()
    local level_to_load = GetLevelToLoad();
    load_level(level_to_load);
end

--------------------------------------------------------------------------------
function StartTitleMovie()
    local titleScreen = CreateMovieScreen(TITLE_MOVIE_SCREEN);
    titleScreen:SetDuration(8.14);
    titleScreen:SetTerminatesOnAnyKey(true);

    if SHOW_MOVIE_1 then
        local movie = CreateMovieEntity();
        movie:AttachTo(titleScreen);
        movie:SetXY(512,384); 
        movie:SetScale(1024./640.);
        movie:SetRepeat(false);
        movie:PlayMovie("data/Video/title.ogg", "data/Sound/Logo_sound.ogg");
    end
	
	local logoTexture = (EXTRAS_BUILD) and "data/Texture/Logo/EAPLogo_intro_CE.png" or "data/Texture/Logo/EAPLogo_intro.png"
    local anim = titleScreen:CreateAnimation(logoTexture);
    anim:SetHotSpot(260, 109);
    anim:SetPosXY(530, 390);
    anim:SetPosZ(200);
    
    local af = CreateAnimationEffector();
    af:AttachTo( anim );
    af:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    af:SetKey(_TRACK_O, 0, 0);
    af:SetKey(_TRACK_O, 4, 0);
    af:SetKey(_TRACK_O, 5, 1);
    af:Play();
    
    GM:SetCurrentScreen(titleScreen);
end

--------------------------------------------------------------------------------
function StartIntroMovie()

    StopAmbientMusic(false);
    
    introscreen = CreateMovieScreen(INTRO_ONE_MOVIE_SCREEN);
    introscreen:SetDuration(45);
    introscreen:SetTerminatesOnAnyKey(false);
    
    if SHOW_MOVIE_3 then
        local movie = CreateMovieEntity();
        movie:AttachTo(introscreen);
        movie:SetXY(512,384);
        movie:SetScale(1024./640.);
        movie:SetRepeat(false);
        movie:PlayMovie("data/Video/intro.ogg", "data/Music/intro_music.ogg");
    end
    
    -- top margin
    anim = introscreen:CreateAnimation("data/Texture/Scenes/common/margin.jpg");
    anim:SetHotSpot(512, 32);
    anim:SetPosXY(512, 32);
    anim:SetPosZ(200);

    -- bottom margin
    anim = introscreen:CreateAnimation("data/Texture/Scenes/common/margin.jpg");
    anim:SetHotSpot(512, 32);
    anim:SetPosXY(512, 768 - 32);
    anim:SetPosZ(200);
    
    local skipButton = MakeButton(1024 - 50, 768 - 32, 100, 30);
    skipButton:SetSpriteUp("SkipButton");
	skipButton:SetSpriteOver("SkipButtonOver");
	skipButton:SetSpriteDown("SkipButtonDown");
	skipButton:SetActionSound("Menu_button_click");
	
	skipButton:AttachTo(introscreen);
	skipButton:SetAction("SkipIntro");
	skipButton:SetZ(201);
	
	GM:SetCurrentScreen(introscreen);
end

function SkipIntro()
    introscreen:Finish();
end

--------------------------------------------------------------------------------
function StartLoadingScreen()

    local loadingScreen = CreateLoadScreen(LOADING_SCREEN);
    
    if SHOW_MOVIE_2 then
        local movie = CreateMovieEntity();
        movie:AttachTo(loadingScreen);
        movie:SetXY(512,384); 
        movie:SetZ(1);
        movie:SetScale(1024./640.);
        movie:SetRepeat(true);
        movie:PlayMovie("data/Video/loading.ogg", "");
    end
	
	local logoTexture = (EXTRAS_BUILD) and "data/Texture/Logo/EAPLogo_intro_CE.png" or "data/Texture/Logo/EAPLogo_intro.png"
	local anim = loadingScreen:CreateAnimation(logoTexture);
    anim:SetHotSpot(260, 109);
    anim:SetPosXY(530, 390);
    anim:SetPosZ(200);
    
    local progressBar = CreateProgressBar();
    progressBar:SetRect(83, 744, 877, 20);
    progressBar:SetBackgroundTexture("data/texture/loading/load_border.png");
    progressBar:SetProgressTexture("data/texture/loading/load_part.png");
    progressBar:SetZ(102);
    loadingScreen:SetProgressBar(progressBar);
    
    GM:SetCurrentScreen(loadingScreen);
end

--------------------------------------------------------------------------------
function StartApplication()

    local showIntro = SHOW_INTRO;
    if (showIntro)
    then
    
        local logoScreen = CreateLogoScreen(DISTRIBUTOR_LOGO_SCREEN);
        logoScreen:SetDuration(2.0);
        logoScreen:SetLogo("data/Texture/Logo/BigFishLogo.jpg");
        GM:SetCurrentScreen(logoScreen);
        
        --GetGameApp()->HideCursor();
        --StartLoadingScreen();
        --StartTitleMovie();
    else
        StartMainMenu();
        --StartTitleMovie();
	    --StartGame();
    end
    
end

--------------------------------------------------------------------------------
function ScreenDidFinish(screen)

    if (exitingApp)
    then
        return;
    end

    local screenName = screen:GetName();
    if (screenName == DISTRIBUTOR_LOGO_SCREEN)
    then
        local logoScreen = CreateLogoScreen(ERS_LOGO_SCREEN);
        logoScreen:SetDuration(2.0);
        logoScreen:SetLogo("data/Texture/Logo/ERSLogo.jpg");
        GM:SetCurrentScreen(logoScreen);
        
        return;
    end
    
    if (screenName == ERS_LOGO_SCREEN)
    then 
        StartTitleMovie();
        return;
    end
    
    if (screenName == TITLE_MOVIE_SCREEN)
    then
        StartLoadingScreen();
        return;
    end
    
    if (screenName == LOADING_SCREEN)
    then
        StartIntroMovie();
        return;
    end
    
    if (screenName == INTRO_ONE_MOVIE_SCREEN)
    then
        StartMainMenu();
        return;
    end
    
    if (mainMenu and PopUpScreensMgr:GetTop() == nil)
    then
        if (GM:GetCurrPlayer() == nil)
        then
            ShowInputPlayerNameScreen();
        else
            StartMainMenuContentScreen();
        end
    end
    
end

--------------------------------------------------------------------------------
function CleanUpGameResources()

    while(active_zones:GetTop())
    do
        active_zones:GetTop().SetVisible(false);
    end
    
    tutorScreen = nil;
    
    DragObject = nil;
    
    DestroyCurrentLevel();
    PopUpScreensMgr:DestroyAll();
    ingameMenu = nil;
    MainMenuComposition = nil;
    ToolsToAddOnInventoryShown:PopAll();
end

--------------------------------------------------------------------------------
function AppFocusLost()

    if (mainMenu and not extraContentScreen) then
        mainMenu:Stop();
    end
    
    local topScreen = PopUpScreensMgr:GetTop();
    
    if topScreen == nil and play_field ~= nil then
        StartIngameMenu();
    elseif topScreen and topScreen:GetName() ~= "MainMenu" then
        topScreen:Stop();
    end
    
end

--------------------------------------------------------------------------------
function AppFocusGain()
    if (mainMenu and not extraContentScreen) then
        mainMenu:Go();
    end
    
    local topScreen = PopUpScreensMgr:GetTop();
    
    if topScreen and topScreen:GetName() ~= "MainMenu" then
        topScreen:Go();
    end
end

--------------------------------------------------------------------------------
function FullScreenChange()

    if (optionsScreen)
    then
        optionsScreen.fullscreenCheckbox:SetState(APP:IsFullScreen() and 1 or 0);
        SetFullscreen();
    end

end

--------------------------------------------------------------------------------
function BuildYesNoScreen(message, yesCallback, noCallback)

    local screen = CreateScreen();
    screen:SetName("YesNoScreen " .. message);
    screen:SetLayer(POPUP_WINDOWS_LAYER);
    local bg = screen:CreateAnimation("data/texture/GUI/Square_dialog_back.png"); -- 618, 474
    bg:SetHotSpot(309, 237);
    bg:SetPosXY(512, 350);
    bg:SetPosZ(1);
    
    local yesButton = MakeButton(446, 570, 100, 40);
    yesButton:GetCaption():SetFont(BUTTONS_FONT);
    yesButton:SetSpriteUp("YesButton");
	yesButton:SetSpriteOver("YesButtonOver");
	yesButton:SetSpriteDown("YesButtonDown");
	yesButton:SetZ(2);
	--yesButton:GetCaption():SetScale(1.);
	
	yesButton:AttachTo(screen);
	yesButton:SetAction(yesCallback);
	yesButton:SetActionSound("Menu_button_click");
	--yesButton:SetTitle("Yes");
	
	local noButton = MakeButton(578, 570, 100, 40);
	noButton:GetCaption():SetFont(BUTTONS_FONT);
	noButton:SetZ(2);
	--noButton:GetCaption():SetScale(1.);
	
	noButton:AttachTo(screen);
	noButton:SetAction(noCallback);
	noButton:SetActionSound("Menu_button_click");
	noButton:SetSpriteUp("NoButton");
	noButton:SetSpriteOver("NoButtonOver");
	noButton:SetSpriteDown("NoButtonDown");
	--noButton:SetTitle("No");
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, message);
	--textBox:SetHotSpot(298, 206);
	textBox:SetXY(512, 314);
	textBox:SetZ(2);
	textBox:SetScale(1.0);
	textBox:SetRect(412, 264, 200, 100);
	textBox:AttachTo(screen);
    
    screen.textBox   = textBox;
    screen.yesButton = yesButton;
    screen.noButton  = noButton;
    return screen;
    
end

--------------------------------------------------------------------------------
function BuildOKScreen(message, okCallback)

    local zOffset = 50;
    local screen = CreateScreen();
    screen:SetName("OKScreen " .. message);
    screen:SetLayer(POPUP_WINDOWS_LAYER);
    
    local bg = screen:CreateAnimation("data/texture/GUI/Square_dialog_back.png"); -- 618, 474
    bg:SetHotSpot(309, 237);
    bg:SetPosXY(512, 350);
    bg:SetPosZ(0);
    
    local OKButton = MakeButton(512, 570, 100, 40);
    OKButton:GetCaption():SetFont(BUTTONS_FONT);
	OKButton:GetCaption():SetScale(0.8);
	OKButton:SetZ(5 + zOffset);
	
	OKButton:AttachTo(screen);
	OKButton:SetAction(okCallback);
	OKButton:SetSpriteUp("OKButton");
	OKButton:SetSpriteOver("OKButtonOver");
	OKButton:SetSpriteDown("OKButtonDown");
	--OKButton:SetTitle("OK");
	OKButton:SetActionSound("Menu_button_click");
	
	local textBox = CreateTextEntity(INGAME_MENU_FONT, message);
	--textBox:SetHotSpot(298, 206);
	textBox:SetXY(512, 340);
	textBox:SetZ(1 + zOffset);
	textBox:SetScale(1.0);
	textBox:SetRect(412, 264, 200, 200);
	textBox:AttachTo(screen);
	textBox:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE);
	
	screen.textBox  = textBox;
	screen.bg       = bg;
	screen.OKButton = OKButton;
    
    return screen;
    
end

--------------------------------------------------------------------------------
function DisplayMessage(title, message, tag)

    if (CannotSwitchWindowedScreen)
    then
        return;
    end
    CannotSwitchWindowedScreen = 8;
    local callback = "PopCurrentPopUpScreen";
    if (tag == SYSTEM_MESSAGE_TAG_CANNOT_WINDOWED)
    then
        callback = "KillCannotSwitchWindowedScreen";
    end
    local scr = BuildOKScreen("EMPTY", callback);
    scr.textBox:SetText(message);
    scr.textBox:SetXY(512, 360 - 180);
    scr.textBox:SetBorderSize(520, 400);
    
    PopUpScreensMgr:Push(scr);

end

--------------------------------------------------------------------------------
function KillCannotSwitchWindowedScreen()

    CannotSwitchWindowedScreen = nil;
    PopCurrentPopUpScreen();
end
--------------------------------------------------------------------------------

function CheatOpenExtrasScenes()
    if EXTRAS_BUILD and not IsExtrasOpen() then
        GM:GetCurrPlayer().m_gameState:AddState(EXTRAS_MODE_PREFIX.."EXTRAS_OPEN", "true");
        
        if mainMenu then
            StartExtrasEnterScreen();
        end
    end
end