// %obj = MissionInfo object
function SceneGui::initDirectorMeter(%this, %score)
{
   // Whether we are displaying DirectorChair1 or DirectorChair2   
   %this.directorChairUsing1 = true;

   DirectorChair1.setAlpha(1);
   DirectorChair2.setAlpha(0);

   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);

   // Position and dimensions of MeterStars
   %this.MeterStarPX[1] = 597; %this.MeterStarPY[1] = 156;
   %this.MeterStarPX[2] = 622; %this.MeterStarPY[2] = 115;
   %this.MeterStarPX[3] = 589; %this.MeterStarPY[3] =  85;
   %this.MeterStarPX[4] = 619; %this.MeterStarPY[4] =  33;
   %this.MeterStarPX[5] = 588; %this.MeterStarPY[5] = -32;
   
   if(!($pref::MeterPositionRight $= "true" || $pref::MeterPositionRight $= 1))
   {
      %this.MeterStarPX[1] -= 620;
      %this.MeterStarPX[2] -= 620;
      %this.MeterStarPX[3] -= 620;
      %this.MeterStarPX[4] -= 620;
      %this.MeterStarPX[5] -= 620;
   }
   else
   {
      Cam0.setPosition(7, 3);
      CamMouseHandler.setPosition(7, 3);
   }
   
   %this.MeterStarEX[1] =  74; %this.MeterStarEY[1] =  75;
   %this.MeterStarEX[2] =  85; %this.MeterStarEY[2] =  85;
   %this.MeterStarEX[3] =  96; %this.MeterStarEY[3] =  85;
   %this.MeterStarEX[4] =  98; %this.MeterStarEY[4] = 104;
   %this.MeterStarEX[5] = 124; %this.MeterStarEY[5] = 127;
   
   MeterStar1.setPosition(%this.MeterStarPX[1],%this.MeterStarPY[1]);
   MeterStar2.setPosition(%this.MeterStarPX[2],%this.MeterStarPY[2]);
   MeterStar3.setPosition(%this.MeterStarPX[3],%this.MeterStarPY[3]);
   MeterStar4.setPosition(%this.MeterStarPX[4],%this.MeterStarPY[4]);
   MeterStar5.setPosition(%this.MeterStarPX[5],%this.MeterStarPY[5]);
   
   if($CurSceneName $= "SpyLevel"){ // Spy level
      %path = "scriptsAndAssets/data/ui/ho/spy";

      %this.directorX = 559;
      %this.directorY = 291;
      %this.directorW = 241;
      %this.directorH = 309;
      
   }else if($CurSceneName $= "GangsterLevel"){ // Gangster level
      %path = "scriptsAndAssets/data/ui/ho/gangster";   

      %this.directorX = 627;
      %this.directorY = 370;
      %this.directorW = 173;
      %this.directorH = 230;

   }else if($CurSceneName $= "KungFuLevel"){ // Martial arts level
      %path = "scriptsAndAssets/data/ui/ho/kungfu";

      %this.directorX = 627;
      %this.directorY = 331;
      %this.directorW = 173;
      %this.directorH = 269;

   }else if($CurSceneName $= "MusicalLevel"){ // Big musical level
      %path = "scriptsAndAssets/data/ui/ho/musical";

      %this.directorX = 627;
      %this.directorY = 324;
      %this.directorW = 173;
      %this.directorH = 276;

   }else if($CurSceneName $= "DinosaurLevel"){ // Dinosaur level
      %path = "scriptsAndAssets/data/ui/ho/dinosaur";

      %this.directorX = 629;
      %this.directorY = 298;
      %this.directorW = 171;
      %this.directorH = 302;
      
   }else if($CurSceneName $= "SciFiLevel"){ // SciFi level
      %path = "scriptsAndAssets/data/ui/ho/scifi";

      %this.directorX = 629;
      %this.directorY = 298;
      %this.directorW = 171;
      %this.directorH = 302;
      
   }else if($CurSceneName $= "WesternLevel"){ // Western level
      %path = "scriptsAndAssets/data/ui/ho/western";

      %this.directorX = 629;
      %this.directorY = 298;
      %this.directorW = 171;
      %this.directorH = 302;
      
   }else if($CurSceneName $= "MonsterLevel"){ // Monster level
      %path = "scriptsAndAssets/data/ui/ho/monster";

      %this.directorX = 629;
      %this.directorY = 298;
      %this.directorW = 171;
      %this.directorH = 302;
   }
      
   Chair.setDML(%path @ "/chair.png");
   
   if($pref::MeterPositionRight $= "true" || $pref::MeterPositionRight $= 1)
   {
      
      if($pref::NShoppingListItems5 $= "true" || $pref::NShoppingListItems5 $= 1){
         HUD.setDML(%path @ "/bg.png");
      }else{
         HUD.setDML(%path @ "/bg7.png");
      }
      
      DirectorMeterBar.setPosition(614,4);
      DirectorMeterBarGlow.setPosition(614,4);      
      DirectorMeter.setPosition(640-30,50-30);

//      Cam0.setPosition(9, 4);
   }
   else
   {
      HUD.setDML(%path @ "/bgL.png");
      
      DirectorMeterBar.setPosition(614-620,4);
      DirectorMeterBarGlow.setPosition(614-620,4);      
      DirectorMeter.setPosition(640-30-620,50-30);

//      Cam0.setPosition(9+21, 4);
   }
   
   DirectorChairHint.setDML(%path @ "/director_hint.png");
   
   %path = %path @ "/director_normal.png";
   DirectorChair1.setDML(%path);
   DirectorChair2.setDML(%path);

   %this.hiddenDirectorChair = false;   
   
   %this.starsGained = 0;
   %this.directorMeterMax = 60;   
   %this.directorMeterCur = %score;
   %this.directorMeterFracHolder = 0;
//   %this.directorMeterCur = %this.directorMeterMax;
   
   %this.directorMeterCurDML = "";   
   %this.directorMeterLastDML = "";
   if(%this.directorMeterCur > %this.directorMeterMax) %this.directorMeterCur = %this.directorMeterMax;
   if(%this.directorMeterCur < 1)  %this.directorMeterCur = 1;
   //DirectorMeter.setDML("scriptsAndAssets/data/ui/ho/meter/"@%this.directorMeterCur@".png");
   
   for (%i = 1; %i <= %this.directorMeterMax; %i++)
   {
      %this.gradPX[%i] = 30;
      %this.gradPY[%i] = (421 + 30 - %i*7);
      %this.gradEX[%i] = 23;
      %this.gradEY[%i] = 7;
      
      %grad = new GuiPlasticBitmapCtrl() {
         canSaveDynamicFields = "0";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = %this.gradPX[%i] SPC %this.gradPY[%i];
         Extent = %this.gradEX[%i] SPC %this.gradEY[%i];
         MinExtent = "2 2";
         canSave = "1";
         Visible = %i <= %this.directorMeterCur;
         hovertime = "1000";
         alpha = (%i <= %this.directorMeterCur) ? "1" : "0";
         color = "255 255 255 255";
         unitClip = "0 0 1 1";
         DML = "scriptsAndAssets/data/ui/ho/meter/meter"@%i@".png";
         wrap = "0";
         isPulsing = false;
      };
            
      DirectorMeter.add(%grad);
   }

   %this.directorMeterEnablePulse = false;
   %this.directorMeterPulseTime = 300;
   %this.directorMeterEnablePulseInc = true;
   DirectorMeterBar.animateColor(255, 255, 255, 0, %this.directorMeterPulseTime, "0 0");
   scheduleObjLCA(%this, "HO", %this.directorMeterPulseTime, "onDirectorMeterPulse", 0);
   
   %this.updateDirectorMeter(true);
   
//   if(%this.directorMeterCur $= %this.directorMeterMax)
//      DirectorMeterStar.setAlpha(1);
//   else
//      DirectorMeterStar.setAlpha(0);
            
   %this.checkDirectorMeterPulsing();
   
   %this.isPulsingGlowDirector = false;
   %this.pulseGlowDirectorEventID = -1;
   
   GrayBkgnd.setDML("");
   BlurBkgnd.setDML("");
}

