--[[
Spell Alert (마법경고)
	Author:		Awen
	Modified by:	Mithryn
	Thanks:		Quu (Code contribution)
				Cyron (German Translation)
				Awen (Original Code)
	한글화 편집 :	22섭 말퓨리온 Atone 길드 세이클럽

]]--

SAConfig = { };
SAConfig.color = { };
SAConfig.on = 1;
SAConfig.emote_on = 1;
SAConfig.targetonly = 1;
SAConfig.heals = 1;
SAConfig.cc = 1;
SAConfig.dispelable = 1;
SAConfig.damage = 1;
SAConfig.misc = 1;
SAConfig.dismounter = 0;
SAConfig.combat = 0;
SAConfig.soul = 1;
SAConfig.autorez = 0;
SAConfig.summon = 0;
SAConfig.pos = -60;
SAConfig.shadow = 1;
SAConfig.zoom = 26;
SAConfig.color.at = {r=1,g=0,b=0};
SAConfig.color.he = {r=1,g=1,b=0};
SAConfig.color.bu = {r=0,g=1,b=0};
SAConfig.color.em = {r=1,g=0.6,b=0};
SAConfig.color.to = {r=0.5,g=0.5,b=0.5};
SAConfig.radio = 3;
SAConfig.drag = 5;
SAConfig.holdTime = 1;

local kind = 0;
local sa_gains = { };
local sa_color_prev = {r=1,g=1,b=1};
local fadeInTime = 0.1;
local fadeOutTime = 0.4;
local startTime = 0;
local cType = 0;	-- 0 = damage, 1 = heal, 2 = buff, 3 = emote, 4 = totem
local shadow;
local getX, getY;
local change = 0;
local changed = false;
local sa_options = 0;
local partyNum = 0;
local raidNum = 0;
local raidList = {};
local SaMount;

function SpellAlert_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("RAID_ROSTER_UPDATE");
	this:RegisterEvent("PARTY_MEMBERS_CHANGED");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_CREATURE_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_CREATURE_VS_CREATURE_BUFF");
	this:RegisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_HOSTILEPLAYER_BUFF");
	this:RegisterEvent("CHAT_MSG_MONSTER_EMOTE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE");
	this:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_BUFFS");
	this:RegisterEvent("CHAT_MSG_SPELL_AURA_GONE_OTHER");
    
	this:RegisterEvent("CHAT_MSG_SPELL_AURA_GONE_SELF");	
	this:RegisterEvent("PLAYER_REGEN_DISABLED");	
	this:RegisterEvent("PLAYER_REGEN_ENABLED");	
	this:RegisterEvent("RESURRECT_REQUEST");	
	this:RegisterEvent("CONFIRM_SUMMON");

	this:RegisterEvent("READY_CHECK");
	this:RegisterEvent("CHAT_MSG_ADDON");
	
	--[[ 탈것 관련 ]]--
	--this:RegisterEvent("TAXIMAP_OPENED");
	--this:RegisterEvent("GOSSIP_SHOW");
	this:RegisterEvent("PLAYER_AURAS_CHANGED");
   	this:RegisterEvent("UI_ERROR_MESSAGE");

	SlashCmdList["SpellAlertCOMMAND"] = SpellAlertHandler;
	SLASH_SpellAlertCOMMAND1 = "/spellalert";
	SLASH_SpellAlertCOMMAND2 = "/마법경고";
	SLASH_SpellAlertCOMMAND3 = "/마법경보";
end

