function SaveGameList::OnWake()
{
   BuildSaveList();   
   $screenActive = true;
}

// this will search for all save games and assign their saveid to the button

function BuildSaveList()
{
   // first clear out the buttons
   
   for(%i=1;%i<=10;%i++)
   {
      %control = "save" @ %i;
      %control.SetText("");
      %control.SetProfile(GuiButton28FF);
      
      %control.saveid = "";
               
      %control = "save" @ %i @ "desc";
      %control.SetText("");
   }

   %i=1;
   
   // now search for any saved games   
   
   for(%file = findFirstFile("~/save*.sav"); %file !$= ""; %file = findNextFile("~/save*.sav"))  
   {
      %fo = new fileobject();
      
      %fo.OpenForRead(%file);
      
      for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
      {
         %word = GetWord(%line, 0);
         
         switch$ (%word)
         {         
            case "SaveName":
               %savename = GetWord(%line, 1);              
            case "Saveid":
               %saveid = GetWord(%line, 1);
            case "clock":
               %time = restWords(%line);
            
         }
      }
      
      if(%saveid !$= "")
      {
         %control = "save" @ %i;      
         %control.SetText(%savename);
         %control.saveid = %saveid;
         %control.savename = %savename;
         
         %control = "save" @ %i @ "desc";
         %control.SetText(%time);
                  
         %i++;
      }
            
      %fo.close();      
   }
}

// when the player selects a button with a save game

function SelectGame(%slot)
{
   %control = "save" @ %slot;
 
   $SaveName = %control.savename;
  
   %saveid = %control.saveid;
   
   if(%saveid $= "")
   {
      alxPlay(buzzer);
      return;
   }

   alxPlay(click);

   %control.SetProfile(GuiButton28Red);
   
   if($saveid !$= %saveid && $saveid !$= "")
      $SaveControl.SetProfile(GuiButton28FF);
   
   $SaveControl = %control;
   $saveid = %saveid;
}

function SaveGameList::OnSleep()
{
   $screenActive = false;
   alxPlay(click);   
}

function InitSaveGame()
{
   canvas.pushdialog(SaveGameList);
   alxPlay(click);   
}

// this will let us put in a splash screen before actually loading a game

function PreLoadGame()
{
   if($saveid !$= "")
   {
      alxPlay(click);
      canvas.SetContent(LoadingScreen);
      canvas.cursoroff();
      ResetLoadingBar();
      ChangeLoadingBar(100);
      schedule(100, 0, "LoadGame");   
   }
}

function DeleteGame()
{
   if($saveid $= "")
      return;
      
   for(%file = findFirstFile("~/save*.sav"); %file !$= ""; %file = findNextFile("~/save*.sav"))  
   {
      %fo = new fileobject();
      
      %fo.OpenForRead(%file);
      
      %match = "";
            
      for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
      {
         %word = GetWord(%line, 0);
         
         if(%word $= "Saveid")
         {
            %saveid = getWord(%line, 1);
            
            if(%saveid $= $saveid)
               %match = true;                     
         }
      }

      %fo.close();      
      
      // if this is the file we want to delete
      
      if(%match $= true)
      {      
         %fo = new fileobject();

         %file = "~/save" @ $saveid @ ".sav";
       
         %fo.OpenForWrite(%file);   
         
         %fo.WriteLine("saveid ");              
         %fo.WriteLine("savename ");              
            
         %fo.close();   

         %border = $SaveControl @ "border";
         %border.SetBitmap(expandfilename("~/data/images/gui/sml_brdr.png"));            
         $SaveControl.text = "";
         $SaveControl.saveid = "";
         
         %control = $SaveControl @ "desc";
         %control.SetText("");

         $SaveControl = "";
      }
   }
   
   $saveid = "";
   $savename = "";
   
   
}

function CloseSaveGameList()
{
   canvas.PopDialog(SaveGameList);
   canvas.PopDialog(Options);   
   
}
