function SubtitleBox::onResize(%this, %width, %height)
{      
   %fontSize = %this.fontSize *  Canvas.scaleSize;
   if(%fontSize > 62) %fontSize = 62;   
   %this.format = "<just:center><font:Arial:"@%fontSize@"><color:FFFF00>\n";
   %this.setText(%this.format@%this.curSubText);   
   
   if(isObject(%this.boxImage))
      %this.boxImage.setPosition(getWord(%this.Position, 0), (getWord(%this.Position, 1) + %this.fontSize * Canvas.scaleSize));
}

function SubtitleBox::init(%this, %file)
{
   %this.fontSize = 32;
   %this.curSubText = "";
   
   // Force resizing   
   %this.onResize(getWord(Canvas.extent,0), getWord(Canvas.extent,1));
   
   %this.visible = 0;
   %this.subtitleCount = 0;
   if (%this.boxImage $= "")
   {
      %this.boxImage = new GuiBitmapCtrl() {
            canSaveDynamicFields = "0";
            Enabled = "1";
            isContainer = "0";
            HorizSizing = "relative";
            VertSizing = "relative";
            Position = (getWord(%this.Position, 0) SPC (getWord(%this.Position, 1) + %this.fontSize * Canvas.scaleSize));
            Extent = %this.Extent;
            MinExtent = "8 2";
            canSave = "1";
            Visible = "0";
            hovertime = "1000";
            alpha = "1";
            color = "255 255 255 255";
            unitClip = "0 0 1 1";
            bitmap = "scriptsAndAssets/data/ui/SubtitleBox";
            wrap = "0";
      };

      GuiCenter.add(%this.boxImage);   
      GuiCenter.bringToFront(%this);
      GuiCenter.bringToFront(%this.boxImage);
      GuiCenter.bringToFront(Cam0);
   }
   else
      %this.boxImage.Visible = "0";

   %this.parseFile(%file);
   
   %this.curSubtitle = 0;
   scheduleObjLCA(%this,"HO",%this.time[0], "nextSubtitle", 0);
}

function SubtitleBox::parseFile(%this, %file)
{
   %i = -1;
   %this.time[%i++] = 13000;
   %this.subtitle[%i] = "Now you\'re in, head for the vault";
   %this.time[%i++] = 14000;
   %this.subtitle[%i] = "door at the end of that corridor.";
   %this.time[%i++] = 16080;
   %this.subtitle[%i] = "You\'ve been behind that computer for too long.";
   %this.time[%i++] = 17080;
   %this.subtitle[%i] = "I wasn\'t exactly going to try the fire exit.";
   %this.time[%i++] = 25090;
   %this.subtitle[%i] = "You know, I really don\'t";
   %this.time[%i++] = 26090;
   %this.subtitle[%i] = "fancy becoming a living art installation.";
   %this.time[%i++] = 54150;
   %this.subtitle[%i] = "Can you light up these security beams for me?";
   %this.time[%i++] = 57000;
   %this.subtitle[%i] = "Like fireworks.";
   %this.time[%i++] = 59270;
   %this.subtitle[%i] = "I can\'t go over, darling.  You know, I think";
   %this.time[%i++] = 61270;  
   %this.subtitle[%i] = "I may have found a practical application";
   %this.time[%i++] = 63270;
   %this.subtitle[%i] = "for years of yoga and Pilates.";
   
   %this.subtitleCount = %i++;
}

function SubtitleBox::nextSubtitle(%this)
{
   %i = %this.curSubtitle;
   %this.showSubtitle(%this.subtitle[%i]);

   if (%i >= %this.subtitleCount - 1)
   {
      scheduleObjLCA(%this,"HO", 3000, "hideSubtitle", 0);
      return;
   }
   
   %timeToNext = %this.time[%i+1] - %this.time[%i];
   
   if (%timeToNext > 3200)
      scheduleObjLCA(%this,"HO", 3000, "hideSubtitle", 0);

   %this.curSubtitle = %i + 1;
   scheduleObjLCA(%this,"HO",%timeToNext, "nextSubtitle", 0);
}

function SubtitleBox::showSubtitle(%this, %subtitle)
{
   if (%subtitle !$= "")
   {
      %this.setText(%this.format@%subtitle);
      %this.curSubText = %subtitle;
   }
   %this.Visible = 1;
   %this.boxImage.Visible = 1;
}

function SubtitleBox::hideSubtitle(%this)
{
   %this.curSubText = "";   
   %this.Visible = 0;
   %this.boxImage.Visible = 0;   
}
