// Called when the final score window pops up
function OutroGui::onWake(%this)
{  
   Canvas.setWidescreen(OutroGuiCenter, 0, false);
   
   %this.initialize();
   
   %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
   OutroClapper.setDML(%path);    
   OutroClapper.forward();
   
   %path = "scriptsAndAssets/data/ui/clapperboard/inoutro/" @ $CurSceneName @ "_intro_take" @ $CurTake @ ".jpg";
   OutroBoard.setDML(%path);        
   OutroBoard.forward();

   OutroClapper.setVisible(true);
   OutroBoard.setVisible(true);

//   OutroClapperOk.setVisible(true);
   
   %this.lastText = "";   
   %this.lastFontSize = "";
   %this.lastTextExtent = 10;
   %this.lastTextOffset = 0;
   %this.lastArrowButtonY = 0;
   %this.clickedOnExit = false;
   
   %this.skip = false;
   
   OutroGuiBkgnd.setDML("scriptsAndAssets/data/ui/ho/inoutro/" @ $CurSceneName @ "_bkgnd.jpg");
//   OutroGuiBkgnd.setDML("scriptsAndAssets/data/ui/scorescreen.jpg");

   scheduleObjLCA(%this, "HO", 0, "clapperOk", 0);
}

// Called when exit button is pressed
function OutroGui::exitButton(%this)
{
   OutroGui.playedMinigame = false;
   OutroGui.exit();
}

function OutroGui::exitFromMinigame(%this)
{
   %this.schedule(100, "exitFromMinigame2");
}

function OutroGui::exitFromMinigame2(%this)
{
   OutroGui.updateProfile();      
   sfxStopAll();
   
   Canvas.popDialog(OutroGui);
   MainMenuGui.goHome();
//   MainMenuGui.pushThread(map_menu_thread);
//   MapGui.update(true);   
   showWorldMap(true);
}

// Exit outro screen and go back to main menu
function OutroGui::exit(%this)
{
   if(%this.playedMinigame)
   {
      OutroBoard.rewind();
      OutroClapper.rewind();
      
      OutroBoard.setVisible(true);
      OutroClapper.setVisible(true);
      
      OutroBoard.play(1);
      OutroClapper.play(1);
      
      sfxPlay(AudioClapperBoardMoving);
            
      return;
   }
   
   sfxStopAll();
   if(SceneGui.inOutro)
   {
      SceneGui.inOutro = false;
      if(isObject(Minigame))
      {
         OutroGui.remove(Minigame);
         Minigame.delete();
      }
   }
   
   if($updateProfileForStarsCheat == true)
   {
      %this.updateProfileForStarsCheat();
      $updateProfileForStarsCheat = false;
   }
   else
      %this.updateProfile();
      
   if(%this.clickedOnExit $= true)
   {      
      OutroBoard.setVisible(true);
      OutroClapper.setVisible(true);
      
      OutroBoard.play(1);
      OutroClapper.play(1);
   }
   else
   {
      // Load map screen
      Canvas.popDialog(OutroGui);
      MainMenuGui.goHome();

      showWorldMap(true);
   }
}

