########### ЛИНГСАН
create_or_join_lingsan_liga = {	
	if = {
		limit = {
			lingsan_liga_exists = no
		}
		ar_leave_current_faction = yes
		create_faction = lingsan_liga
		mark_as_lingsan_liga_member = yes
		set_variable = { GLOBAL.lingsan_liga_leader = THIS }
	}
	else_if = {
		join_lingsan_liga = yes
	}	
}

ask_to_join_lingsan_liga = {
	custom_effect_tooltip = ask_to_join_lingsan_liga_tt
	hidden_effect = {		
		var:GLOBAL.lingsan_liga_leader = {
			country_event = {
				id = ar_factionevent.3
			}
		}		
	}
}

invite_to_lingsan_liga = {
	custom_effect_tooltip = invite_to_lingsan_liga_tt
	hidden_effect = {		
		country_event = {
			id = ar_factionevent.5
		}		
	}
}

join_lingsan_liga = {
	custom_effect_tooltip = join_lingsan_liga_tt
	hidden_effect = {		
		if = {
			limit = { is_lingsan_liga_member = no }
			ar_leave_current_faction = yes
			var:GLOBAL.lingsan_liga_leader = {				
				add_to_faction = PREV
				PREV = { news_event = { id = ar_factionevent.2 } }	###				
			}
			mark_as_lingsan_liga_member = yes
		}		
	}
}

leave_lingsan_liga = {
	custom_effect_tooltip = leave_lingsan_liga_tt
	hidden_effect = {		
		if = {
			limit = { is_lingsan_liga_member = yes }
			unmark_as_lingsan_liga_member = yes
			leave_faction = yes
		}		
	}
}

mark_as_lingsan_liga_member = {	
	if = {
		limit = { is_lingsan_liga_member = no }
		set_variable = { current_faction = ZCS }
		add_lingsan_liga_ally_strategies = yes
#		add_lingsan_liga_faction_effects = yes
		if = {
			limit = {
				NOT = {
					has_idea = zcs_lingsan_torg_liga_member
				}	
			}
			add_ideas = zcs_lingsan_torg_liga_member
		}
		every_country = {
			limit = {
				is_subject_of = PREV
				is_lingsan_liga_member = no
			}			
			ar_leave_current_faction = yes
			mark_as_lingsan_liga_member = yes
		}
	}	
}

unmark_as_lingsan_liga_member = {	
	if = {
		limit = { is_lingsan_liga_member = yes }	
		remove_lingsan_liga_ally_strategies = yes
#		remove_lingsan_liga_faction_effects = yes
		clear_variable = current_faction
		if = {
			limit = {				
				has_idea = zcs_lingsan_torg_liga_member				
			}
			remove_ideas = zcs_lingsan_torg_liga_member
		}
		add_timed_idea = {
			idea = zcs_krizis_ot_vyhoda_iz_ligi
			days = 365
		}		
		every_country = {
			limit = {
				is_subject_of = PREV
				is_lingsan_liga_member = yes
			}
			unmark_as_lingsan_liga_member = yes
		}
	}	
}

add_lingsan_liga_ally_strategies = {	
	every_country = {
		limit = {
			is_lingsan_liga_member = yes
			NOT = { tag = PREV }
		}		
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = -200
		}
		PREV = {			
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = -200
			}
		}
	}	
}

remove_lingsan_liga_ally_strategies = {	
	every_country = {
		limit = { is_lingsan_liga_member = yes }
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = 200
		}
		PREV = {
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = 200
			}
		}
	}	
}

############# СЕЙИШУ
create_or_join_seishu = {	
	if = {
		limit = {
			seishu_exists = no
		}
		ar_leave_current_faction = yes
		create_faction = Fangyu_xieyi
		mark_as_seishu_member = yes
		set_variable = { GLOBAL.seishu_leader = THIS }
	}
	else_if = {
		join_seishu = yes
	}	
}

ask_to_join_seishu = {
	custom_effect_tooltip = ask_to_join_seishu_tt
	hidden_effect = {		
		var:GLOBAL.seishu_leader = {
			country_event = {
				id = ar_factionevent.9
			}
		}		
	}
}

invite_to_seishu = {
	custom_effect_tooltip = invite_to_seishu_tt
	hidden_effect = {		
		country_event = {
			id = ar_factionevent.11
		}		
	}
}

join_seishu = {
	custom_effect_tooltip = join_seishu_tt
	hidden_effect = {		
		if = {
			limit = { is_seishu_member = no }
			ar_leave_current_faction = yes
			var:GLOBAL.seishu_leader = {				
				add_to_faction = PREV
				PREV = { news_event = { id = ar_factionevent.8 } }	###				
			}
			mark_as_seishu_member = yes
		}		
	}
}

