//-----------------------------------------------------------------------------
// Handle events of GUI controls of the select player menu.
//-----------------------------------------------------------------------------

function SelectPlayerMenuDlg::onWake(%this)
{
   %pXml = loadMenuXML();
   parseSPMenuXML(%pXml);
   %pXml.delete();
   
   SPButton1.Visible = false;
   SPButton2.Visible = false;
   SPButton3.Visible = false;
   SPButton4.Visible = false;
   
   SPButtonDel1.Visible = false;
   SPButtonDel2.Visible = false;
   SPButtonDel3.Visible = false;
   SPButtonDel4.Visible = false;
   
   SPButton1Text.Visible = false;
   SPButton2Text.Visible = false;
   SPButton3Text.Visible = false;
   SPButton4Text.Visible = false;
   
   %this.update();
   
   if(PlayerProfile.PlayerName1 !$= "")
   {
      SPButton1.Visible = true;  
      SPButton1Text.Visible = true;      
      SPButton1Text.setText(PlayerProfile.PlayerName1);
      SPButtonDel1.Visible = true;  
   }
   else
   {
      SPButton1.Visible = false;//true;
      SPButton1Text.Visible = false;//true;
      SPButton1Text.text = "";//$NewPlayerText;
      SPButtonDel1.Visible = false;
      return;
   }
      
   if(PlayerProfile.PlayerName2 !$= "")
   {
      SPButton2.Visible = true;      
      SPButton2Text.setText(PlayerProfile.PlayerName2);
      SPButtonDel2.Visible = true;
      
      SPButton2Text.Visible = true;      
   }
   else
   {
      SPButton2.Visible = false;//true;      
      SPButton2Text.text = "";//$NewPlayerText;      
      SPButtonDel2.Visible = false;
      
      SPButton2Text.Visible = true;           
      return;      
   }
      
   if(PlayerProfile.PlayerName3 !$= "")      
   {
      SPButton3.Visible = true;      
      SPButton3Text.setText(PlayerProfile.PlayerName3);
      SPButtonDel3.Visible = true;
      
      SPButton3Text.Visible = true;      
   }
   else
   {
      SPButton3.Visible = false;//true;      
      SPButton3Text.text = "";//$NewPlayerText;
      SPButtonDel3.Visible = false; 
      
      SPButton3Text.Visible = true;      
      return;         
   }
      
   if(PlayerProfile.PlayerName4 !$= "")      
   {
      SPButton4.Visible = true;      
      SPButton4Text.setText(PlayerProfile.PlayerName4);
      SPButtonDel4.Visible = true;  
      
      SPButton4Text.Visible = true;      
   }
   else
   {
      SPButton4.Visible = false;//true;      
      SPButton4Text.text = "";//$NewPlayerText;
      SPButtonDel4.Visible = false; 
      
      SPButton4Text.Visible = true;      
      return;         
   }
   
   Canvas.setWidescreen(SelectPlayerMenuDlgCenter, 100, false);   
}

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

// Parse XML file looking for menu text tags
function parseSPMenuXML(%pXml)
{
   if(!isObject(%pXml))
      return;
      
   // Push the first child element named "selectplayer" onto the XML document stack.
   if(!%pXml.pushFirstChildElement("selectplayer"))
   {
      echo("Failed to load 'selectplayer' tag.");
      return;
   }
   
   if(%pXml.pushFirstChildElement("title"))
   {
      SPTitleText.text = %pXml.attribute("text");
      %pXml.popElement();
   }

   if(%pXml.pushFirstChildElement("newplayer"))
   {
      $NewPlayerText = %pXml.attribute("text");
      %pXml.popElement();
   }

   if(%pXml.pushFirstChildElement("deleteplayer"))
   {
      %str = %pXml.attribute("text");
      SPButtonDel1Text.text = %str;
      SPButtonDel2Text.text = %str;
      SPButtonDel3Text.text = %str;
      SPButtonDel4Text.text = %str;                  
      %pXml.popElement();
   }

   if(%pXml.pushFirstChildElement("back"))
   {
      SPBackMenuButtonText.text = %pXml.attribute("text");
      %pXml.popElement();
   }
}

function NewPlayer()
{
   NPTextEdit.text = "";      
   MainMenuGui.pushThread(new_player_menu_thread);      
}