function OutroGui::updateProfile(%this)
{
   $LastCurPlayerStars = $CurPlayerStars;
      
   $StarsWonInTake = 0;
   $LastStarsInTake = 0;
      
   switch$($CurPlayerNum)
   {
      case 1:
//         PlayerProfile.ObjectsTaken1 = $CurObjsTaken;
         PlayerProfile.FirstPlay1 = 0; 
         PlayerProfile.CorrectObjs_1 += $CorrectObjsClickedInScene;
         PlayerProfile.HintObjs_1 += $HintObjectsFound;
         PlayerProfile.Insects1 += $InsectsFound;
         PlayerProfile.Animals1 += $AnimalsFound;
         PlayerProfile.Toys1    += $ToysFound;
      case 2:
//         PlayerProfile.ObjectsTaken2 = $CurObjsTaken;
         PlayerProfile.FirstPlay2 = 0; 
         PlayerProfile.CorrectObjs_2 += $CorrectObjsClickedInScene;
         PlayerProfile.HintObjs_2 += $HintObjectsFound;
         PlayerProfile.Insects2 += $InsectsFound;
         PlayerProfile.Animals2 += $AnimalsFound;
         PlayerProfile.Toys2    += $ToysFound;         
      case 3:
//         PlayerProfile.ObjectsTaken3 = $CurObjsTaken;
         PlayerProfile.FirstPlay3 = 0; 
         PlayerProfile.CorrectObjs_3 += $CorrectObjsClickedInScene;
         PlayerProfile.HintObjs_3 += $HintObjectsFound;
         PlayerProfile.Insects3 += $InsectsFound;
         PlayerProfile.Animals3 += $AnimalsFound;
         PlayerProfile.Toys3    += $ToysFound;         
      case 4:
//         PlayerProfile.ObjectsTaken4 = $CurObjsTaken;
         PlayerProfile.FirstPlay4 = 0; 
         PlayerProfile.CorrectObjs_4 += $CorrectObjsClickedInScene;
         PlayerProfile.HintObjs_4 += $HintObjectsFound;
         PlayerProfile.Insects4 += $InsectsFound;
         PlayerProfile.Animals4 += $AnimalsFound;
         PlayerProfile.Toys4    += $ToysFound;         
   }   
   
   switch$($CurPlayerNum)
   {
      case 1: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene1;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake1;
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake1;
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake1;
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake1;
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake1;
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake1;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake1;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake1;
         }

         if(getWord($CurPlayerStarsTake, $CurTake-1) < SceneGui.starsGained){

            $LastStarsInTake = getWord($CurPlayerStarsTake, $CurTake-1);
            $StarsWonInTake = SceneGui.starsGained - getWord($CurPlayerStarsTake, $CurTake-1);

            PlayerProfile.PlayerStars1 += $StarsWonInTake;
            $CurPlayerStars     = PlayerProfile.PlayerStars1;
            
            $CurPlayerStarsTake = setWord($CurPlayerStarsTake, $CurTake-1, SceneGui.starsGained);
         }
      case 2: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene2;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake2;         
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake2;         
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake2;         
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake2;         
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake2;   
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake2;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake2;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake2;
         }
         
         if(getWord($CurPlayerStarsTake, $CurTake-1) < SceneGui.starsGained){

            $LastStarsInTake = getWord($CurPlayerStarsTake, $CurTake-1);
            $StarsWonInTake = SceneGui.starsGained - getWord($CurPlayerStarsTake, $CurTake-1);

            PlayerProfile.PlayerStars2 += $StarsWonInTake;
            $CurPlayerStars     = PlayerProfile.PlayerStars2;
            
            $CurPlayerStarsTake = setWord($CurPlayerStarsTake, $CurTake-1, SceneGui.starsGained);
         }
      case 3: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene3;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake3;         
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake3;         
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake3;         
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake3;         
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake3;    
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake3;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake3;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake3;   
         }
         if(getWord($CurPlayerStarsTake, $CurTake-1) < SceneGui.starsGained){

            $LastStarsInTake = getWord($CurPlayerStarsTake, $CurTake-1);
            $StarsWonInTake = SceneGui.starsGained - getWord($CurPlayerStarsTake, $CurTake-1);

            PlayerProfile.PlayerStars3 += $StarsWonInTake;
            $CurPlayerStars     = PlayerProfile.PlayerStars3;
            
            $CurPlayerStarsTake = setWord($CurPlayerStarsTake, $CurTake-1, SceneGui.starsGained);
         }
      case 4: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene4;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake4;         
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake4;         
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake4;
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake4;         
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake4;  
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake4;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake4;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake4;   
         }
         if(getWord($CurPlayerStarsTake, $CurTake-1) < SceneGui.starsGained){

            $LastStarsInTake = getWord($CurPlayerStarsTake, $CurTake-1);
            $StarsWonInTake = SceneGui.starsGained - getWord($CurPlayerStarsTake, $CurTake-1);

            PlayerProfile.PlayerStars4 += $StarsWonInTake;
            $CurPlayerStars     = PlayerProfile.PlayerStars4;
            
            $CurPlayerStarsTake = setWord($CurPlayerStarsTake, $CurTake-1, SceneGui.starsGained);
         }
   }
   
   if($CurPlayerUTake < 5)
   {
      // Unlock take?
      if($CurTake $= $CurPlayerUTake)
      {
         // Unlock only if minimum number of stars gained is 1
         if(SceneGui.starsGained >= 1)
            $CurPlayerUTake++;
      }
   }   
   
   %newUScene = $CurPlayerUScene;
   