leave_seishu = {
	custom_effect_tooltip = leave_seishu_tt
	hidden_effect = {		
		if = {
			limit = { is_seishu_member = yes }
			unmark_as_seishu_member = yes
			leave_faction = yes
		}		
	}
}

mark_as_seishu_member = {	
	if = {
		limit = { is_seishu_member = no }
		set_variable = { current_faction = ZMN }
		add_seishu_ally_strategies = yes
#		add_seishu_faction_effects = yes
		if = {
			limit = {
				ZMN = {
					has_idea = zmn_seishu_alliance_buff
				}
			}
			add_ideas = zmn_seishu_alliance_buff
		}
		every_country = {
			limit = {
				is_subject_of = PREV
				is_seishu_member = no
			}			
			ar_leave_current_faction = yes
			mark_as_seishu_member = yes
		}
	}	
}

unmark_as_seishu_member = {	
	if = {
		limit = { is_seishu_member = yes }	
		remove_seishu_ally_strategies = yes
#		remove_seishu_faction_effects = yes
		clear_variable = current_faction
		if = {
			limit = {
				has_idea = zmn_seishu_alliance_buff
			}
			remove_ideas = zmn_seishu_alliance_buff
		}
		every_country = {
			limit = {
				is_subject_of = PREV
				is_seishu_member = yes
			}
			unmark_as_seishu_member = yes
		}
	}	
}

add_seishu_ally_strategies = {	
	every_country = {
		limit = {
			is_seishu_member = yes
			NOT = { tag = PREV }
		}		
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = -200
		}
		PREV = {			
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = -200
			}
		}
	}	
}

remove_seishu_ally_strategies = {	
	every_country = {
		limit = { is_seishu_member = yes }
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = 200
		}
		PREV = {
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = 200
			}
		}
	}	
}

############# Туаньцзе
create_or_join_tuantse = {	
	if = {
		limit = {
			tuantse_exists = no
		}
		ar_leave_current_faction = yes
		create_faction = tuanze_faction
		mark_as_tuantse_member = yes
		set_variable = { GLOBAL.tuantse_leader = THIS }
	}
	else_if = {
		join_tuantse = yes
	}	
}

ask_to_join_tuantse = {
	custom_effect_tooltip = ask_to_join_tuantse_tt
	hidden_effect = {		
		var:GLOBAL.tuantse_leader = {
			country_event = {
				id = ar_factionevent.15	#
			}
		}		
	}
}

invite_to_tuantse = {
	custom_effect_tooltip = invite_to_tuantse_tt
	hidden_effect = {		
		country_event = {
			id = ar_factionevent.17	#
		}		
	}
}

join_tuantse = {
	custom_effect_tooltip = join_tuantse_tt
	hidden_effect = {		
		if = {
			limit = { is_tuantse_member = no }
			ar_leave_current_faction = yes
			var:GLOBAL.tuantse_leader = {				
				add_to_faction = PREV
				PREV = { news_event = { id = ar_factionevent.14 } }	###				
			}
			mark_as_tuantse_member = yes
		}		
	}
}

leave_tuantse = {
	custom_effect_tooltip = leave_tuantse_tt
	hidden_effect = {		
		if = {
			limit = { is_tuantse_member = yes }
			unmark_as_tuantse_member = yes
			leave_faction = yes
		}		
	}
}

mark_as_tuantse_member = {	
	if = {
		limit = { is_tuantse_member = no }
		set_variable = { current_faction = ZRE }
		add_tuantse_ally_strategies = yes
#		add_seishu_faction_effects = yes		
		add_ideas = zre_tuantse_faction_buff		
		every_country = {
			limit = {
				is_subject_of = PREV
				is_tuantse_member = no
			}			
			ar_leave_current_faction = yes
			mark_as_tuantse_member = yes
		}
	}	
}

unmark_as_tuantse_member = {	
	if = {
		limit = { is_tuantse_member = yes }	
		remove_tuantse_ally_strategies = yes
#		remove_seishu_faction_effects = yes
		clear_variable = current_faction
		if = {
			limit = {
				has_idea = zre_tuantse_faction_buff
			}
			remove_ideas = zre_tuantse_faction_buff
		}
		every_country = {
			limit = {
				is_subject_of = PREV
				is_tuantse_member = yes
			}
			unmark_as_tuantse_member = yes
		}
	}	
}

add_tuantse_ally_strategies = {	
	every_country = {
		limit = {
			is_tuantse_member = yes
			NOT = { tag = PREV }
		}		
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = -200
		}
		PREV = {			
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = -200
			}
		}
	}	
}