function SpellAlert_OnEvent()
	if (SAConfig.on == 0) then
		return;
	end	
	if (event == "VARIABLES_LOADED") then
		if (not SAConfig) then
			SAConfig = {};	
			SAConfig.on = 1;
			SAConfig.emote_on = 1;
			SAConfig.targetonly = 1;
			SAConfig.heals = 1;
			SAConfig.cc = 1;
			SAConfig.dispelable = 1;
			SAConfig.damage = 1;
			SAConfig.misc = 1;
			SAConfig.dismounter = 0;
			SAConfig.combat = 0;
			SAConfig.soul = 1;
			SAConfig.mess = 0;
			SAConfig.autorez = 0;
			SAConfig.summon = 0;	
			SAConfig.pos = -60;
			SAConfig.shadow = 1;
			SAConfig.zoom = 26;
			SAConfig.radio = 3;
			SAConfig.version = 1.5;
			SAConfig.drag = 5;
			SAConfig.version = 1.6;
			SAConfig.holdTime = 1;						
			SAConfig.color = {};
			SAConfig.color.at = {r=1,g=0,b=0};
			SAConfig.color.he = {r=1,g=1,b=0};
			SAConfig.color.bu = {r=0,g=1,b=0};
			SAConfig.color.em = {r=1,g=0.6,b=0};
			SAConfig.color.to = {r=0.5,g=0.5,b=0.5};
			DEFAULT_CHAT_FRAME:AddMessage("마법경고: 설정 옵션 기본값");
		elseif (not SAConfig.color) then
			SAConfig.color = {};
			SAConfig.color.at = {r=1,g=0,b=0};
			SAConfig.color.he = {r=1,g=1,b=0};
			SAConfig.color.bu = {r=0,g=1,b=0};
			SAConfig.color.em = {r=1,g=0.6,b=0};
			SAConfig.color.to = {r=0.5,g=0.5,b=0.5};
			DEFAULT_CHAT_FRAME:AddMessage("마법경고: 설정 옵션 기본값");
		elseif (not SAConfig.radio) then
			SAConfig.radio = 1;
			DEFAULT_CHAT_FRAME:AddMessage("마법경고: 설정 옵션 기본값");
		elseif (not SAConfig.version or not SAConfig.version == 1.5) then
			SAConfig.version = 1.5;
			SAConfig.drag = 5;
			DEFAULT_CHAT_FRAME:AddMessage("업데이트 마법경고 1.5");
		elseif (not SAConfig.version or not SAConfig.version == 1.6) then
			SAConfig.version = 1.6;
			SAConfig.holdTime = 1;
			DEFAULT_CHAT_FRAME:AddMessage("업데이트 마법경고 1.6");
		end
		if(SAConfig.shadow == 0) then
			shadow = "";
		elseif (SAConfig.shadow == 1) then
			shadow = "OUTLINE";
		elseif (SAConfig.shadow == 2) then
			shadow = "THICKOUTLINE";
		end		
		AlertFrameText:SetTextColor(1, 1, 1);
		SpellAlert_Toggle();
		AlertFrameText:SetFont("Fonts\\2002.TTF", SAConfig.zoom, shadow);
		AlertFrameText:SetText("마법경고 불려오기!");
		if(not SAConfig.holdTime) then
			SAConfig.holdTime = 1;
		end
		AlertFrame_Show()
	end	
    if (event == "PARTY_MEMBERS_CHANGED") then
		local num = GetNumPartyMembers();
		if ((num ~= partyNum) and (GetNumRaidMembers() == 0)) then
			partyNum = num;
			SpellAlert_RaidList(false);
		end
	elseif (event == "RAID_ROSTER_UPDATE") then
		local num = GetNumRaidMembers();
		if (num ~= raidNum) then
			raidNum = num;
			SpellAlert_RaidList(true);
		end
    end
	if (event =="PLAYER_REGEN_DISABLED" and SAConfig.combat == 1) then
		DEFAULT_CHAT_FRAME:AddMessage("전투 상태 시작!!",1,0,0);
		return;
	elseif (event =="PLAYER_REGEN_ENABLED" and SAConfig.combat == 1) then
		DEFAULT_CHAT_FRAME:AddMessage("전투 상태 해제!!",1,0,0);
		return;
	end
	if (event=="RESURRECT_REQUEST") then
		if (SAConfig.autorez == 1) then
			AcceptResurrect();
			getglobal("StaticPopup1Button1"):Hide();
			getglobal("StaticPopup1Button2"):Hide();
			getglobal("StaticPopup1"):Hide();
		end
	elseif (event=="CONFIRM_SUMMON") then
		if (SAConfig.summon == 1) then
			ConfirmSummon();
			getglobal("StaticPopup1Button1"):Hide();
			getglobal("StaticPopup1Button2"):Hide();
			getglobal("StaticPopup1"):Hide();
		end     
	end	 
	if event=="PLAYER_AURAS_CHANGED" then
		SpellAlert_TryDismount();
	elseif event=="UI_ERROR_MESSAGE" then
		--탈것에서 내려야 합니다.
		--이미 타고 있는 상태입니다. 먼저 내려야 합니다.
		--탈것에서 내려야 공격할 수 있습니다.
		if (arg1==SPELL_FAILED_NOT_MOUNTED or arg1==ERR_TAXIPLAYERALREADYMOUNTED or arg1==ERR_ATTACK_MOUNTED) and SAConfig.dismounter == 1 and SaMount then
			CancelPlayerBuff(SaMount);
			SaMount=nil;
		end
	end
	--if (event == "TAXIMAP_OPENED") then 
		--AutoDismount();
	--end
	--if (event == "GOSSIP_SHOW" and not IsControlKeyDown()) then
	--	SkipGossip();
	--end
	if (event == "CHAT_MSG_SPELL_AURA_GONE_SELF") then
		if (SAConfig.soul == 0) then
			return;
		end
		if ( string.find(arg1, "영혼석 부활 효과가 사라졌습니다.") ) then
			if ( GetNumRaidMembers() > 0 ) then
				SendChatMessage("영혼석이 사라졌습니다. 영혼석을 걸어주세요!", "RAID");
			elseif ( UnitInParty("player") ) then
				SendChatMessage("영혼석이 사라졌습니다. 영혼석을 걸어주세요!", "PARTY");
			end
		end
	end		
	local mob, spell;
	if (event == "CHAT_MSG_SPELL_HOSTILEPLAYER_DAMAGE") then
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			if (not SpellAlert_isParty(mob)) then
				cType = 0;
				if (SAConfig.radio == 1) then
					SpellAlert_alert(mob, spell, arg1);
				elseif (SAConfig.radio == 2) then
					SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".."|1을;를; 시전합니다.");
				else
					SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff|1을;를; 시전합니다.|r");
				end				
			end
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_HOSTILEPLAYER_BUFF") then
		for mob, spell, k in string.gmatch(arg1, SA_PTN_SPELL_GAINS_X) do
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			if (not SpellAlert_isParty(mob)) then
				cType = 1;
				if (SAConfig.radio == 1) then
					SpellAlert_alert(mob, spell, arg1);
				elseif (SAConfig.radio == 2) then
					SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".."|1을;를; 시전합니다.");
				else
					SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff|1을;를; 시전합니다.|r");
				end				
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_TOTEM) do
			if (not SpellAlert_isParty(mob)) then
				cType = 4;
				if (SAConfig.radio == 1) then
					SpellAlert_alert(mob, spell, arg1);
				elseif (SAConfig.radio == 2) then
					SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".." 토템을 시전합니다.");
				else
					SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff 토템을 시전합니다.|r");
				end				
			end
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_GAINS) do
			cType = 2;
			if (SAConfig.radio == 1) then
				SpellAlert_alert(mob, spell, arg1);
			elseif (SAConfig.radio == 2) then
				SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".." 효과를 얻었습니다.");
			else
				SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff 효과를 얻었습니다.|r");
			end				
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_CREATURE_VS_CREATURE_BUFF") then
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			cType = 1;
			if (SAConfig.radio == 1) then
				SpellAlert_alert(mob, spell, arg1);
			elseif (SAConfig.radio == 2) then
				SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".."|1을;를; 시전합니다.");
			else
				SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff|1을;를; 시전합니다.|r");
			end				
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_PERIODIC_CREATURE_BUFFS") then
		for mob, spell, k in string.gmatch(arg1, SA_PTN_SPELL_GAINS_X) do
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_GAINS) do
			cType = 2;
			if (SAConfig.radio == 1) then
				SpellAlert_alert(mob, spell, arg1);
			elseif (SAConfig.radio == 2) then
				SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".." 효과를 얻었습니다.");
			else
				SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff 효과를 얻었습니다.|r");
			end				
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_CREATURE_VS_CREATURE_DAMAGE") then
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_BEGIN_CAST) do
			cType = 0;
			if (SAConfig.radio == 1) then
				SpellAlert_alert(mob, spell, arg1);
			elseif (SAConfig.radio == 2) then
				SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".."|1을;를; 시전합니다.");
			else
				SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff|1을;를; 시전합니다.|r");
			end				
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_PERIODIC_HOSTILEPLAYER_BUFFS") then
		for mob, spell, temp in string.gmatch(arg1, SA_PTN_SPELL_GAINS_X) do
			return;
		end
		for mob, spell in string.gmatch(arg1, SA_PTN_SPELL_GAINS) do
			if ( (spell == SA_WOTF) or
			     (spell == SA_BERSERKER_RAGE) ) then
				sa_gains[mob] = {};
				sa_gains[mob].spell = spell;
				sa_gains[mob].time = GetTime();
			end
			cType = 2;
			if (SAConfig.radio == 1) then
				SpellAlert_alert(mob, spell, arg1);
			elseif (SAConfig.radio == 2) then
				SpellAlert_alert(mob, spell, mob.."|1이;가; |cffffffff"..spell.."|r".." 효과를 얻었습니다.");
			else
				SpellAlert_alert(mob, spell, "|cffffffff"..mob.."|1이;가; |r"..spell.."|cffffffff 효과를 얻었습니다.|r");
			end				
			return;
		end
	elseif (event == "CHAT_MSG_SPELL_AURA_GONE_OTHER") then
		for spell, mob in string.gmatch(arg1, SA_PTN_SPELL_FADE) do
			if ( (spell == SA_WOTF) or
			     (spell == SA_BERSERKER_RAGE) ) then
				local tt = sa_gains[mob];
				if (tt) then
					if (tt.spell == spell) then
						if (GetTime() - tt.time <= 30) then
							cType = 2;
							if (SAConfig.radio == 1) then
								SpellAlert_alert(mob, spell, arg1);
							elseif (SAConfig.radio == 2) then
								SpellAlert_alert(mob, spell, mob.."의 몸에서 |cffffffff"..spell.."|r".." 효과가 사라졌습니다.");
							else
								SpellAlert_alert(mob, spell, "|cffffffff"..mob.."의 몸에서 |r"..spell.."|cffffffff 효과가 사라졌습니다.|r");
							end				
						end
					end
					tt[mob] = nil;
					return;
				end
			end
		end      
	elseif (event == "CHAT_MSG_EMOTE") then
		if (arg1) then
			if (SAConfig.emote_on == 1) then
				cType = 3;
				SpellAlert_alert("", "", arg1);
			end
		end
	elseif (event == "CHAT_MSG_MONSTER_EMOTE") then
		local name = arg2;
		if (not name) then
			name = "nil";
		end
		if (arg1) then
			if (SAConfig.emote_on == 1) then
				cType = 3;
				SpellAlert_alert(name, arg1, format(arg1, arg2)); 
			end
		end
	end	