//   if($CurPlayerStars < 5) // Paris
//      %newUScene = 1;
   if($CurPlayerStars >= 3 && $CurPlayerStars < 6) // Fiji
      %newUScene = 2;
   if($CurPlayerStars >= 6 && $CurPlayerStars < 12) // London
      %newUScene = 3;
   if($CurPlayerStars >= 12 && $CurPlayerStars < 25) // Hong Kong
      %newUScene = 4;
   if($CurPlayerStars >= 25 && $CurPlayerStars < 50) // Mexico
      %newUScene = 5;
   if($CurPlayerStars >= 50 && $CurPlayerStars < 75) // Tokyo
      %newUScene = 6;
   if($CurPlayerStars >= 75 && $CurPlayerStars < 100) // New York
      %newUScene = 7;
   if($CurPlayerStars >= 100) // Hollywood
      %newUScene = 8;
      
   // Any scene was unlocked?
   if($CurPlayerUScene !$= %newUScene)
   {
      $LastUnlockedScene = $CurPlayerUScene;
      $CurUnlockedScene = %newUScene;
      
      $CurPlayerUScene = %newUScene;
      
      $SceneJustUnlocked = true;
   }
   else
   {
      $SceneJustUnlocked = false;
      $LastUnlockedScene = "";
   }

   switch$($CurPlayerNum)
   {
      case 1:
         PlayerProfile.PlayerUScene1         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake1          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake1      = $CurPlayerStarsTake;
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake1     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake1 = $CurPlayerStarsTake;         
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake1       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake1   = $CurPlayerStarsTake;         
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake1      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake1  = $CurPlayerStarsTake;         
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake1     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake1 = $CurPlayerStarsTake;  
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake1        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake1    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake1      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake1  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake1      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake1  = $CurPlayerStarsTake;                               
         }
         PlayerProfile.PlayerStars1          = $CurPlayerStars;
      case 2: 
         PlayerProfile.PlayerUScene2         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake2          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake2      = $CurPlayerStarsTake;         
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake2     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake2 = $CurPlayerStarsTake;                  
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake2       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake2   = $CurPlayerStarsTake;                  
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake2      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake2  = $CurPlayerStarsTake;                  
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake2     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake2 = $CurPlayerStarsTake;       
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake2        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake2    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake2      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake2  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake2      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake2  = $CurPlayerStarsTake;           
         }
         PlayerProfile.PlayerStars2          = $CurPlayerStars;
      case 3: 
         PlayerProfile.PlayerUScene3         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake3          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake3      = $CurPlayerStarsTake;
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake3     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake3 = $CurPlayerStarsTake;
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake3       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake3   = $CurPlayerStarsTake;
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake3      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake3  = $CurPlayerStarsTake;
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake3     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake3 = $CurPlayerStarsTake;
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake3        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake3    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake3      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake3  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake3      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake3  = $CurPlayerStarsTake;                         
         }
         PlayerProfile.PlayerStars3          = $CurPlayerStars;
      case 4: 
         PlayerProfile.PlayerUScene4         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake4          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake4      = $CurPlayerStarsTake;         
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake4     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake4 = $CurPlayerStarsTake;         
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake4       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake4   = $CurPlayerStarsTake;         
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake4      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake4  = $CurPlayerStarsTake;         
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake4     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake4 = $CurPlayerStarsTake;  
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake4        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake4    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake4      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake4  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake4      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake4  = $CurPlayerStarsTake;                                
         }
         PlayerProfile.PlayerStars4          = $CurPlayerStars;
   }
      
   SavePlayerProfile();   
}

function OutroGui::updateProfileForStarsCheat(%this)
{
//   SceneGui.starsGained = 5;
   $LastCurPlayerStars = $CurPlayerStars;

   $LastStarsInTake = 0;
   
   switch$($CurPlayerNum)
   {
      case 1: 
         PlayerProfile.FirstPlay1 = 0; 
         PlayerProfile.CorrectObjs_1 += $CorrectObjsClickedInScene;
      case 2: 
         PlayerProfile.FirstPlay2 = 0; 
         PlayerProfile.CorrectObjs_2 += $CorrectObjsClickedInScene;
      case 3:
         PlayerProfile.FirstPlay3 = 0; 
         PlayerProfile.CorrectObjs_3 += $CorrectObjsClickedInScene;
      case 4:
         PlayerProfile.FirstPlay4 = 0; 
         PlayerProfile.CorrectObjs_4 += $CorrectObjsClickedInScene;
   }   
   
   switch$($CurPlayerNum)
   {
      case 1: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene1;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake1;
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake1;
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake1;
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake1;
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake1;
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake1;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake1;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake1;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake1;
         }

            PlayerProfile.PlayerStars1 += $StarsWonInTake;
            $CurPlayerStars     = PlayerProfile.PlayerStars1;

      case 2: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene2;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake2;         
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake2;         
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake2;         
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake2;         
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake2;   
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake2;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake2;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake2;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake2;
         }
         

            PlayerProfile.PlayerStars2 += $StarsWonInTake;
            $CurPlayerStars     = PlayerProfile.PlayerStars2;

         
      case 3: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene3;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake3;         
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake3;         
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake3;         
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake3;         
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake3;    
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake3;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake3;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake3;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake3;   
         }

            PlayerProfile.PlayerStars3 += $StarsWonInTake;
            $CurPlayerStars     = PlayerProfile.PlayerStars3;

      case 4: 
         $CurPlayerUScene        = PlayerProfile.PlayerUScene4;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : $CurPlayerUTake     = PlayerProfile.PlayerSpyUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSpyStarsTake4;         
         case "GangsterLevel": $CurPlayerUTake     = PlayerProfile.PlayerGangsterUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerGangsterStarsTake4;         
         case "KungFuLevel"  : $CurPlayerUTake     = PlayerProfile.PlayerKungFuUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerKungFuStarsTake4;
         case "MusicalLevel" : $CurPlayerUTake     = PlayerProfile.PlayerMusicalUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMusicalStarsTake4;         
         case "DinosaurLevel": $CurPlayerUTake     = PlayerProfile.PlayerDinosaurUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerDinosaurStarsTake4;  
         case "SciFiLevel":    $CurPlayerUTake     = PlayerProfile.PlayerSciFiUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerSciFiStarsTake4;
         case "WesternLevel":  $CurPlayerUTake     = PlayerProfile.PlayerWesternUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerWesternStarsTake4;
         case "MonsterLevel":  $CurPlayerUTake     = PlayerProfile.PlayerMonsterUTake4;
                               $CurPlayerStarsTake = PlayerProfile.PlayerMonsterStarsTake4;   
         }

         PlayerProfile.PlayerStars4 += $StarsWonInTake;
         $CurPlayerStars     = PlayerProfile.PlayerStars4;

   }
   
   if($CurPlayerUTake < 4)
   {
      // Unlock take?
      if($CurTake $= $CurPlayerUTake){
         $CurPlayerUTake++;
      }
   }
   
   %newUScene = $CurPlayerUScene;

