// Called when the final score window pops up
function WorldMapGui::onWakeIntro(%this, %show)
{
   %this.initializeIntro();
   
   %this.lastText = "";   
   %this.lastFontSize = "";
   %this.lastTextExtent = 10;
   %this.lastTextOffset = 0;
   %this.lastArrowButtonY = 0;
   
   WMIntroGuiBkgnd.setDML("scriptsAndAssets/data/ui/ho/inoutro/Mentor_bkgnd.jpg");
   
   // Check whether to set visible
   WorldMapInOutro.setVisible(%show);
   
   // Play background noise
   sfxPlay(BkgndNoiseSpyLevel);
}

function WorldMapGui::startMap(%this)
{
   WorldMapInOutro.setVisible(false);
   
   MapClapper.setVisible(true);
   MapBoard.setVisible(true);   
   MapClapper.forward();
   MapBoard.forward();
   MapClapper.playRev(1);
   MapBoard.playRev(1);
   sfxPlay(AudioClapperBoardMoving);   
   
   %this.onWake2();
}

// Skip world map intro screen
function WorldMapGui::skip(%this)
{
   sfxStopAll();
   
   switch$($CurPlayerNum)
   {
      case 1: PlayerProfile.RunMentor1 = 1;
      case 2: PlayerProfile.RunMentor2 = 1;
      case 3: PlayerProfile.RunMentor3 = 1;
      case 4: PlayerProfile.RunMentor4 = 1;
   }
   
   SavePlayerProfile();
      
   // Show clapper board
   %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
   MapClapper.setDML(%path);    
   MapClapper.rewind();
   
   if($returningFromScene && $CurSceneName $= "SciFiLevel" && $CurTake $= 4)
      %path = "scriptsAndAssets/data/ui/clapperboard/inoutro/SciFiLevel_intro_take4.jpg";
   else
      %path = "scriptsAndAssets/data/ui/clapperboard/minigame/Minigame_end.jpg";
   MapBoard.setDML(%path);        
   MapBoard.rewind();

   MapClapper.setVisible(true);
   MapBoard.setVisible(true);
   MapClapper.play(1);
   MapBoard.play(1);
   
   sfxPlay(AudioClapperBoardMoving);
}

function WorldMapGui::playClapperBoardCloseSound(%this)
{
   sfxPlay(AudioClapperBoardClose);
}

function WorldMapGui::initializeIntro(%this)
{      
   WMIntroDirTriggerImga.setAlpha(0);
   WMIntroDirTriggerImga.setVisible(false);
   WMIntroDirTriggerImgb.setAlpha(0);
   WMIntroDirTriggerImgb.setVisible(false);
   %this.crossFadingDirector = false;
   
   WMIntroDirTriggerBubble.setVisible(false);   
   WMIntroDirTriggerBubbleText.setVisible(false);
   WMIntroDirTriggerBubbleText.setText("");
   
   WMIntroDirTriggerArrow.setVisible(false);
   WMIntroDirTriggerArrowText.setVisible(false);   

   // Read "continue" text   
   echo("Load Tutorial XML");
   %path = "./scriptsAndAssets/data/ui/tutorial/tutorial.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 "tutorial" onto 
   // the XML document stack.
   if(!%pXml.pushFirstChildElement("tutorial"))
   {
      echo("Failed to load tutorial tag.");
      return;
   }
   
   %this.continueText = %pXml.attribute("continuetext");
   %pXml.popElement();
   %pXml.delete();   
   
   WMIntroDirTriggerArrowText.setText(%this.continueText);
   
   %this.loadDirectorXML();
}

