//-----------------------------------------------------------------------------

// Variables used by client scripts & code.  The ones marked with (c)
// are accessed from code.  Variables preceeded by Pref:: are client
// preferences and stored automatically in the ~/client/prefs.cs file
// in between sessions.
//
//    (c) Client::MissionFile             Mission file name
//    ( ) Client::Password                Password for server join

//    (?) Pref::Player::CurrentFOV
//    (?) Pref::Player::DefaultFov
//    ( ) Pref::Input::KeyboardTurnSpeed

//    (c) pref::Master[n]                 List of master servers
//    (c) pref::Net::RegionMask     
//    (c) pref::Client::ServerFavoriteCount
//    (c) pref::Client::ServerFavorite[FavoriteCount]
//    .. Many more prefs... need to finish this off

// Moves, not finished with this either...
//    (c) firstPerson
//    $mv*Action...

//-----------------------------------------------------------------------------
// These are variables used to control the shell scripts and
// can be overriden by mods:
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// loadMaterials - load all materials.cs files
//-----------------------------------------------------------------------------
function loadMaterials()
{
   for( %file = findFirstFile( "*/materials.cs" ); %file !$= ""; %file = findNextFile( "*/materials.cs" ))
   {
      exec( %file );
   }
}

function reloadMaterials()
{
   reloadTextures();
   loadMaterials();
   reInitMaterials();
}

function releaseWorldMapGui()
{
   if(isObject(WorldMapGui))
      WorldMapGui.delete();
   if(isObject(ParticleFx))
      ParticleFx.delete();      
   if(isObject(LoadingWorldMapGui))
      LoadingWorldMapGui.delete();      
}

function initWorldMapGui()
{
   releaseWorldMapGui();
   exec("./ui/worldMapGui.gui");
   exec("./scripts/ho/ParticleFx.cs");   
   exec("./scripts/menu/worldMapGui.cs");
}

function initSceneGui()
{
   releaseSceneGui();   
   exec("./ui/SceneGui.gui");      
   exec("./scripts/ho/cameraLightBehavior.cs");   
   exec("./scripts/ho/ParticleFx.cs");
   exec("./scripts/ho/ParticleFxDirMeter.cs");   
   exec("./scripts/ho/directorBehavior.cs");
   exec("./scripts/ho/shoppingList.cs");
   exec("./scripts/minigame/util.cs");   
   exec("./scripts/minigame/MiniGame.cs");
   exec("./scripts/minigame/MiniGameParticleFx.cs");
   exec("./scripts/ho/Hints.cs");   
   exec("./scripts/ho/SceneGui.cs");
   exec("./scripts/ho/Tutorial.cs");
   exec("./scripts/ho/Subtitle.cs");
}

function releaseSceneGui()
{
   if(isObject(SubtitleBox))
      SubtitleBox.delete();
      
   if(isObject(Minigame))
      Minigame.delete();
      
   if(isObject(MinigameParticleFx))
      MinigameParticleFx.delete();
      
   if(isObject(ShoppingList))
      ShoppingList.delete();
      
   if(isObject(ParticleFx))
      ParticleFx.delete();
      
   if(isObject(ParticleFxDirMeter))
      ParticleFxDirMeter.delete();
      
   if(isObject(HintSystem))
      HintSystem.delete();
   
   if(isObject(SceneGui))
      SceneGui.delete();
}