//   if($CurPlayerStars < 5) // Paris
//      %newUScene = 1;
   if($CurPlayerStars >= 3 && $CurPlayerStars < 5) // Fiji
      %newUScene = 2;
   if($CurPlayerStars >= 5 && $CurPlayerStars < 10) // London
      %newUScene = 3;
   if($CurPlayerStars >= 10 && $CurPlayerStars < 25) // Hong Kong
      %newUScene = 4;
   if($CurPlayerStars >= 25 && $CurPlayerStars < 50) // Mexico
      %newUScene = 5;
   if($CurPlayerStars >= 50 && $CurPlayerStars < 75) // Tokyo
      %newUScene = 6;
   if($CurPlayerStars >= 75 && $CurPlayerStars < 100) // New York
      %newUScene = 7;
   if($CurPlayerStars >= 100) // Hollywood
      %newUScene = 8;
            
   // Any scene was unlocked?
   if($CurPlayerUScene !$= %newUScene)
   {
      $LastUnlockedScene = $CurPlayerUScene;
      $CurUnlockedScene = %newUScene;
      
      $CurPlayerUScene = %newUScene;
            
      $SceneJustUnlocked = true;
   }
   else
      $SceneJustUnlocked = false;

   switch$($CurPlayerNum)
   {
      case 1:
         PlayerProfile.PlayerUScene1         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake1          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake1      = $CurPlayerStarsTake;
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake1     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake1 = $CurPlayerStarsTake;         
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake1       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake1   = $CurPlayerStarsTake;         
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake1      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake1  = $CurPlayerStarsTake;         
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake1     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake1 = $CurPlayerStarsTake;  
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake1        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake1    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake1      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake1  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake1      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake1  = $CurPlayerStarsTake;                               
         }
         PlayerProfile.PlayerStars1          = $CurPlayerStars;
      case 2: 
         PlayerProfile.PlayerUScene2         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake2          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake2      = $CurPlayerStarsTake;         
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake2     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake2 = $CurPlayerStarsTake;                  
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake2       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake2   = $CurPlayerStarsTake;                  
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake2      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake2  = $CurPlayerStarsTake;                  
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake2     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake2 = $CurPlayerStarsTake;       
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake2        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake2    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake2      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake2  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake2      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake2  = $CurPlayerStarsTake;           
         }
         PlayerProfile.PlayerStars2          = $CurPlayerStars;
      case 3: 
         PlayerProfile.PlayerUScene3         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake3          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake3      = $CurPlayerStarsTake;
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake3     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake3 = $CurPlayerStarsTake;
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake3       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake3   = $CurPlayerStarsTake;
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake3      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake3  = $CurPlayerStarsTake;
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake3     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake3 = $CurPlayerStarsTake;
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake3        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake3    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake3      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake3  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake3      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake3  = $CurPlayerStarsTake;                         
         }
         PlayerProfile.PlayerStars3          = $CurPlayerStars;
      case 4: 
         PlayerProfile.PlayerUScene4         = $CurPlayerUScene;
         switch$($CurSceneName)
         {
         case "SpyLevel"     : PlayerProfile.PlayerSpyUTake4          = $CurPlayerUTake;
                               PlayerProfile.PlayerSpyStarsTake4      = $CurPlayerStarsTake;         
         case "GangsterLevel": PlayerProfile.PlayerGangsterUTake4     = $CurPlayerUTake;
                               PlayerProfile.PlayerGangsterStarsTake4 = $CurPlayerStarsTake;         
         case "KungFuLevel"  : PlayerProfile.PlayerKungFuUTake4       = $CurPlayerUTake;
                               PlayerProfile.PlayerKungFuStarsTake4   = $CurPlayerStarsTake;         
         case "MusicalLevel" : PlayerProfile.PlayerMusicalUTake4      = $CurPlayerUTake;
                               PlayerProfile.PlayerMusicalStarsTake4  = $CurPlayerStarsTake;         
         case "DinosaurLevel": PlayerProfile.PlayerDinosaurUTake4     = $CurPlayerUTake;
                               PlayerProfile.PlayerDinosaurStarsTake4 = $CurPlayerStarsTake;  
         case "SciFiLevel":    PlayerProfile.PlayerSciFiUTake4        = $CurPlayerUTake;
                               PlayerProfile.PlayerSciFiStarsTake4    = $CurPlayerStarsTake;                        
         case "WesternLevel":  PlayerProfile.PlayerWesternUTake4      = $CurPlayerUTake;
                               PlayerProfile.PlayerWesternStarsTake4  = $CurPlayerStarsTake;                        
         case "MonsterLevel":  PlayerProfile.PlayerMonsterUTake4      = $CurPlayerUTake;
                               PlayerProfile.PlayerMonsterStarsTake4  = $CurPlayerStarsTake;                                
         }
         PlayerProfile.PlayerStars4          = $CurPlayerStars;
   }
      
   SavePlayerProfile();   
}