end

function SpellAlert_SelfCombat(msg)
	AlertFrameText:SetText(msg);
	AlertFrameText:SetTextColor(SAConfig.color.at.r,SAConfig.color.at.g,SAConfig.color.at.b);
	AlertFrame_Show();
end

function isMounted(mountTexture)
	for i = 1, getn(MOUNTTYPES) do
		if ( string.find(mountTexture, MOUNTTYPES[i]) ) then
			return true ;
		end
	end
	return false ;
end

function AutoDismount()
	for i=0,15 do
		if GetPlayerBuffTexture(i) then
			if ( isMounted(GetPlayerBuffTexture(i)) ) then
				CancelPlayerBuff(i) ;				
			end
		end
	end
end

--function SkipGossip()
--	local itc = 1;
--	local list = {GetGossipOptions()} ;
--	for i = 1, getn(list) do
--		if( SKIPTHISGOSSIP[ list[i] ] ) then
--			SelectGossipOption(itc) ;
--			return ;
--		end
--		if ( i - math.floor(i/2)*2 == 0 ) then
--			itc = itc + 1 ;
--		end
--	end
--end

function SpellAlert_TryDismount()
	for i = 0, 10 do
		local texture = GetPlayerBuffTexture(i);
		if (texture) then
			if (string.find(texture,"Spell_Nature_Swiftness") or string.find(texture,"Ability_Mount") or string.find(texture,"_QirajiCrystal_") or string.find(texture,"INV_Misc_Foot_Kodo")) then
				SaMount=i;
				return;
			end
		else
			SaMount=nil;
			break;
		end
	end
