internal string vesselConcat(contractMission m)
        {
            if (m == null || !HighLogic.LoadedSceneIsFlight)
                return vesselIDString;

            Vessel v = FlightGlobals.ActiveVessel;

            if (v == null)
                return vesselIDString;

            bool withVessel = ContainsVessel(v);
            bool currentMission = m == this;

            if (withVessel && !currentMission)
                removeFromVessels(v, false);
            else if (!withVessel && currentMission)
                addToVessels(v, false);

            List<Vessel> source = currentVessels.Values.ToList();

            return vesselConcat(source);
        }
		internal contractMission setCurrentMission(string s)
		{
			contractMission m = getMissionList(s, true);

			if (m != null)
			{
				currentMission = m;
				return currentMission;
			}

			currentMission = masterMission;
			return currentMission;
		}
		//Initializes all missions that were added during the loading process
		internal void loadAllMissionLists()
		{
			foreach (contractMission m in missionList.Values)
			{
				if (m != null)
				{
					if (m.MasterMission)
					{
						m.buildMissionList();
						foreach (contractContainer c in masterList.Values)
							m.addContract(c, true, false);
						masterMission = m;
					}
					else
						m.buildMissionList();
				}
			}
		}
		internal bool addMissionList(contractMission mission)
		{
			if (!missionList.ContainsKey(mission.Name))
			{
				missionList.Add(mission.Name, mission);
				return true;
			}
			else
				DMC_MBE.LogFormatted("Missions List Already Contains Mission Of Name: [{0}]; Please Rename", name);

			return false;
		}
		//Used to add the master contract mission list; usually when something has gone wrong
		internal void addFullMissionList()
		{
			string s = "MasterMission";

			if (missionList.ContainsKey(s))
				removeMissionList(s);

			if (addMissionList(s))
			{
				missionList[s].MasterMission = true;
				addAllContractsToMaster();
				masterMission = missionList[s];
			}
		}
		//Convert all of our saved strings into the appropriate arrays for each game scene
		public override void OnLoad(ConfigNode node)
		{
			instance = this;

			try
			{
				//The first step is manually checking for active contracts from the Game ConfigNode (ie persistent.sfs file); the count of active contracts will be used later when the window is loading
				ConfigNode gameNode = HighLogic.CurrentGame.config;
				if (gameNode != null)
				{
					ConfigNode contractSystemNode = gameNode.GetNodes("SCENARIO").FirstOrDefault(c => c.GetValue("name") == "ContractSystem");
					if (contractSystemNode != null)
					{
						ConfigNode cNode = contractSystemNode.GetNode("CONTRACTS");
						if (cNode != null)
						{
							foreach (ConfigNode C in cNode.GetNodes("CONTRACT"))
							{
								if (C == null)
									continue;

								if (C.HasValue("autoAccept"))
								{
									if (C.GetValue("autoAccept") == "True")
										continue;
								}

								if (C.HasValue("state"))
								{
									if (C.GetValue("state") == "Active")
										contractCount++;
								}
							}
						}
						else
							DMC_MBE.LogFormatted("Contract System Can't Be Checked... Node Invalid");
					}
				}

				//The next step is checking the current version number and comparing it to the saved value;
				//if a newer version number is detected the rest of the load process is skipped;
				//this resets all values for version updates
				//if (version == contractAssembly.Version)
				//{
					ConfigNode scenes = node.GetNode("Contracts_Window_Parameters");
					if (scenes != null)
					{
						windowPos = stringSplit(scenes.GetValue("WindowPosition"));
						windowVisible = stringSplitBool(scenes.GetValue("WindowVisible"));
						int[] winPos = new int[4] { windowPos[4 * currentScene(HighLogic.LoadedScene)], windowPos[(4 * currentScene(HighLogic.LoadedScene)) + 1], windowPos[(4 * currentScene(HighLogic.LoadedScene)) + 2], windowPos[(4 * currentScene(HighLogic.LoadedScene)) + 3] };

						//All saved contract missions are loaded here
						//Each mission has a separate contract list
						foreach (ConfigNode m in scenes.GetNodes("Contracts_Window_Mission"))
						{
							if (m == null)
								continue;

							string name;
							string activeString = "";
							string hiddenString = "";
							string vesselString = "";
							bool ascending, showActive;
							int sortMode;
							bool master = false;

							if (m.HasValue("MissionName"))
								name = m.GetValue("MissionName");
							else
								continue;

							if (name == "MasterMission")
								master = true;

							if (m.HasValue("ActiveListID"))
								activeString = m.GetValue("ActiveListID");
							if (m.HasValue("HiddenListID"))
								hiddenString = m.GetValue("HiddenListID");
							if (m.HasValue("VesselIDs"))
								vesselString = m.GetValue("VesselIDs");

							if (!bool.TryParse(m.GetValue("AscendingSort"), out ascending))
								ascending = true;
							if (!bool.TryParse(m.GetValue("ShowActiveList"), out showActive))
								showActive = true;
							if (!int.TryParse(m.GetValue("SortMode"), out sortMode))
								sortMode = 0;

							contractMission mission = new contractMission(name, activeString, hiddenString, vesselString, ascending, showActive, sortMode, master);

							if (master)
							{
								masterMission = mission;
								DMC_MBE.LogFormatted_DebugOnly("Setting Master Mission During Load");
							}

							if (!missionList.ContainsKey(name))
								missionList.Add(name, mission);
						}
						loadWindow(winPos);
					}
				//}

				version = contractAssembly.Version;
			}
			catch (Exception e)
			{
				DMC_MBE.LogFormatted("Contracts Window Settings Cannot Be Loaded: {0}", e);
			}
		}
        private void buildPopup(int id, int size)
        {
            if (popup)
            {
                if (showSort)
                {
                    popupRect = new Rect(10, 20, 80 + size * 15, 132 + size * 23);
                    GUI.Box(popupRect, "", contractSkins.dropDown);

                    var sortTypes = Enum.GetNames(typeof(sortClass));

                    for (int i = 0; i < sortTypes.Length; i++)
                    {
                        if (GUI.Button(new Rect(popupRect.x + 2, popupRect.y + 2 + ((21 + size * 4) * i), popupRect.width - 4, 20 + size * 5), sortTypes[i], contractSkins.sortMenu))
                        {
                            showSort = false;
                            currentMission.OrderMode = (sortClass)Enum.Parse(typeof(sortClass), sortTypes[i]);
                            refreshContracts(cList);
                        }
                    }
                }

                else if (rebuild)
                {
                    popupRect = new Rect(10, WindowRect.height - 180, 230, 150);
                    GUI.Box(popupRect, "", contractSkins.dropDown);
                    GUI.Label(new Rect(popupRect.x + 7, popupRect.y + 10, popupRect.width - 14, 100), "Rebuild\nContracts Window + Display:\n\n<b>Will Not</b> Affect Contract Status", contractSkins.resetBox);
                    if (GUI.Button(new Rect(popupRect.x + 20, popupRect.y + 110, popupRect.width - 40, 25), "Reset Display", contractSkins.resetButton))
                    {
                        LogFormatted("Rebuilding Contract Window List");
                        rebuildList();
                        resetWindow();
                        popup = false;
                        rebuild = false;
                    }
                }

                else if (agencyPopup)
                {
                    popupRect = new Rect(10, 40, 230 + size * 20, 80);
                    GUI.Box(popupRect, "", contractSkins.dropDown);
                    Rect r = new Rect(popupRect.x + 5, popupRect.y + 10, 84, 60);
                    GUI.Box(r, "", contractSkins.agentBackground);
                    r.x += 10;
                    r.y += 10;
                    r.width = 64;
                    r.height = 40;
                    GUI.Label(r, currentAgent.LogoScaled);
                    r.x += 85;
                    r.y -= 10;
                    r.width = 120 + size * 20;
                    r.height = 60;
                    GUI.Label(r, currentAgent.Name, contractSkins.agentName);
                }

                else if (missionCreator)
                {
                    popupRect = new Rect(20, 30, 210 + size * 20, 200);
                    GUI.Box(popupRect, "", contractSkins.dropDown);
                    if (!missionTextBox)
                    {
                        for (int i = 0; i < missionList.Count; i++)
                        {
                            missionScroll = GUI.BeginScrollView(popupRect, missionScroll, new Rect(0, 0, 190 + size * 20, 25 * missionList.Count));
                            Rect r = new Rect(2, (25 * i) + 2, 140 + size * 20, 25);
                            if (i == 0)
                            {
                                if (GUI.Button(r, "Create New Mission", contractSkins.missionMenuCenter))
                                {
                                    inputField = "";
                                    missionTextBox = true;
                                    popup = true;
                                }
                            }
                            else
                            {
                                contractMission m = missionList[i];
                                bool containsContract = m.containsContract(tempContainer.Container.Contract.ContractGuid);

                                r.x += 15;

                                if (containsContract)
                                {
                                    GUI.DrawTexture(new Rect(r.x - 15, r.y + 6, 12 + size * 2, 10 + size * 2), contractSkins.checkIcon);

                                    GUI.Label(r, m.Name, contractSkins.missionMenu);
                                }
                                else
                                {
                                    if (GUI.Button(r, m.Name, contractSkins.missionMenu))
                                    {
                                        m.addContract(tempContainer.Container, true, true);
                                        popup = false;
                                        missionCreator = false;
                                    }
                                }

                                r.x += 145 + size * 18;
                                r.y += 4;
                                r.width = 15 + size * 5;

                                GUI.Label(r, m.ActiveContracts.ToString(), contractSkins.timerGood);

                                if (!m.MasterMission && containsContract)
                                {
                                    r.x += 15 + size * 2;
                                    r.width = 14 + size * 4;
                                    r.height = 14 + size * 4;

                                    if (GUI.Button(r, new GUIContent(contractSkins.cancelMissionIcon, "Remove From Mission List"), contractSkins.texButtonSmall))
                                    {
                                        if (m == currentMission)
                                            nextRemoveMissionList.Add(tempContainer);
                                        else
                                            m.removeContract(tempContainer.Container);
                                    }
                                }
                            }
                            GUI.EndScrollView();
                        }
                    }
                    else
                    {
                        Rect r = new Rect(popupRect.x + 2, popupRect.y + 2, 150 + size * 20, 25);
                        GUI.Label(r, "Create New Mission", contractSkins.missionMenuCenter);

                        r.y += 30;

                        inputField = GUI.TextField(r, inputField, 20);

                        r.y += 30;

                        if (GUI.Button(r, "Create Mission", contractSkins.resetButton))
                        {
                            if (!string.IsNullOrEmpty(inputField))
                            {
                                if (contractScenario.Instance.addMissionList(inputField))
                                {
                                    contractMission cM = contractScenario.Instance.getMissionList(inputField);
                                    if (cM != null)
                                        cM.addContract(tempContainer.Container, true, true);
                                    missionList = contractScenario.Instance.getAllMissions();
                                    popup = false;
                                    missionTextBox = false;
                                    missionCreator = false;
                                }
                            }
                            else
                                ScreenMessages.PostScreenMessage("Contract mission list must have a name", 5f, ScreenMessageStyle.UPPER_CENTER);
                        }
                    }
                }

                else if (missionSelector)
                {
                    popupRect = new Rect(20, 30, 180 + size * 20, 200);
                    GUI.Box(popupRect, "", contractSkins.dropDown);
                    for (int i = 0; i < missionList.Count; i++)
                    {
                        missionScroll = GUI.BeginScrollView(popupRect, missionScroll, new Rect(0, 0, 160 + size * 20, 25 * missionList.Count));
                        Rect r = new Rect(2, (25 * i) + 2, 140 + size * 20, 25);
                        contractMission m = missionList[i];
                        if (GUI.Button(r, m.Name, contractSkins.missionMenu))
                        {
                            currentMission = contractScenario.Instance.setCurrentMission(m.Name);

                            if (currentMission.ShowActiveMissions)
                                cList = currentMission.ActiveMissionList;
                            else
                                cList = currentMission.HiddenMissionList;

                            pinnedList = currentMission.loadPinnedContracts(cList);

                            refreshContracts(cList);

                            popup = false;
                            missionSelector = false;
                        }
                        r.x += 145 + size * 18;
                        r.y += 4;
                        r.width = 15 + size * 5;
                        GUI.Label(r, m.ActiveContracts.ToString(), contractSkins.timerGood);
                        GUI.EndScrollView();
                    }
                }

                else if (toolbar)
                {
                    popupRect = new Rect(10, WindowRect.height - 170, 230 + (size * 20), 140);
                    GUI.Box(popupRect, "", contractSkins.dropDown);
                    Rect r = new Rect(popupRect.x + 10, popupRect.y + 10, popupRect.width - 20, 30);
                    GUI.Label(r, "Toolbar Options:", contractSkins.resetBox);

                    r.y += 30;

                    if (ToolbarManager.ToolbarAvailable)
                    {
                        contractScenario.Instance.stockToolbar = GUI.Toggle(r, contractScenario.Instance.stockToolbar, " Use Stock Toolbar");
                        r.y += 30;
                    }

                    if (stockToolbar || !ToolbarManager.ToolbarAvailable)
                    {
                        contractScenario.Instance.replaceStockToolbar = GUI.Toggle(r, contractScenario.Instance.replaceStockToolbar, " Replace Stock Toolbar");
                        r.y += 30;
                    }

                    r.x += 70;
                    r.width = 70;

                    if (GUI.Button(r, "Close", contractSkins.resetButton))
                    {
                        toolbar = false;
                        popup = false;
                    }
                }

                else if (replaceStockPopup)
                {
                    popupRect = new Rect(10, WindowRect.height - 195, 230 + (size * 20), 165);
                    GUI.Box(popupRect, "", contractSkins.dropDown);
                    Rect r = new Rect(popupRect.x + 5, popupRect.y + 5, popupRect.width - 10, 90);
                    GUI.Label(r, "Warning:\nReplacing Stock Contracts App May Produce Errors\nUse This Option\nAt Your Own Risk", contractSkins.resetBox);

                    r.y += 95;
                    r.height = 30;

                    contractScenario.Instance.replaceStockWarned = GUI.Toggle(r, contractScenario.Instance.replaceStockWarned, "Do Not Display This Warning");

                    r.x += 70;
                    r.y += 30;
                    r.width = 75;

                    if (GUI.Button(r, "Confirm", contractSkins.resetButton))
                    {
                        popup = false;
                        replaceStockPopup = false;
                        if (contractScenario.Instance.appLauncherButton != null)
                        {
                            contractScenario.Instance.appLauncherButton.replaceStockApp();
                            replaceStock = true;
                            contractScenario.Instance.replaceStockToolbar = true;
                        }
                        else
                            LogFormatted("Error In Setting Stock App Launcher Button...");
                    }
                }

                else if (missionEdit)
                {
                    popupRect = new Rect(20, 40, 180 + size * 20, 200);
                    GUI.Box(popupRect, "", contractSkins.dropDown);

                    Rect r = new Rect(popupRect.x + 2, popupRect.y + 2, popupRect.width - 14, 30);
                    GUI.Label(r, "Edit Mission Title", contractSkins.resetBox);

                    r.x += 10;
                    r.y += 30;
                    r.width = 150 + size * 20;
                    r.height = 25;

                    editField = GUI.TextField(r, editField, 20);

                    r.y += 30;

                    if (GUI.Button(r, "Change Name", contractSkins.resetButton))
                    {
                        if (!string.IsNullOrEmpty(editField))
                        {
                            string oldName = currentMission.Name;
                            contractScenario.Instance.removeMissionList(oldName, false);

                            currentMission.Name = editField;

                            if (contractScenario.Instance.addMissionList(currentMission))
                            {
                                missionList = contractScenario.Instance.getAllMissions();
                                currentMission = contractScenario.Instance.setCurrentMission(editField);

                                if (currentMission.ShowActiveMissions)
                                    cList = currentMission.ActiveMissionList;
                                else
                                    cList = currentMission.HiddenMissionList;

                                pinnedList = currentMission.loadPinnedContracts(cList);

                                refreshContracts(cList);

                                popup = false;
                                missionEdit = false;
                            }
                            else
                                currentMission.Name = oldName;
                        }
                        else
                            ScreenMessages.PostScreenMessage("Contract mission list must have a name", 5f, ScreenMessageStyle.UPPER_CENTER);
                    }

                    r.x -= 10;
                    r.y += 30;
                    r.width = popupRect.width - 14;
                    r.height = 60;

                    GUI.Label(r, "Delete Current Mission:\n<b>Will Not</b> Affect Contract Status", contractSkins.resetBox);

                    r.x += 10;
                    r.y += 60;
                    r.width -= 25;
                    r.height = 25;

                    if (GUI.Button(r, "Delete Mission", contractSkins.resetButton))
                    {
                        contractScenario.Instance.removeMissionList(currentMission.Name);
                        missionList = contractScenario.Instance.getAllMissions();
                        currentMission = contractScenario.Instance.MasterMission;

                        if (currentMission.ShowActiveMissions)
                            cList = currentMission.ActiveMissionList;
                        else
                            cList = currentMission.HiddenMissionList;

                        pinnedList = currentMission.loadPinnedContracts(cList);

                        refreshContracts(cList);

                        popup = false;
                        missionEdit = false;
                    }
                }

                else
                    popup = false;
            }
        }
        //Reset contract list from the "refresh" button
        private void rebuildList()
        {
            contractScenario.Instance.loadAllContracts();
            contractScenario.Instance.addFullMissionList();

            currentMission = contractScenario.Instance.MasterMission;

            cList.Clear();
            pinnedList.Clear();

            foreach (Contract c in ContractSystem.Instance.Contracts)
            {
                contractContainer cC = contractScenario.Instance.getContract(c.ContractGuid);
                if (cC != null)
                    currentMission.addContract(cC, true, false);
            }

            cList = currentMission.ActiveMissionList;
            refreshContracts(cList);
        }
 //Load window position and size settings
 private void PersistenceLoad()
 {
     if (contractScenario.Instance != null)
     {
         stockToolbar = contractScenario.Instance.stockToolbar;
         replaceStock = contractScenario.Instance.replaceStockToolbar;
         cList.Clear();
         missionList = contractScenario.Instance.getAllMissions();
         currentMission = missionList[0];
         if (currentMission == null)
             currentMission = new contractMission("");
         WindowRect = contractScenario.Instance.windowRects[sceneInt];
         if (contractScenario.Instance.fontSmall)
             contractSkins.normalFontSize = 0;
         else
             contractSkins.normalFontSize = 1;
         if (contractScenario.Instance.windowSize == 0)
             contractSkins.windowFontSize = 0;
         else
             contractSkins.windowFontSize = 2;
         contractSkins.initializeSkins();
         WindowStyle = contractSkins.newWindowStyle;
         DragRect = new Rect(0, 0, WindowRect.width - 19, WindowRect.height - 24 - contractScenario.Instance.windowSize * 8);
         Visible = contractScenario.Instance.windowVisible[sceneInt];
         TooltipsEnabled = contractScenario.Instance.toolTips;
         if (Visible)
             StartRepeatingWorker(5);
         if (WindowRect.width < 100)
             resetWindow();
     }
 }
 //Initial contract load
 private void generateList()
 {
     contractScenario.Instance.loadAllContracts();
     contractScenario.Instance.loadAllMissionLists();
     if (HighLogic.LoadedSceneIsFlight)
         currentMission = contractScenario.Instance.setLoadedMission(FlightGlobals.ActiveVessel);
     else
         currentMission = contractScenario.Instance.MasterMission;
 }