function OutroGui::clapperOk(%this)
{   
   OutroClapperOk.setVisible(false);
/*   
   if($returningFromScene){
      %this.exit();
      return;
   }   
*/   
      
   if(!%this.playMinigame || SceneGui.earlyQuit)
   {
      OutroClapper.setVisible(false);
      OutroBoard.setVisible(false);

      %this.exit();
      return;
   }
      
   // Play background noise
   sfxPlay(BkgndNoise@SceneGui.currentLevel);     
      
   OutroClapper.playRev(1);
   OutroBoard.playRev(1);
   
   sfxPlay(AudioClapperBoardMoving);
}

function OutroClapper::onStoppedAtStart(%this)
{
   OutroClapper.setVisible(false);
   OutroBoard.setVisible(false);
}

function OutroClapper::onStoppedAtEnd(%this)
{
   if(OutroGui.clickedOnExit $= true)
      scheduleObjLCA(OutroGui, "HO", 200, "exitClicked", 0);      

   //OutroClapperOk.setVisible(true);
   OutroGui.playMinigame = false;
   OutroGui.playedMinigame = false;      
}

function OutroGui::exitClicked(%this)
{
   $returningFromScene = true;
   OutroGui.clickedOnExit = false;
   
   // Load map screen
   Canvas.popDialog(OutroGui);
   MainMenuGui.goHome();

   showWorldMap(true);   
}

function OutroGui::initialize(%this)
{
   %this.playedMinigame = false;
   
   OutroDirTriggerImga.setAlpha(0);
   OutroDirTriggerImga.setVisible(false);
   OutroDirTriggerImgb.setAlpha(0);
   OutroDirTriggerImgb.setVisible(false);
   %this.crossFadingDirector = false;
   
   OutroDirTriggerBubble.setVisible(false);   
   OutroDirTriggerBubbleText.setVisible(false);
   OutroDirTriggerBubbleText.setText("");
   
   OutroDirTriggerArrow.setVisible(false);
   
   %this.loadDirectorXML();
}