// Parse XML file looking for director behavior tags
function WorldMapGui::loadDirectorXML(%this)
{
   if(%this.showIntro $= true)
      %path = "./scriptsAndAssets/data/ui/directorMentorIntro.xml";
   else
      %path = "./scriptsAndAssets/data/ui/directorMentorOutro.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;
   }
   
   %this.DirTriggerNum = 0;
   if(!%pXml.pushFirstChildElement("ani"))
   {
      echo("Director animations not found.");
      return;
   }
   
   if(%this.showIntro $= true)
      %comp = "intro";
   else
      %comp = "outro";
   
   while(true)
   {
      %showat = %pXml.attribute("showat");

      if(%showat $= %comp)
      {
         %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");

            // Pause button?
            %havePauseButton = %pXml.attribute("pause");
            if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
            %this.DirTrigger[%this.DirTriggerNum,"pause",%textNum]=%havePauseButton;
            
            %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");

                  // Pause button?                  
                  %havePauseButton = %pXml.attribute("pause");
                  if(%havePauseButton !$= "true") %havePauseButton = false; else %havePauseButton = true;
                  %this.DirTrigger[%this.DirTriggerNum,"pause",%textNum]=%havePauseButton;
                  
                  %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();   
   
   echo("Debug: WM XML loaded");
}

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

// Show director (stage 2: show director and bubble)
function WorldMapGui::showDirAni2(%this, %triggerNum)
{
   %path = "scriptsAndAssets/data/ui/ho/inoutro/directors/mentor";    
   %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(canvas.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(canvas.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);
}

// Show director bubble text
function WorldMapGui::showDirAniText1(%this, %triggerNum, %textNum)
{
   // Scale factors when resolution is different than 800x600
   %winScaleX = 1.0 / 800 * getWord(canvas.getExtent(),0);
   %winScaleY = 1.0 / 600 * getWord(canvas.getExtent(),1);
      
   %path = "scriptsAndAssets/data/ui/ho/inoutro/directors/mentor";   
   %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(WMIntroDirTriggerArrow.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;
   
   %this.updateSpeechBubble();

   if(%pause)
   {
       WMIntroDirTriggerArrow.setVisible(true);
       WMIntroDirTriggerArrowText.setVisible(true);
       WMIntroDirTriggerArrow.triggerNum = %triggerNum;
       WMIntroDirTriggerArrow.textNum = %textNum;
   }
   else
   {
      WMIntroDirTriggerArrow.setVisible(false);
      WMIntroDirTriggerArrowText.setVisible(false);
      %this.showDirAniText2(%triggerNum, %textNum);
   }
}

// Update the speech bubble text
function WorldMapGui::updateSpeechBubble(%this)
{
   WMIntroDirTriggerBubbleText.setVisible(false);
   WMIntroDirTriggerBubbleText.setText(%this.lastText);
   WMIntroDirTriggerBubble.setVisible(true);
   WMIntroDirTriggerBubbleText.setVisible(true);   
}

// Second part of showDirAniText2
function WorldMapGui::showDirAniText2(%this, %triggerNum, %textNum)
{
   sfxPlay(AudioButtonPress); //Button Press Sound
   
   %classID = "HO";   

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

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

function WorldMapGui::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 WorldMapGui::hideDirAni(%this, %triggerNum)
{
   %this.lastFontSize = "";
   WMIntroDirTriggerArrow.setVisible(false);  
   WMIntroDirTriggerArrowText.setVisible(false);
   WMIntroDirTriggerBubble.Visible = false;
   WMIntroDirTriggerBubbleText.Visible = false;
     
   %showTime = 500;
   %this.DirTriggerImg.setAlpha(1);
   %this.DirTriggerImg.setVisible(true);         
   %this.DirTriggerImg.animateAlpha(0, 0, %showTime, "0 0");
   
   %this.skip();
}

function MapClapper::onStoppedAtEnd(%this)
{   
   WorldMapGui.playClapperBoardCloseSound();
   WorldMapGui.schedule(500, "startMap");
}

// 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 WorldMapGui::swapDirectors(%this, %newDML, %aniTime)
{
   if(%this.DirTriggerImg $= WMIntroDirTriggerImga)
   {
      %objOrig = WMIntroDirTriggerImga;
      %objDest = WMIntroDirTriggerImgb;
   }
   else
   {
      %objOrig = WMIntroDirTriggerImgb;
      %objDest = WMIntroDirTriggerImga;
   }
   
   %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 WorldMapGui::onEndDirectorCrossFading(%this)
{
   %this.crossFadingDirector = false;
}