// Pulse the gained star in the center of the window
function SceneGui::pulseCenteredStar(%this, %i)
{
   // Pulse grow
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   %obj = ("MeterStar"@%i);
   %obj.setAlpha(0);
   %obj.setVisible(true);
   %obj.animateAlpha(1, 0, 500, "0 0");
         
   %middleX =  360;
   %middleY =  220;
   
   %obj.setPosition((%middleX-%this.MeterStarEX[%i]/2) * %winScaleX, (%middleY-%this.MeterStarEY[%i]/2) * %winScaleY);
   
   %origPX = getWord(%obj.getPosition(), 0);
   %origPY = getWord(%obj.getPosition(), 1);
   %origEX = getWord(%obj.getExtent(),   0);
   %origEY = getWord(%obj.getExtent(),   1);   

   %extX = %origEX * 5.0;
   %extY = %origEY * 5.0;
   %posX = %origPX - (%extX/2 - %origEX/2);
   %posY = %origPY - (%extY/2 - %origEY/2);

   %obj.animateBounds(%posX, %posY, %extX, %extY, 0, 500, "1 2");   
      
   scheduleObjLCA(%this, "HO", 500, "pulseCenteredStar2", 5, %i, %origPX, %origPY, %origEX, %origEY);
}

// Helper function of pulseCenteredStar2
function SceneGui::pulseCenteredStar2(%this, %i, %origPX, %origPY, %origEX, %origEY)
{
   // Pulse shrink  
   ("MeterStar"@%i).animateBounds(%origPX, %origPY, %origEX, %origEY, 0, 500, "1 2");
      
   scheduleObjLCA(%this, "HO", 500, "pulseCenteredStar3", 1, %i);
}

// Helper function of pulseCenteredStar3
function SceneGui::pulseCenteredStar3(%this, %i)
{
   // Animated star to director meter
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   %obj = ("MeterStar"@%i);
         
   // Move star to director meter
   sfxPlay(AudioMovingStarFinal);

   %obj.animateBounds(
      %this.MeterStarPX[%i]*%winScaleX, %this.MeterStarPY[%i]*%winScaleY, 
      %this.MeterStarEX[%i]*%winScaleX, %this.MeterStarEY[%i]*%winScaleY, 
      0, 1400, "1 2"); // 2 6

   // Start spinning
   %this.starRotateStop = false;
   %speed = 360 / 18;
   %obj.rotation = 0;
   scheduleObjLCA(%this, "HO", 50, "pulseCenteredStarRotate", 2, %i, %speed);
      
   // Stop spinning
   scheduleObjLCA(%this, "HO", 1100, "pulseCenteredStarRotateStop", 0);      
   
   // Shake meter
   scheduleObjLCA(%this, "HO", 1450, "pulseDirectorMeterShake", 1, %i);
   
   // Pulse effect   
   scheduleObjLCA(%this, "HO", 1300, "pulseGlowDirectorMeter", 2, 1, 1.5);
}

// Rotate the star while it spins to the director meter
function SceneGui::pulseCenteredStarRotate(%this, %i, %speed)
{
   %obj = ("MeterStar"@%i);
   
   %newRotation = (%obj.rotation + %speed) % 360;
   %obj.rotation = %newRotation;

   if(%this.starRotateStop)
   {
      if(%newRotation $= 0)
      {
         cancelScheduleLCA(%this.startRotateID);
         %this.starRotateStop = false;
         return;
      }
   }
      
   %this.startRotateID = scheduleObjLCA(%this, "HO", 50, "pulseCenteredStarRotate", 2, %i, %speed);   
}

// Stop star rotation
function SceneGui::pulseCenteredStarRotateStop(%this)
{
   %this.starRotateStop = true;
}

// Shake director meter
function SceneGui::pulseDirectorMeterShake(%this, %i)
{
   %this.starRotateStop = true;
   
	//echo ("PLAYING :  AudioStarGain"@%i);
	sfxPlay("AudioStarGain"@%i);
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);   
   
   // Save position
   %this.directorMeterOrigPos = DirectorMeter.getPosition();
   %this.directorMeterBarOrigPos = DirectorMeterBar.getPosition();
   %this.directorMeterBarGlowOrigPos = DirectorMeterBarGlow.getPosition();

   // Explosion effect
   ParticleFxDirMeter.ExplosionFx(
      (%this.MeterStarPX[%i]+15+%this.MeterStarEX[%i]/2)*%winScaleX, 
      (%this.MeterStarPY[%i]+15+%this.MeterStarEX[%i]/2)*%winScaleY, 2.5, 1.8);
      
   %this.pulseDirectorMeterShakeHelper(50, 15);
}

// Helper of pulseDirectorMeterShake
// %speed of the shaking (in ms)
// %times is the number of times to shake
function SceneGui::pulseDirectorMeterShakeHelper(%this, %speed, %times)
{
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   %randX = getRandom(-2,2);
   %randY = getRandom(-2,2);
   
   DirectorMeter.setPosition(
      getWord(%this.directorMeterOrigPos,0) + (%randX*%winScaleX),
      getWord(%this.directorMeterOrigPos,1) + (%randY*%winScaleY));
      
   DirectorMeterBar.setPosition(
      getWord(%this.directorMeterBarOrigPos,0) + (%randX*%winScaleX),
      getWord(%this.directorMeterBarOrigPos,1) + (%randY*%winScaleY));
      
   DirectorMeterBarGlow.setPosition(
      getWord(%this.directorMeterBarGlowOrigPos,0) + (%randX*%winScaleX),
      getWord(%this.directorMeterBarGlowOrigPos,1) + (%randY*%winScaleY));      
      
   for(%j=1;%j<=5;%j++)
   {
      ("MeterStar"@%j).setPosition(
         (%this.MeterStarPX[%j] + %randX)*%winScaleX,
         (%this.MeterStarPY[%j] + %randY)*%winScaleY);
   }
      
   if(%times <= 1)
   {
      DirectorMeter.setPosition(getWord(%this.directorMeterOrigPos,0),getWord(%this.directorMeterOrigPos,1));
      DirectorMeterBar.setPosition(getWord(%this.directorMeterBarOrigPos,0),getWord(%this.directorMeterBarOrigPos,1));
      DirectorMeterBarGlow.setPosition(getWord(%this.directorMeterBarGlowOrigPos,0),getWord(%this.directorMeterBarGlowOrigPos,1));
      
      for(%j=1;%j<=5;%j++)
      {
         ("MeterStar"@%j).setPosition(
            %this.MeterStarPX[%j]*%winScaleX, %this.MeterStarPY[%j]*%winScaleY);
      }
   }
   else
      scheduleObjLCA(%this, "HO", %speed, "pulseDirectorMeterShakeHelper", 2, %speed, %times-1);      
}

// Pulse the director meter object %obj
// %delay is in secs
function SceneGui::pulseDirectorMeter(%this, %i, %delay)
{
   %obj = DirectorMeter.getObject(%i);
   %obj.setAlpha(0);
   %obj.setVisible(true);
   %obj.animateAlpha(1, %delay*1000, 250, "0 0");
   
   scheduleObjLCA(%this, "HO", %delay*1000, "pulseDirectorMeterHelper1", 2, %obj, %i);  
}

// Helper function of pulseDirectorMeter
function SceneGui::pulseDirectorMeterHelper1(%this, %obj, %i)
{
   if(%obj.isPulsing $= true)
      return;
	sfxPlay(AudioPointGiven);
   if(%i > %this.directorMeterCur)
      return;
      
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);     
      
   // Grow
   %obj.isPulsing = true;
   %origPX = %this.gradPX[%i+1];//getWord(%obj.getPosition(), 0);
   %origPY = %this.gradPY[%i+1];//getWord(%obj.getPosition(), 1);
   %origEX = %this.gradEX[%i+1];//getWord(%obj.getExtent(),   0);
   %origEY = %this.gradEY[%i+1];//getWord(%obj.getExtent(),   1);

   %extX = %this.gradEX[%i+1] * 5.0;
   %extY = %this.gradEY[%i+1] * 5.0;
   %posX = %this.gradPX[%i+1] - ((%extX/2) - (%this.gradEX[%i+1]/2));
   %posY = %this.gradPY[%i+1] - ((%extY/2) - (%this.gradEY[%i+1]/2));
   
   %posX *= %winScaleX;
   %posY *= %winScaleY;
   %extX *= %winScaleX;
   %extY *= %winScaleY;

   %obj.animateBounds(%posX, %posY, %extX, %extY, 0, 300, "1 2");  
   scheduleObjLCA(%this, "HO", 300, "pulseDirectorMeterHelper2", 5, %obj, %origPX, %origPY, %origEX, %origEY);
}