function initMinigameTable()
{
   $MinigameTable["SpyLevel",     "1"] = 1; // Swat
   $MinigameTable["SpyLevel",     "2"] = 2;
   $MinigameTable["SpyLevel",     "4"] = 1; // Swat
   $MinigameTable["GangsterLevel","2"] = 3;
   $MinigameTable["GangsterLevel","3"] = 4;
   $MinigameTable["KungFuLevel",  "1"] = 5;
   $MinigameTable["KungFuLevel",  "3"] = 6;
   $MinigameTable["KungFuLevel",  "4"] = 6;
   $MinigameTable["DinosaurLevel","2"] = 7;
   $MinigameTable["DinosaurLevel","4"] = 14;   
   $MinigameTable["SciFiLevel",   "4"] = 8;
   $MinigameTable["WesternLevel", "4"] = 9;
   $MinigameTable["SciFiLevel",   "1"] = 10;
   $MinigameTable["SciFiLevel",   "3"] = 10;   
   $MinigameTable["MusicalLevel", "1"] = 11;
   $MinigameTable["MusicalLevel", "4"] = 12;
   $MinigameTable["WesternLevel", "2"] = 13;
   $MinigameTable["DinosaurLevel","3"] = 14;
   $MinigameTable["MonsterLevel", "4"] = 15;
   $MinigameTable["MonsterLevel", "1"] = 16;
   $MinigameTable["MonsterLevel", "3"] = 16;   
}

//-----------------------------------------------------------------------------
function initClient()
{
   echo("\n--------- Initializing " @ $appName @ ": Client Scripts ---------");
   
   $OnExitDlg = false;
   $OnResumeDlg = false;

   // Make sure this variable reflects the correct state.
   $Server::Dedicated = false;

   // Game information used to query the master server
   $Client::GameTypeQuery = $appName;
   $Client::MissionTypeQuery = "Any";
   
   $FirstTime = true;
   $ReplayButtonPressed = false;
   
   $CurPlayerNum = 1;
   $CurPlayerName = "";
   HintSystem.playerHintCount = 0; //Why do I need it here?
   $returningFromScene = false;

   exec("./ui/customProfiles.cs"); // override the base profiles if necessary

   // The common module provides basic client functionality
   initBaseClient();

   // Use our prefs to configure our Canvas/Window
   configureCanvas();

   // Set the window title
   if (isObject(Canvas))
//      Canvas.setWindowTitle(getEngineName() @ " (" @ getVersionString() @ ") - " @ $appName);
      Canvas.setWindowTitle($appName);
      
   /// Load client-side Audio Profiles/Descriptions
   exec("./scripts/audioProfiles.cs");

   // Load up the Game GUIs
   exec("./ui/defaultGameProfiles.cs");
   exec("./ui/PlayGui.gui");
//   exec("./ui/SceneGui.gui");
      
//   exec("./ui/IntroGui.gui");
//   exec("./ui/OutroGui.gui");
   exec("./ui/PauseDlg.gui");
   exec("./ui/OptionsHODlg.gui");
   exec("./scripts/ho/OptionsHODlg.cs");
   exec("./ui/FocusDlg.gui");
   exec("./ui/MinigameQuitDlg.gui");
   exec("./scripts/minigame/MinigameQuitDlg.cs");
   exec("./ui/ExitDlg.gui");
   exec("./scripts/ho/ExitDlg.cs");
   exec("./ui/DelDlg.gui");
   exec("./ui/DuplicateDlg.gui");

   // Load up the shell GUIs
   exec("./ui/mainMenuGui.gui");
   exec("./ui/StartupGui.gui");

   // Menu gui
   exec("./ui/MainMenuDlg.gui");
   exec("./ui/InstructionsMenuDlg.gui");
   exec("./ui/CreditsMenuDlg.gui");
//   exec("./ui/MapGui.gui");
   exec("./ui/LoadLevelGui.gui");   
   exec("./ui/AwardRoom.gui");
   
   // Schedule manager
   exec("./scripts/scheduleLCA.cs");
   initScheduleLCA();
  
   // Client scripts
   exec("./scripts/client.cs");
   exec("./scripts/menu/client_xml.cs");   
   exec("./scripts/game.cs");
   exec("./scripts/missionDownload.cs");
   exec("./scripts/serverConnection.cs");
   
   exec("./scripts/menu/mainMenuGui.cs");
   exec("./scripts/menu/mainMenuDlg.cs");
   exec("./scripts/menu/newPlayerMenuDlg.cs");
   exec("./scripts/menu/selectPlayerMenuDlg.cs"); 
   exec("./scripts/menu/MenuParticleFx.cs");     
//   exec("./scripts/menu/mapGui.cs");
   exec("./scripts/menu/loadLevelGui.cs");   
   exec("./scripts/menu/AwardRoom.cs");
   exec("./scripts/menu/optionsMenuDlg.cs");
   exec("./scripts/menu/playMinigameDlg.cs"); 
   exec("./scripts/menu/instructionsMenuDlg.cs");
   exec("./scripts/menu/creditsMenuDlg.cs");
   
   exec("./scripts/playGui.cs");
   exec("./scripts/ho/IntroGui.cs"); 
   exec("./scripts/ho/OutroGui.cs");
   exec("./scripts/startMissionGui.cs");

   exec("./ui/NewPlayerMenuDlg.gui");
   exec("./ui/SelectPlayerMenuDlg.gui");   
   exec("./ui/OptionsMenuDlg.gui");      
   exec("./ui/playMinigameDlg.gui");
   //exec("./scripts/menu/minigameMenuDlg.gui");

   // load menu thread data
   exec("./menu_threads.cs");
   
   // Load useful Materials
   exec("./scripts/glowBuffer.cs");
   exec("./scripts/shaders.cs");
   exec("./scripts/commonMaterialData.cs" );

   // Default player key bindings
   exec("./scripts/default.bind.cs");

   if (isFile("./config.cs"))
      exec("./config.cs");
      
   loadMaterials();

   // Really shouldn't be starting the networking unless we are
   // going to connect to a remote server, or host a multi-player
   // game.
   setNetPort(0);
   
   // Copy saved script prefs into C++ code.
   setDefaultFov( $pref::Player::defaultFov );
   setZoomSpeed( $pref::Player::zoomSpeed );

   // Start up the main menu... this is sepaRat1Pathed out into a 
   // method for easier mod override.

   // Connect to server if requested.
   if ($JoinGameAddress !$= "") {
      // If we are instantly connecting to an address, load the
      // main menu then attempt the connect.
      loadMainMenu();
      connect($JoinGameAddress, "", $Pref::Player::Name);
   }
   else {
      // Otherwise go to the splash screen.
      Canvas.setCursor("DefaultCursor");
      loadStartup();
   }
   
   initMinigameTable();
   
   $GameAspectRatio = 4 / 3;
   //nomusicplease();
   
   $hideMouseCounter = 1;
}

