eoanb_USA_hold_presidential_election = {
	eoanb_USA_clean_previous_election = yes
	every_owned_state = {
		limit = {
			has_state_flag = is_USA_state
		}
		eoanb_USA_setup_state_political_alignment = yes
		eoanb_USA_presidential_election = yes
	}
	ROOT = { eoanb_USA_presidential_election_declare_winner = yes }
	ROOT = { eoanb_USA_party_popularity_update = yes }
}

eoanb_USA_update_political_pie_chart = {
	eoanb_USA_clean_previous_election = yes
	every_owned_state = {
		limit = {
			has_state_flag = is_USA_state
		}
		eoanb_USA_setup_state_political_alignment = yes
		eoanb_USA_presidential_election = yes
	}
	ROOT = { eoanb_USA_party_popularity_update = yes }
}

eoanb_USA_presidential_election_setup = {
	set_temp_variable = { temp_LIB_popularity = LIB_popularity } #Liberal
	set_temp_variable = { temp_CON_popularity = CON_popularity } #Conservative
	set_temp_variable = { temp_EGL_popularity = EGL_popularity } #Egalitarian
	set_temp_variable = { temp_PER_popularity = PER_popularity } #Peripheral
	set_temp_variable = { temp_SOC_popularity = SOC_popularity } #Socialist
	set_temp_variable = { temp_POP_popularity = POP_popularity } #C Populist
	set_temp_variable = { temp_FUN_popularity = FUN_popularity } #Fundamental
	set_temp_variable = { temp_COM_popularity = COM_popularity } #Communist
	set_temp_variable = { temp_AUT_popularity = AUT_popularity } #Autocratic
	set_temp_variable = { temp_ANA_popularity = ANA_popularity } #Anarchist
}