// Helper function of pulseDirectorMeter
function SceneGui::pulseDirectorMeterHelper2(%this, %obj, %origPX, %origPY, %origEX, %origEY)
{
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   %origPX *= %winScaleX;
   %origPY *= %winScaleY;
   %origEX *= %winScaleX;
   %origEY *= %winScaleY;   
   
   // Shrink
   %obj.animateBounds(%origPX, %origPY, %origEX, %origEY, 0, 300, "1 2");
   scheduleObjLCA(%this, "HO", 300, "pulseDirectorMeterHelper3", 1, %obj);
}

function SceneGui::pulseDirectorMeterHelper3(%this, %obj)
{
   %obj.isPulsing = false;
}

// Pulse the glow of the director meter by %pulses times, each one lasting
// %pulseTime seconds
function SceneGui::pulseGlowDirectorMeter(%this, %pulses, %pulseTime)
{
   if(%this.isPulsingGlowDirector)
      cancelScheduleLCA(%this.pulseGlowDirectorEventID);

   %this.isPulsingGlowDirector = true;

   %this.pulseGlowDirectorMeterCore(%pulses, %pulseTime);
}

// Helper function of pulseGlowDirectorMeter
function SceneGui::pulseGlowDirectorMeterCore(%this, %pulses, %pulseTime)
{
   %time = (%pulseTime*1000)/2;
   DirectorMeterBarGlow.animateAlpha(1, 0, %time, "0 0");
   
   %this.pulseGlowDirectorEventID = 
      scheduleObjLCA(%this, "HO", %time, "pulseGlowDirectorMeterCore2", 2, %pulses, %pulseTime);
}

function SceneGui::pulseGlowDirectorMeterCore2(%this, %pulses, %pulseTime)
{
   %time = (%pulseTime*1000)/2;
   DirectorMeterBarGlow.animateAlpha(0, 0, %time, "0 0");   
   
   if(%pulses > 1)
   {
      %this.pulseGlowDirectorEventID = 
         scheduleObjLCA(%this, "HO", %time, "pulseGlowDirectorMeterCore", 2, %pulses-1, %pulseTime);
   }
   else
      %this.isPulsingGlowDirector = false;
}

function SceneGui::checkDirectorMeterPulsing(%this)
{
   // Start pulsing?
   if(%this.directorMeterCur <= %this.directorMeterMax/4)
   {
      %this.directorMeterPulseTime = 500;
      %this.directorMeterEnablePulse = true;
   }
   else
   if(%this.directorMeterCur <= %this.directorMeterMax/3)
   {
      %this.directorMeterPulseTime = 1000;
      %this.directorMeterEnablePulse = true;
   }   
   else
   {
      %this.directorMeterEnablePulse = false;
      DirectorMeterBar.animateColor(255, 255, 255, 0, %this.directorMeterPulseTime, "0 0");
      %this.directorMeterEnablePulseInc = true;      
   }
}

// Called when pulsing the director meter
function SceneGui::onDirectorMeterPulse(%this)
{
   if(%this.directorMeterEnablePulse)
   {
      if(!%this.directorMeterEnablePulseInc)
      {
         DirectorMeterBar.animateColor(255, 255, 255, 0, %this.directorMeterPulseTime, "0 0");
         %this.directorMeterEnablePulseInc = true;
      }
      else
      {
         if(%this.directorMeterPulseTime $= 500)
            DirectorMeterBar.animateColor(255, 128, 128, 0, %this.directorMeterPulseTime, "0 0");         
         else
            DirectorMeterBar.animateColor(255, 196, 196, 0, %this.directorMeterPulseTime, "0 0");                  
         %this.directorMeterEnablePulseInc = false;         
      }
   }
   
   scheduleObjLCA(%this, "HO", %this.directorMeterPulseTime, "onDirectorMeterPulse", 0);
}

function SceneGui::addToDirectorMeter(%this, %dmPoints)
{
   %this.directorMeterFracHolder += %dmPoints;
   if (mAbs(%this.directorMeterFracHolder) < 1)
      return;
   
   %dmPoints = (%this.directorMeterFracHolder > 0? 1 : -1)*mFloor(mAbs(%this.directorMeterFracHolder));
   
   %this.directorMeterFracHolder -= %dmPoints;
   
   if (mAbs(%this.directorMeterFracHolder) < 0.001)
      %this.directorMeterFracHolder = 0;
   
   if(%dmPoints > 0) 
   {
//      if(%this.directorMeterCur $= %this.directorMeterMax-1)
//         DirectorMeterStar.animateAlpha(1, 0, 500, "0 0");

      %i = %this.directorMeterCur;         
      %this.directorMeterCur += %dmPoints;
      if(%this.directorMeterCur > %this.directorMeterMax)
         %this.directorMeterCur = %this.directorMeterMax;
      
      %delay = 0;
      for (; %i < %this.directorMeterCur; %i++)
      {         
         SceneGui.pulseDirectorMeter(%i, %delay);
         %delay += 0.5;
      }
         
      //sfxPlay(AudioWinPointDirectorMeter); 
      
      %this.updateDirectorMeter(false);
      return;
   }

//   if(%this.directorMeterCur $= %this.directorMeterMax)
//      DirectorMeterStar.animateAlpha(0, 0, 500, "0 0");

   %i = %this.directorMeterCur - 1;         
   %this.directorMeterCur += %dmPoints;
   if(%this.directorMeterCur < 0)
      %this.directorMeterCur = 0;
   
   for (; %i >= %this.directorMeterCur; %i--)
      DirectorMeter.getObject(%i).setVisible(false);
   
   if(%this.directorMeterCur < 1)
   {      
      if(SceneGui.dirBehaviorBadEnding $= true)
      {
         %this.showingEnding = true;
         SceneGui.showDirAni1(40000, SceneGui.dirTrigger[40000,"type",0], false, false);
      }
      else
      {
         $returningFromScene = true;
         %this.pauseHOGame();         
         OutroGui.updateProfile();
         %this.loadOutroScreen();
      }
         
      return;
   }
   %this.updateDirectorMeter(false);
}