//-----------------------------------------------------------------------------

function loadMainMenu()
{
   // Startup the client with the Main menu...
   if (isObject( MainMenuGui ))
      Canvas.setContent( MainMenuGui );
   Canvas.setCursor("DefaultCursor");
}

// Called on WM_CLOSE messages
function exitGame()
{
   if(isObject(SceneGui))
      SceneGui.pauseHOGame();
      
   if(isObject(WorldMapGui) || (MazeMinigame.isPlaying && MazeMinigame.gameMode $= "Dragger") || InsectMinigame.isPlaying || PunchMinigame.isPlaying)
   {
      lockMouse(false);
      Canvas.showCursor();
   }
   
   pauseSchedulesLCA("Minigame");
   pauseSchedulesLCA("MinigameParticle");
   
   $OnExitDlg = true; 
   Canvas.pushDialog(ExitDlg);   
}

function showWorldMap(%showClapperboard)
{
   $ShowWorldMapClapperboard = %showClapperboard;
      
   // Load WorldMapGui scripts
   initWorldMapGui();     
   
   $InWorldMap = true;
   LoadWorldMap();
}

// Load world map
function LoadWorldMap()
{
   WorldMapGui.camShape1 = Camera1;   
   
   if(isObject(LoadingWorldMapGui))
      LoadingWorldMapGui.delete();   
   
   exec("./ui/loadingWorldMapGui.gui");
   exec("./scripts/loadingWorldMapGui.cs");

   // Display the loading GUI
   Canvas.setContent(LoadingWorldMapGui);
   Canvas.repaint();
   
   schedule(500, 0, LoadWorldMap2);
}