eoanb_USA_party_popularity_update = {
	set_variable = { USA_LIB_pie_popularity = USA_LIB_total_votes }
	set_variable = { USA_CON_pie_popularity = USA_CON_total_votes }
	set_variable = { USA_EGL_pie_popularity = USA_EGL_total_votes }
	set_variable = { USA_PER_pie_popularity = USA_PER_total_votes }
	set_variable = { USA_SOC_pie_popularity = USA_SOC_total_votes }
	set_variable = { USA_POP_pie_popularity = USA_POP_total_votes }
	set_variable = { USA_FUN_pie_popularity = USA_FUN_total_votes }
	set_variable = { USA_SOL_pie_popularity = USA_COM_total_votes }
	set_variable = { USA_AUT_pie_popularity = USA_AUT_total_votes }
	set_variable = { USA_ANA_pie_popularity = USA_ANA_total_votes }

	set_variable = { USA_total_votes_pie = USA_LIB_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_CON_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_EGL_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_PER_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_SOC_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_POP_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_FUN_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_SOL_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_AUT_pie_popularity }
	add_to_variable = { USA_total_votes_pie = USA_ANA_pie_popularity }

	log = "LIBERALS: [?USA_LIB_pie_popularity] CONSERVATIVES: [?USA_CON_pie_popularity]"
	log = "EGALITARIAN: [?USA_EGL_pie_popularity] PERIPHERAL: [?USA_PER_pie_popularity]"
	log = "TOTAL VOTES: [?USA_total_votes_pie]"

	divide_variable = { USA_LIB_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_CON_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_EGL_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_PER_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_SOC_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_POP_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_FUN_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_SOL_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_AUT_pie_popularity = USA_total_votes_pie }
	divide_variable = { USA_ANA_pie_popularity = USA_total_votes_pie }

	multiply_variable = { USA_LIB_pie_popularity = 100 }
	multiply_variable = { USA_CON_pie_popularity = 100 }
	multiply_variable = { USA_EGL_pie_popularity = 100 }
	multiply_variable = { USA_PER_pie_popularity = 100 }
	multiply_variable = { USA_SOC_pie_popularity = 100 }
	multiply_variable = { USA_POP_pie_popularity = 100 }
	multiply_variable = { USA_FUN_pie_popularity = 100 }
	multiply_variable = { USA_SOL_pie_popularity = 100 }
	multiply_variable = { USA_AUT_pie_popularity = 100 }
	multiply_variable = { USA_ANA_pie_popularity = 100 }

	#Sometimes due to rounding it makes it off slightly by 1% or -1%

	set_temp_variable = { USA_total_votes_pie_CHECK = USA_LIB_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_CON_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_EGL_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_PER_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_SOC_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_POP_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_FUN_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_SOL_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_AUT_pie_popularity }
	add_to_temp_variable = { USA_total_votes_pie_CHECK = USA_ANA_pie_popularity }

	if = {
		limit = {
			check_variable = { USA_total_votes_pie_CHECK < 100 }
		}
		set_temp_variable = { USA_total_votes_pie_CHECK_add = 100 }
		subtract_from_temp_variable = { USA_total_votes_pie_CHECK_add = USA_total_votes_pie_CHECK }
		if = {
			limit = {
				check_variable = { USA_LIB_pie_popularity > 9 }
			}
			add_to_variable = { USA_LIB_pie_popularity = USA_total_votes_pie_CHECK_add }
		}
		else_if = {
			limit = {
				check_variable = { USA_CON_pie_popularity > 9 }
			}
			add_to_variable = { USA_CON_pie_popularity = USA_total_votes_pie_CHECK_add }
		}
		else_if = {
			limit = {
				check_variable = { USA_EGL_pie_popularity > 9 }
			}
			add_to_variable = { USA_EGL_pie_popularity = USA_total_votes_pie_CHECK_add }
		}
		else_if = {
			limit = {
				check_variable = { USA_PER_pie_popularity > 9 }
			}
			add_to_variable = { USA_PER_pie_popularity = USA_total_votes_pie_CHECK_add }
		}
	}
	if = {
		limit = {
			check_variable = { USA_total_votes_pie_CHECK > 100 }
		}
		subtract_from_temp_variable = { USA_total_votes_pie_CHECK = 100 }
		if = {
			limit = {
				check_variable = { USA_LIB_pie_popularity > 9 }
			}
			subtract_from_variable = { USA_LIB_pie_popularity = USA_total_votes_pie_CHECK }
		}
		else_if = {
			limit = {
				check_variable = { USA_CON_pie_popularity > 9 }
			}
			subtract_from_variable = { USA_CON_pie_popularity = USA_total_votes_pie_CHECK }
		}
		else_if = {
			limit = {
				check_variable = { USA_EGL_pie_popularity > 9 }
			}
			subtract_from_variable = { USA_EGL_pie_popularity = USA_total_votes_pie_CHECK }
		}
		else_if = {
			limit = {
				check_variable = { USA_PER_pie_popularity > 9 }
			}
			subtract_from_variable = { USA_PER_pie_popularity = USA_total_votes_pie_CHECK }
		}
	}

	USA = {
		set_popularities = {
			centrism = USA_LIB_pie_popularity
			traditional_conservatism = USA_CON_pie_popularity
			social_egalitarianism = USA_EGL_pie_popularity
			radical_democracy = USA_PER_pie_popularity
			ideology_radical_socialism = USA_SOC_pie_popularity
			chauvinist_populism = USA_POP_pie_popularity
			fundamentalism = USA_FUN_pie_popularity
			ideology_social_liberalism = USA_SOL_pie_popularity
			anarchism = USA_ANA_pie_popularity
			autocracy = USA_AUT_pie_popularity
		}
	}
}

eoanb_USA_presidential_election = {
	clr_state_flag = LIB_election_victory
	clr_state_flag = CON_election_victory
	clr_state_flag = EGL_election_victory
	clr_state_flag = PER_election_victory
	eoanb_USA_hold_presidential_election_substates = yes
	if = {
		limit = {
			is_in_array = {
				state_culture_array = 417 #Are there African Americans in this state?
			}
		}
		eoanb_USA_afroamerican_vote_count = yes
	}
	eoanb_USA_allocate_electoral_votes = yes
	if = {
		limit = {
			AND = {
				NOT = { has_state_flag = is_sub_CAL_USA_state }
				NOT = { has_state_flag = is_sub_VIR_USA_state }
				NOT = { has_state_flag = is_sub_MIC_USA_state }
			}
		}
		eoanb_USA_hold_presidential_election_vote = yes
	}
}

