function LoadingWorldMapGui::onAdd(%this)
{
   %this.qLineCount = 0;
}

//------------------------------------------------------------------------------
function LoadingWorldMapGui::onWake(%this)
{
   if(!$ShowWorldMapClapperboard)
      %this.add(getRandomMovieFact()); 
   
   Canvas.setWidescreen(LoadingWorldMapGuiCenter, 0, false);
   
   if($ShowWorldMapClapperboard)
   {
      %path = "scriptsAndAssets/data/ui/clapperboard/clapper.jpg";
      WMLoadingClapper.setDML(%path);
      WMLoadingClapper.forward();
      
      if(OutroGui.playedMinigame)
         %path = "scriptsAndAssets/data/ui/clapperboard/minigame/Minigame_end.jpg";   
      else
         %path = "scriptsAndAssets/data/ui/clapperboard/inoutro/" @ $CurSceneName @ "_intro_take" @ $CurTake @ ".jpg";
         
      WMLoadingBoard.setDML(%path);
      WMLoadingBoard.forward();

      WMLoadingClapper.setVisible(true);
      WMLoadingBoard.setVisible(true);
      
//      LoadingWorldMapGui.bitmap = "scriptsAndAssets/data/ui/loading_screens/" @ $CurSceneName @ "/take_" @ $CurTake;
//      LoadingWorldMapGuiBkgnd.bitmap = "scriptsAndAssets/data/ui/loading_screens/load-screen.jpg";
   }
   else
   {
      %scene[1] = "DinosaurLevel";
      %scene[2] = "GangsterLevel";
      %scene[3] = "KungFuLevel";
      %scene[4] = "MonsterLevel";
      %scene[5] = "MusicalLevel";
      %scene[6] = "SciFiLevel";
      %scene[7] = "SpyLevel";
      %scene[8] = "WesternLevel";
      
      %i = getRandom(1,8);
      %j = getRandom(1,4);
      %path = "scriptsAndAssets/data/ui/loading_screens/" @ %scene[%i] @ "/take_" @ %j;
//      LoadingWorldMapGui.bitmap = %path;
//      LoadingWorldMapGuiBkgnd.bitmap = "scriptsAndAssets/data/ui/loading_screens/load-screen.jpg";
      
      // Play sound...
      CloseMessagePopup();
   }
}

//------------------------------------------------------------------------------
function LoadingWorldMapGui::onSleep(%this)
{
   if($ShowWorldMapClapperboard)
      return;
         
   // Clear the load info:
   if ( %this.qLineCount !$= "" )
   {
      for ( %line = 0; %line < %this.qLineCount; %line++ )
         %this.qLine[%line] = "";
   }      
   %this.qLineCount = 0;

   WMLoadingProgress.setValue( 0 );
}

function LoadingWorldMapGui::onResize(%this, %width, %height)
{
   Canvas.setWidescreen(LoadingWorldMapGuiCenter, 2000, true);
}

function getRandomMovieFact()
{
//   MovieFacts.setProfile(GuiCenteredBigTextProfile);

   if ($MovieFacts $= "")
   {
       $MovieFacts = new ScriptObject() { 
            canSaveDynamicFields = "1"; 
            className = "MovieFacts";
            };
       $MovieFacts.loadMovieFacts();
      
   }

   if (!($MovieFacts.movieFactsCount > 0))
      return;

   %movieFact = new GuiBitmapTextCtrl() {
         canSaveDynamicFields = "1";
         className = "MovieFacts";
         Enabled = "1";
         isContainer = "0";
         HorizSizing = "relative";
         VertSizing = "relative";
         Position = "150 100";
         Extent = "455 215";
         MinExtent = "8 2";
         canSave = "1";
         Visible = "1";
         hovertime = "1000";
         alpha = "1";
         color = "0 0 0 255";
         unitClip = "0 0 1 1";
         lockMouse = "0";
         Margin = "0 0 0 0";
         Padding = "0 0 0 0";
         AnchorTop = "1";
         AnchorBottom = "0";
         AnchorLeft = "1";
         AnchorRight = "0";
         text = "";
         maxLength = "-1";
         bitmap = "scriptsAndAssets/data/ui/komika";
         scale = "0";
         fixedscale = "1";
         fixedscalefactor = "0.6";
         border = "6";
         centerize = "1";
         charspacing = "1";
         charaspect = "1";
         metric = "scriptsAndAssets/data/ui/komika.ini";
      };

   %movieFact.setText($MovieFacts.movieFacts[getRandom($MovieFacts.movieFactsCount - 1)]);
   
   return %movieFact;
}

function MovieFacts::loadMovieFacts(%this)
{
   %path = "./scriptsAndAssets/data/ui/MovieFacts.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 "movie_facts" onto 
   // the XML document stack.
   if(!%pXml.pushFirstChildElement("movie_facts"))
   {
      echo("Failed to movie facts tag.");
      %pXml.delete();   

      return;
   }
   
   %count = 0;
   if(%pXml.pushFirstChildElement("fact"))
   {
      %this.movieFacts[0] = %pXml.attribute("text");
      %this.movieFactsSz[0] = %pXml.attributeF32("size");
   
      %count = 1;
      while(%pXml.nextSiblingElement("fact"))
      {
         %this.movieFacts[%count] = %pXml.attribute("text");
         %this.movieFactsSz[%count] = %pXml.attributeF32("size");
         %count++;
      }
      
      %pXml.popElement();
   }
   
   %pXml.popElement();
   
   %this.movieFactsCount = %count;
   
   %pXml.delete();   
   
   for (%i = 0; %i < %this.movieFactsCount; %i++)
   {
      %mvFact = %this.movieFacts[%i];
      %procMvFact = "";
      %len = strlen(%mvFact);
      %lastSpc = 0;
      %c = 0;   
      for (%c = 0; %c < %len; %c++)
      {
         %chr = getSubStr(%mvFact,%c,1); 
         if (%chr $= " ")
            %lastSpc = %c;
         
         if (%c >= 39)
         {
            %procMvFact = %procMvFact@"\n"@getSubStr(%mvFact, 0, %lastSpc);
            %mvFact = getSubStr(%mvFact, %lastSpc+1, strlen(%mvFact) - (%lastSpc+1));
            %c -= %lastSpc;
            %lastSpc = 0;
            %len = strlen(%mvFact);
         }
      }
      
       %procMvFact = getSubStr(%procMvFact, 1, strlen(%procMvFact) - 1)@"\n"@%mvFact;     
       %this.movieFacts[%i] = %procMvFact;
   }
   
   
}