function SceneGui::updateDirectorMeter(%this, %firstTime)
{
   //DirectorMeter.setDML("scriptsAndAssets/data/ui/ho/meter/"@%this.directorMeterCur@".png");
   
   // Set stars gained
   %newStarsGained = %this.starsGained;
   if(%this.directorMeterCur >= 60)
      %newStarsGained = 5;
   else   
   if(%this.directorMeterCur >= 55)
      %newStarsGained = 4;
   else   
   if(%this.directorMeterCur >= 50)
      %newStarsGained = 3;
   else   
   if(%this.directorMeterCur >= 45)   
      %newStarsGained = 2;
   else
   if(%this.directorMeterCur >= 40)
      %newStarsGained = 1;
   else      
      %newStarsGained = 0;
      
   %aniTime = 1.5;
   
   if(%firstTime)
   {
      MeterStar1.setAlpha(0);
      MeterStar2.setAlpha(0);
      MeterStar3.setAlpha(0);
      MeterStar4.setAlpha(0);
      MeterStar5.setAlpha(0);
      
      for(%j=1; %j<=%newStarsGained; %j++) ("MeterStar"@%j).setAlpha(1);
   }
   else
   if(%newStarsGained !$= %this.starsGained)
   {
      if(%newStarsGained > %this.starsGained)
      {
         if(%newStarsGained > 0)
         {
            sfxPlay(AudioWinStar);            
            %this.pulseCenteredStar(%newStarsGained);
         }
      }
      
      switch$(%newStarsGained)
      {
         case 0:
            MeterStar1.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar2.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar3.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar4.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar5.animateAlpha(0, 0, %aniTime*1000, "0 0");         
         case 1: 
            MeterStar1.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar2.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar3.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar4.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar5.animateAlpha(0, 0, %aniTime*1000, "0 0");
         case 2:
            MeterStar1.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar2.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar3.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar4.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar5.animateAlpha(0, 0, %aniTime*1000, "0 0");
         case 3:
            MeterStar1.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar2.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar3.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar4.animateAlpha(0, 0, %aniTime*1000, "0 0");
            MeterStar5.animateAlpha(0, 0, %aniTime*1000, "0 0");
         case 4:
            MeterStar1.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar2.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar3.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar4.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar5.animateAlpha(0, 0, %aniTime*1000, "0 0");
         case 5:
            MeterStar1.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar2.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar3.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar4.animateAlpha(1, 0, %aniTime*1000, "0 0");
            MeterStar5.animateAlpha(1, 0, %aniTime*1000, "0 0");
      }
   }
   %this.starsGained = %newStarsGained;
      
   if($CurSceneName $= "SpyLevel") // Spy level
      %path = "scriptsAndAssets/data/ui/ho/spy";
   else if($CurSceneName $= "GangsterLevel") // Gangster level
      %path = "scriptsAndAssets/data/ui/ho/gangster";   
   else if($CurSceneName $= "KungFuLevel") // Martial arts level
      %path = "scriptsAndAssets/data/ui/ho/kungfu";
   else if($CurSceneName $= "MusicalLevel") // Big musical level
      %path = "scriptsAndAssets/data/ui/ho/musical"; 
   else if($CurSceneName $= "DinosaurLevel") // Dinosaur level
      %path = "scriptsAndAssets/data/ui/ho/dinosaur"; 
   else if($CurSceneName $= "SciFiLevel") // SciFi level
      %path = "scriptsAndAssets/data/ui/ho/scifi"; 
   else if($CurSceneName $= "WesternLevel") // Western level
      %path = "scriptsAndAssets/data/ui/ho/western"; 
   else if($CurSceneName $= "MonsterLevel") // Monster level
      %path = "scriptsAndAssets/data/ui/ho/monster"; 
      
   %t = mRound(%this.directorMeterMax / 5);
   if(%this.directorMeterCur <= %t)
      %this.directorMeterCurDML = %path @ "/director_Vangry.png";
   else if(%this.directorMeterCur <= %t*2)
      %this.directorMeterCurDML = %path @ "/director_angry.png";      
   else if(%this.directorMeterCur <= %t*3)
      %this.directorMeterCurDML = %path @ "/director_normal.png";      
   else if(%this.directorMeterCur <= %t*4)
      %this.directorMeterCurDML = %path @ "/director_happy.png";
   else if(%this.directorMeterCur <= %t*5)
      %this.directorMeterCurDML = %path @ "/director_Vhappy.png";
      
   %dir1 = DirectorChair2;
   %dir2 = DirectorChair1;
   %doTween = false;

   if(%this.directorMeterLastDML $= "")
      %this.directorMeterLastDML = %this.directorMeterCurDML;
      
   if(%this.directorMeterLastDML !$= %this.directorMeterCurDML)
   {
      %doTween = true;
      %this.directorMeterLastDML = %this.directorMeterCurDML;     
      
      sfxPlay(AudioDirectorMoves); 
   }
      
   if(%doTween)
   {
      if(%this.directorChairUsing1)
      { 
         %dir1 = DirectorChair1;
         %dir2 = DirectorChair2;
      }
      %this.directorChairUsing1 = !%this.directorChairUsing1;
   }
   %dir2.setDML(%this.directorMeterCurDML);
      
   if(%doTween && %this.directorBehaviorBusy $= false)
   {
      if(%this.hiddenDirectorChair $= false)
      {
         %dir1.animateAlpha(0, 0, 500, "0 0");
         %dir2.animateAlpha(1, 0, 500, "0 0");
      }
   }
   
   %this.checkDirectorMeterPulsing();   
}

// Wait until screenshot is taken
function SceneGui::showScoreStarsWaitBlur(%this)
{
   // Screenshot ready?
   if(Canvas.screenshotCaptured)
   {
      // Show score frame, gray and blur images
      Canvas.screenshotCaptured = false;

      BlurBkgnd.setDML("scriptsAndAssets/data/ui/ho/score/blurred.png");
      BlurBkgnd.animateAlpha(1, 0, 750, "0 0");
      
      ScoreFrame.animateAlpha(1, 0, 500, "0 0");      
      ScoreStarMeter.animateAlpha(1, 0, 500, "0 0");
      ScoreReplayButton.animateAlpha(1, 500, 250, "0 0");
      ScoreMapButton.animateAlpha(1, 500, 250, "0 0");
      
      %directorPath = "scriptsAndAssets/data/ui/ho/score/directors/";
      %directorPath = %directorPath @ $CurSceneName;
      
      if(%this.starsGained $= 0)
      {
         %directorPath = %directorPath @ "_angry.png";      
         ScoreText.setDML("scriptsAndAssets/data/ui/ho/score/text_angry.png");
      }
      else if(%this.starsGained >= 1 && %this.starsGained <= 3)
      {
         %directorPath = %directorPath @ "_normal.png";      
         ScoreText.setDML("scriptsAndAssets/data/ui/ho/score/text_normal.png");         
      }
      else if(%this.starsGained >= 4)
      {
         %directorPath = %directorPath @ "_happy.png";     
         ScoreText.setDML("scriptsAndAssets/data/ui/ho/score/text_happy.png");         
      }
      
      ScoreDirector.setDML(%directorPath);
      ScoreDirector.animateAlpha(1, 250, 250, "0 0");
      
      // Show awards gained
      AwardRoom.addAwardGui(FinalScore);
      %time = %this.EvaluateAwardsAtScore();
      
      if(%time > 1000)
         %time -= 1000;

      // Go to part 2
      scheduleObjLCA(%this, "HO", 1000 + %time, "showScoreStarsPart2", 2, 1, 0.25);
   }
   else   
   {
      // Wait a bit more
      scheduleObjLCA(%this, "HO", 200, "showScoreStarsWaitBlur", 0);
   }
}

// Show the "That's a effort" title and animates the stars
// from the director meter
function SceneGui::showScoreStarsPart1(%this)
{
   %this.aniSpeed = 0.1;
   
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   DirectorChair1.animateAlpha(0, 0, 500, "0 0");
   DirectorChair2.animateAlpha(0, 0, 500, "0 0");
   DirectorChairHint.animateAlpha(0, 0, 500, "0 0");
   
   for(%i=1; %i<=5; %i++)
   {
      %obj = ("MeterStar"@%i);
      FinalScore.add(%obj);
      FinalScore.pushToBack(%obj);
   }
   
   // Put ParticleFx in ScoreParticleFx
   ScoreParticleFx.add(ParticleFx);
   
   FinalScore.setVisible(true);
   
   // Compensate widescreen offset
   %centerPos = GuiCenter.getPosition();
   %centerRes = GuiCenter.getExtent();
   
   // Capture screenshot and apply blur filter only
   Canvas.captureScreen(111*%winScaleX,  41*%winScaleY, 
                        603*%winScaleX, 428*%winScaleY, false, 
                        getWord(%centerPos,0), getWord(%centerPos,1),
                        getWord(%centerRes,0), getWord(%centerRes,1));

   // Wait until the screenshot-ready flag is set
   scheduleObjLCA(%this, "HO", 500, "showScoreStarsWaitBlur", 0);
}

// Pulse star before animating it
function SceneGui::showScoreStarsPart2(%this, %starNum, %aniTime)
{
   if(%this.starsGained >= %starNum)
   {
      %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
      %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
      
      // Shrink in-place
      %obj = ("MeterStar" @ %starNum);
      
      %extX = getWord(%obj.getExtent(),   0) * 0.7;
      %extY = getWord(%obj.getExtent(),   1) * 0.7;
      %posX = getWord(%obj.getPosition(), 0) + ((getWord(%obj.getExtent(), 0) - %extX) / 2);
      %posY = getWord(%obj.getPosition(), 1) + ((getWord(%obj.getExtent(), 1) - %extY) / 2);
      
//      %extX *= %winScaleX;
//      %extY *= %winScaleY;
//      %posX *= %winScaleX;
//      %posY *= %winScaleY;

      %obj.animateBounds(%posX, %posY, %extX, %extY, 0, %aniTime*1000*%this.aniSpeed, "0 0");
      
      ScoreStarMeter.setDML("scriptsAndAssets/data/ui/ho/score/meter" @ %starNum-1 @ ".png");

      scheduleObjLCA(%this, "HO", %aniTime*1000*%this.aniSpeed, "showScoreStarsPart3", 2, %starNum, 1.0);
   }
   else
      scheduleObjLCA(%this, "HO", 1500*%this.aniSpeed, "showScoreStarsPart6", 0);
}