end

function SpellAlertHandler(msg)
	SpellAlert_OptionsLoad();
	SAOptions:Show();
end

function SpellAlert_alert(mob, spell, msg)
	if (GetLocale()=="koKR") then
		if (SA_SPELLS_IGNORE[spell]) then
			return;
		end
		if (SAConfig.heals == 0) then
			if (SA_SPELLS_HEALS[spell] == 1) then
				return;
			end
		end
		if (SAConfig.cc == 0) then
			if (SA_SPELLS_CC[spell] == 1) then
				return;
			end
		end
		if (SAConfig.dispelable == 0) then
			if (SA_SPELLS_DISPELABLE[spell] == 1) then
				return;
			end
		end
		if (SAConfig.damage == 0) then
			if (SA_SPELLS_DAMAGE[spell] == 1) then
				return;
			end
		end
	end
	SpellAlert_banner(msg, mob);
end

function AlertFrame_Color()
	if (SA_SPELLS_HEALS[spell] == 1) then
		cType = 1;
	end
	if (SA_SPELLS_CC[spell] == 1) then
		cType = 0;	
	end
	if (SA_SPELLS_DISPELABLE[spell] == 1) then
		cType = 2;	
	end	
	if (SA_SPELLS_DAMAGE[spell] == 1) then
		cType = 0;
	end
	if (cType == 0) then
		AlertFrameText:SetTextColor(SAConfig.color.at.r,SAConfig.color.at.g,SAConfig.color.at.b);
	elseif (cType == 1) then
		AlertFrameText:SetTextColor(SAConfig.color.he.r,SAConfig.color.he.g,SAConfig.color.he.b);
	elseif (cType == 2) then
		AlertFrameText:SetTextColor(SAConfig.color.bu.r,SAConfig.color.bu.g,SAConfig.color.bu.b);
	elseif (cType == 3) then
		AlertFrameText:SetTextColor(SAConfig.color.em.r,SAConfig.color.em.g,SAConfig.color.em.b);
	elseif (cType == 4) then
		AlertFrameText:SetTextColor(SAConfig.color.to.r,SAConfig.color.to.g,SAConfig.color.to.b);
	end	
