void cScreenCentreManagement::RefreshSelectedJobType()
{
	selection = GetSelectedItemFromList(girllist_id);
	if (selection < 0) return;
	selected_girl = g_Centre.GetGirl(g_CurrCentre, selection);
	u_int job = (Day0Night1 ? selected_girl->m_NightJob : selected_girl->m_DayJob);
	// set the job filter
	if (job >= g_Centre.m_JobManager.JobFilterIndex[JOBFILTER_COUNSELINGCENTRE] && job < g_Centre.m_JobManager.JobFilterIndex[JOBFILTER_COUNSELINGCENTRE + 1])
		SetSelectedItemInList(jobtypelist_id, JOBFILTER_COUNSELINGCENTRE);
	else SetSelectedItemInList(jobtypelist_id, JOBFILTER_COMMUNITYCENTRE);
	SetJob = true;
}
/*
* return next girl in the sorted list
*/
sGirl *cScreenGirlDetails::get_next_girl()
{
	sGirl *next_girl = 0;

	g_LogFile.write("Where is the girl??");

	if (g_Clinic.GetGirlsCurrentBrothel(selected_girl) != -1)
	{
		g_LogFile.write("She is in the Clinic");
		next_girl = g_Clinic.GetGirl(0, g_Clinic.GetGirlPos(0, selected_girl) + 1);
	}
	else if (g_Studios.GetGirlsCurrentBrothel(selected_girl) != -1)
	{
		g_LogFile.write("She is in the Studio");
		next_girl = g_Studios.GetGirl(0, g_Studios.GetGirlPos(0, selected_girl) + 1);
	}
	else if (g_Arena.GetGirlsCurrentBrothel(selected_girl) != -1)
	{
		g_LogFile.write("She is in the Arena");
		next_girl = g_Arena.GetGirl(0, g_Arena.GetGirlPos(0, selected_girl) + 1);
	}
	else if (g_Centre.GetGirlsCurrentBrothel(selected_girl) != -1)
	{
		g_LogFile.write("She is in the Centre");
		next_girl = g_Centre.GetGirl(0, g_Centre.GetGirlPos(0, selected_girl) + 1);
	}
	else if (g_House.GetGirlsCurrentBrothel(selected_girl) != -1)
	{
		g_LogFile.write("She is in the House");
		next_girl = g_House.GetGirl(0, g_House.GetGirlPos(0, selected_girl) + 1);
	}
	else if (g_Farm.GetGirlsCurrentBrothel(selected_girl) != -1)
	{
		g_LogFile.write("She is in the Farm");
		next_girl = g_Farm.GetGirl(0, g_Farm.GetGirlPos(0, selected_girl) + 1);
	}
	else
	{
		if (selected_girl->m_DayJob == JOB_INDUNGEON)
		{
			g_LogFile.write("She is in the Dungeon");
			next_girl = g_Brothels.GetDungeon()->GetGirl(g_Brothels.GetDungeon()->GetGirlPos(selected_girl) + 1)->m_Girl;
		}
		else
		{
			g_LogFile.write("She is in a Brothel");
			next_girl = g_Brothels.GetGirl(g_CurrBrothel, g_Brothels.GetGirlPos(g_CurrBrothel, selected_girl) + 1);
		}
	}

	return next_girl;
}
void cScreenCentreManagement::ViewSelectedGirl()
{
	if (selected_girl)
	{
		if (GirlDead(selected_girl)) return;

		//load up the cycle_girls vector with the ordered list of girl IDs
		FillSortedIDList(girllist_id, &cycle_girls, &cycle_pos);
		for (int i = cycle_girls.size(); i-- > 0;)
		{  // no viewing dead girls
			if (g_Centre.GetGirl(g_CurrCentre, cycle_girls[i])->health() <= 0)
				cycle_girls.erase(cycle_girls.begin() + i);
		}

		g_InitWin = true;
		g_WinManager.push("Girl Details");
		return;
	}
}
/*
* the selected girl is to be removed from the current list; returns next selected girl
*/
sGirl *cScreenGirlDetails::remove_selected_girl()
{
	sGirl *next_girl = 0;

	if (cycle_girls.size() == 0) {
		return 0;
	}

	int cur_id = cycle_girls[cycle_pos];

	cycle_girls.erase(cycle_girls.begin() + cycle_pos);  // remove her

	if (cycle_pos >= (int)cycle_girls.size())
	{  // if this girl was the last in the list, move list position -1 if possible
		if (cycle_pos > 0)
			cycle_pos--;
		else  // or, maybe we have no more girls in the list
			return next_girl;
	}

	if (selected_girl->m_DayJob == JOB_INDUNGEON) next_girl = g_Brothels.GetDungeon()->GetGirl(cycle_girls[cycle_pos])->m_Girl;
	else if (selected_girl->m_InHouse)	next_girl = g_House.GetGirl(0, cycle_girls[cycle_pos]);
	else if (selected_girl->m_InFarm)	next_girl = g_Farm.GetGirl(0, cycle_girls[cycle_pos]);
	else if (selected_girl->m_InClinic)	next_girl = g_Clinic.GetGirl(0, cycle_girls[cycle_pos]);
	else if (selected_girl->m_InCentre)	next_girl = g_Centre.GetGirl(0, cycle_girls[cycle_pos]);
	else if (selected_girl->m_InArena)	next_girl = g_Arena.GetGirl(0, cycle_girls[cycle_pos]);
	else if (selected_girl->m_InStudio)	next_girl = g_Studios.GetGirl(0, cycle_girls[cycle_pos]);
	else next_girl = g_Brothels.GetGirl(g_CurrBrothel, cycle_girls[cycle_pos]);

	for (int i = cycle_girls.size(); i--> 0;)
	{  // all girls with ID higher than removed girl need their ID reduced
		if (cycle_girls[i] > cur_id)
			cycle_girls[i]--;
	}

	return next_girl;
}
void cScreenCentreManagement::check_events()
{
	// no events means we can go home
	if (g_InterfaceEvents.GetNumEvents() == 0)
		return;

	// if it's the back button, pop the window off the stack and we're done
	if (g_InterfaceEvents.CheckButton(back_id)) {
		g_InitWin = true;
		g_WinManager.Pop();
		return;
	}
	if (g_InterfaceEvents.CheckButton(viewdetails_id))
	{
		ViewSelectedGirl();
	}


	if (g_InterfaceEvents.CheckButton(day_id))
	{
		DisableButton(day_id, true);
		DisableButton(night_id, false);
		Day0Night1 = SHIFT_DAY;
		RefreshSelectedJobType();
	}
	if (g_InterfaceEvents.CheckButton(night_id))
	{
		DisableButton(day_id, false);
		DisableButton(night_id, true);
		Day0Night1 = SHIFT_NIGHT;
		RefreshSelectedJobType();
	}
	if (g_InterfaceEvents.CheckListbox(jobtypelist_id))
	{
		selection = GetSelectedItemFromList(jobtypelist_id);

		if (selection == -1)
			EditTextItem("Nothing Selected", jobtypedesc_id);
		else
		{
			// populate Jobs listbox with jobs in the selected category
			RefreshJobList();
			string jdmessage = g_Centre.m_JobManager.JobFilterDesc[selection];

			if ((g_Centre.GetNumGirlsOnJob(g_CurrCentre, JOB_COUNSELOR, 0) < 1 &&
				g_Centre.GetNumberPatients(0) > 0) ||
				(g_Centre.GetNumGirlsOnJob(g_CurrCentre, JOB_COUNSELOR, 1) < 1 &&
				g_Centre.GetNumberPatients(1) > 0))
			{
				jdmessage += gettext("\n*** A Counselor is required to guide Rehab and Therapy patients. ");
			}
			EditTextItem(jdmessage, jobtypedesc_id);
		}
	}
	if (g_InterfaceEvents.CheckListbox(joblist_id))
	{
		bool fulltime = g_CTRLDown;

		selection = GetSelectedItemFromList(joblist_id);
		if (selection != -1)
		{
			// first handle the descriptions
			EditTextItem(g_Centre.m_JobManager.JobDesc[selection], jobdesc_id);

			// Now assign the job to all the selected girls
			int pos = 0;
			int GSelection = GetNextSelectedItemFromList(girllist_id, 0, pos);
			while (GSelection != -1)
			{
				u_int new_job = selection;
				selected_girl = g_Centre.GetGirl(g_CurrCentre, GSelection);
				if (selected_girl)
				{
					int old_job = (Day0Night1 ? selected_girl->m_NightJob : selected_girl->m_DayJob);

					// handle special job requirements and assign
					// if HandleSpecialJobs returns true, the job assignment was modified or cancelled

					if (g_Centre.m_JobManager.HandleSpecialJobs(g_CurrCentre, selected_girl, new_job, old_job, Day0Night1, fulltime))
					{
						new_job = (Day0Night1 ? selected_girl->m_NightJob : selected_girl->m_DayJob);
						SetSelectedItemInList(joblist_id, new_job, false);
					}

					// update the girl's listing to reflect the job change
					ss.str("");
					ss << g_Centre.m_JobManager.JobName[selected_girl->m_DayJob];
					SetSelectedItemColumnText(girllist_id, GSelection, ss.str(), m_ListBoxes[girllist_id]->DayJobColumn());
					ss.str("");
					ss << g_Centre.m_JobManager.JobName[selected_girl->m_NightJob];
					SetSelectedItemColumnText(girllist_id, GSelection, ss.str(), m_ListBoxes[girllist_id]->NightJobColumn());

					// refresh job worker counts for former job and current job
					SetSelectedItemText(joblist_id, old_job, g_Centre.m_JobManager.JobDescriptionCount(old_job, 0, Day0Night1, false, false, false, true));
					SetSelectedItemText(joblist_id, new_job, g_Centre.m_JobManager.JobDescriptionCount(new_job, 0, Day0Night1, false, false, false, true));
				}
				if (selected_girl->m_DayJob == JOB_REHAB)	// `J` added
				{
					ss.str("");
					ss << g_Centre.m_JobManager.JobName[selected_girl->m_DayJob] << " (" << 3 - selected_girl->m_WorkingDay << ")";
					SetSelectedItemColumnText(girllist_id, GSelection, ss.str(), m_ListBoxes[girllist_id]->DayJobColumn());
				}
				else if (selected_girl->m_YesterDayJob == JOB_REHAB && selected_girl->m_DayJob != JOB_REHAB && ((selected_girl->m_WorkingDay > 0) || selected_girl->m_PrevWorkingDay > 0))
				{
					ss.str("");
					ss << g_Centre.m_JobManager.JobName[selected_girl->m_DayJob] << " **";
					SetSelectedItemColumnText(girllist_id, GSelection, ss.str(), m_ListBoxes[girllist_id]->DayJobColumn());
				}

				if (selected_girl->m_NightJob == JOB_REHAB)	// `J` added
				{
					ss.str("");
					ss << g_Centre.m_JobManager.JobName[selected_girl->m_NightJob] << " (" << 3 - selected_girl->m_WorkingDay << ")";
					SetSelectedItemColumnText(girllist_id, GSelection, ss.str(), m_ListBoxes[girllist_id]->NightJobColumn());
				}
				else if (selected_girl->m_YesterNightJob == JOB_REHAB && selected_girl->m_NightJob != JOB_REHAB && ((selected_girl->m_WorkingDay > 0) || selected_girl->m_PrevWorkingDay > 0))
				{
					ss.str("");
					ss << g_Centre.m_JobManager.JobName[selected_girl->m_NightJob] << " **";
					SetSelectedItemColumnText(girllist_id, GSelection, ss.str(), m_ListBoxes[girllist_id]->NightJobColumn());
				}
				if (selected_girl && selected_girl->m_YesterDayJob == JOB_REHAB && new_job != JOB_REHAB && (selected_girl->m_WorkingDay > 0 || selected_girl->m_PrevWorkingDay > 0))
				{	// `J` added
					EditTextItem(g_Centre.m_JobManager.JobDesc[new_job]
						+ "\n** This girl was in Rehab, if you send her somewhere else, she will have to start her Rehab over."
						, jobdesc_id);
				}
				GSelection = GetNextSelectedItemFromList(girllist_id, pos + 1, pos);
			}
		}
		else	EditTextItem("Nothing Selected", jobdesc_id);
	}

	if (g_InterfaceEvents.CheckListbox(girllist_id))
	{
		selection = GetSelectedItemFromList(girllist_id);
		if (selection != -1)
		{
			selected_girl = g_Centre.GetGirl(g_CurrCentre, selection);
			if (ListDoubleClicked(girllist_id)) ViewSelectedGirl();		// If double-clicked, try to bring up girl details
			DisableButton(freeslave_id, selected_girl->is_free());
			DisableButton(viewdetails_id, false);
			RefreshSelectedJobType();
		}
		else
		{
			DisableButton(freeslave_id, true);
			DisableButton(viewdetails_id, true);
			selected_girl = 0;
			selection = -1;
		}
		lastNum = -2;
		update_image();
		return;
	}
	if (g_InterfaceEvents.CheckButton(freeslave_id))
	{
		if (selected_girl)
		{
			if (IsMultiSelected(girllist_id))
			{  // multiple girls selected
				g_MessageQue.AddToQue("Are you sure you wish to give these girls their freedom?", 0);
				g_ChoiceManager.CreateChoiceBox(224, 112, 352, 384, 0, 2, 32, strlen("Keep as a slaves"));
				g_ChoiceManager.AddChoice(0, "Grant Freedom", 0);
				g_ChoiceManager.AddChoice(0, "Keep as a slaves", 1);
				g_ChoiceManager.SetActive(0);
				FreeGirl = true;
			}
			else  // only one girl selected
			{
				if (GirlDead(selected_girl)) return;
				g_MessageQue.AddToQue("Are you sure you wish to give " + selected_girl->m_Realname + " her freedom?", 0);
				g_ChoiceManager.CreateChoiceBox(224, 112, 352, 384, 0, 2, 32, strlen("Keep as a slave"));
				g_ChoiceManager.AddChoice(0, "Grant Freedom", 0);
				g_ChoiceManager.AddChoice(0, "Keep as a slave", 1);
				g_ChoiceManager.SetActive(0);
				FreeGirl = true;
			}
		}
		return;
	}
	if (g_InterfaceEvents.CheckButton(transfer_id))
	{
		g_InitWin = true;
		g_WinManager.Push(TransferGirls, &g_TransferGirls);
		return;
	}
}
void cScreenCentreManagement::init()
{

	if (FreeGirl)
	{
		if (g_ChoiceManager.GetChoice(0) == 0)
		{
			vector<int> girl_array;
			GetSelectedGirls(&girl_array);  // get and sort array of girls

			for (int i = girl_array.size(); i--> 0;)	// OK, we have the array, now step through it backwards
			{
				selected_girl = g_Centre.GetGirl(g_CurrCentre, girl_array[i]);
				if (GirlDead(selected_girl) || !selected_girl->is_slave()) continue;  // if dead or not a slave, can't free her
				if (selected_girl)
				{
					selected_girl->m_States &= ~(1 << STATUS_SLAVE);
					The_Player->disposition(5);
					g_Girls.UpdateStat(selected_girl, STAT_PCLOVE, 10);
					g_Girls.UpdateStat(selected_girl, STAT_PCFEAR, -20);
					g_Girls.UpdateStat(selected_girl, STAT_PCHATE, -25);
					g_Girls.UpdateStat(selected_girl, STAT_OBEDIENCE, 10);
					g_Girls.UpdateStat(selected_girl, STAT_HAPPINESS, 70);
					
					selected_girl->m_AccLevel = cfg.initial.girls_accom();
					selected_girl->m_Stats[STAT_HOUSE] = cfg.initial.girls_house_perc();
					g_InitWin = true;
				}
			}
		}
		g_ChoiceManager.Free();
		FreeGirl = false;
	}

	g_CurrentScreen = SCREEN_CENTRE;
	if (!g_InitWin) return;

	Focused();
	g_InitWin = false;


	////////////////////

	selection = GetSelectedItemFromList(girllist_id);
	string centre;
	centre += g_Centre.GetName(g_CurrCentre);
	EditTextItem(centre, curcentre_id);

	//selected_girl = 0;

	// clear the lists
	ClearListBox(girllist_id);
	ClearListBox(jobtypelist_id);

	// add the job filters
	//	for(int i=0; i<NUMJOBTYPES; i++)  // loop through all job types
	AddToListBox(jobtypelist_id, JOBFILTER_COMMUNITYCENTRE, g_Centre.m_JobManager.JobFilterName[JOBFILTER_COMMUNITYCENTRE]);
	AddToListBox(jobtypelist_id, JOBFILTER_COUNSELINGCENTRE, g_Centre.m_JobManager.JobFilterName[JOBFILTER_COUNSELINGCENTRE]);
	RefreshJobList();
	SetSelectedItemInList(jobtypelist_id, JOBFILTER_COMMUNITYCENTRE);

	//get a list of all the column names, so we can find which data goes in that column
	vector<string> columnNames;
	m_ListBoxes[girllist_id]->GetColumnNames(columnNames);
	int numColumns = columnNames.size();
	string* Data = new string[numColumns];

	// Add girls to list
	for (int i = 0; i<g_Centre.GetNumGirls(g_CurrCentre); i++)
	{
		sGirl* gir = g_Centre.GetGirl(g_CurrCentre, i);
		if (selected_girl == gir)
			selection = i;

		unsigned int item_color = COLOR_BLUE;
		if (g_Girls.GetStat(gir, STAT_HEALTH) <= 30 || g_Girls.GetStat(gir, STAT_TIREDNESS) >= 80 || g_Girls.GetStat(gir, STAT_HAPPINESS) <= 30)
			item_color = COLOR_RED;

		gir->OutputGirlRow(Data, columnNames);
		AddToListBox(girllist_id, i, Data, numColumns, item_color);
	}
	delete[] Data;

	lastNum = -1;
	g_InitWin = false;

	if (selection >= 0)
	{
		while (selection > GetListBoxSize(girllist_id) && selection != -1)
			selection--;
	}
	if (selection >= 0)
		SetSelectedItemInList(girllist_id, selection);
	else
		SetSelectedItemInList(girllist_id, 0);

	DisableButton(day_id, (Day0Night1 == SHIFT_DAY));
	DisableButton(night_id, (Day0Night1 == SHIFT_NIGHT));

	update_image();
}