//--- OBJECT WRITE BEGIN ---
new GuiChunkedBitmapCtrl(startMissionGui) {
   profile = "GuiContentProfile";
   horizSizing = "width";
   vertSizing = "height";
   position = "0 0";
   extent = "640 480";
   minExtent = "8 8";
   visible = "1";
   helpTag = "0";
   bitmap = "./background.jpg";
   useVariable = "0";
   tile = "0";

   new GuiControl() {
      profile = "GuiWindowProfile";
      horizSizing = "center";
      vertSizing = "center";
      position = "92 86";
      extent = "455 308";
      minExtent = "8 8";
      visible = "1";
      helpTag = "0";

      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "12 36";
         extent = "72 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Select Mission:";
         maxLength = "255";
      };
      new GuiCheckBoxCtrl(ML_isMultiplayer) {
         profile = "GuiCheckBoxProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "155 266";
         extent = "147 23";
         minExtent = "8 8";
         visible = "1";
         variable = "pref::HostMultiPlayer";
         helpTag = "0";
         text = "Multiplayer Mission";
         groupNum = "-1";
         buttonType = "ToggleButton";
            maxLength = "255";
      };
      new GuiButtonCtrl() {
         profile = "GuiButtonProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "320 265";
         extent = "127 23";
         minExtent = "8 8";
         visible = "1";
         command = "SM_StartMission();";
         helpTag = "0";
         text = "Launch Mission";
         groupNum = "-1";
         buttonType = "PushButton";
      };
      new GuiScrollCtrl() {
         profile = "GuiScrollProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "10 62";
         extent = "436 200";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         willFirstRespond = "1";
         hScrollBar = "dynamic";
         vScrollBar = "alwaysOn";
         constantThumbHeight = "0";
         childMargin = "0 0";
            defaultLineHeight = "15";

         new GuiTextListCtrl(SM_missionList) {
            profile = "GuiTextArrayProfile";
            horizSizing = "right";
            vertSizing = "bottom";
            position = "2 2";
            extent = "414 48";
            minExtent = "8 8";
            visible = "1";
            helpTag = "0";
            enumerate = "0";
            resizeCell = "1";
            columns = "0";
            fitParentWidth = "1";
            clipColumnText = "0";
               noDuplicates = "false";
         };
      };
      new GuiTextEditCtrl() {
         profile = "GuiTextEditProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "98 15";
         extent = "134 18";
         minExtent = "8 8";
         visible = "1";
         variable = "pref::Player::Name";
         helpTag = "0";
         maxLength = "255";
         historySize = "0";
         password = "0";
         tabComplete = "0";
         sinkAllKeyEvents = "0";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "12 11";
         extent = "63 18";
         minExtent = "8 8";
         visible = "1";
         helpTag = "0";
         text = "Player Name:";
         maxLength = "255";
      };
      new GuiButtonCtrl() {
         profile = "GuiButtonProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "10 266";
         extent = "127 23";
         minExtent = "8 8";
         visible = "1";
         command = "Canvas.setContent(mainMenuGui);";
         helpTag = "0";
         text = "<< Back";
         groupNum = "-1";
         buttonType = "PushButton";
      };
      new GuiTextCtrl() {
         profile = "GuiTextProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "52 286";
         extent = "345 18";
         minExtent = "8 2";
         visible = "1";
         helpTag = "0";
         text = "Change server settings by visiting the Network tab in the options dialog.";
         maxLength = "255";
      };
   };
};
//--- OBJECT WRITE END ---


//----------------------------------------
function SM_StartMission()
{
   %id = SM_missionList.getSelectedId();
   %mission = getField(SM_missionList.getRowTextById(%id), 1);

   if ($pref::HostMultiPlayer)
      %serverType = "MultiPlayer";
   else
      %serverType = "SinglePlayer";

   createServer(%serverType, %mission);
   %conn = new GameConnection(ServerConnection);
   RootGroup.add(ServerConnection);
   %conn.setConnectArgs($pref::Player::Name);
   %conn.setJoinPassword($Client::Password);
   %conn.connectLocal();
}


//----------------------------------------
function startMissionGui::onWake()
{
   SM_missionList.clear();
   %i = 0;
   for(%file = findFirstFile($Server::MissionFileSpec);
         %file !$= ""; %file = findNextFile($Server::MissionFileSpec))  
      if (strStr(%file, "CVS/") == -1 && strStr(%file, "common/") == -1)
         SM_missionList.addRow(%i++, getMissionDisplayName(%file) @ "\t" @ %file );
   SM_missionList.sort(0);
   SM_missionList.setSelectedRow(0);
   SM_missionList.scrollVisible(0);
}   


//----------------------------------------
function getMissionDisplayName( %missionFile ) 
{
   %file = new FileObject();
   
   %MissionInfoObject = "";
   
   if ( %file.openForRead( %missionFile ) ) {
		%inInfoBlock = false;
		
		while ( !%file.isEOF() ) {
			%line = %file.readLine();
			%line = trim( %line );
			
			if( %line $= "new ScriptObject(MissionInfo) {" )
				%inInfoBlock = true;
			else if( %inInfoBlock && %line $= "};" ) {
				%inInfoBlock = false;
				%MissionInfoObject = %MissionInfoObject @ %line; 
				break;
			}
			
			if( %inInfoBlock )
			   %MissionInfoObject = %MissionInfoObject @ %line @ " "; 	
		}
		
		%file.close();
	}
	%MissionInfoObject = "%MissionInfoObject = " @ %MissionInfoObject;
	eval( %MissionInfoObject );
	
   %file.delete();

   if( %MissionInfoObject.name !$= "" )
      return %MissionInfoObject.name;
   else
      return fileBase(%missionFile); 
}