// Set new player name and exit to scene selection menu
function PlayerSelected(%playerNum)
{
   $CurPlayerNum = %playerNum;
   
   switch$($CurPlayerNum)
   {
      case 1: $CurPlayerName = PlayerProfile.PlayerName1;
      case 2: $CurPlayerName = PlayerProfile.PlayerName2;
      case 3: $CurPlayerName = PlayerProfile.PlayerName3;
      case 4: $CurPlayerName = PlayerProfile.PlayerName4;      
   }
   
   $pref::Player::Name = $CurPlayerName;
   $pref::Player::Num  = $CurPlayerNum;
   
   showWorldMap(false);
}

function RemovePlayer(%playerNum)
{
   SelectPlayerMenuDlg.playerNumToDel = %playerNum;
   
   Canvas.pushDialog(DelDlg);
}

// Called when the user confirms deleting
function RemovePlayer2()
{
   %playerNum = SelectPlayerMenuDlg.playerNumToDel;
   
   $CurPlayerNum = %playerNum;
   $CurPlayerName = "";

   $pref::Player::Name = $CurPlayerName;
   $pref::Player::Num  = $CurPlayerNum;

   switch(%playerNum)
   {
      case 1: 
         PlayerProfile.PlayerName1 = "";
         if(PlayerProfile.PlayerName2 !$= "")
         {
            PlayerProfile.PlayerName1 = PlayerProfile.PlayerName2;
            PlayerProfile.PlayerName2 = "";
         }
         if(PlayerProfile.PlayerName3 !$= "")
         {
            PlayerProfile.PlayerName2 = PlayerProfile.PlayerName3;
            PlayerProfile.PlayerName3 = "";
         }
         if(PlayerProfile.PlayerName4 !$= "")
         {
            PlayerProfile.PlayerName3 = PlayerProfile.PlayerName4;
            PlayerProfile.PlayerName4 = "";
         }
      case 2:
         PlayerProfile.PlayerName2 = "";
         if(PlayerProfile.PlayerName3 !$= "")
         {
            PlayerProfile.PlayerName2 = PlayerProfile.PlayerName3;
            PlayerProfile.PlayerName3 = "";
         }
         if(PlayerProfile.PlayerName4 !$= "")
         {
            PlayerProfile.PlayerName3 = PlayerProfile.PlayerName4;
            PlayerProfile.PlayerName4 = "";
         }
      case 3: 
         PlayerProfile.PlayerName3 = "";
         if(PlayerProfile.PlayerName4 !$= "")
         {
            PlayerProfile.PlayerName3 = PlayerProfile.PlayerName4;
            PlayerProfile.PlayerName4 = "";
         }
      case 4: 
         PlayerProfile.PlayerName4 = "";
   }
   
   SelectPlayerMenuDlg.update();
   
   %remainingPlayers = 0;
   if(PlayerProfile.PlayerName1 !$= "") %remainingPlayers++;
   if(PlayerProfile.PlayerName2 !$= "") %remainingPlayers++;
   if(PlayerProfile.PlayerName3 !$= "") %remainingPlayers++;
   if(PlayerProfile.PlayerName4 !$= "") %remainingPlayers++;
               
   SavePlayerProfile();
   
   if(%remainingPlayers $= 0)
   {
      NPTextEdit.text = "";
      MainMenuGui.popThread();
      MainMenuGui.pushThread(new_player_menu_thread);
   }
   else
   {
      MainMenuGui.popThread();
      MainMenuGui.pushThread(select_player_menu_thread);
   }
}

function SPButton1::onMouseEnter(%this)
{
   SPButton1Text.color = "255 128 0 255";
}

function SPButton1::onMouseLeave(%this)
{
   SPButton1Text.color = "0 0 0 255";
}

function SPButton2::onMouseEnter(%this)
{
   SPButton2Text.color = "255 128 0 255";
}

function SPButton2::onMouseLeave(%this)
{
   SPButton2Text.color = "0 0 0 255";
}

function SPButton3::onMouseEnter(%this)
{
   SPButton3Text.color = "255 128 0 255";
}

function SPButton3::onMouseLeave(%this)
{
   SPButton3Text.color = "0 0 0 255";
}

function SPButton4::onMouseEnter(%this)
{
   SPButton4Text.color = "255 128 0 255";
}

function SPButton4::onMouseLeave(%this)
{
   SPButton4Text.color = "0 0 0 255";
}

function SelectPlayerMenuDlg::update(%this)
{
   // Can't allow more than 4 players
   if(PlayerProfile.PlayerName1 !$= "" &&
      PlayerProfile.PlayerName2 !$= "" &&
      PlayerProfile.PlayerName3 !$= "" &&
      PlayerProfile.PlayerName4 !$= "")
      SPNewPlayerButton.setVisible(false);
   else
      SPNewPlayerButton.setVisible(true);   
}