// Animate star %starNum (1-5) from director meter to score title
function SceneGui::showScoreStarsPart3(%this, %starNum, %aniTime)
{
   if(%this.starsGained >= %starNum)
   {
      %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
      %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);

      // Destination position and extent of each star
      %posX[1] = 212; %posY[1] = 239;
      %extX[1] = 40/*37*/;  %extY[1] = 41/*38*/;

      %posX[2] = 244; %posY[2] = 203;
      %extX[2] = 48/*52*/;  %extY[2] = 48/*49*/;

      %posX[3] = 295; %posY[3] = 163;
      %extX[3] = 73;  %extY[3] = 62/*69*/;

      %posX[4] = 386; %posY[4] = 138;
      %extX[4] = 96/*95*/;  %extY[4] = 102/*82*/;
      
      %posX[5] = 483; %posY[5] = 132;
      %extX[5] = 139/*138*/;  %extY[5] = 142/*126*/;

      for(%i=1; %i<=5; %i++)
      {
         %extX[%i] *= %winScaleX;
         %extY[%i] *= %winScaleY;
         %posX[%i] *= %winScaleX;
         %posY[%i] *= %winScaleY;
      }

      sfxPlay(AudioMovingStarFinal);
      %obj = ("MeterStar" @ %starNum);
      %obj.deltaRotation = "-220";
      %obj.animateBounds(%posX[%starNum], %posY[%starNum], %extX[%starNum], %extY[%starNum], 0, %aniTime*1000*%this.aniSpeed, "1 2");

      scheduleObjLCA(%this, "HO", %aniTime*1000*%this.aniSpeed, "showScoreStarsPart4", 1, %starNum);
   }
}

// Do a quick pulse (grow) in the star at the score meter
function SceneGui::showScoreStarsPart4(%this, %starNum)
{
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   // Crossfade meter star with score star
   %meterStar = ("MeterStar" @ %starNum);
   %scoreStar = ("ScoreStar" @ %starNum);
   %meterStar.animateAlpha(0, 0, 250, "0 0");
   %scoreStar.setVisible(true);
   %scoreStar.animateAlpha(1, 0, 250, "0 0");
   
   if(%starNum $= 5)   
      ScoreStarMeter.setDML("scriptsAndAssets/data/ui/ho/score/meter5.png");   
  
   // Pulse      
   %origPX = getWord(%scoreStar.getPosition(), 0);
   %origPY = getWord(%scoreStar.getPosition(), 1);
   %origEX = getWord(%scoreStar.getExtent(), 0);
   %origEY = getWord(%scoreStar.getExtent(), 1);
   
   %posX[1] = 228; %posY[1] = 257;
   %posX[2] = 268; %posY[2] = 224;
   %posX[3] = 334; %posY[3] = 193;
   %posX[4] = 433; %posY[4] = 176;
   %posX[5] = 548; %posY[5] = 190;

   for(%i=1; %i<=5; %i++)
   {
      %posX[%i] += 15;
      %posY[%i] += 15;
      
      %posX[%i] *= %winScaleX;
      %posY[%i] *= %winScaleY;
   }
   
   ParticleFx.ScoreFx(%posX[%starNum], %posY[%starNum], 2, 1.8);
      
   %extX = getWord(%scoreStar.getExtent(),   0) * 1.7;
   %extY = getWord(%scoreStar.getExtent(),   1) * 1.7;
   %posX = getWord(%scoreStar.getPosition(), 0) - ((%extX/2) - (getWord(%scoreStar.getExtent(), 0)/2));
   %posY = getWord(%scoreStar.getPosition(), 1) - ((%extY/2) - (getWord(%scoreStar.getExtent(), 1)/2));

   %scoreStar.animateBounds(%posX, %posY, %extX, %extY, 0, 250, "1 2");  // 1 2
      
   scheduleObjLCA(%this, "HO", 250, "showScoreStarsPart5", 5, %starNum, %origPX, %origPY, %origEX, %origEY);
}

// Do a quick pulse (shrink) in the star at the score meter
function SceneGui::showScoreStarsPart5(%this, %starNum, %origPX, %origPY, %origEX, %origEY)
{   
   // Pulse   
   ("ScoreStar" @ %starNum).animateBounds(%origPX, %origPY, %origEX, %origEY, 0, 250, "2 2");  // 2 2 
      
   scheduleObjLCA(%this, "HO", 250, "showScoreStarsPart2", 2, %starNum+1, 0.25);
}

// Show text
function SceneGui::showScoreStarsPart6(%this)
{
   ScoreText.animateAlpha(1, 0, 250, "0 0");
   
   scheduleObjLCA(%this, "HO", 250, "showScoreStarsPart7", 0);      
}

// Show grayscale background
function SceneGui::showScoreStarsPart7(%this)
{
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
   
   // Compensate widescreen offset
   %centerPos = GuiCenter.getPosition();
   %centerRes = GuiCenter.getExtent();
   
   // Capture screenshot and apply grayscale filter only
   Canvas.captureScreen(111*%winScaleX,  41*%winScaleY, 
                        603*%winScaleX, 428*%winScaleY, true,
                        getWord(%centerPos,0), getWord(%centerPos,1),
                        getWord(%centerRes,0), getWord(%centerRes,1));                        
   
   // Wait until the screenshot-ready flag is set
   scheduleObjLCA(%this, "HO", 200, "showScoreStarsWaitGrayscale", 0);
}

// Last part of star animation (exit scene)
function SceneGui::showScoreStarsPart8(%this)
{
//   %this.loadOutroScreen();
}

// Wait until grayscale screenshot is taken
function SceneGui::showScoreStarsWaitGrayscale(%this)
{
   // Screenshot ready?
   if(Canvas.screenshotCaptured)
   {
      // Show score frame, gray and blur images
      Canvas.screenshotCaptured = false;
      
      GrayBkgnd.setDML("scriptsAndAssets/data/ui/ho/score/grayscale.png");
      GrayBkgnd.animateAlpha(1, 0, 750, "0 0");            
      
      // Go to part 7
      scheduleObjLCA(%this, "HO", 500, "showScoreStarsPart8", 0);
   }
   else   
   {
      // Wait a bit more
      scheduleObjLCA(%this, "HO", 200, "showScoreStarsWaitGrayScale", 0);
   }
}

function SceneGui::showDirectorChair(%this)
{
   %this.showingDirectorChair = true;
   
   %dir = DirectorChair2;
   if(%this.directorChairUsing1)
      %dir = DirectorChair1;
   
   // Deal with resizing
//   %winScaleX = 1.0 / 800 * getWord(canvas.getExtent(),0);
//   %winScaleY = 1.0 / 600 * getWord(canvas.getExtent(),1);   
//   DirectorChair.animateCenter(120.5 * %winScaleX, 445.5 * %winScaleY, 0, 2000, "2 3");
   %dir.animateAlpha(1, 0, 500, "0 0");
   
   %this.hiddenDirectorChair = false;   
}

function SceneGui::hideDirectorChair(%this)
{
   %this.hidingDirectorChair = true;
   %this.hiddenDirectorChair = true;

   %dir = DirectorChair2;
   if(%this.directorChairUsing1)
      %dir = DirectorChair1;
   
//   %winScaleX = 1.0 / 800 * getWord(canvas.getExtent(),0);
//   %winScaleY = 1.0 / 600 * getWord(canvas.getExtent(),1);   
//   DirectorChair.animateCenter(120.5 * %winScaleX, 754.5 * %winScaleY, 0, 800, "1 3");
   %dir.animateAlpha(0, 0, 500, "0 0");
}

function DirectorChair1::onAnimationStopped(%this)
{
//   if(SceneGui.hidingDirectorChair)
//      DirectorChair.Visible = false;
      
   SceneGui.showingDirectorChair = false;
   SceneGui.hidingDirectorChair = false;   
   
   if(SceneGui.showingDirectorBehavior)
   {
      SceneGui.showingDirectorBehavior = false;
      
      if(%this.dirTrigger[SceneGui.curTriggerNum,"showType"] !$= "")
         SceneGui.showDirAni2(SceneGui.curTriggerNum);
   }
}

function DirectorChair2::onAnimationStopped(%this)
{
//   if(SceneGui.hidingDirectorChair)
//      DirectorChair.Visible = false;
      
   SceneGui.showingDirectorChair = false;
   SceneGui.hidingDirectorChair = false;   
   
   if(SceneGui.showingDirectorBehavior)
   {
      SceneGui.showingDirectorBehavior = false;
      
      if(%this.dirTrigger[SceneGui.curTriggerNum,"showType"] !$= "")
         SceneGui.showDirAni2(SceneGui.curTriggerNum);
   }
}