eoanb_USA_hold_presidential_election_substates = {
	if = {
		limit = {
			state = 378
		}
		USA = {
			every_owned_state = {
				limit = { has_state_flag = is_sub_CAL_USA_state }
				378 = {
					add_to_temp_variable = { substate_population = PREV.state_population_k }
				}
			}
		}
	}
	if = {
		limit = {
			state = 362
		}
		USA = {
			every_owned_state = {
				limit = { has_state_flag = is_sub_VIR_USA_state }
				362 = {
					add_to_temp_variable = { substate_population = PREV.state_population_k }
				}
			}
		}
	}
	if = {
		limit = {
			state = 393
		}
		USA = {
			every_owned_state = {
				limit = { has_state_flag = is_sub_MIC_USA_state }
				393 = {
					add_to_temp_variable = { substate_population = PREV.state_population_k }
				}
			}
		}
	}
}

eoanb_USA_afroamerican_vote_count = {
	#This part will accomodate the African Americans being unable to vote in the US. Sorry, but slaves in the South will not vote.
	if = {
		limit = {
			USA = { has_idea = full_equality }
		}
		set_temp_variable = { afroamerican_votes_counted_percentage = 1 }
	}
	else_if = {
		limit = {
			USA = { has_idea = limited_equality }
		}
		set_temp_variable = { afroamerican_votes_counted_percentage = 0.85 }
	}
	else_if = {
		limit = {
			USA = { has_idea = segregation }
		}
		set_temp_variable = { afroamerican_votes_counted_percentage = 0.5 }
	}
	else_if = {
		limit = {
			USA = { has_idea = racial_laws }
		}
		set_temp_variable = { afroamerican_votes_counted_percentage = 0.25 }
	}
	else_if = {
		limit = {
			has_state_flag = is_USA_slave_state
		}
		set_temp_variable = { afroamerican_votes_counted_percentage = 0.05 }
	}
	else = {
		set_temp_variable = { afroamerican_votes_counted_percentage = 0.1 }
	}
	for_each_loop = {
		array = state_culture_array
		index = ii
		value = vv
		break = calculate_vote_break

		if = {
			limit = {
				is_in_array = {
					state_culture_array = 417 #Are there African Americans in this state?
				}
				check_variable = { vv = 417 }
			}
			set_temp_variable = { afroamerican_population = state_population_k }
			multiply_temp_variable = { afroamerican_population = state_culture_array_num^ii }
			round_temp_variable = afroamerican_population
			set_temp_variable = { calculate_vote_break = 1 }
		}
	}

	set_temp_variable = { afroamerican_vote = afroamerican_population }
	log = "[This.GetName] : There are [?afroamerican_population]k African-Americans here"
}