function LoadWorldMap2()
{
   // Start up the server
   createServer("SinglePlayer", expandFileName("~/data/missions/WorldMap.mis"));
   %conn = new GameConnection(ServerConnection);
   RootGroup.add(ServerConnection);
   %conn.setConnectArgs($pref::Player::Name);
   %conn.setJoinPassword($Client::Password);
   %conn.connectLocal();
   
   WorldMapGui.initCameras();   
}

// %take: Take number [1-4]
function LoadGameLevel(%levelName, %take)
{
   %minigameTriggerTime = 0; // Trigger time of the minigame (in seconds)
   SceneGui.currentLevel = %levelName;
   playMinigameDlg.currentLevel = %levelName;

   SceneGui.camShape1 = Camera1;
   SceneGui.camShape2 = Camera2;
   SceneGui.camShape3 = Camera3;
   SceneGui.camShape4 = Camera4;

   if(isObject(LoadingGui))
      LoadingGui.delete();
      
   exec("./ui/loadingGui.gui");
   exec("./scripts/loadingGui.cs");

   // Display the loading GUI
   Canvas.setContent(LoadingGui);
   //LOAD_MapName.setText( "Creating Level" );
  	//LOAD_MapDescription.setText( "<font:Arial:16>Please wait while the level is started...");
   Canvas.repaint();

   schedule(500, 0, LoadGameLevel2, %levelName, %take);
}

function LoadGameLevel2(%levelName, %take)
{
   // Start up the server
   createServer("SinglePlayer", expandFileName("~/data/missions/"@%levelName@"_Take"@%take@".mis"));
   %conn = new GameConnection(ServerConnection);
   RootGroup.add(ServerConnection);
   %conn.setConnectArgs($pref::Player::Name);
   %conn.setJoinPassword($Client::Password);
   %conn.connectLocal();
   
   SceneGui.initCameras();   
}

function givemestars(%nStars)
{
   if((%nStars >=0) && (%nStars <=5)){
      if(%nStars == 0) SceneGui.directorMeterCur = 1;
      else if(%nStars == 1) SceneGui.directorMeterCur = 40;
      else if(%nStars == 2) SceneGui.directorMeterCur = 45;
      else if(%nStars == 3) SceneGui.directorMeterCur = 50;
      else if(%nStars == 4) SceneGui.directorMeterCur = 55;
      else if(%nStars == 5) SceneGui.directorMeterCur = 60;
      
      SceneGui.updateDirectorMeter(false);
      
      SceneGui.pauseHOGame();         
      SceneGui.showScoreStarsPart1();
      
      echo(" ");
      echo("HEY!!! Cheating, huh?!");
   }else{
      echo(" ");
      echo("Do you want " @ %nStars @ " stars?! Are you dreaming?!");
   }
}

function givemeawardstars(%nStars)
{

   $StarsWonInTake = %nStars;
   $updateProfileForStarsCheat = true;
   SceneGui.pauseHOGame();         
   SceneGui.showScoreStarsPart1();
   
}

function givemepoints(%nPoints)
{
   SceneGui.addToDirectorMeter(%nPoints);
      
   echo(" ");
   echo("HEY!!! Cheating, huh?! =)");
}

function nomusicplease()
{
   sfxSetChannelVolume($MusicAudioType, 0);
}