end

function SpellAlert_banner(msg, mob)
	if (SAConfig.targetonly == 0) then
		AlertFrameText:SetText(msg);
		AlertFrame_Color();
		AlertFrame_Show();
	else
		local targetname = UnitName("target");
		if (mob == targetname) then
			AlertFrameText:SetText(msg);
			AlertFrame_Color();
			AlertFrame_Show();
		end
	end
end

function SpellAlert_RaidList(isRaid)
	raidList = { };
	local max, g, name;
	if (isRaid) then
		max = 40;
		g = "raid";
	else
		max = 4;
		g = "party";
	end
	for i = 1, max do
		name = UnitName(g..i);
		if (name) then
			raidList[name] = true; 
		end
		name = UnitName(g.."pet"..i);
		if (name) then
			raidList[name] = true;
		end
	end
end

function SpellAlert_isParty(name)
	--[[
	if ( GetNumRaidMembers() > 0 ) then
		for i = 1, 39, 1 do
			local partyname = UnitName("raid" .. i);
			if ( name == partyname) then
				return 1;
			end
		end
		return nil;
	elseif ( GetNumPartyMembers() > 0 ) then
		for i = 1, 4, 1 do
		local partyname = UnitName("party" .. i);
			if (name == partyname) then
				return 1;
			end
		end
		return nil;
	end
	--]]
	return raidList[name];
end


function SpellAlert_OpenColorPicker(button, kinda)
	kind = kinda;
	if ( not button ) then
		button = this;
	end
	ColorPickerFrame.func = SpellAlert_SetColor;

	if (kind == 0) then
		sa_color_prev.r = SAConfig.color.at.r;
		sa_color_prev.g = SAConfig.color.at.g;
		sa_color_prev.b = SAConfig.color.at.b;
	elseif (kind == 1) then
		sa_color_prev.r = SAConfig.color.he.r;
		sa_color_prev.g = SAConfig.color.he.g;
		sa_color_prev.b = SAConfig.color.he.b;
	elseif (kind == 2) then
		sa_color_prev.r = SAConfig.color.bu.r;
		sa_color_prev.g = SAConfig.color.bu.g;
		sa_color_prev.b = SAConfig.color.bu.b;
	elseif (kind == 3) then
		sa_color_prev.r = SAConfig.color.em.r;
		sa_color_prev.g = SAConfig.color.em.g;
		sa_color_prev.b = SAConfig.color.em.b;
	elseif (kind == 4) then
		sa_color_prev.r = SAConfig.color.to.r;
		sa_color_prev.g = SAConfig.color.to.g;
		sa_color_prev.b = SAConfig.color.to.b;
	end
	
	ColorPickerFrame.cancelFunc = SpellAlert_CancelColor;
	if (kind == 0) then
		ColorPickerFrame:SetColorRGB(SAConfig.color.at.r, SAConfig.color.at.g, SAConfig.color.at.b);
	elseif (kind == 1) then
		ColorPickerFrame:SetColorRGB(SAConfig.color.he.r, SAConfig.color.he.g, SAConfig.color.he.b);
	elseif (kind == 2) then
		ColorPickerFrame:SetColorRGB(SAConfig.color.bu.r, SAConfig.color.bu.g, SAConfig.color.bu.b);
	elseif (kind == 3) then
		ColorPickerFrame:SetColorRGB(SAConfig.color.em.r, SAConfig.color.em.g, SAConfig.color.em.b);
	elseif (kind == 4) then
		ColorPickerFrame:SetColorRGB(SAConfig.color.to.r, SAConfig.color.to.g, SAConfig.color.to.b);
	end
	ColorPickerFrame:Show();
end