eoanb_USA_hold_presidential_election_vote = {
	set_temp_variable = { electorate_population = state_population_k }
	add_to_temp_variable = { electorate_population = substate_population }
	subtract_from_temp_variable = { electorate_population = afroamerican_population }
	multiply_temp_variable = { electorate_population = 0.8 } #Turnout!
	multiply_temp_variable = { afroamerican_vote = 0.8 } #Turnout!

	if = {
		limit = {
			USA = { has_idea = property_owners_suffrage }
		}
		divide_temp_variable = { electorate_population = 100 }
		divide_temp_variable = { afroamerican_vote = 100 }
	}
	else_if = {
		limit = {
			USA = { has_idea = universal_male_suffrage }
		}
		divide_temp_variable = { electorate_population = 4 }
		divide_temp_variable = { afroamerican_vote = 4 }
	}
	else_if = {
		limit = {
			USA = { has_idea = universal_suffrage }
		}
		divide_temp_variable = { electorate_population = 2 }
		divide_temp_variable = { afroamerican_vote = 2 }
	}

	multiply_temp_variable = { afroamerican_vote = afroamerican_votes_counted_percentage }
	add_to_temp_variable = { electorate_population = afroamerican_vote }

	### So let's say a state has 100 Liberal popularity, 50 Traditional Conservative popularity, and 50 Peripheral popularity.

	### Randomizer. Ideologies that don't have any votes are ignored. Assume Liberals gain an extra 20 votes and Peripheral lose 10 votes in this example

	if = {
		limit = {
			check_variable = {
				temp_LIB_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_LIB
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_LIB_popularity = temp_random_LIB }
		clamp_temp_variable = {
			var = temp_LIB_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_CON_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_CON
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_CON_popularity = temp_random_CON }
		clamp_temp_variable = {
			var = temp_CON_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_EGL_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_EGL
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_EGL_popularity = temp_random_EGL }
		clamp_temp_variable = {
			var = temp_EGL_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_PER_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_PER
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_PER_popularity = temp_random_PER }
		clamp_temp_variable = {
			var = temp_PER_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_SOC_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_SOC
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_SOC_popularity = temp_random_SOC }
		clamp_temp_variable = {
			var = temp_SOC_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_POP_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_POP
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_POP_popularity = temp_random_POP }
		clamp_temp_variable = {
			var = temp_POP_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_FUN_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_FUN
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_FUN_popularity = temp_random_FUN }
		clamp_temp_variable = {
			var = temp_FUN_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_COM_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_COM
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_COM_popularity = temp_random_COM }
		clamp_temp_variable = {
			var = temp_COM_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_AUT_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_AUT
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_AUT_popularity = temp_random_AUT }
		clamp_temp_variable = {
			var = temp_AUT_popularity
			min = 0
			max = 1020
		}
	}
	if = {
		limit = {
			check_variable = {
				temp_ANA_popularity > 0
			}
		}
		randomize_temp_variable = {
			var = temp_random_ANA
			min = -20
			max = 20
		}
		add_to_temp_variable = { temp_ANA_popularity = temp_random_ANA }
		clamp_temp_variable = {
			var = temp_ANA_popularity
			min = 0
			max = 1020
		}
	}

	### It adds these two popularities to total_voting_percentage_temp, so total_voting_percentage_temp = 200, with 20 added and 10 removed from the randomizer so it's actually 210

	set_temp_variable = { total_voting_percentage_temp = temp_LIB_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_CON_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_EGL_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_PER_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_SOC_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_POP_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_FUN_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_COM_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_AUT_popularity }
	add_to_temp_variable = { total_voting_percentage_temp = temp_ANA_popularity }

	### Then divides these popularities among the total voting percentage. So Liberals have 100 normal popularity in a normal total of 200 percentage, giving the Liberals 50% of the vote in that state, while the Conservatives get 25% of the vote and Peripheral got 25%. However the extra 10 random votes gives the liberals 57.14% (120) and the conservatives 23.81% (50) and peripheral 19.05% (40) out of 210 'votes'.

	divide_temp_variable = { temp_LIB_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_CON_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_EGL_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_PER_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_SOC_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_POP_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_FUN_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_COM_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_AUT_popularity = total_voting_percentage_temp }
	divide_temp_variable = { temp_ANA_popularity = total_voting_percentage_temp }

	### Assuming state has 400,000 people. Male Suffrage with 100% turnout leads to 100,000 voters, 57,140 vote Liberal, 23,810 vote Conservative, 19,050 vote Peripheral. A total of 100,000.

	### Before we restore the pops to their percentage, we need to add up the substates into their central state so that substates don't count as their own state.

	if = {
		limit = {
			has_state_flag = is_sub_CAL_USA_state
		}
		add_to_temp_variable = { temp_LIB_popularity@378 = THIS.temp_LIB_popularity }
		add_to_temp_variable = { temp_CON_popularity@378 = THIS.temp_CON_popularity }
		add_to_temp_variable = { temp_EGL_popularity@378 = THIS.temp_EGL_popularity }
		add_to_temp_variable = { temp_PER_popularity@378 = THIS.temp_PER_popularity }
	}
	else_if = {
		limit = {
			has_state_flag = is_sub_VIR_USA_state
		}
		add_to_temp_variable = { temp_LIB_popularity@362 = THIS.temp_LIB_popularity }
		add_to_temp_variable = { temp_CON_popularity@362 = THIS.temp_CON_popularity }
		add_to_temp_variable = { temp_EGL_popularity@362 = THIS.temp_EGL_popularity }
		add_to_temp_variable = { temp_PER_popularity@362 = THIS.temp_PER_popularity }
	}
	else_if = {
		limit = {
			has_state_flag = is_sub_MIC_USA_state
		}
		add_to_temp_variable = { temp_LIB_popularity@393 = THIS.temp_LIB_popularity }
		add_to_temp_variable = { temp_CON_popularity@393 = THIS.temp_CON_popularity }
		add_to_temp_variable = { temp_EGL_popularity@393 = THIS.temp_EGL_popularity }
		add_to_temp_variable = { temp_PER_popularity@393 = THIS.temp_PER_popularity }
	}
	else = {
		divide_temp_variable = { temp_LIB_popularity = 100 }
		divide_temp_variable = { temp_CON_popularity = 100 }
		divide_temp_variable = { temp_EGL_popularity = 100 }
		divide_temp_variable = { temp_PER_popularity = 100 }
		divide_temp_variable = { temp_SOC_popularity = 100 }
		divide_temp_variable = { temp_POP_popularity = 100 }
		divide_temp_variable = { temp_FUN_popularity = 100 }
		divide_temp_variable = { temp_COM_popularity = 100 }
		divide_temp_variable = { temp_ANA_popularity = 100 }

		set_variable = { LIB_popularity_percent = temp_LIB_popularity }
		set_variable = { CON_popularity_percent = temp_CON_popularity }
		set_variable = { EGL_popularity_percent = temp_EGL_popularity }
		set_variable = { PER_popularity_percent = temp_PER_popularity }
		set_variable = { SOC_popularity_percent = temp_SOC_popularity }
		set_variable = { POP_popularity_percent = temp_POP_popularity }
		set_variable = { FUN_popularity_percent = temp_FUN_popularity }
		set_variable = { COM_popularity_percent = temp_COM_popularity }
		set_variable = { ANA_popularity_percent = temp_ANA_popularity }


		#Finally we can count up all the votes and boom! The Election is over!

		multiply_temp_variable = { temp_LIB_popularity = electorate_population }
		multiply_temp_variable = { temp_CON_popularity = electorate_population }
		multiply_temp_variable = { temp_EGL_popularity = electorate_population }
		multiply_temp_variable = { temp_PER_popularity = electorate_population }
		multiply_temp_variable = { temp_SOC_popularity = electorate_population }
		multiply_temp_variable = { temp_POP_popularity = electorate_population }
		multiply_temp_variable = { temp_FUN_popularity = electorate_population }
		multiply_temp_variable = { temp_COM_popularity = electorate_population }
		multiply_temp_variable = { temp_AUT_popularity = electorate_population }
		multiply_temp_variable = { temp_ANA_popularity = electorate_population }

		multiply_temp_variable = { temp_LIB_popularity = 100 }
		multiply_temp_variable = { temp_CON_popularity = 100 }
		multiply_temp_variable = { temp_EGL_popularity = 100 }
		multiply_temp_variable = { temp_PER_popularity = 100 }
		multiply_temp_variable = { temp_SOC_popularity = 100 }
		multiply_temp_variable = { temp_POP_popularity = 100 }
		multiply_temp_variable = { temp_FUN_popularity = 100 }
		multiply_temp_variable = { temp_COM_popularity = 100 }
		multiply_temp_variable = { temp_AUT_popularity = 100 }
		multiply_temp_variable = { temp_ANA_popularity = 100 }

		set_variable = { LIB_total_votes = temp_LIB_popularity }
		set_variable = { CON_total_votes = temp_CON_popularity }
		set_variable = { EGL_total_votes = temp_EGL_popularity }
		set_variable = { PER_total_votes = temp_PER_popularity }
		set_variable = { SOC_total_votes = temp_SOC_popularity }
		set_variable = { POP_total_votes = temp_POP_popularity }
		set_variable = { FUN_total_votes = temp_FUN_popularity }
		set_variable = { COM_total_votes = temp_COM_popularity }
		set_variable = { AUT_total_votes = temp_AUT_popularity }
		set_variable = { ANA_total_votes = temp_ANA_popularity }

		### Get an accurate selection of the total votes

		set_variable = { total_votes = 0 }

		add_to_variable = { total_votes = LIB_total_votes }
		add_to_variable = { total_votes = CON_total_votes }
		add_to_variable = { total_votes = EGL_total_votes }
		add_to_variable = { total_votes = PER_total_votes }
		add_to_variable = { total_votes = SOC_total_votes }
		add_to_variable = { total_votes = POP_total_votes }
		add_to_variable = { total_votes = FUN_total_votes }
		add_to_variable = { total_votes = COM_total_votes }
		add_to_variable = { total_votes = AUT_total_votes }
		add_to_variable = { total_votes = ANA_total_votes }

		add_to_variable = { ROOT.USA_LIB_total_votes = THIS.LIB_total_votes }
		add_to_variable = { ROOT.USA_CON_total_votes = THIS.CON_total_votes }
		add_to_variable = { ROOT.USA_EGL_total_votes = THIS.EGL_total_votes }
		add_to_variable = { ROOT.USA_PER_total_votes = THIS.PER_total_votes }
		add_to_variable = { ROOT.USA_SOC_total_votes = THIS.SOC_total_votes }
		add_to_variable = { ROOT.USA_POP_total_votes = THIS.POP_total_votes }
		add_to_variable = { ROOT.USA_FUN_total_votes = THIS.FUN_total_votes }
		add_to_variable = { ROOT.USA_COM_total_votes = THIS.COM_total_votes }
		add_to_variable = { ROOT.USA_AUT_total_votes = THIS.AUT_total_votes }
		add_to_variable = { ROOT.USA_ANA_total_votes = THIS.ANA_total_votes }

		add_to_variable = { ROOT.USA_total_votes = THIS.total_votes }

		if = {
			limit = {
				check_variable = { LIB_total_votes > CON_total_votes }
				check_variable = { LIB_total_votes > EGL_total_votes }
				check_variable = { LIB_total_votes > PER_total_votes }
				check_variable = { LIB_total_votes > SOC_total_votes }
				check_variable = { LIB_total_votes > POP_total_votes }
				check_variable = { LIB_total_votes > FUN_total_votes }
				check_variable = { LIB_total_votes > COM_total_votes }
				check_variable = { LIB_total_votes > AUT_total_votes }
				check_variable = { LIB_total_votes > ANA_total_votes }
			}
			set_state_flag = LIB_election_victory
			add_to_variable = { ROOT.LIB_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] liberal electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { CON_total_votes > LIB_total_votes }
				check_variable = { CON_total_votes > EGL_total_votes }
				check_variable = { CON_total_votes > PER_total_votes }
				check_variable = { CON_total_votes > SOC_total_votes }
				check_variable = { CON_total_votes > POP_total_votes }
				check_variable = { CON_total_votes > FUN_total_votes }
				check_variable = { CON_total_votes > COM_total_votes }
				check_variable = { CON_total_votes > AUT_total_votes }
				check_variable = { CON_total_votes > ANA_total_votes }
			}
			set_state_flag = CON_election_victory
			add_to_variable = { ROOT.CON_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] conservative electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { EGL_total_votes > LIB_total_votes }
				check_variable = { EGL_total_votes > CON_total_votes }
				check_variable = { EGL_total_votes > PER_total_votes }
				check_variable = { EGL_total_votes > SOC_total_votes }
				check_variable = { EGL_total_votes > POP_total_votes }
				check_variable = { EGL_total_votes > FUN_total_votes }
				check_variable = { EGL_total_votes > COM_total_votes }
				check_variable = { EGL_total_votes > AUT_total_votes }
				check_variable = { EGL_total_votes > ANA_total_votes }
			}
			set_state_flag = EGL_election_victory
			add_to_variable = { ROOT.EGL_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] egalitarian electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { PER_total_votes > LIB_total_votes }
				check_variable = { PER_total_votes > CON_total_votes }
				check_variable = { PER_total_votes > EGL_total_votes }
				check_variable = { PER_total_votes > SOC_total_votes }
				check_variable = { PER_total_votes > POP_total_votes }
				check_variable = { PER_total_votes > FUN_total_votes }
				check_variable = { PER_total_votes > COM_total_votes }
				check_variable = { PER_total_votes > AUT_total_votes }
				check_variable = { PER_total_votes > ANA_total_votes }
			}
			set_state_flag = PER_election_victory
			add_to_variable = { ROOT.PER_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] peripheral electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { SOC_total_votes > LIB_total_votes }
				check_variable = { SOC_total_votes > CON_total_votes }
				check_variable = { SOC_total_votes > EGL_total_votes }
				check_variable = { SOC_total_votes > PER_total_votes }
				check_variable = { SOC_total_votes > POP_total_votes }
				check_variable = { SOC_total_votes > FUN_total_votes }
				check_variable = { SOC_total_votes > COM_total_votes }
				check_variable = { SOC_total_votes > AUT_total_votes }
				check_variable = { SOC_total_votes > ANA_total_votes }
			}
			set_state_flag = SOC_election_victory
			add_to_variable = { ROOT.SOC_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] socialist electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { POP_total_votes > LIB_total_votes }
				check_variable = { POP_total_votes > CON_total_votes }
				check_variable = { POP_total_votes > EGL_total_votes }
				check_variable = { POP_total_votes > PER_total_votes }
				check_variable = { POP_total_votes > SOC_total_votes }
				check_variable = { POP_total_votes > FUN_total_votes }
				check_variable = { POP_total_votes > COM_total_votes }
				check_variable = { POP_total_votes > AUT_total_votes }
				check_variable = { POP_total_votes > ANA_total_votes }
			}
			set_state_flag = POP_election_victory
			add_to_variable = { ROOT.POP_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] populist electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { FUN_total_votes > LIB_total_votes }
				check_variable = { FUN_total_votes > CON_total_votes }
				check_variable = { FUN_total_votes > EGL_total_votes }
				check_variable = { FUN_total_votes > PER_total_votes }
				check_variable = { FUN_total_votes > SOC_total_votes }
				check_variable = { FUN_total_votes > POP_total_votes }
				check_variable = { FUN_total_votes > COM_total_votes }
				check_variable = { FUN_total_votes > AUT_total_votes }
				check_variable = { FUN_total_votes > ANA_total_votes }
			}
			set_state_flag = FUN_election_victory
			add_to_variable = { ROOT.FUN_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] fundamental electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { COM_total_votes > LIB_total_votes }
				check_variable = { COM_total_votes > CON_total_votes }
				check_variable = { COM_total_votes > EGL_total_votes }
				check_variable = { COM_total_votes > PER_total_votes }
				check_variable = { COM_total_votes > SOC_total_votes }
				check_variable = { COM_total_votes > POP_total_votes }
				check_variable = { COM_total_votes > FUN_total_votes }
				check_variable = { COM_total_votes > AUT_total_votes }
				check_variable = { COM_total_votes > ANA_total_votes }
			}
			set_state_flag = COM_election_victory
			add_to_variable = { ROOT.COM_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] communist electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { AUT_total_votes > LIB_total_votes }
				check_variable = { AUT_total_votes > CON_total_votes }
				check_variable = { AUT_total_votes > EGL_total_votes }
				check_variable = { AUT_total_votes > PER_total_votes }
				check_variable = { AUT_total_votes > SOC_total_votes }
				check_variable = { AUT_total_votes > POP_total_votes }
				check_variable = { AUT_total_votes > FUN_total_votes }
				check_variable = { AUT_total_votes > COM_total_votes }
				check_variable = { AUT_total_votes > ANA_total_votes }
			}
			set_state_flag = AUT_election_victory
			add_to_variable = { ROOT.AUT_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] autocratic electoral votes."
		}
		else_if = {
			limit = {
				check_variable = { ANA_total_votes > LIB_total_votes }
				check_variable = { ANA_total_votes > CON_total_votes }
				check_variable = { ANA_total_votes > EGL_total_votes }
				check_variable = { ANA_total_votes > PER_total_votes }
				check_variable = { ANA_total_votes > SOC_total_votes }
				check_variable = { ANA_total_votes > POP_total_votes }
				check_variable = { ANA_total_votes > FUN_total_votes }
				check_variable = { ANA_total_votes > COM_total_votes }
				check_variable = { ANA_total_votes > AUT_total_votes }
			}
			set_state_flag = ANA_election_victory
			add_to_variable = { ROOT.ANA_electoral_votes = electoral_votes }
			log = "[This.GetName] : Recieved [?electoral_votes] anarchist electoral votes."
		}
		log = "[This.GetName] : Voted with [?LIB_total_votes]k liberal votes, making up [?LIB_popularity_percent|%]."
		log = "[This.GetName] : Voted with [?CON_total_votes]k conservative votes, making up [?CON_popularity_percent|%]."
		log = "[This.GetName] : Voted with [?EGL_total_votes]k social egalitarian votes, making up [?EGL_popularity_percent|%]."
		log = "[This.GetName] : Voted with [?PER_total_votes]k periperhal votes, making up [?PER_popularity_percent|%]."
		log = "[This.GetName] : Total number of votes was [?total_votes]k."
	}
}
eoanb_USA_clean_previous_election = {
	clear_variable = LIB_electoral_votes
	clear_variable = CON_electoral_votes
	clear_variable = EGL_electoral_votes
	clear_variable = PER_electoral_votes
	clear_variable = USA_LIB_total_votes
	clear_variable = USA_CON_total_votes
	clear_variable = USA_EGL_total_votes
	clear_variable = USA_PER_total_votes
	clear_variable = USA_SOC_total_votes
	clear_variable = USA_POP_total_votes
	clear_variable = USA_FUN_total_votes
	clear_variable = USA_COM_total_votes
	clear_variable = USA_AUT_total_votes
	clear_variable = USA_ANA_total_votes
	clear_variable = USA_total_votes
}
eoanb_USA_presidential_election_declare_winner = {
	log = "[This.GetName] : Liberals got [?LIB_electoral_votes] electoral votes. Conservatives got [?CON_electoral_votes] electoral votes."
	log = "[This.GetName] : Egalitarians got [?EGL_electoral_votes] electoral votes. Peripheral got [?PER_electoral_votes] electoral votes."
}