// Parse XML file looking for director behavior tags
function OutroGui::loadDirectorXML(%this)
{
   %this.playMinigame = false;   
   %path = "./scriptsAndAssets/data/ui/director" @ $CurSceneName @ "_take" @ $CurTake @ ".xml";
   
   %pXml = new SimXMLDocument();
   // Load and parse XML file.
   if(!%pXml.loadFile(%path))
   {
      echo("Failed to load " @ %path);
      return;
   }
   
   // Push the first child element named "director" onto 
   // the XML document stack.
   if(!%pXml.pushFirstChildElement("director"))
   {
      echo("Failed to load director tag.");
      return;
   }
   
   // Fields of SceneGui.dirTrigger[SceneGui.dirTriggerNum]      
   // 10 = text size

   %this.DirTriggerNum = 0;
   if(!%pXml.pushFirstChildElement("ani"))
   {
      echo("Director animations not found.");
      return;
   }
   
   while(true)
   {
      %showat = %pXml.attribute("showat");
             
      if(%showat $= "outro")
      {
         %showat = %pXml.attributeF32("showat");
         
         %textNum = 0;
         if(%pXml.pushFirstChildElement("bubble"))
         {
            %this.DirTrigger[%this.DirTriggerNum,"text",%textNum] = %pXml.attribute("text");
            %this.DirTrigger[%this.DirTriggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
            %this.DirTrigger[%this.DirTriggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;  
            %this.DirTrigger[%this.DirTriggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");
            %this.DirTrigger[%this.DirTriggerNum,"hintValue",%textNum]=%pXml.attributeS32("hintvalue");
            %this.DirTrigger[%this.DirTriggerNum,"hintCondition",%textNum]=%pXml.attribute("hintcondition");

            // Pause button?
            %havePauseButton = %pXml.attribute("pause");
            if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
            %this.DirTrigger[%this.DirTriggerNum,"pause",%textNum]=%havePauseButton;
            
            // Play minigame?
            %playMinigame = %pXml.attribute("playminigame");
            if(%playMinigame !$= "true") %playMinigame = false; else %playMinigame = true;
            %this.DirTrigger[%this.DirTriggerNum,"playMinigame",%textNum]=%playMinigame;
            if(%playMinigame) %this.playMinigame = true;
            
            %textNum++;         
         
            while(true)
            {
               if(%pXml.nextSiblingElement("bubble"))
               {
                  %this.DirTrigger[%this.DirTriggerNum,"text",%textNum] = %pXml.attribute("text");
                  %this.DirTrigger[%this.DirTriggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
                  %this.DirTrigger[%this.DirTriggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;
                  %this.DirTrigger[%this.DirTriggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");
                  %this.DirTrigger[%this.DirTriggerNum,"hintValue",%textNum]=%pXml.attributeS32("hintvalue");
                  %this.DirTrigger[%this.DirTriggerNum,"hintCondition",%textNum]=%pXml.attribute("hintcondition");

                  // Pause button?                  
                  %havePauseButton = %pXml.attribute("pause");
                  if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
                  %this.DirTrigger[%this.DirTriggerNum,"pause",%textNum]=%havePauseButton;
                  
                  // Play minigame?
                  %playMinigame = %pXml.attribute("playminigame");
                  if(%playMinigame !$= "true") %playMinigame = false; else %playMinigame = true;
                  %this.DirTrigger[%this.DirTriggerNum,"playMinigame",%textNum]=%playMinigame;
                  if(%playMinigame) %this.playMinigame = true;                  
                  
                  %textNum++;
               }
               else
               {
                  %this.DirTrigger[%this.DirTriggerNum,"text",%textNum] = "";
                  break;               
               }
            }
            %pXml.popElement();
         }
         
         %this.showDirAni1(%this.DirTriggerNum);
            
         %this.DirTriggerNum++;            
      }
      
      // Go to next animation tag
      if(!%pXml.nextSiblingElement("ani"))
         break;
   }
   
   %pXml.popElement();
   
   %pXml.delete();   
}

// Show director (stage 1: hide director chair)
function OutroGui::showDirAni1(%this, %triggerNum)
{  
   %this.DirTriggerImg = OutroDirTriggerImga;   
   %this.CurTriggerNum = %triggerNum;
   %this.showDirAni2(%triggerNum);
}

// Show director (stage 2: show director and bubble)
function OutroGui::showDirAni2(%this, %triggerNum)
{
   %path = "scriptsAndAssets/data/ui/ho/inoutro/";    
   switch$($CurSceneName)
   {
      case "SpyLevel":      %path = %path @ "directors/spy";
      case "GangsterLevel": %path = %path @ "directors/gangster";
      case "KungFuLevel":   %path = %path @ "directors/kungfu";
      case "MusicalLevel":  %path = %path @ "directors/musical";
      case "DinosaurLevel": %path = %path @ "directors/dinosaur";
      case "SciFiLevel":    %path = %path @ "directors/scifi";
      case "WesternLevel":  %path = %path @ "directors/western";
      case "MonsterLevel":  %path = %path @ "directors/monster";
   }
   %this.DirTriggerImg.setDML(%path @ %this.DirTrigger[%triggerNum,"imageid",0] @ ".png");
   %this.DirTriggerImg.Visible = true;
     
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(OutroGuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(OutroGuiCenter.getExtent(),1);   

   %showTime = 500;
   %this.DirTriggerImg.setAlpha(0);
   %this.DirTriggerImg.setVisible(true);         
   %this.DirTriggerImg.animateAlpha(1, 0, 500, "0 0");         

   %this.schedule(%showTime + 200, "showDirAniText1", %triggerNum, 0);
}

// Vertically centers ML text in a bubble control
// We use a scheduling scheme to wait for Torque to update the control extent
// so we can compute the center position.
// %bubble = instance of bubble bitmap control
// %text = instance of Gui to be centered
// %origText = extent of %text
// %vOffset = a vertical offset to be added lastly to the text
// %arrowButtonY = vertical position of the arrow button, if presented
function OutroGui::vCenterTextInBubble(%this, %bubble, %text, %origExtent, %vOffset, %arrowButtonY)
{
   %bubble.setVisible(true);
   %text.setVisible(true);  
   
/*   %extent = %text.getExtent();   
   if(getWord(%extent,1) !$= getWord(%origExtent,1))
   {
      %centery = (getWord(%bubble.getExtent(),1) - getWord(%extent,1)) / 2;
      
      %newY = getWord(%bubble.getPosition(),1)+%centery;
      if(%vOffset !$= 0 && getWord(%extent,1)+%newY < %arrowButtonY)
         %vOffset = 0;
      %text.setPosition(getWord(%text.getPosition(),0), %newY+%vOffset);
      
      %bubble.setVisible(true);
      %text.setVisible(true);
   }
   else
      %this.schedule(50, "vCenterTextInBubble", %bubble, %text, %origExtent, %vOffset, %arrowButtonY);*/   
}

// Show director bubble text
function OutroGui::showDirAniText1(%this, %triggerNum, %textNum)
{
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(OutroGuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(OutroGuiCenter.getExtent(),1); 
   
   // Check hint condition
   %hintvalue = %this.DirTrigger[%triggerNum,"hintvalue",%textNum];
   %hintcondition = %this.DirTrigger[%triggerNum,"hintcondition",%textNum];
   if(%hintcondition !$= "")
   {
      %skip = true;
      
      switch$(%hintcondition)
      {
         case "less":
            if(Minigame.hintsGained < %hintvalue)
               %skip = false;
         
         case "greater":
            if(Minigame.hintsGained > %hintvalue)
               %skip = false;
      }
      
      if(%skip)
      {
         %this.showDirAniText3(%triggerNum, %textNum, 0);
         return;
      }
   }

   %path = "scriptsAndAssets/data/ui/ho/inoutro/";
   switch$($CurSceneName)
   {
      case "SpyLevel":      %path = %path @ "directors/spy";
      case "GangsterLevel": %path = %path @ "directors/gangster";
      case "KungFuLevel":   %path = %path @ "directors/kungfu";
      case "MusicalLevel":  %path = %path @ "directors/musical";
      case "DinosaurLevel": %path = %path @ "directors/dinosaur";
      case "SciFiLevel":    %path = %path @ "directors/scifi";
      case "WesternLevel":  %path = %path @ "directors/western";
      case "MonsterLevel":  %path = %path @ "directors/monster";
   }
//   %this.DirTriggerImg.setDML(%path @ %this.DirTrigger[%triggerNum,"imageid",%textNum] @ ".png");

   %newDML = %path @ %this.DirTrigger[%triggerNum,"imageid",%textNum] @ ".png";
   %this.swapDirectors(%newDML, 200);

   %vOffset = 0;
   %arrowButtonY = 280;
   %pause = %this.DirTrigger[%triggerNum,"pause",%textNum];
   if(%pause) 
      %vOffset -= getWord(OutroDirTriggerArrow.getExtent(),1)/2;

   %text = %this.DirTrigger[%triggerNum,"text",%textNum];
   %text = strreplace(%text, "%player%" , $CurPlayerName);
   
   %this.lastText = %text;
   %this.lastFontSize = %this.DirTrigger[%triggerNum,"textSize",%textNum];
   %this.lastTextOffset = %vOffset;
   %this.lastArrowButtonY = %arrowButtonY;
   
   //OutroDirTriggerBubbleText.setText("<just:center><font:Comic Sans MS:"@%this.DirTrigger[%triggerNum,"textSize",%textNum]@">"@%text);
   //%this.vCenterTextInBubble(OutroDirTriggerBubble, OutroDirTriggerBubbleText, %extent, %vOffset, %arrowButtonY);
   %this.updateSpeechBubble();
   
   if(%pause)
   {
       OutroDirTriggerArrow.setVisible(true);
       OutroDirTriggerArrow.triggerNum = %triggerNum;
       OutroDirTriggerArrow.textNum = %textNum;
   }
   else
   {
      OutroDirTriggerArrow.setVisible(false);
      %this.showDirAniText2(%triggerNum, %textNum);
   }
}

// Update the speech bubble text
function OutroGui::updateSpeechBubble(%this)
{
//   if(%this.lastFontSize $= "")
//      return;
   
   %text = %this.lastText;   
   %fontSize = %this.lastFontSize * Canvas.scaleSize;
   if(%fontSize > 62) %fontSize = 62;
   
   OutroDirTriggerBubbleText.setVisible(false);
   //OutroDirTriggerBubbleText.setExtent(getWord(OutroDirTriggerBubbleText.getExtent(),0),1);
   //%extent = OutroDirTriggerBubbleText.getExtent();   
   %vOffset      = %this.lastTextOffset;
   %arrowButtonY = %this.lastArrowButtonY;
   
   OutroDirTriggerBubbleText.setText(%text);
   %this.vCenterTextInBubble(OutroDirTriggerBubble, OutroDirTriggerBubbleText, %extent, %vOffset, %arrowButtonY);
}

// Second part of showDirAniText2
function OutroGui::showDirAniText2(%this, %triggerNum, %textNum)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   // Go directly to minigame or exit
   if(%this.skip)
   {
      // Do we have a minigame at the end?
      do 
      {
         if(%this.DirTrigger[%triggerNum,"playMinigame",%textNum])
         {
            %this.playMinigame();
            %this.triggerNum = %triggerNum;
            %this.textNum = %textNum;
            return;            
         }
         %textNum++;
      } while(%this.DirTrigger[%triggerNum,"text",%textNum] !$= "")
   }
      
   %classID = "HO";   

   %duration = %this.DirTrigger[%triggerNum,"textDuration",%textNum];
   if(%duration < 0)
      %duration = 0;

   if(%this.DirTrigger[%triggerNum,"pause",%textNum])
      %duration = 0;
      
   // Play minigame?
   if(%this.dirTrigger[%triggerNum,"playMinigame",%textNum])
   {
      %this.playMinigame();      
      %this.triggerNum = %triggerNum;
      %this.textNum = %textNum;
      return;      
   }

   %this.showDirAniText3(%triggerNum, %textNum, %duration);
}

function OutroGui::showDirAniText3(%this, %triggerNum, %textNum, %duration)
{
   if(%this.DirTrigger[%triggerNum,"text",%textNum+1] !$= "")
   {
      // Show next text
      %this.schedule(%duration, "showDirAniText1", %triggerNum, %textNum+1);
   }
   else
   {
      // End of text tags
      %this.schedule(%duration, "hideDirAni", %triggerNum);
   }   
}

// Hide director
function OutroGui::hideDirAni(%this, %triggerNum)
{
   %this.lastFontSize = "";   
   OutroDirTriggerArrow.setVisible(false);  
   OutroDirTriggerBubble.Visible = false;
   OutroDirTriggerBubbleText.Visible = false;
   
   %showTime = 500;
   %this.DirTriggerImg.setAlpha(1);
   %this.DirTriggerImg.setVisible(true);
   %this.DirTriggerImg.animateAlpha(0, 0, %showTime, "0 0");

   %this.exitButton();   
}

function OutroGui::onResize(%this, %width, %height)
{
   %fontSize = 36 * Canvas.scaleSize; if(%fontSize > 62) %fontSize = 62;
   GuiLCAButtonProfile2.fontSize = %fontSize;
   
   OutroGuiExitButton.setProfile(GuiDefaultProfile);   
   OutroGuiExitButton.setProfile(GuiLCAButtonProfile2);
   
   %this.updateSpeechBubble();   
   
   Canvas.setWidescreen(OutroGuiCenter, 2000, true);
}

// Play minigame during outro screen
function OutroGui::playMinigame(%this)
{
   %this.playedMinigame = true;
   sfxStopAll();   
   SceneGui.inOutro = true;
   if(isObject(Minigame))
      Minigame.delete();
   exec("scriptsAndAssets/client/ui/Minigame.gui");
   OutroGui.add(Minigame);
   OutroGui.pushToBack(Minigame);
   
   Minigame.play();
}

// Triggers a cross-fading effect between current director's talking pose and next pose
// %newDML is the path of the new pose image
// %aniTime Crossfading time, in ms
function OutroGui::swapDirectors(%this, %newDML, %aniTime)
{
   if(%this.DirTriggerImg $= OutroDirTriggerImga)
   {
      %objOrig = OutroDirTriggerImga;
      %objDest = OutroDirTriggerImgb;
   }
   else
   {
      %objOrig = OutroDirTriggerImgb;
      %objDest = OutroDirTriggerImga;
   }
   
   %objDest.setDML(%newDML);
   if(%objOrig.DML $= %objDest.DML)
      return;   

   %objOrig.setAlpha(1);
   %objOrig.setVisible(true);
   %objDest.setAlpha(0);
   %objDest.setVisible(true);   

   %this.DirTriggerImg = %objDest;
   %this.crossFadingDirector = true;
   %objOrig.animateAlpha(0, 0, %aniTime, "0 0");
   %objDest.animateAlpha(1, 0, %aniTime, "0 0");
   scheduleObjLCA(%this, "HO", %aniTime, "onEndDirectorCrossFading", 0);   
}

// Called when the crossfading effect of director's finishes
function OutroGui::onEndDirectorCrossFading(%this)
{
   %this.crossFadingDirector = false;
}