function unlockAll()
{
   $CurPlayerUScene = 8;
   $CurPlayerStars = 100;
   
   $CurPlayerGangsterUTake = 5;
   $CurPlayerSpyUTake = 5;
   $CurPlayerKungFuUTake = 5;
   $CurPlayerMusicalUTake = 5;
   $CurPlayerDinosaurUTake = 5;
   $CurPlayerSciFiUTake = 5;
   $CurPlayerWesternUTake = 5;
   $CurPlayerMonsterUTake = 5;
   
   $CurPlayerHints = 99;
    
   switch$($CurPlayerNum)
   {
      case 1:
         PlayerProfile.PlayerUScene1        = $CurPlayerUScene;
         PlayerProfile.PlayerGangsterUTake1 = $CurPlayerGangsterUTake;
         PlayerProfile.PlayerSpyUTake1      = $CurPlayerSpyUTake;
         PlayerProfile.PlayerKungFuUTake1   = $CurPlayerKungFuUTake;
         PlayerProfile.PlayerMusicalUTake1  = $CurPlayerMusicalUTake;
         PlayerProfile.PlayerDinosaurUTake1 = $CurPlayerDinosaurUTake;
         PlayerProfile.PlayerSciFiUTake1    = $CurPlayerSciFiUTake;
         PlayerProfile.PlayerWesternUTake1  = $CurPlayerWesternUTake;
         PlayerProfile.PlayerMonsterUTake1  = $CurPlayerMonsterUTake;
         PlayerProfile.PlayerStars1         = $CurPlayerStars;
         
         PlayerProfile.PlayerHints1         = $CurPlayerHints;
         
         PlayerProfile.Minigame1_1  = 1;
         PlayerProfile.Minigame2_1  = 1;
         PlayerProfile.Minigame3_1  = 1;
         PlayerProfile.Minigame4_1  = 1;
         PlayerProfile.Minigame5_1  = 1;
         PlayerProfile.Minigame6_1  = 1;
         PlayerProfile.Minigame7_1  = 1;
         PlayerProfile.Minigame8_1  = 1;
         PlayerProfile.Minigame9_1  = 1;
         PlayerProfile.Minigame10_1 = 1;
         PlayerProfile.Minigame11_1 = 1;
         PlayerProfile.Minigame12_1 = 1;
         PlayerProfile.Minigame13_1 = 1;
         PlayerProfile.Minigame14_1 = 1;
         PlayerProfile.Minigame15_1 = 1;
         PlayerProfile.Minigame16_1 = 1;
         
      case 2: 
         PlayerProfile.PlayerUScene2        = $CurPlayerUScene;
         PlayerProfile.PlayerGangsterUTake2 = $CurPlayerGangsterUTake;
         PlayerProfile.PlayerSpyUTake2      = $CurPlayerSpyUTake;
         PlayerProfile.PlayerKungFuUTake2   = $CurPlayerKungFuUTake;
         PlayerProfile.PlayerMusicalUTake2  = $CurPlayerMusicalUTake;
         PlayerProfile.PlayerDinosaurUTake2 = $CurPlayerDinosaurUTake;
         PlayerProfile.PlayerSciFiUTake2    = $CurPlayerSciFiUTake;
         PlayerProfile.PlayerWesternUTake2  = $CurPlayerWesternUTake;
         PlayerProfile.PlayerMonsterUTake2  = $CurPlayerMonsterUTake;
         PlayerProfile.PlayerStars2         = $CurPlayerStars;
         
         PlayerProfile.PlayerHints2         = $CurPlayerHints;
         
         PlayerProfile.Minigame1_2  = 1;
         PlayerProfile.Minigame2_2  = 1;
         PlayerProfile.Minigame3_2  = 1;
         PlayerProfile.Minigame4_2  = 1;
         PlayerProfile.Minigame5_2  = 1;
         PlayerProfile.Minigame6_2  = 1;
         PlayerProfile.Minigame7_2  = 1;
         PlayerProfile.Minigame8_2  = 1;
         PlayerProfile.Minigame9_2  = 1;
         PlayerProfile.Minigame10_2 = 1;
         PlayerProfile.Minigame11_2 = 1;
         PlayerProfile.Minigame12_2 = 1;
         PlayerProfile.Minigame13_2 = 1;
         PlayerProfile.Minigame14_2 = 1;
         PlayerProfile.Minigame15_2 = 1;
         PlayerProfile.Minigame16_2 = 1;
         
      case 3: 
         PlayerProfile.PlayerUScene3        = $CurPlayerUScene;
         PlayerProfile.PlayerGangsterUTake3 = $CurPlayerGangsterUTake;
         PlayerProfile.PlayerSpyUTake3      = $CurPlayerSpyUTake;
         PlayerProfile.PlayerKungFuUTake3   = $CurPlayerKungFuUTake;
         PlayerProfile.PlayerMusicalUTake3  = $CurPlayerMusicalUTake;
         PlayerProfile.PlayerDinosaurUTake3 = $CurPlayerDinosaurUTake;
         PlayerProfile.PlayerSciFiUTake3    = $CurPlayerSciFiUTake;
         PlayerProfile.PlayerWesternUTake3  = $CurPlayerWesternUTake;
         PlayerProfile.PlayerMonsterUTake3  = $CurPlayerMonsterUTake;
         PlayerProfile.PlayerStars3         = $CurPlayerStars;
         
         PlayerProfile.PlayerHints3         = $CurPlayerHints;
         
         PlayerProfile.Minigame1_3  = 1;
         PlayerProfile.Minigame2_3  = 1;
         PlayerProfile.Minigame3_3  = 1;
         PlayerProfile.Minigame4_3  = 1;
         PlayerProfile.Minigame5_3  = 1;
         PlayerProfile.Minigame6_3  = 1;
         PlayerProfile.Minigame7_3  = 1;
         PlayerProfile.Minigame8_3  = 1;
         PlayerProfile.Minigame9_3  = 1;
         PlayerProfile.Minigame10_3 = 1;
         PlayerProfile.Minigame11_3 = 1;
         PlayerProfile.Minigame12_3 = 1;
         PlayerProfile.Minigame13_3 = 1;
         PlayerProfile.Minigame14_3 = 1;
         PlayerProfile.Minigame15_3 = 1;
         PlayerProfile.Minigame16_3 = 1;
         
      case 4: 
         PlayerProfile.PlayerUScene4        = $CurPlayerUScene;
         PlayerProfile.PlayerGangsterUTake4 = $CurPlayerGangsterUTake;
         PlayerProfile.PlayerSpyUTake4      = $CurPlayerSpyUTake;
         PlayerProfile.PlayerKungFuUTake4   = $CurPlayerKungFuUTake;
         PlayerProfile.PlayerMusicalUTake4  = $CurPlayerMusicalUTake;
         PlayerProfile.PlayerDinosaurUTake4 = $CurPlayerDinosaurUTake;
         PlayerProfile.PlayerSciFiUTake4    = $CurPlayerSciFiUTake;
         PlayerProfile.PlayerWesternUTake4  = $CurPlayerWesternUTake;
         PlayerProfile.PlayerMonsterUTake4  = $CurPlayerMonsterUTake;
         PlayerProfile.PlayerStars4         = $CurPlayerStars;
         
         PlayerProfile.PlayerHints4         = $CurPlayerHints;
         
         PlayerProfile.Minigame1_4  = 1;
         PlayerProfile.Minigame2_4  = 1;
         PlayerProfile.Minigame3_4  = 1;
         PlayerProfile.Minigame4_4  = 1;
         PlayerProfile.Minigame5_4  = 1;
         PlayerProfile.Minigame6_4  = 1;
         PlayerProfile.Minigame7_4  = 1;
         PlayerProfile.Minigame8_4  = 1;
         PlayerProfile.Minigame9_4  = 1;
         PlayerProfile.Minigame10_4 = 1;
         PlayerProfile.Minigame11_4 = 1;
         PlayerProfile.Minigame12_4 = 1;
         PlayerProfile.Minigame13_4 = 1;
         PlayerProfile.Minigame14_4 = 1;
         PlayerProfile.Minigame15_4 = 1;
         PlayerProfile.Minigame16_4 = 1;
   }
   
   SavePlayerProfile();   
   
   echo("All scenes/takes unlocked and saved to current player profile");
}