function SceneGui::initDirectorBehavior(%this)
{
   %this.showingDirectorBehavior = false;
   %this.hidingDirectorBehavior = false;
   
   %this.dirBehaviorMinigameIntro = false;
   %this.dirBehaviorMinigameOutro = false;
   
   %this.dirBehaviorGoodEnding = false;
   %this.dirBehaviorBadEnding = false;   
   %this.showingEnding = false;
   
   // This is true whenever the director is being shown
   %this.directorBehaviorBusy = false;
   
   DirTriggerImg1.setAlpha(0);
   DirTriggerImg1.setVisible(false);
   
   DirTriggerImg2a.setAlpha(0);
   DirTriggerImg2a.setVisible(false);
   DirTriggerImg2b.setAlpha(0);
   DirTriggerImg2b.setVisible(false);
   
   %this.crossFadingDirector = false;   
   
   DirTriggerBubble.setVisible(false);   
   DirTriggerBubbleText.setVisible(false);
   
   %this.lastText = "";   
   %this.lastFontSize = "";
   //%this.lastTextExtent = 10;
   //%this.lastTextOffset = 0;
   //%this.lastArrowButtonY = 0;
      
   %this.loadDirectorXML();
}

// Parse XML file looking for director behavior tags
function SceneGui::loadDirectorXML(%this)
{
   echo("Load Director XML");
   %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 %this.dirTrigger[%this.dirTriggerNum]      
   // 10 = text size

   %this.dirTriggerNum = 0;
   if(!%pXml.pushFirstChildElement("ani"))
   {
      echo("Director animations not found.");
      return;
   }
   
   while(true)
   {
      %type         = %pXml.attribute("type");      
      %showat       = %pXml.attribute("showat");
      %pause        = %pXml.attribute("pause");
      if(%pause $= "true") %pause = true; else %pause = false;
      
      %triggerNum = %this.dirTriggerNum;
      if(%showat $= "goodending")
      {
         %triggerNum = 30000; // Special ID for goodending
         %this.dirBehaviorGoodEnding = true;
      }
      else
      {
         %triggerNum = 40000; // Special ID for badending
         %this.dirBehaviorBadEnding = true;
      }
         
      // Whether to pause the game when displaying the ballons
      %this.dirTrigger[%triggerNum,"pause"] = %pause;
      
      %this.dirTrigger[%triggerNum,"type"] = %type;
      
      %this.dirTrigger[%triggerNum,"showat"] = %showat;
            
      if(%showat !$= "intro" && %showat !$= "outro")
      {
         if(%showat $= "goodending" || %showat $= "badending")
         {               
            %textNum = 0;
            if(%pXml.pushFirstChildElement("bubble"))
            {
               echo("Text: " @ %pXml.attribute("text"));
               %this.dirTrigger[%triggerNum,"text",%textNum] = %pXml.attribute("text");
               %this.dirTrigger[%triggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
               %this.dirTrigger[%triggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;
               %this.dirTrigger[%triggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");

               // Pause button?
               %havePauseButton = %pXml.attribute("pause");
               if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
               %this.dirTrigger[%triggerNum,"pause",%textNum]=%havePauseButton;
               
               // Replay button?
               %haveReplayButton = %pXml.attribute("replay");
               if(%haveReplayButton !$= "true") %haveReplayButton = false; else %haveReplayButton = true;               
               %this.dirTrigger[%triggerNum,"replay",%textNum] = %haveReplayButton;
               
               %textNum++;            
               
               while(true)
               {
                  // Go to next bubble tag
                  if(%pXml.nextSiblingElement("bubble"))
                  {
                     echo("Text: " @ %pXml.attribute("text"));
                     %this.dirTrigger[%triggerNum,"text",%textNum] = %pXml.attribute("text");
                     %this.dirTrigger[%triggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
                     %this.dirTrigger[%triggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;
                     %this.dirTrigger[%triggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");

                     // Pause button?
                     %havePauseButton = %pXml.attribute("pause");
                     if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
                     %this.dirTrigger[%triggerNum,"pause",%textNum]=%havePauseButton;
                     
                     // Replay button?
                     %haveReplayButton = %pXml.attribute("replay");
                     if(%haveReplayButton !$= "true") %haveReplayButton = false; else %haveReplayButton = true;               
                     %this.dirTrigger[%triggerNum,"replay",%textNum] = %haveReplayButton;
                     
                     %textNum++;
                  }
                  else
                  {
                     %this.dirTrigger[%triggerNum,"text",%textNum] = "";
                     break;
                  }
               }
               %pXml.popElement();
            }
            else
               %this.dirTrigger[%triggerNum,"text",%textNum] = "";      
         }
         else
         if(%showat !$= "minigameintro" && %showat !$= "minigameoutro")
         {
            %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");

               // Use pause button?
               %havePauseButton = %pXml.attribute("pause");
               if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
               %this.dirTrigger[%this.dirTriggerNum,"pause",%textNum]=%havePauseButton;
               
               // Replay button?
               %haveReplayButton = %pXml.attribute("replay");
               if(%haveReplayButton !$= "true") %haveReplayButton = false; else %haveReplayButton = true;               
               %this.dirTrigger[%this.dirTriggerNum,"replay",%textNum] = %haveReplayButton;               
               
               %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");

                     // Use pause button?
                     %havePauseButton = %pXml.attribute("pause");
                     if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
                     %this.dirTrigger[%this.dirTriggerNum,"pause",%textNum]=%havePauseButton;
                     
                     // Replay button?
                     %haveReplayButton = %pXml.attribute("replay");
                     if(%haveReplayButton !$= "true") %haveReplayButton = false; else %haveReplayButton = true;               
                     %this.dirTrigger[%this.dirTriggerNum,"replay",%textNum] = %haveReplayButton;
                     
                     %textNum++;
                  }
                  else
                  {
                     %this.dirTrigger[%this.dirTriggerNum,"text",%textNum] = "";
                     break;               
                  }
               }
               %pXml.popElement();
            }
            
            scheduleObjLCA(%this, "HO", %showat*1000, "showDirAni1", 4,
                  %this.dirTriggerNum, %type, false, false);
         }
         else
         {
            if(%showat $= "minigameintro")
            {
               %triggerNum = 10000; // Special ID for minigame intro
               %this.dirBehaviorMinigameIntro = true;
            }
            else // minigameoutro
            {
               %triggerNum = 20000; // Special ID for minigame outro
               %this.dirBehaviorMinigameOutro = true;
            }
               
            %textNum = 0;
            if(%pXml.pushFirstChildElement("bubble"))
            {
               %this.dirTrigger[%triggerNum,"text",%textNum] = %pXml.attribute("text");
               %this.dirTrigger[%triggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
               %this.dirTrigger[%triggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;
               %this.dirTrigger[%triggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");
               %this.DirTrigger[%triggerNum,"hintValue",%textNum]=%pXml.attributeS32("hintvalue");
               %this.DirTrigger[%triggerNum,"hintCondition",%textNum]=%pXml.attribute("hintcondition");

               // Pause button?
               %havePauseButton = %pXml.attribute("pause");
               if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
               %this.dirTrigger[%triggerNum,"pause",%textNum]=%havePauseButton;
               
               // Replay button?
               %haveReplayButton = %pXml.attribute("replay");
               if(%haveReplayButton !$= "true") %haveReplayButton = false; else %haveReplayButton = true;               
               %this.dirTrigger[%triggerNum,"replay",%textNum] = %haveReplayButton;               
               
               %textNum++;            
               
               while(true)
               {
                  // Go to next bubble tag
                  if(%pXml.nextSiblingElement("bubble"))
                  {
                     %this.dirTrigger[%triggerNum,"text",%textNum] = %pXml.attribute("text");
                     %this.dirTrigger[%triggerNum,"textSize",%textNum] = %pXml.attributeF32("size");
                     %this.dirTrigger[%triggerNum,"textDuration",%textNum] = %pXml.attributeF32("duration")*1000;
                     %this.dirTrigger[%triggerNum,"imageid",%textNum]=%pXml.attributeS32("imageid");
                     %this.DirTrigger[%triggerNum,"hintValue",%textNum]=%pXml.attributeS32("hintvalue");
                     %this.DirTrigger[%triggerNum,"hintCondition",%textNum]=%pXml.attribute("hintcondition");

                     // Pause button?
                     %havePauseButton = %pXml.attribute("pause");
                     if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
                     %this.dirTrigger[%triggerNum,"pause",%textNum]=%havePauseButton;
                     
                     // Replay button?
                     %haveReplayButton = %pXml.attribute("replay");
                     if(%haveReplayButton !$= "true") %haveReplayButton = false; else %haveReplayButton = true;               
                     %this.dirTrigger[%triggerNum,"replay",%textNum] = %haveReplayButton;
                     
                     %textNum++;                  
                  }
                  else
                  {
                     %this.dirTrigger[%triggerNum,"text",%textNum] = "";
                     break;
                  }
               }
               %pXml.popElement();
            }
            else
               %this.dirTrigger[%triggerNum,"text",%textNum] = "";
         }
      }

      %this.dirTriggerNum++;
      
      // Go to next animation tag
      if(!%pXml.nextSiblingElement("ani"))
         break;
   }
   
   %pXml.popElement();
   
   %pXml.delete();   
}

// Show director (stage 1: hide director chair)
function SceneGui::showDirAni1(%this, %triggerNum, %type, %introMg, %outroMg)
{
   // If goodending or badending, send directors to front
   if(%triggerNum $= 30000 || %triggerNum $= 40000)
   {
      SceneGui.pushToBack(DirTriggerImg1);
      SceneGui.pushToBack(DirTriggerImg2a);
      SceneGui.pushToBack(DirTriggerImg2b);
      SceneGui.pushToBack(Minigame);
   }
   
   if(%this.directorBehaviorBusy $= true)
      return;
   
   %this.directorBehaviorBusy = true;
   
   %this.hidingDirectorBehavior = false;
   %this.showingDirectorBehavior = true;
   %this.curDirectorBehaviorType = %type;
   if(%this.curDirectorBehaviorType $= "cam0")
      %this.dirTriggerImg = DirTriggerImg1;
   else
      %this.dirTriggerImg = DirTriggerImg2a;   
   
   %this.curTriggerNum = %triggerNum;
   %this.hideDirectorChair();
   
   if(%introMg !$= true && %outroMg !$= true)   
   {
      if(%this.dirTrigger[%triggerNum,"pause"])
         SceneGui.pauseHOGame();
   }
   
   %this.showDirAni2(%triggerNum, %introMg, %outroMg);
}

// Show director (stage 2: show director and bubble)
function SceneGui::showDirAni2(%this, %triggerNum, %introMg, %outroMg)
{
   %this.hidingDirectorBehavior = false;
   
   if(%this.curDirectorBehaviorType !$= "cam0")
      MenuButton.setVisible(false);         
   
   if($CurSceneName $= "SpyLevel") // Spy level
      %path = "scriptsAndAssets/data/ui/ho/spy";
   else if($CurSceneName $= "GangsterLevel") // Gangster level
      %path = "scriptsAndAssets/data/ui/ho/gangster";   
   else if($CurSceneName $= "KungFuLevel") // Martial arts level
      %path = "scriptsAndAssets/data/ui/ho/kungfu";
   else if($CurSceneName $= "MusicalLevel") // Big musical level
      %path = "scriptsAndAssets/data/ui/ho/musical";  
   else if($CurSceneName $= "DinosaurLevel") // Dinosaur level
      %path = "scriptsAndAssets/data/ui/ho/dinosaur";   
   else if($CurSceneName $= "SciFiLevel") // SciFi level
      %path = "scriptsAndAssets/data/ui/ho/scifi"; 
   else if($CurSceneName $= "WesternLevel") // Western level
      %path = "scriptsAndAssets/data/ui/ho/western"; 
   else if($CurSceneName $= "MonsterLevel") // Monster level
      %path = "scriptsAndAssets/data/ui/ho/monster"; 
   
   %this.dirTriggerImg.setDML(%path @ "/director_talking" @ %this.dirTrigger[%triggerNum,"imageid",0] @ ".png");
   %this.dirTriggerImg.setVisible(true);
   
   // DirTriggerImg properties:
   // Position = "44 258";
   // Extent = "241 309";
   
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);   

   // From
   %showTime = 1000;
   if(%this.dirTrigger[%triggerNum,"type"] $= "left")
   {
      if(%this.curDirectorBehaviorType $= "cam0")
      {
         %this.dirTriggerImg.setPosition(-83.5 * %winScaleX, 164 * %winScaleY);
         %this.dirTriggerImg.animateCenter(157.5 * %winScaleX, 318.5 * %winScaleY, 0, %showTime, "2 3");
      }
      else
      {
         %this.dirTriggerImg.setPosition(-120.5 * %winScaleX, 291 * %winScaleY);
         %this.dirTriggerImg.animateCenter(120.5 * %winScaleX, 445.5 * %winScaleY, 0, %showTime, "2 3");
      }
   }
   else
   if(%this.dirTrigger[%triggerNum,"type"] $= "below")
   {
      if(%this.curDirectorBehaviorType $= "cam0")
      {
         %this.dirTrigerImg.setPosition(37 * %winScaleX, 473 * %winScaleY);
         %this.dirTriggerImg.animateCenter(157.5 * %winScaleX, 318.5 * %winScaleY, 0, %showTime, "2 3");
      }
      else
      {
         %this.dirTriggerImg.setPosition(0 * %winScaleX, 600 * %winScaleY);
         %this.dirTriggerImg.animateCenter(120.5 * %winScaleX, 445.5 * %winScaleY, 0, %showTime, "2 3");
      }
   }
   else
   {
      %showTime = 500;
      if(%this.curDirectorBehaviorType $= "cam0")
      {

         %this.dirTriggerImg.setPosition(37 * %winScaleX, 164 * %winScaleY);        
         %this.dirTriggerImg.setAlpha(0);
         %this.dirTriggerImg.setVisible(true);         
         %this.dirTriggerImg.animateAlpha(1, 0, 500, "0 0");         
      }
      else
      {
         //%this.dirTriggerImg.setPosition(0 * %winScaleX, 291 * %winScaleY);
         %this.dirTriggerImg.setPosition(%this.directorX * %winScaleX, %this.directorY * %winScaleY);    
         %this.dirTriggerImg.setExtent(%this.directorW * %winScaleX, %this.directorH * %winScaleY);    
         %this.dirTriggerImg.setAlpha(0);
         %this.dirTriggerImg.setVisible(true);         
         %this.dirTriggerImg.animateAlpha(1, 0, 500, "0 0");         
      }
   }

   scheduleObjLCA(%this, "HO", %showTime + 200, "showDirAniText1", 4, %triggerNum, 0, %introMg, %outroMg);
}

// 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 SceneGui::vCenterTextInBubble(%this, %bubble, %text, %origExtent, %vOffset, %arrowButtonY)
{
   %bubble.setVisible(true);
   %text.setVisible(true);  
   %text.dump();  
   /*%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
      scheduleObjLCA(%this, "Other", 50, "vCenterTextInBubble", 5, %bubble, %text, %origExtent, %vOffset, %arrowButtonY);
*/
}

// Show director bubble text
function SceneGui::showDirAniText1(%this, %triggerNum, %textNum, %introMg, %outroMg)
{
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.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, %introMg, %outroMg);
         return;
      }
   }  
      
   if($CurSceneName $= "SpyLevel") // Spy level
      %path = "scriptsAndAssets/data/ui/ho/spy";
   else if($CurSceneName $= "GangsterLevel") // Gangster level
      %path = "scriptsAndAssets/data/ui/ho/gangster";   
   else if($CurSceneName $= "KungFuLevel") // Martial arts level
      %path = "scriptsAndAssets/data/ui/ho/kungfu";
   else if($CurSceneName $= "MusicalLevel") // Big musical level
      %path = "scriptsAndAssets/data/ui/ho/musical";
   else if($CurSceneName $= "DinosaurLevel") // Dinosaur level
      %path = "scriptsAndAssets/data/ui/ho/dinosaur";
   else if($CurSceneName $= "SciFiLevel") // SciFi level
      %path = "scriptsAndAssets/data/ui/ho/scifi"; 
   else if($CurSceneName $= "WesternLevel") // Western level
      %path = "scriptsAndAssets/data/ui/ho/western"; 
   else if($CurSceneName $= "MonsterLevel") // Monster level
      %path = "scriptsAndAssets/data/ui/ho/monster"; 
      
   %newDML = %path @ "/director_talking" @ %this.dirTrigger[%triggerNum,"imageid",%textNum] @ ".png";
   %this.swapDirectors(%newDML, 200);
//   %this.dirTriggerImg.setDML();

   %vOffset = 0;
   %arrowButtonY = 0;
   %replay = %this.dirTrigger[%triggerNum,"replay",%textNum];
   %pause = %this.dirTrigger[%triggerNum,"pause",%textNum];
   if(%pause) 
      %vOffset -= getWord(DirTriggerArrow.getExtent(),1)/2;
   
   if(%this.curDirectorBehaviorType $= "cam0")
   {
      %arrowButtonY = 418-40;
      DirTriggerArrow.setPosition((455+30)*%winScaleX, %arrowButtonY*%winScaleY);
      DirTriggerBubble.setPosition(220*%winScaleX, 216*%winScaleY);
      DirTriggerBubbleText.setPosition(268*%winScaleX, 237*%winScaleY);
   }
   else
   {
      %arrowButtonY = 435;
      DirTriggerArrow.setPosition(455*%winScaleX, %arrowButtonY*%winScaleY);
      DirTriggerBubble.setPosition(230*%winScaleX, 276*%winScaleY);
      DirTriggerBubbleText.setPosition(278*%winScaleX, 287*%winScaleY);
   }
      
//   DirTriggerBubbleText.setVisible(false);
//   DirTriggerBubbleText.setExtent(getWord(DirTriggerBubbleText.getExtent(),0),1);
//   %extent = DirTriggerBubbleText.getExtent();   

   %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;
   
//   DirTriggerBubbleText.setText("<just:center><font:Comic Sans MS:"@%this.dirTrigger[%triggerNum,"textSize",%textNum]@">"@%text);
//   %this.vCenterTextInBubble(DirTriggerBubble, DirTriggerBubbleText, %extent, %vOffset, %arrowButtonY);
   %this.updateSpeechBubble();
   
   if(%replay)
   {
      DirTriggerReplay.setVisible(true);
   }

   if(%pause)
   {
      DirTriggerArrow.setVisible(true);
      DirTriggerArrow.triggerNum = %triggerNum;
      DirTriggerArrow.textNum = %textNum;
      DirTriggerArrow.introMg = %introMg;
      DirTriggerArrow.outroMg = %outroMg;
   }
   else
   {
      DirTriggerArrow.setVisible(false);
      %this.showDirAniText2(%triggerNum, %textNum, %introMg, %outroMg);
   }
}

// Second part of showDirAniText2
function SceneGui::showDirAniText2(%this, %triggerNum, %textNum, %introMg, %outroMg)
{
   sfxPlay(AudioButtonPress);
   
   %classID = "HO";   
   if(%introMg $= true || %outroMg $= true)
      %classID = "Minigame";

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

   if(%this.dirTrigger[%triggerNum,"pause",%textNum])
      %duration = 0;
      
   %this.showDirAniText3(%triggerNum, %textNum, %duration, %introMg, %outroMg);      
}

function SceneGui::showDirAniText3(%this, %triggerNum, %textNum, %duration, %introMg, %outroMg)
{
   if(%this.dirTrigger[%triggerNum,"text",%textNum+1] !$= "")
   {
      // Show next text
      scheduleObjLCA(%this, %classID, %duration, "showDirAniText1", 4, %triggerNum, %textNum+1, %introMg, %outroMg);
   }
   else
   {
      // End of text tags
      scheduleObjLCA(%this, %classID, %duration, "hideDirAni", 3, %triggerNum, %introMg, %outroMg);
   }
}

// Hide director
function SceneGui::hideDirAni(%this, %triggerNum, %introMg, %outroMg)
{
   %this.directorBehaviorBusy = false;
   
   if(!SceneGui.dirBehaviorBadEnding)
   {
      DirTriggerArrow.setVisible(false);
      DirTriggerBubbleText.setVisible(false);
   }

   %this.hidingDirectorBehavior = true;
   %this.showingDirectorBehavior = false;
   
   %this.lastFontSize = "";
   
   if(!SceneGui.dirBehaviorBadEnding)
   {
      DirTriggerBubble.setVisible(false);
   }
   
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(GuiCenter.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(GuiCenter.getExtent(),1);
  
   // DirTriggerImg properties:
   // Position = "44 258";
   // Extent = "241 309";
  
   // To
   %hideTime = %this.dirTrigger[%triggerNum,"hideAniTime"];
   if(%this.dirTrigger[%triggerNum,"hideType"] $= "left")
   {
      if(%this.curDirectorBehaviorType $= "cam0")
      {
         %this.dirTriggerImg.animateCenter(-83.5 * %winScaleX, 318.5 * %winScaleY, 0, %hideTime, "1 3");
      }
      else
      {
         %this.dirTriggerImg.animateCenter(-120.5 * %winScaleX, 445.5 * %winScaleY, 0, %hideTime, "1 3");
      }
   }
   else
   if(%this.dirTrigger[%triggerNum,"hideType"] $= "below")   
   {
      if(%this.curDirectorBehaviorType $= "cam0")
      {
         %this.dirTriggerImg.animateCenter(157.5 * %winScaleX, 627.5 * %winScaleY, 0, %hideTime, "1 3");   
      }
      else
      {
         %this.dirTriggerImg.animateCenter(120.5 * %winScaleX, 754.5 * %winScaleY, 0, %hideTime, "1 3");
      }
   }
   else
   {
      if(!%this.showingEnding)
      {
         DirTriggerArrow.setVisible(false);
         DirTriggerBubbleText.setVisible(false);
         DirTriggerBubble.setVisible(false);
         
         %showTime = 500;
         if(%this.curDirectorBehaviorType $= "cam0")
         {
            %this.dirTriggerImg.setPosition(37 * %winScaleX, 164 * %winScaleY);
            %this.dirTriggerImg.setAlpha(1);
            %this.dirTriggerImg.setVisible(true);
            %this.dirTriggerImg.animateAlpha(0, 0, 500, "0 0");         
         }
         else
         {
            %this.dirTriggerImg.setPosition(0 * %winScaleX, 291 * %winScaleY);
            %this.dirTriggerImg.setAlpha(1);
            %this.dirTriggerImg.setVisible(true);         
            %this.dirTriggerImg.animateAlpha(0, 0, 500, "0 0");         
         }
         %this.showDirectorChair();
      }
   }
   
   if(%introMg $= true)
   {
      // Animate clapper board tutorial (in)
      MiniGame.setVisible(true);
      MiniGame.playClapperBoardTutIn();
      return;
   }
   else
   if(%outroMg $= true)
   {
      MiniGame.quit();
      return;
   }
   
   if(%this.showingEnding)
   {
      $returningFromScene = true;
      
      if(!SceneGui.dirBehaviorBadEnding)
         %this.showScoreStarsPart1();
      else
         %this.loadOutroScreen();
         
      return;
   }
   
   if(%this.dirTrigger[%triggerNum,"pause"])
      SceneGui.resumeHOGame();
}

function DirTriggerImg1::onAnimationStopped(%this)
{
   if(SceneGui.hidingDirectorBehavior $= true)
   {
      if(SceneGui.dirTrigger[%triggerNum,"hideType"] !$= "")
      {
         %this.Visible = false;
         SceneGui.showDirectorChair();
      }
      SceneGui.directorBehaviorBusy = false;      
   }
}

function DirTriggerImg2::onAnimationStopped(%this)
{
   if(SceneGui.hidingDirectorBehavior $= true)
   {
      if(SceneGui.dirTrigger[%triggerNum,"hideType"] !$= "")
      {
         %this.Visible = false;
         SceneGui.showDirectorChair();
      }
      SceneGui.directorBehaviorBusy = false;      
   }
}

// Update the speech bubble text
function SceneGui::updateSpeechBubble(%this)
{
   if(%this.lastFontSize $= "")
      return;
   
   %text = %this.lastText;   
   //%fontSize = %this.lastFontSize * Canvas.scaleSize;
   //if(%fontSize > 62) %fontSize = 62;
      
   //DirTriggerBubbleText.setVisible(false);
   //DirTriggerBubbleText.setExtent(242 / 800 * getWord(Canvas.Extent,0), 2);
   %extent = DirTriggerBubbleText.getExtent();   
   //%vOffset      = %this.lastTextOffset;
   //%arrowButtonY = %this.lastArrowButtonY;
   
   DirTriggerBubbleText.setText(%text);
   
   DirTriggerBubble.setVisible(true);
   DirTriggerBubbleText.setVisible(true);
   //%this.vCenterTextInBubble(DirTriggerBubble, DirTriggerBubbleText, %extent, %vOffset, %arrowButtonY);
}

// 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 SceneGui::swapDirectors(%this, %newDML, %aniTime)
{
   if(%this.dirTriggerImg $= DirTriggerImg2a)
   {
      %objOrig = DirTriggerImg2a;
      %objDest = DirTriggerImg2b;
   }
   else
   {
      %objOrig = DirTriggerImg2b;
      %objDest = DirTriggerImg2a;
   }
   
   %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 SceneGui::onEndDirectorCrossFading(%this)
{
   %this.crossFadingDirector = false;
}