function SpellAlert_SetColor()
	local r,g,b = ColorPickerFrame:GetColorRGB();
	local swatch,frame;
	if (kind == 0) then
		swatch = getglobal("SAat_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SAConfig.color.at.r = r;
		SAConfig.color.at.g = g;
		SAConfig.color.at.b = b;
		AlertFrameText:SetTextColor(SAConfig.color.at.r,SAConfig.color.at.g,SAConfig.color.at.b);
	elseif (kind == 1) then
		swatch = getglobal("SAhe_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SAConfig.color.he.r = r;
		SAConfig.color.he.g = g;
		SAConfig.color.he.b = b;
		AlertFrameText:SetTextColor(SAConfig.color.he.r,SAConfig.color.he.g,SAConfig.color.he.b);
	elseif (kind == 2) then
		swatch = getglobal("SAbu_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SAConfig.color.bu.r = r;
		SAConfig.color.bu.g = g;
		SAConfig.color.bu.b = b;
		AlertFrameText:SetTextColor(SAConfig.color.bu.r,SAConfig.color.bu.g,SAConfig.color.bu.b);
	elseif (kind == 3) then
		swatch = getglobal("SAem_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SAConfig.color.em.r = r;
		SAConfig.color.em.g = g;
		SAConfig.color.em.b = b;
		AlertFrameText:SetTextColor(SAConfig.color.em.r,SAConfig.color.em.g,SAConfig.color.em.b);
	elseif (kind == 4) then
		swatch = getglobal("SAto_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SAConfig.color.to.r = r;
		SAConfig.color.to.g = g;
		SAConfig.color.to.b = b;
		AlertFrameText:SetTextColor(SAConfig.color.to.r,SAConfig.color.to.g,SAConfig.color.to.b);
	end

end

function SpellAlert_CancelColor()
	if (kind == 0) then
		swatch = getglobal("SAat_NormalTexture");
		SAConfig.color.at.r = sa_color_prev.r;
		SAConfig.color.at.g = sa_color_prev.g;
		SAConfig.color.at.b = sa_color_prev.b;
		swatch:SetVertexColor(sa_color_prev.r, sa_color_prev.g, sa_color_prev.b);
	elseif (kind == 1) then
		swatch = getglobal("SAhe_NormalTexture");
		SAConfig.color.he.r = sa_color_prev.r;
		SAConfig.color.he.g = sa_color_prev.g;
		SAConfig.color.he.b = sa_color_prev.b;
		swatch:SetVertexColor(sa_color_prev.r, sa_color_prev.g, sa_color_prev.b);
	elseif (kind == 2) then
		swatch = getglobal("SAbu_NormalTexture");
		SAConfig.color.bu.r = sa_color_prev.r;
		SAConfig.color.bu.g = sa_color_prev.g;
		SAConfig.color.bu.b = sa_color_prev.b;
		swatch:SetVertexColor(sa_color_prev.r, sa_color_prev.g, sa_color_prev.b);
	elseif (kind == 3) then
		swatch = getglobal("SAem_NormalTexture");
		SAConfig.color.em.r = sa_color_prev.r;
		SAConfig.color.em.g = sa_color_prev.g;
		SAConfig.color.em.b = sa_color_prev.b;
		swatch:SetVertexColor(sa_color_prev.r, sa_color_prev.g, sa_color_prev.b);
	elseif (kind == 4) then
		swatch = getglobal("SAto_NormalTexture");
		SAConfig.color.to.r = sa_color_prev.r;
		SAConfig.color.to.g = sa_color_prev.g;
		SAConfig.color.to.b = sa_color_prev.b;
		swatch:SetVertexColor(sa_color_prev.r, sa_color_prev.g, sa_color_prev.b);
	end
	AlertFrameText:SetTextColor(0.3,0.3,0.3);
end

function SpellAlert_OptionsClose()
	SAConfig.on = SpellAlert_Bool2Int(SAOptionsEnable:GetChecked());
	SAConfig.emote_on = SpellAlert_Bool2Int(SAOptionsEmote:GetChecked());
	SAConfig.targetonly = SpellAlert_Bool2Int(SAOptionsTargetOnly:GetChecked());
	
	SAConfig.dismounter = SpellAlert_Bool2Int(SAOptionsDismounter:GetChecked());
	SAConfig.combat = SpellAlert_Bool2Int(SAOptionsCombat:GetChecked());
	SAConfig.soul = SpellAlert_Bool2Int(SAOptionsSoul:GetChecked());
	SAConfig.autorez = SpellAlert_Bool2Int(SAOptionsAutorez:GetChecked());
	SAConfig.summon = SpellAlert_Bool2Int(SAOptionsSummon:GetChecked());
	
	AlertFrameText:SetText(" ");
	sa_options = 0;
	SAOptions:Hide();
	AlertFrame:ClearAllPoints();
	getX = AlertFrame:GetLeft();
	getY = AlertFrame:GetTop();
	SADrag:Hide();
end

function SpellAlert_OptionsLoad()
	local boxColor;
	SAOptionsEnable:SetChecked(SpellAlert_Int2Bool(SAConfig.on));			
	SAOptionsEmote:SetChecked(SpellAlert_Int2Bool(SAConfig.emote_on));		
	SAOptionsTargetOnly:SetChecked(SpellAlert_Int2Bool(SAConfig.targetonly));	

	SAOptionsCombat:SetChecked(SpellAlert_Int2Bool(SAConfig.combat));
	SAOptionsDismounter:SetChecked(SpellAlert_Int2Bool(SAConfig.dismounter));	
	SAOptionsSoul:SetChecked(SpellAlert_Int2Bool(SAConfig.soul));
	SAOptionsAutorez:SetChecked(SpellAlert_Int2Bool(SAConfig.autorez));		
	SAOptionsSummon:SetChecked(SpellAlert_Int2Bool(SAConfig.summon));
	
	SASizeSlider:SetValue(SAConfig.zoom);
	SAPosSlider:SetValue(SAConfig.pos);
	SAShadowSlider:SetValue(SAConfig.shadow);
	SADelaySlider:SetValue(SAConfig.holdTime);
	boxColor = getglobal("SAat_NormalTexture");
	boxColor:SetVertexColor(SAConfig.color.at.r,SAConfig.color.at.g,SAConfig.color.at.b);
	boxColor = getglobal("SAhe_NormalTexture");
	boxColor:SetVertexColor(SAConfig.color.he.r,SAConfig.color.he.g,SAConfig.color.he.b);
	boxColor = getglobal("SAbu_NormalTexture");
	boxColor:SetVertexColor(SAConfig.color.bu.r,SAConfig.color.bu.g,SAConfig.color.bu.b);
	boxColor = getglobal("SAem_NormalTexture");
	boxColor:SetVertexColor(SAConfig.color.em.r,SAConfig.color.em.g,SAConfig.color.em.b);
	boxColor = getglobal("SAto_NormalTexture");
	boxColor:SetVertexColor(SAConfig.color.to.r,SAConfig.color.to.g,SAConfig.color.to.b);

	SAOptionsRadio1:SetChecked(0);
	SAOptionsRadio2:SetChecked(0);
	SAOptionsRadio3:SetChecked(0);
	SAOptionsRadioA:SetChecked(0);
	SAOptionsRadioB:SetChecked(0);
	
	sa_options = 1;
	AlertFrameText:SetAlpha(1);
	AlertFrameText:Show();
	AlertFrameText:SetTextColor(0.3,0.3,0.3);
	if (SAConfig.radio == 1) then
		SAOptionsRadio1:SetChecked(1);
		AlertFrameText:SetText("<적의 이름>|1이;가; <주문>|1을;를; 시전합니다!");
	elseif (SAConfig.radio == 2) then
		SAOptionsRadio2:SetChecked(1);
		AlertFrameText:SetText("<적의 이름>|1이;가; |cffffffff<주문>|r|1을;를; 시전합니다!");
	else
		SAOptionsRadio3:SetChecked(1);
		AlertFrameText:SetText("|cffffffff<적의 이름>|1이;가;|r <주문>|cffffffff|1을;를; 시전합니다!|r");
	end
	if (SAConfig.drag == 5) then
		SAOptionsRadioA:SetChecked(1);
	else
		SAOptionsRadioB:SetChecked(1);
	end
	SpellAlert_Toggle();
end

function SA_Radio_OnClick()
	local radioId = this:GetID();
	if(radioId <= 3) then
		SAConfig.radio = radioId;
	else
		SAConfig.drag = radioId;
	end
	SA_Radio_Update();
end

function SA_Radio_Update()
	SAOptionsRadio1:SetChecked(0);
	SAOptionsRadio2:SetChecked(0);
	SAOptionsRadio3:SetChecked(0);
	SAOptionsRadioA:SetChecked(0);
	SAOptionsRadioB:SetChecked(0);
	
	if (SAConfig.radio == 1) then
		SAOptionsRadio1:SetChecked(1);
		AlertFrameText:SetText("<적의 이름>|1이;가; <주문>|1을;를; 시전합니다!");
	elseif (SAConfig.radio == 2) then
		SAOptionsRadio2:SetChecked(1);
		AlertFrameText:SetText("<적의 이름>|1이;가; |cffffffff<주문>|r|1을;를; 시전합니다!");
	else
		SAOptionsRadio3:SetChecked(1);
		AlertFrameText:SetText("|cffffffff<적의 이름>|1이;가;|r <주문>|cffffffff|1을;를; 시전합니다!|r");
	end

	if (SAConfig.drag == 5) then
		SAOptionsRadioA:SetChecked(1);
	else
		SAOptionsRadioB:SetChecked(1);
	end
	SpellAlert_Toggle()
end

function SpellAlert_Toggle()
	if (SAConfig.drag == 5) then
		SADrag:SetMovable(false);
		AlertFrame:ClearAllPoints();
		AlertFrame:SetPoint("TOP", 0, SAConfig.pos);
		AlertFrameText:ClearAllPoints();
		AlertFrameText:SetPoint("CENTER", "AlertFrame", "CENTER");
		AlertFrameText:SetJustifyH("CENTER");
		SADrag:Hide();
	else
		if (sa_options == 1) then
			SADrag:Show();
		end
		
		AlertFrame:ClearAllPoints();

		if ((SADrag:GetLeft()+32) < (GetScreenWidth()/2)) then
			AlertFrame:SetPoint("LEFT", "SADrag", "LEFT", 0, -30);
			AlertFrameText:ClearAllPoints();
			AlertFrameText:SetPoint("LEFT", "AlertFrame", "LEFT");
			AlertFrameText:SetJustifyH("LEFT");
		else
			AlertFrame:SetPoint("RIGHT", "SADrag", "RIGHT", 0, -30);
			AlertFrameText:ClearAllPoints();
			AlertFrameText:SetPoint("RIGHT", "AlertFrame", "RIGHT");
			AlertFrameText:SetJustifyH("RIGHT");
		end
		SADrag:SetMovable(true);
	end
end

function AlertFrame_Adjust()
	if (SADrag:IsVisible()) then
		if ((SADrag:GetLeft()+32) < (GetScreenWidth()/2)) then
			if (change == 0 and not changed) then
				AlertFrame:ClearAllPoints();
				AlertFrame:SetPoint("LEFT", "SADrag", "LEFT", 0, -30);
				AlertFrameText:ClearAllPoints();
				AlertFrameText:SetPoint("LEFT", "AlertFrame", "LEFT");
				AlertFrameText:SetJustifyH("LEFT");
				changed = true;
			elseif (change == 1 and changed) then
				change = 0;
				changed = false;
			end
			AlertFrameText:SetWidth(GetScreenWidth() - AlertFrame:GetLeft());	
		else
			if (change == 1 and not changed) then
				AlertFrame:ClearAllPoints();
				AlertFrame:SetPoint("RIGHT", "SADrag", "RIGHT", 0, -30);
				AlertFrameText:ClearAllPoints();
				AlertFrameText:SetPoint("RIGHT", "AlertFrame", "RIGHT");
				AlertFrameText:SetJustifyH("RIGHT");
				changed = true;
			elseif (change == 0 and changed) then
				change = 1;
				changed = false;
			end
			AlertFrameText:SetWidth(GetScreenWidth() - (GetScreenWidth() - AlertFrame:GetRight()));
		end
	end
end

function SpellAlert_UpdateLook()
	if(SAConfig.shadow == 0) then
		shadow = "";
	elseif (SAConfig.shadow == 1) then
		shadow = "OUTLINE";
	elseif (SAConfig.shadow == 2) then
		shadow = "THICKOUTLINE";
	end	
	AlertFrameText:SetFont("Fonts\\2002.TTF", SAConfig.zoom, shadow);
end


function SpellAlert_UpdatePosition()
	AlertFrame:ClearAllPoints();
	AlertFrame:SetPoint("TOP", 0, SAConfig.pos);
end

function SpellAlert_Bool2Int(val)
	if (val) then
		return 1;
	else
		return 0;
	end
end

function SpellAlert_Int2Bool(val)
	if (val == 0) then
		return nil;
	else
		return 1;
	end
end

function AlertFrame_Show()
	startTime = GetTime();
	AlertFrameText:Show();
end

function SpellAlert_OnUpdate()
	local targetName = UnitName("target");
	if (targetName) then
		sa_targetClass = UnitClass("target");
		sa_targetHostile = UnitIsEnemy("player", "target");
		sa_targetName = targetName;
	else
--		if (sa_targetName and (sa_targetClass == "사냥꾼") and sa_targetHostile) then
--			SpellAlert_retargetHunter();
--		end
	end
	if(sa_options == 0) then
		local elapsed = GetTime() - startTime;
		if ( elapsed < fadeInTime ) then
			local alpha = (elapsed / fadeInTime);
			AlertFrameText:SetAlpha(alpha);
			return;
		end
		if ( elapsed < (fadeInTime + SAConfig.holdTime) ) then
			AlertFrameText:SetAlpha(1.0);
			return;
		end
		if ( elapsed < (fadeInTime + SAConfig.holdTime + fadeOutTime) ) then
			local alpha = 1.0 - ((elapsed - SAConfig.holdTime - fadeInTime) / fadeOutTime);
			AlertFrameText:SetAlpha(alpha);
			return;
		end
		AlertFrameText:Hide();
	end
end