function onLoseFocus()
{
   if(!$pausedOnLoseFocus)
   {
      pauseSchedulesLCA("Minigame");
      pauseSchedulesLCA("MinigameParticle");
      $pausedOnLoseFocus = true;      
      if(isObject(SceneGui) && !$isAnimPaused)
      {  
         $HOPausedOnLoseFocus = true;
         SceneGui.pauseHOGame();
      }
      else
         $HOPausedOnLoseFocus = false;
         
      //I know... that is a weird bug fixing but works...   
      if (isObject(PipeMinigame))
      {
         if (!PipeMinigame.ConfettiIsPlaying)
            Minigame.pushToBack(PipeMinigame);
      }

      if (isObject(SliderMinigame))
      {
         if (!SliderMinigame.ConfettiIsPlaying)
            Minigame.pushToBack(SliderMinigame);
      }
      
      if (isObject(SliderMinigame))
      {
         if (!SliderMinigame.ConfettiIsPlaying)
            Minigame.pushToBack(SliderMinigame);
      }

      if (isObject(BejeweledMinigame))
      {
         if (!BejeweledGui.ConfettiIsPlaying)
            Minigame.pushToBack(BejeweledMinigame);
      }
   }

   sfxSetChannelVolume($MusicAudioType, 0);
   sfxSetChannelVolume($SimAudioType, 0);
   sfxSetChannelVolume($GuiAudioType, 0);
}