eoanb_USA_allocate_electoral_votes = {
	set_variable = { electoral_votes = 0 }
	set_temp_variable = { population_check_temp = state_population_k }
	add_to_temp_variable = { population_check_temp = substate_population }
	divide_temp_variable = { population_check_temp = 150 }
	round_temp_variable = population_check_temp
	add_to_variable = { electoral_votes = population_check_temp }
	add_to_variable = { electoral_votes = 3 }
}

eoanb_USA_setup_state_political_alignment = {
	if = {
		limit = {
			has_state_flag = is_free_USA_state
			NOT = {
				has_state_flag = is_sub_CAL_USA_state
				has_state_flag = is_sub_VIR_USA_state
				has_state_flag = is_sub_MIC_USA_state
			}
		}
		set_temp_variable = { substate_population = 0 }

		set_temp_variable = { temp_LIB_popularity = 70 } #Liberal
		set_temp_variable = { temp_CON_popularity = 50 } #Conservative
		set_temp_variable = { temp_EGL_popularity = 10 } #Egalitarian
		set_temp_variable = { temp_PER_popularity = 0 } #Peripheral
		set_temp_variable = { temp_SOC_popularity = 0 } #Socialist
		set_temp_variable = { temp_POP_popularity = 30 } #C Populist
		set_temp_variable = { temp_FUN_popularity = 0 } #Fundamental
		set_temp_variable = { temp_COM_popularity = 0 } #Communist
		set_temp_variable = { temp_AUT_popularity = 0 } #Autocratic
		set_temp_variable = { temp_ANA_popularity = 0 } #Anarchist
	}
	else_if = {
		limit = {
			has_state_flag = is_slave_USA_state
			NOT = {
				has_state_flag = is_sub_CAL_USA_state
				has_state_flag = is_sub_VIR_USA_state
				has_state_flag = is_sub_MIC_USA_state
			}
		}
		set_temp_variable = { substate_population = 0 }

		set_temp_variable = { temp_LIB_popularity = 0 } #Liberal
		set_temp_variable = { temp_CON_popularity = 50 } #Conservative
		set_temp_variable = { temp_EGL_popularity = 0 } #Egalitarian
		set_temp_variable = { temp_PER_popularity = 70 } #Peripheral
		set_temp_variable = { temp_SOC_popularity = 0 } #Socialist
		set_temp_variable = { temp_POP_popularity = 30 } #C Populist
		set_temp_variable = { temp_FUN_popularity = 0 } #Fundamental
		set_temp_variable = { temp_COM_popularity = 0 } #Communist
		set_temp_variable = { temp_AUT_popularity = 0 } #Autocratic
		set_temp_variable = { temp_ANA_popularity = 0 } #Anarchist
	}
}