remove_tuantse_ally_strategies = {	
	every_country = {
		limit = { is_tuantse_member = yes }
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = 200
		}
		PREV = {
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = 200
			}
		}
	}	
}

#### СНЕГ
create_or_join_sneg = {	
	if = {
		limit = {
			sneg_exists = no
		}
		ar_leave_current_faction = yes
		create_faction = europe_democr_union
		mark_as_sneg_member = yes
		set_variable = { GLOBAL.sneg_leader = THIS }
	}
	else_if = {
		join_sneg = yes
	}	
}

ask_to_join_sneg = {
	custom_effect_tooltip = ask_to_join_sneg_tt
	hidden_effect = {		
		var:GLOBAL.sneg_leader = {
			country_event = {
				id = romanimp.133
			}
		}		
	}
}

invite_to_sneg = {
	custom_effect_tooltip = invite_to_sneg_tt
	hidden_effect = {		
		country_event = {
			id = ar_factionevent.17	# ТУТ ТУАНЦЗЕ И НАДО ПЕРЕДЕЛАТЬ!!
		}		
	}
}

join_sneg = {
	custom_effect_tooltip = join_sneg_tt
	hidden_effect = {		
		if = {
			limit = { is_sneg_member = no }
			ar_leave_current_faction = yes
			var:GLOBAL.sneg_leader = {				
				add_to_faction = PREV
				PREV = { news_event = { id = ar_factionevent.14 } }	# ТУТ ТУАНЦЗЕ И НАДО ПЕРЕДЕЛАТЬ!!			
			}
			mark_as_sneg_member = yes
		}		
	}
}

leave_sneg = {
	custom_effect_tooltip = leave_sneg_tt
	hidden_effect = {		
		if = {
			limit = { is_sneg_member = yes }
			unmark_as_sneg_member = yes
			leave_faction = yes
		}		
	}
}

mark_as_sneg_member = {	
	if = {
		limit = { is_sneg_member = no }
		set_variable = { current_faction = ZRI }
		add_sneg_ally_strategies = yes		
#		add_ideas = zre_tuantse_faction_buff		
		every_country = {
			limit = {
				is_subject_of = PREV
				is_sneg_member = no
			}			
			ar_leave_current_faction = yes
			mark_as_sneg_member = yes
		}
	}	
}

unmark_as_sneg_member = {	
	if = {
		limit = { is_sneg_member = yes }	
		remove_sneg_ally_strategies = yes
		clear_variable = current_faction
		# if = {
			# limit = {
				# has_idea = zre_tuantse_faction_buff
			# }
			# remove_ideas = zre_tuantse_faction_buff
		# }
		every_country = {
			limit = {
				is_subject_of = PREV
				is_sneg_member = yes
			}
			unmark_as_sneg_member = yes
		}
	}	
}

add_sneg_ally_strategies = {	
	every_country = {
		limit = {
			is_sneg_member = yes
			NOT = { tag = PREV }
		}		
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = 200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = -200
		}
		PREV = {			
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = 200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = -200
			}
		}
	}	
}

remove_sneg_ally_strategies = {	
	every_country = {
		limit = { is_sneg_member = yes }
		add_ai_strategy = {
			type = alliance
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = befriend
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = protect
			id = PREV
			value = -200
		}
		add_ai_strategy = {
			type = ignore
			id = PREV
			value = 200
		}
		PREV = {
			add_ai_strategy = {
				type = alliance
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = befriend
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = protect
				id = PREV
				value = -200
			}
			add_ai_strategy = {
				type = ignore
				id = PREV
				value = 200
			}
		}
	}	
}


################## ОБЩИЕ
ar_join_from_faction = {	
	ar_leave_current_faction = yes
	if = {
		limit = { FROM = { is_lingsan_liga_member = yes } }
		join_lingsan_liga = yes
	}
	else_if = {
		limit = { FROM = { is_seishu_member = yes } }
		join_seishu = yes
	}
	else_if = {
		limit = { FROM = { is_tuantse_member = yes } }
		join_tuantse = yes
	}
	else_if = {
		limit = { FROM = { is_sneg_member = yes } }
		join_sneg = yes
	}

	
}

ar_leave_current_faction = {	
	if = {
		limit = { is_lingsan_liga_member = yes }
		leave_lingsan_liga = yes
	}
	else_if = {
		limit = { is_seishu_member = yes }
		leave_seishu = yes
	}
	else_if = {
		limit = { is_tuantse_member = yes }
		leave_tuantse = yes
	}
	else_if = {
		limit = { is_sneg_member = yes }
		leave_sneg = yes
	}