function onGainFocus()
{
   if($pausedOnLoseFocus)
   {
//      if(isObject(SceneGui))
//      {
   
         lockMouse(false);
         Canvas.showCursor();
   
         $OnResumeDlg = true;
         Canvas.pushDialog(FocusDlg);

         if(isObject(PlayMinigameDlg))
         {
            if(PlayMinigameDlg.isPlayingMinigame)
            {
               %curMinigame = PlayMinigameDlg.curMinigame;
               if(!%curMinigame.ConfettiIsPlaying)
                  PlayMinigameDlg.pushToBack(PlayMinigameDlg.curMinigame);
            }
         }
         
   //      $pausedOnLoseFocus = false;
   //      SceneGui.resumeHOGame();
//      }
   }
   
   sfxSetChannelVolume($MusicAudioType, $pref::SFX::channelVolume[$MusicAudioType]);
   sfxSetChannelVolume($SimAudioType, $pref::SFX::channelVolume[$SimAudioType]);
   sfxSetChannelVolume($GuiAudioType, $pref::SFX::channelVolume[$GuiAudioType]);
}

function onResumeAfterLosingFocus()
{
   $pausedOnLoseFocus = false;
    
   if((MazeMinigame.isPlaying && MazeMinigame.gameMode $= "Dragger") || InsectMinigame.isPlaying || PunchMinigame.isPlaying) 
   { 
      lockMouse(true); 
      Canvas.hideCursor(); 
   } 
   resumeSchedulesLCA("Minigame"); 
   resumeSchedulesLCA("MinigameParticle");
   if(isObject(SceneGui) && $HOPausedOnLoseFocus)
   {
      $HOPausedOnLoseFocus = false; 
      SceneGui.resumeHOGame();
   }
   
   $OnResumeDlg = false;
   Canvas.popDialog(FocusDlg);
   
   $mouseLeftWindow = true;
}

function onNCMouseLeave()
{
   $mouseLeftWindow = true;
}

function onWindowMaximized()
{
   schedule(500, 0, "Options::ToggleFullScreen", true);
}

// Called whenever the left mouse button is released
function onLMouseUp()
{
   if(isObject(WorldMapGui))
   {
      $WorldGameGuiClicked = false;   
      
      // Avoid the mouse dragging to get stuck
      if(WorldMapGui.mouseDragging)
      {
         WorldMapGui.canIPlaySound = false;      
         WorldMapGui.mouseDragging = false;
         ArcBall.onMouseUp();
      }      
   }
}

function onSuspendOS()
{
   /*
   if(isObject(SceneGui) || isObject(WorldMapGui))
   {
      if(isObject(MainMenuGui))
         MainMenuGui.goHome();
   }*/
   
   if($pref::Video::fullScreen $= "true" || $pref::Video::fullScreen $= 1)
   {
      $ResumeInFullScreen = true;
      Options::ToggleFullScreen();
      echo("ToggleFullScreen Called");
   }
   else
      $ResumeInFullScreen = false;
      
   //scheduleObjLCA(MainMenuGui, "HO", 300, "goHome", 0);      
}

function onResumeOS()
{
/*   MainMenuGui.goHome();   
   if($ResumeInFullScreen)
   {
      Options.ToggleFullScreen();
   } */
}