--[[
Spell Alert

	By:    Balyn
        Thanks:		Quu (Code contribution)
                        Mithryn (Code contribution)
			Awen (Original Code)

]]

SpellAlertLog = { };

SpellAlertConfig = { };
SpellAlertConfig.color = { };
SpellAlertConfig.on = 1;
SpellAlertConfig.emote_on = 1;
SpellAlertConfig.NPCs_on = 0;
SpellAlertConfig.CCFriendly_on = 1;
SpellAlertConfig.totems = 0;
SpellAlertConfig.targetonly = 1;
SpellAlertConfig.heals = 1;
SpellAlertConfig.cc = 1;
SpellAlertConfig.warning = 1;
SpellAlertConfig.damage = 1;
SpellAlertConfig.misc = 0;
SpellAlertConfig.pos = -90;
SpellAlertConfig.shadow = 2;
SpellAlertConfig.zoom = 20;
SpellAlertConfig.color.at = {r=1,g=0,b=0};
SpellAlertConfig.color.he = {r=0,g=0,b=1};
SpellAlertConfig.color.bu = {r=0,g=1,b=0};
SpellAlertConfig.color.em = {r=1,g=0.96,b=0.41};
SpellAlertConfig.color.dangerous = {r=0.6705882352941176,g=0.,b=1};
SpellAlertConfig.color.totem = {r=1,g=0.6,b=0};
SpellAlertConfig.radio = 3;
SpellAlertConfig.drag = 5;
SpellAlertConfig.holdTime = 3;

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 startTime2 = 0;
local cType = 0;	-- 0 = damage, 1 = heal, 2 = buff, 3 = emote, 4 = warning
local shadow;
local getX, getY;
local change = 0;
local changed = false;
local spellAlertText = "";
local saTextColor = "";
local saTextColorEnd = "";
local saSpellColor = "";
local saSpellColorEnd = "";
local saFrameHide;
local saFrameHide2;
local saPrintName;
local saDestName;

local sa_options = 0;
local saAlertFrame;

local bit_bor	= bit.bor

local COMBATLOG_OBJECT_AFFILIATION_MINE		= COMBATLOG_OBJECT_AFFILIATION_MINE		or 0x00000001
local COMBATLOG_OBJECT_AFFILIATION_PARTY	= COMBATLOG_OBJECT_AFFILIATION_PARTY	or 0x00000002
local COMBATLOG_OBJECT_AFFILIATION_RAID		= COMBATLOG_OBJECT_AFFILIATION_RAID		or 0x00000004
local COMBATLOG_OBJECT_AFFILIATION_OUTSIDER	= COMBATLOG_OBJECT_AFFILIATION_OUTSIDER	or 0x00000008
local COMBATLOG_OBJECT_AFFILIATION_MASK		= COMBATLOG_OBJECT_AFFILIATION_MASK		or 0x0000000F
-- Reaction
local COMBATLOG_OBJECT_REACTION_FRIENDLY	= COMBATLOG_OBJECT_REACTION_FRIENDLY	or 0x00000010
local COMBATLOG_OBJECT_REACTION_NEUTRAL		= COMBATLOG_OBJECT_REACTION_NEUTRAL		or 0x00000020
local COMBATLOG_OBJECT_REACTION_HOSTILE		= COMBATLOG_OBJECT_REACTION_HOSTILE		or 0x00000040
local COMBATLOG_OBJECT_REACTION_MASK		= COMBATLOG_OBJECT_REACTION_MASK		or 0x000000F0
-- Ownership
local COMBATLOG_OBJECT_CONTROL_PLAYER		= COMBATLOG_OBJECT_CONTROL_PLAYER		or 0x00000100
local COMBATLOG_OBJECT_CONTROL_NPC			= COMBATLOG_OBJECT_CONTROL_NPC			or 0x00000200
local COMBATLOG_OBJECT_CONTROL_MASK			= COMBATLOG_OBJECT_CONTROL_MASK			or 0x00000300
-- Unit type
local COMBATLOG_OBJECT_TYPE_PLAYER			= COMBATLOG_OBJECT_TYPE_PLAYER			or 0x00000400
local COMBATLOG_OBJECT_TYPE_NPC				= COMBATLOG_OBJECT_TYPE_NPC				or 0x00000800
local COMBATLOG_OBJECT_TYPE_PET				= COMBATLOG_OBJECT_TYPE_PET				or 0x00001000
local COMBATLOG_OBJECT_TYPE_GUARDIAN		= COMBATLOG_OBJECT_TYPE_GUARDIAN		or 0x00002000
local COMBATLOG_OBJECT_TYPE_OBJECT			= COMBATLOG_OBJECT_TYPE_OBJECT			or 0x00004000
local COMBATLOG_OBJECT_TYPE_MASK			= COMBATLOG_OBJECT_TYPE_MASK			or 0x0000FC00

-- Special cases (non-exclusive)
local COMBATLOG_OBJECT_TARGET				= COMBATLOG_OBJECT_TARGET				or 0x00010000
local COMBATLOG_OBJECT_FOCUS				= COMBATLOG_OBJECT_FOCUS				or 0x00020000
local COMBATLOG_OBJECT_MAINTANK				= COMBATLOG_OBJECT_MAINTANK				or 0x00040000
local COMBATLOG_OBJECT_MAINASSIST			= COMBATLOG_OBJECT_MAINASSIST			or 0x00080000
local COMBATLOG_OBJECT_RAIDTARGET1			= COMBATLOG_OBJECT_RAIDTARGET1			or 0x00100000
local COMBATLOG_OBJECT_RAIDTARGET2			= COMBATLOG_OBJECT_RAIDTARGET2			or 0x00200000
local COMBATLOG_OBJECT_RAIDTARGET3			= COMBATLOG_OBJECT_RAIDTARGET3			or 0x00400000
local COMBATLOG_OBJECT_RAIDTARGET4			= COMBATLOG_OBJECT_RAIDTARGET4			or 0x00800000
local COMBATLOG_OBJECT_RAIDTARGET5			= COMBATLOG_OBJECT_RAIDTARGET5			or 0x01000000
local COMBATLOG_OBJECT_RAIDTARGET6			= COMBATLOG_OBJECT_RAIDTARGET6			or 0x02000000
local COMBATLOG_OBJECT_RAIDTARGET7			= COMBATLOG_OBJECT_RAIDTARGET7			or 0x04000000
local COMBATLOG_OBJECT_RAIDTARGET8			= COMBATLOG_OBJECT_RAIDTARGET8			or 0x08000000
local COMBATLOG_OBJECT_NONE					= COMBATLOG_OBJECT_NONE					or 0x80000000
local COMBATLOG_OBJECT_SPECIAL_MASK			= COMBATLOG_OBJECT_SPECIAL_MASK			or 0xFFFF0000

local LIB_FILTER_RAIDTARGET	= bit_bor(
	COMBATLOG_OBJECT_RAIDTARGET1, COMBATLOG_OBJECT_RAIDTARGET2, COMBATLOG_OBJECT_RAIDTARGET3, COMBATLOG_OBJECT_RAIDTARGET4,
	COMBATLOG_OBJECT_RAIDTARGET5, COMBATLOG_OBJECT_RAIDTARGET6, COMBATLOG_OBJECT_RAIDTARGET7, COMBATLOG_OBJECT_RAIDTARGET8
)
local LIB_FILTER_ME = bit_bor(
	COMBATLOG_OBJECT_AFFILIATION_MINE,
	COMBATLOG_OBJECT_CONTROL_PLAYER,
	COMBATLOG_OBJECT_TYPE_PLAYER
)
local LIB_FILTER_HOSTILE_PLAYER = bit_bor(
	COMBATLOG_OBJECT_AFFILIATION_OUTSIDER,
	COMBATLOG_OBJECT_CONTROL_PLAYER,
	COMBATLOG_OBJECT_TYPE_PLAYER,
	COMBATLOG_OBJECT_REACTION_HOSTILE
)
local LIB_FILTER_FRIENDLY_PLAYER = bit_bor(
	COMBATLOG_OBJECT_CONTROL_PLAYER,
	COMBATLOG_OBJECT_TYPE_PLAYER,
	COMBATLOG_OBJECT_REACTION_FRIENDLY
)
local LIB_FILTER_HOSTILE_NPC = bit_bor(
	COMBATLOG_OBJECT_AFFILIATION_OUTSIDER,
	COMBATLOG_OBJECT_CONTROL_NPC,
	COMBATLOG_OBJECT_TYPE_NPC,
	COMBATLOG_OBJECT_REACTION_HOSTILE
)
local LIB_FILTER_PET = bit_bor(
	COMBATLOG_OBJECT_CONTROL_PLAYER,
	COMBATLOG_OBJECT_TYPE_PET
)
local LIB_FILTER_MY_PET = bit_bor(
	COMBATLOG_OBJECT_AFFILIATION_MINE,
	COMBATLOG_OBJECT_CONTROL_PLAYER,
	COMBATLOG_OBJECT_TYPE_PET
)
local LIB_FILTER_HOSTILE_PET = bit_bor(
	COMBATLOG_OBJECT_AFFILIATION_OUTSIDER,
	COMBATLOG_OBJECT_CONTROL_PLAYER,
	COMBATLOG_OBJECT_TYPE_PET
)
local LIB_FILTER_PARTY = bit_bor(COMBATLOG_OBJECT_TYPE_PLAYER, COMBATLOG_OBJECT_AFFILIATION_PARTY)
local LIB_FILTER_RAID  = bit_bor(COMBATLOG_OBJECT_TYPE_PLAYER, COMBATLOG_OBJECT_AFFILIATION_RAID)
local LIB_FILTER_GROUP = bit_bor(LIB_FILTER_PARTY, LIB_FILTER_RAID)


function SpellAlert_OnLoad(self)

	self:RegisterEvent("VARIABLES_LOADED");
	self:RegisterEvent("CHAT_MSG_MONSTER_EMOTE");
	self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");

	SlashCmdList["SpellAlertCOMMAND"] = SpellAlertHandler;
	SLASH_SpellAlertCOMMAND1 = "/spellalert";
	SLASH_SpellAlertCOMMAND2 = "/마법경고";
	SLASH_SpellAlertCOMMAND3 = "/마법경보";
	DEFAULT_CHAT_FRAME:AddMessage("SpellAlert을 불려옵니다. 옵션을 불려오기 위해 '/마법경고'를 입력하세요.");
	DEFAULT_CHAT_FRAME:AddMessage("SpellAlert은 Balyn에 의해 수정되었습니다.");

end

function SpellAlert_OnEvent(self, event, ...)
--	DEFAULT_CHAT_FRAME:AddMessage(event)
	if (SpellAlertConfig.on == 0) then
		return;
	end

	if (event == "VARIABLES_LOADED") then
--        	if (not SpellAlertLog) then
--            		SpellAlertLog = {};
--			SpellAlertLog["Test Spell"] = {};
--	        end
		if (not SpellAlertConfig) then
			SpellAlertConfig = {};
			SpellAlertConfig.on = 1;
			SpellAlertConfig.emote_on = 1;
			SpellAlertConfig.NPCs_on = 0;
			SpellAlertConfig.CCFriendly_on = 1;
			SpellAlertConfig.totems = 0;
			SpellAlertConfig.targetonly = 1;
			SpellAlertConfig.heals = 1;
			SpellAlertConfig.cc = 1;
			SpellAlertConfig.warning = 1;
			SpellAlertConfig.damage = 1;
			SpellAlertConfig.misc = 0;
			SpellAlertConfig.pos = -90;
			SpellAlertConfig.shadow = 2;
			SpellAlertConfig.zoom = 20;
			SpellAlertConfig.radio = 3;
			SpellAlertConfig.drag = 5;
			SpellAlertConfig.version = 2.44;
			SpellAlertConfig.holdTime = 3;
			SpellAlertConfig.color = {};
			SpellAlertConfig.color.at = {r=1,g=0,b=0};
			SpellAlertConfig.color.he = {r=0,g=0,b=1};
			SpellAlertConfig.color.bu = {r=0,g=1,b=0};
			SpellAlertConfig.color.em = {r=1,g=0.96,b=0.41};
			SpellAlertConfig.color.dangerous = {r=0.6705882352941176,g=0.,b=1};
			SpellAlertConfig.color.totem = {r=1,g=0.6,b=0};
		end

		if(SpellAlertConfig.shadow == 0) then
			shadow = "";
		elseif (SpellAlertConfig.shadow == 1) then
			shadow = "OUTLINE";
		elseif (SpellAlertConfig.shadow == 2) then
			shadow = "THICKOUTLINE";
		end

		if (SpellAlertConfig.CCFriendly_on == nil) then
			SpellAlertConfig.CCFriendly_on = 1;
		end
		if (SpellAlertConfig.totems == nil) then
			SpellAlertConfig.totems = 0;
		end
		if (SpellAlertConfig.color.dangerous == nil) then
			SpellAlertConfig.color.dangerous = {r=0.6705882352941176,g=0.,b=1};
		end
		if (SpellAlertConfig.color.totem == nil) then
			SpellAlertConfig.color.totem = {r=1,g=0.6,b=0};
		end

		SA_MessageFrame:Show();
		SA_AlertFrame:Show();
		SA_AlertFrameStatusBar1String1:Show();
		SA_AlertFrameStatusBar2String2:Show();
		SA_AlertFrameStatusBar1:SetBackdropColor(0, 0, 0, 0);
		SA_AlertFrameStatusBar2:SetBackdropColor(0, 0, 0, 0);
		SA_AlertFrameStatusBar1:SetBackdropBorderColor(0, 0, 0, 0);
		SA_AlertFrameStatusBar2:SetBackdropBorderColor(0, 0, 0, 0);
		SA_AlertFrameStatusBar1:SetStatusBarColor(0, 0, 0, 0);
		SA_AlertFrameStatusBar2:SetStatusBarColor(0, 0, 0, 0);
--		SA_AlertFrameStatusBar1String1:SetTextColor(1, 1, 1);
		SpellAlert_Toggle();
		SA_AlertFrameStatusBar1String1:SetFont("Fonts\\2002.TTF", SpellAlertConfig.zoom, shadow);
		SA_AlertFrameStatusBar2String2:SetFont("Fonts\\2002.TTF", SpellAlertConfig.zoom, shadow);
		SA_AlertFrameStatusBar1String1:SetText("SpellAlert을 불려옵니다!");
		AlertFrame_Show(self);
		SpellAlert_SetStringColors();

	end

	local mob, spell;

	if (event == "CHAT_MSG_MONSTER_EMOTE") then
		local name = arg2;
		local emoteText = string.gsub(arg1, "%%s", name);
		local targetGUID = UnitGUID("target");
		if (not name) then
			name = "nil";
		end
		if (arg1) then
			if (SpellAlertConfig.emote_on == 1) then
				cType = 6;
--				SA_AlertFrameText:SetTextColor(1,0.6,0);
				SpellAlert_alert(self, "EMOTE", targetGUID, arg1, emoteText);
			end
		end
	end
end


function SpellAlert_CombatLogEvent(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)

	if (SpellAlertConfig.on == 0) then
		return;
	end

--	DEFAULT_CHAT_FRAME:AddMessage(eventtype)

	if eventtype == "SWING_DAMAGE" then
--		SpellAlert_SwingDamage(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "RANGE_DAMAGE" or eventtype == "SPELL_DAMAGE" or eventtype == "SPELL_PERIODIC_DAMAGE" or eventtype == "DAMAGE_SHIELD" or eventtype == "DAMAGE_SPLIT" then
--		SpellAlert_SpellDamage(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "ENVIRONMENTAL_DAMAGE" then
--		SpellAlert_EnvironmentalDamage(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SWING_MISSED" then -- Elsia: Missed block
--		SpellAlert_SwingMissed(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "RANGE_MISSED" or eventtype == "SPELL_MISSED" or eventtype == "SPELL_PERIODIC_MISSED" or eventtype == "DAMAGE_SHIELD_MISSED" then
--		SpellAlert_SpellMissed(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_HEAL" or eventtype == "SPELL_PERIODIC_HEAL" then -- Elsia: Heals
--		SpellAlert_SpellHeal(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_ENERGIZE" or eventtype == "SPELL_PERIODIC_ENERGIZE" then -- Elsia: Power gains
--		SpellAlert_SpellEnergize(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_EXTRA_ATTACKS" then
--		SpellAlert_SpellExtraAttacks(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_INTERRUPT" then
--		SpellAlert_SpellInterrupt(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_DRAIN" or eventtype == "SPELL_LEECH" or eventtype == "SPELL_PERIODIC_DRAIN" or eventtype == "SPELL_PERIODIC_LEECH" then -- Elsia: Drains & Leeches
--		SpellAlert_SpellDrainLeech(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_DISPEL_FAILED" or eventtype == "SPELL_AURA_DISPELLED" or eventtype == "SPELL_AURA_STOLEN" then -- Elsia: Failed dispell
--		SpellAlert_SpellAuraDispelledStolen(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_AURA_APPLIED" or eventtype == "SPELL_AURA_REMOVED" then
		SpellAlert_SpellAuraAppliedRemoved(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, ...);
	elseif eventtype == "SPELL_AURA_APPLIED_DOSE" or eventtype == "SPELL_AURA_REMOVED_DOSE" then
		SpellAlert_SpellAuraAppliedRemovedDose(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_CAST_START" then
		SpellAlert_SpellCastStart(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_CAST_SUCCESS" or eventtype == "SPELL_INSTAKILL" or eventtype == "SPELL_DURABILITY_DAMAGE" or eventtype == "SPELL_DURABILITY_DAMAGE_ALL" then
		SpellAlert_SpellCastSuccess(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_SUMMON" then
--or eventtype == "SPELL_CREATE" then
		SpellAlert_Summon(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "SPELL_CAST_FAILED" then -- Elsia: Spell aborts/fails
--		SpellAlert_SpellCastFailed(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "ENCHANT_APPLIED" or eventtype == "ENCHANT_APPLIED" then
--		SpellAlert_EnchantAppliedRemoved(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	elseif eventtype == "PARTY_KILL" then
--		SpellAlert_PartyKill(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags)
	elseif eventtype == "UNIT_DIED" or eventtype == "UNIT_DESTROYED" then
--		SpellAlert_UnitDied(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags)
	end

end

function SpellAlert_SpellAuraAppliedRemoved(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool, auraType)

	if (
	    (bit.band(dstFlags, LIB_FILTER_HOSTILE_PLAYER) == LIB_FILTER_HOSTILE_PLAYER) or
	    (  (SpellAlertConfig.NPCs_on == 1)  and (bit.band(dstFlags, LIB_FILTER_HOSTILE_NPC) == LIB_FILTER_HOSTILE_NPC)  )
	   ) then
		if (eventtype == "SPELL_AURA_APPLIED") and (auraType ~= "DEBUFF") then
			cType = 2;
			saPrintName = SpellAlert_TrimName(dstName,dstFlags);
--			SpellAlert_alert(self, eventtype, dstGUID, spellName, saTextColor..saPrintName.." gains "..saTextColorEnd..saSpellColor..spellName..saSpellColorEnd..".");
			SpellAlert_alert(self, eventtype, dstGUID, spellName, saTextColor..saPrintName..saTextColorEnd.."|1이;가;  "..saSpellColor..spellName..saSpellColorEnd.." 효과를 얻었습니다.");
--			DEFAULT_CHAT_FRAME:AddMessage(eventtype.." / " ..dstName.." / "..auraType.." / "..spellName)
		else
			if (SA_SPELLS_BUFFS_FADE[spellName] == 1) then
				cType = 2;
				saPrintName = SpellAlert_TrimName(dstName,dstFlags);
--				SpellAlert_alert(self, eventtype, dstGUID, spellName, saSpellColor..spellName..saSpellColorEnd..saTextColor.." fades from "..saPrintName.."."..saTextColorEnd);
				SpellAlert_alert(self, eventtype, dstGUID, spellName, saTextColor..saPrintName..saTextColorEnd.."에게서 "..saSpellColor..spellName..saSpellColorEnd.." 효과가 사라졌습니다.");
			end
		end
	elseif (
          (bit.band(dstFlags, LIB_FILTER_FRIENDLY_PLAYER) == LIB_FILTER_FRIENDLY_PLAYER)  and
          (SpellAlertConfig.CCFriendly_on == 1)
         ) then
		if (eventtype == "SPELL_AURA_APPLIED") and (auraType == "DEBUFF") and (SA_SPELLS_CC_AFFLICTED[spellName] == 1) then
			cType = 2;
			saPrintName = SpellAlert_TrimName(dstName,dstFlags);
--			SpellAlert_alert(self, eventtype, dstGUID, spellName, saTextColor..saPrintName.." is afflicted by "..saTextColorEnd..saSpellColor..spellName..saSpellColorEnd..".");
			SpellAlert_alert(self, eventtype, dstGUID, spellName, saTextColor..saPrintName..saTextColorEnd.."|1이;가; "..saSpellColor..spellName..saSpellColorEnd.."에 걸렸습니다.");
--			DEFAULT_CHAT_FRAME:AddMessage(eventtype.." / " ..dstName.." / "..auraType.." / "..spellName);
		end
	end

end

function SpellAlert_SpellCastStart(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool)

	if (
	    (bit.band(srcFlags, LIB_FILTER_HOSTILE_PLAYER) == LIB_FILTER_HOSTILE_PLAYER) or
	    (  (SpellAlertConfig.NPCs_on == 1)  and (bit.band(srcFlags, LIB_FILTER_HOSTILE_NPC) == LIB_FILTER_HOSTILE_NPC)  )
	   ) then
		cType = 2;
		saPrintName = SpellAlert_TrimName(srcName,srcFlags);
		if (dstName ~= nil) then
			saDestName = saTextColor.." on "..SpellAlert_TrimName(dstName,dstFlags);
		else
			saDestName = "";
		end
--		DEFAULT_CHAT_FRAME:AddMessage("|cffff3333" .."Spellalert Cast Start: "..eventtype..": "..spellName);
--		SpellAlert_alert(self, eventtype, srcGUID, spellName, saTextColor..saPrintName.." begins to cast "..saTextColorEnd..saSpellColor..spellName..saSpellColorEnd..saDestName..".");
		SpellAlert_alert(self, eventtype, srcGUID, spellName, saTextColor..saPrintName..saTextColorEnd.."|1이;가; "..saSpellColor..spellName..saSpellColorEnd..saDestName.."|1을;를; 시전합니다.");
	end

end

function SpellAlert_SpellCastSuccess(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool)

	if (
	    (bit.band(srcFlags, LIB_FILTER_HOSTILE_PLAYER) == LIB_FILTER_HOSTILE_PLAYER) or
	    (  (SpellAlertConfig.NPCs_on == 1)  and (bit.band(srcFlags, LIB_FILTER_HOSTILE_NPC) == LIB_FILTER_HOSTILE_NPC)  )
	   ) then
		if ( (SA_SPELLS_INSTANT[spellName] == 1) or (SA_SPELLS_TOTEMS[spellName] == 1) or (SA_SPELLS_CC[spellName] == 1) ) then
			cType = 2;
			saPrintName = SpellAlert_TrimName(srcName,srcFlags);
			if (dstName ~= nil) then
				saDestName = saTextColor.." on "..SpellAlert_TrimName(dstName,dstFlags);
			else
				saDestName = "";
			end
--			if strfind(spellName, "Totem") then
--			   	DEFAULT_CHAT_FRAME:AddMessage("|cffff3333" .."Spellalert TOTEM1: "..eventtype..": "..spellName);
--			end
--			DEFAULT_CHAT_FRAME:AddMessage("|cffff3333" .."Spellalert Cast Success: "..eventtype..": "..spellName);
--			SpellAlert_alert(self, eventtype, srcGUID, spellName, saTextColor..saPrintName.." casts "..saTextColorEnd..saSpellColor..spellName..saSpellColorEnd..saDestName..".");
			SpellAlert_alert(self, eventtype, srcGUID, spellName, saTextColor..saPrintName..saTextColorEnd.."|1이;가; "..saSpellColor..spellName..saSpellColorEnd..saDestName.."|1을;를; 시전합니다.");
		else
--			DEFAULT_CHAT_FRAME:AddMessage("|cffff3333" .."Spellalert BLOCKED: "..eventtype..": "..spellName);
		end
	end

end

function SpellAlert_Summon(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags,...)
	if (
	    (bit.band(srcFlags, LIB_FILTER_HOSTILE_PLAYER) == LIB_FILTER_HOSTILE_PLAYER) or
	    (  (SpellAlertConfig.NPCs_on == 1)  and (bit.band(srcFlags, LIB_FILTER_HOSTILE_NPC) == LIB_FILTER_HOSTILE_NPC)  )
	   ) then
--		cType = 6;
--		SpellAlert_alert(eventtype, srcName, dstName, dstGUID, saTextColor..srcName.." summons "..saTextColorEnd..saSpellColor..dstName..saSpellColorEnd..".");
--		DEFAULT_CHAT_FRAME:AddMessage(eventtype.." / " ..srcName.." / "..dstName)
	end
end


function SpellAlert_SpellAuraAppliedRemovedDose(self, event, timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, spellId, spellName, spellSchool, auraType, amount)
--	DEFAULT_CHAT_FRAME:AddMessage("|cffff3333" .."Spellalert: "..eventtype.." / " ..spellName.." / "..dstName.." / "..amount)
	if (
	    (bit.band(dstFlags, LIB_FILTER_HOSTILE_PLAYER) == LIB_FILTER_HOSTILE_PLAYER) or
	    (  (SpellAlertConfig.NPCs_on == 1)  and (bit.band(dstFlags, LIB_FILTER_HOSTILE_NPC) == LIB_FILTER_HOSTILE_NPC)  )
	   ) then
		if (SA_SPELLS_PERIODIC[spellName] == 1)  and (eventtype == "SPELL_AURA_APPLIED_DOSE") then
			cType = 2;
			saPrintName = SpellAlert_TrimName(dstName,dstFlags);
--			SpellAlert_alert(self, eventtype, dstGUID, spellName, saTextColor..saPrintName.." gains "..saTextColorEnd..saSpellColor..spellName.." ("..amount..")"..saSpellColorEnd..".");
			SpellAlert_alert(self, eventtype, dstGUID, spellName, saTextColor..saPrintName..saTextColorEnd.."|1이;가; "..saSpellColor..spellName.." ("..amount..")"..saSpellColorEnd.." 효과를 얻었습니다.");
		else
--			DEFAULT_CHAT_FRAME:AddMessage("|cffff3333" .."Spellalert BLOCKED: "..eventtype.." / " ..spellName.." / "..dstName.." / "..amount)
		end
	end

end


function SpellAlert_setOptions()

	SpellAlertConfig.on = 1;
	SpellAlertConfig.emote_on = 1;
	SpellAlertConfig.NPCs_on = 0;
	SpellAlertConfig.CCFriendly_on = 1;
	SpellAlertConfig.totems = 0;
	SpellAlertConfig.targetonly = 1;
	SpellAlertConfig.heals = 1;
	SpellAlertConfig.cc = 1;
	SpellAlertConfig.warning = 1;
	SpellAlertConfig.damage = 1;
	SpellAlertConfig.misc = 0;
	SpellAlertConfig.pos = -90;
	SpellAlertConfig.shadow = 2;
	SpellAlertConfig.zoom = 20;
	SpellAlertConfig.color.at = {r=1,g=0,b=0};
	SpellAlertConfig.color.he = {r=0,g=0,b=1};
	SpellAlertConfig.color.bu = {r=0,g=1,b=0};
	SpellAlertConfig.color.em = {r=1,g=0.96,b=0.41};
	SpellAlertConfig.color.dangerous = {r=0.6705882352941176,g=0.,b=1};
	SpellAlertConfig.color.totem = {r=1,g=0.6,b=0};
	SpellAlertConfig.radio = 3;
	SpellAlertConfig.drag = 5;
	SpellAlertConfig.holdTime = 3;

	SAOptionsEnable:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.on));
	SAOptionsEmote:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.emote_on));
	SAOptionsNPCs:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.NPCs_on));
	SAOptionsCCFriendly:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.CCFriendly_on));
	SAOptionsTotems:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.totems));
	SAOptionsTargetOnly:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.targetonly));
	SAOptionsHeals:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.heals));
	SAOptionsCC:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.cc));
	SAOptionsWarning:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.warning));
	SAOptionsMisc:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.misc));
	SAOptionsDamage:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.damage));
	SASizeSlider:SetValue(SpellAlertConfig.zoom);
	SAPosSlider:SetValue(SpellAlertConfig.pos);
	SAShadowSlider:SetValue(SpellAlertConfig.shadow);
	boxColor = getglobal("SAat_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b);
	boxColor = getglobal("SAhe_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.he.r,SpellAlertConfig.color.he.g,SpellAlertConfig.color.he.b);
	boxColor = getglobal("SAbu_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.bu.r,SpellAlertConfig.color.bu.g,SpellAlertConfig.color.bu.b);
	boxColor = getglobal("SAem_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.em.r,SpellAlertConfig.color.em.g,SpellAlertConfig.color.em.b);
	boxColor = getglobal("SADangerous_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.dangerous.r,SpellAlertConfig.color.dangerous.g,SpellAlertConfig.color.dangerous.b);
	boxColor = getglobal("SATotem_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.totem.r,SpellAlertConfig.color.totem.g,SpellAlertConfig.color.totem.b);

	SAOptionsRadio1:SetChecked(0);
	SAOptionsRadio2:SetChecked(0);
	SAOptionsRadio3:SetChecked(0);

	SA_AlertFrame:ClearAllPoints();
	SA_AlertFrame:SetPoint("CENTER", UIParent, "CENTER");
	SA_AlertFrame:SetPoint("TOP", 0, SpellAlertConfig.pos);

	sa_options = 1;
--	SA_AlertFrameText:SetAlpha(1);
--	SA_AlertFrameText:Show();
	SAOptionsRadio3:SetChecked(1);

--	SA_AlertFrameText:SetTextColor(SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b);
--	SA_AlertFrameText:SetText("|cffffffff<적대적 대상>|r이 <주문>을 시전합니다!");
--	AlertFrame:AddMessage("|cffffffff<적대적 대상>|r이 <주문>을 시전합니다!", SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b, 1, 3);


--	SA_AlertFrameText2:SetAlpha(1);
--	SA_AlertFrameText2:Show();
--	SA_AlertFrameText2:SetTextColor(SpellAlertConfig.color.dangerous.r,SpellAlertConfig.color.dangerous.g,SpellAlertConfig.color.dangerous.b);
--	SA_AlertFrameText2:SetText("|cffffffff<적대적 대상>|r이 <위험 주문>을 시전합니다!");

end


function SpellAlert_TrimName(name,flags)

	local _, _, printName, printRealm = string.find(name, "(.+)%-(.+)");

--	if (printName ~= nil) and (printRealm ~= nil) and (bit.band(flags, LIB_FILTER_HOSTILE_PLAYER) == LIB_FILTER_HOSTILE_PLAYER) then
	if (printName ~= nil) and (printRealm ~= nil) then
		return printName;
	else
		return name;
	end

end

function SpellAlertHandler(msg)
--      DEFAULT_CHAT_FRAME:AddMessage("|cffffff00" .. "SpellAlertMsg" .. ": " .. msg);

      if msg == "print" then
        SpellAlert_PrintLog();
      else
        SpellAlert_OptionsLoad();
        SAOptions:Show();
      end
end

function SpellAlert_alert(self, eventtype, combatlogGUID, spell, msg)
	if (GetLocale()=="enUS") then

		if (SA_SPELLS_IGNORE[spell] == 1) then
			return;
		end

		if (SpellAlertConfig.heals == 0) then
			if (SA_SPELLS_HEALS[spell] == 1) then
				return;
			end
		end

		if (SpellAlertConfig.cc == 0) then
			if (SA_SPELLS_CC[spell] == 1) then
				return;
			end
		end

		if (SpellAlertConfig.warning == 0) then
			if (SA_SPELLS_WARNING[spell] == 1) then
				return;
			end
		end

		if (SpellAlertConfig.damage == 0) then
			if (SA_SPELLS_DAMAGE[spell] == 1) then
				return;
			end
		end

		if (SpellAlertConfig.totems == 0) then
			if (SA_SPELLS_TOTEMS[spell] == 1) then
				return;
			end
		end

		if (SpellAlertConfig.misc == 0) then
			if (
			  (SA_SPELLS_DAMAGE[spell] ~= 1) and (SA_SPELLS_WARNING[spell] ~= 1) and (SA_SPELLS_CC[spell] ~= 1) and (SA_SPELLS_HEALS[spell] ~= 1)
                          and (SA_SPELLS_TOTEMS[spell] ~= 1) and (SA_SPELLS_CC_AFFLICTED[spell] ~= 1) and (SA_SPELLS_PERIODIC[spell] ~= 1)
			) then
				return;
			end
		end

	end
--	DEFAULT_CHAT_FRAME:AddMessage("|cff7fff7f" .."Spellalert: "..eventtype..": "..msg);
	SpellAlert_banner(self, msg, combatlogGUID, spell);
end

function AlertFrame_Color(spell)

	saAlertFrame = 1;
	if (SA_SPELLS_HEALS[spell] == 1) then
		cType = 1;
--		DEFAULT_CHAT_FRAME:AddMessage("cType1:Heal ->"..spell);
	end

	if (SA_SPELLS_CC[spell] == 1) then
		cType = 3;
--		DEFAULT_CHAT_FRAME:AddMessage("cType0:CC ->"..spell);
	end

	if (SA_SPELLS_DAMAGE[spell] == 1) then
		cType = 0;
--		DEFAULT_CHAT_FRAME:AddMessage("cType0:Damage ->"..spell);
	end

	if (SA_SPELLS_TOTEMS[spell] == 1) then
		cType = 5;
--		DEFAULT_CHAT_FRAME:AddMessage("cType5:Damage ->"..spell);
	end

	if (SA_SPELLS_WARNING[spell] == 1) then
		cType = 4;
		saAlertFrame = 2;
--		DEFAULT_CHAT_FRAME:AddMessage("cType4:Damage ->"..spell);
	end


--	if strfind(spell, "Totem") then
--		cType = 4;
--	end

	if (cType == 0) then
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b);
	elseif (cType == 1) then
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.he.r,SpellAlertConfig.color.he.g,SpellAlertConfig.color.he.b);
	elseif (cType == 2) then
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.bu.r,SpellAlertConfig.color.bu.g,SpellAlertConfig.color.bu.b);
	elseif (cType == 3) then
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.em.r,SpellAlertConfig.color.em.g,SpellAlertConfig.color.em.b);
	elseif (cType == 4) then
		SA_AlertFrameStatusBar2String2:SetTextColor(SpellAlertConfig.color.dangerous.r,SpellAlertConfig.color.dangerous.g,SpellAlertConfig.color.dangerous.b);
	elseif (cType == 5) then
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.totem.r,SpellAlertConfig.color.totem.g,SpellAlertConfig.color.totem.b);
	elseif (cType == 6) then
		--Orange for emotes
		SA_AlertFrameStatusBar1String1:SetTextColor(1,0.6,0);
	end

end

function SpellAlert_banner(self, msg, combatlogGUID, spell)

-- begin code to record all spells that this addon picks up. can use later to sort them into categories
--      local i, v;
--      local foundValue = false;
--      for i,v in ipairs(SpellAlertLog) do
--          if (i == spell) then
--              foundValue = true;
--              break;
--          end
--      end
--      if (foundValue == false) then
--          if SpellAlertLog[spell] == nil then
--              DEFAULT_CHAT_FRAME:AddMessage("|cffffff00" .. "SpellAlert" .. ": " .. spell);
--              SpellAlertLog[spell] = {};
--              SpellAlertLog[spell][1] = 1;
--          else
--              SpellAlertLog[spell][1] = SpellAlertLog[spell][1] + 1;
--          end
--      end
-- end code to record all spells this addon picks up

	local saSpellOriginatorValid = false;
	if (SpellAlertConfig.targetonly == 0) then
		saSpellOriginatorValid = true;
	else
		local targetGUID = UnitGUID("target");
		local focusGUID = UnitGUID("focus");
		if (combatlogGUID == targetGUID) or (combatlogGUID == focusGUID) then
			saSpellOriginatorValid = true;
		end
	end

	if (saSpellOriginatorValid) then

		AlertFrame_Color(spell);

		if (saAlertFrame == 1) then
			SA_AlertFrameStatusBar1String1:SetText(msg);
		else
			SA_AlertFrameStatusBar2String2:SetText(msg);
		end;

		AlertFrame_Show(self);
	end

end


function SpellAlert_PrintLog()
      DEFAULT_CHAT_FRAME:AddMessage("|cffffff00" .. "SpellAlert" .. ": " .. "Print Log");
      for i,v in ipairs(SpellAlertLog) do
        DEFAULT_CHAT_FRAME:AddMessage("|cffffff00" .. "SpellAlert" .. ": " .. v .. ": " .. i);
      end
end


function SpellAlert_isParty(name)
	for i = 1, 4, 1 do
		local partyname = UnitName("party" .. i);
		if (name == partyname) then
			return 1;
		end
	end
	return nil;
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 = SpellAlertConfig.color.at.r;
		sa_color_prev.g = SpellAlertConfig.color.at.g;
		sa_color_prev.b = SpellAlertConfig.color.at.b;
	elseif (kind == 1) then
		sa_color_prev.r = SpellAlertConfig.color.he.r;
		sa_color_prev.g = SpellAlertConfig.color.he.g;
		sa_color_prev.b = SpellAlertConfig.color.he.b;
	elseif (kind == 2) then
		sa_color_prev.r = SpellAlertConfig.color.bu.r;
		sa_color_prev.g = SpellAlertConfig.color.bu.g;
		sa_color_prev.b = SpellAlertConfig.color.bu.b;
	elseif (kind == 3) then
		sa_color_prev.r = SpellAlertConfig.color.em.r;
		sa_color_prev.g = SpellAlertConfig.color.em.g;
		sa_color_prev.b = SpellAlertConfig.color.em.b;
	elseif (kind == 4) then
		sa_color_prev.r = SpellAlertConfig.color.dangerous.r;
		sa_color_prev.g = SpellAlertConfig.color.dangerous.g;
		sa_color_prev.b = SpellAlertConfig.color.dangerous.b;
	elseif (kind == 5) then
		sa_color_prev.r = SpellAlertConfig.color.totem.r;
		sa_color_prev.g = SpellAlertConfig.color.totem.g;
		sa_color_prev.b = SpellAlertConfig.color.totem.b;
	end

	ColorPickerFrame.cancelFunc = SpellAlert_CancelColor;
	if (kind == 0) then
		ColorPickerFrame:SetColorRGB(SpellAlertConfig.color.at.r, SpellAlertConfig.color.at.g, SpellAlertConfig.color.at.b);
	elseif (kind == 1) then
		ColorPickerFrame:SetColorRGB(SpellAlertConfig.color.he.r, SpellAlertConfig.color.he.g, SpellAlertConfig.color.he.b);
	elseif (kind == 2) then
		ColorPickerFrame:SetColorRGB(SpellAlertConfig.color.bu.r, SpellAlertConfig.color.bu.g, SpellAlertConfig.color.bu.b);
	elseif (kind == 3) then
		ColorPickerFrame:SetColorRGB(SpellAlertConfig.color.em.r, SpellAlertConfig.color.em.g, SpellAlertConfig.color.em.b);
	elseif (kind == 4) then
		ColorPickerFrame:SetColorRGB(SpellAlertConfig.color.dangerous.r, SpellAlertConfig.color.dangerous.g, SpellAlertConfig.color.dangerous.b);
	elseif (kind == 5) then
		ColorPickerFrame:SetColorRGB(SpellAlertConfig.color.totem.r, SpellAlertConfig.color.totem.g, SpellAlertConfig.color.totem.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);
		SpellAlertConfig.color.at.r = r;
		SpellAlertConfig.color.at.g = g;
		SpellAlertConfig.color.at.b = b;
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b);
	elseif (kind == 1) then
		swatch = getglobal("SAhe_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SpellAlertConfig.color.he.r = r;
		SpellAlertConfig.color.he.g = g;
		SpellAlertConfig.color.he.b = b;
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.he.r,SpellAlertConfig.color.he.g,SpellAlertConfig.color.he.b);
	elseif (kind == 2) then
		swatch = getglobal("SAbu_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SpellAlertConfig.color.bu.r = r;
		SpellAlertConfig.color.bu.g = g;
		SpellAlertConfig.color.bu.b = b;
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.bu.r,SpellAlertConfig.color.bu.g,SpellAlertConfig.color.bu.b);
	elseif (kind == 3) then
		swatch = getglobal("SAem_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SpellAlertConfig.color.em.r = r;
		SpellAlertConfig.color.em.g = g;
		SpellAlertConfig.color.em.b = b;
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.em.r,SpellAlertConfig.color.em.g,SpellAlertConfig.color.em.b);
	elseif (kind == 4) then
		swatch = getglobal("SADangerous_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SpellAlertConfig.color.dangerous.r = r;
		SpellAlertConfig.color.dangerous.g = g;
		SpellAlertConfig.color.dangerous.b = b;
		SA_AlertFrameStatusBar2String2:SetTextColor(SpellAlertConfig.color.dangerous.r,SpellAlertConfig.color.dangerous.g,SpellAlertConfig.color.dangerous.b);
	elseif (kind == 5) then
		swatch = getglobal("SATotem_NormalTexture");
		swatch:SetVertexColor(r,g,b);
		SpellAlertConfig.color.totem.r = r;
		SpellAlertConfig.color.totem.g = g;
		SpellAlertConfig.color.totem.b = b;
		SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.totem.r,SpellAlertConfig.color.totem.g,SpellAlertConfig.color.totem.b);
	end

end

function SpellAlert_CancelColor()
	if (kind == 0) then
		swatch = getglobal("SAat_NormalTexture");
		SpellAlertConfig.color.at.r = sa_color_prev.r;
		SpellAlertConfig.color.at.g = sa_color_prev.g;
		SpellAlertConfig.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");
		SpellAlertConfig.color.he.r = sa_color_prev.r;
		SpellAlertConfig.color.he.g = sa_color_prev.g;
		SpellAlertConfig.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");
		SpellAlertConfig.color.bu.r = sa_color_prev.r;
		SpellAlertConfig.color.bu.g = sa_color_prev.g;
		SpellAlertConfig.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");
		SpellAlertConfig.color.em.r = sa_color_prev.r;
		SpellAlertConfig.color.em.g = sa_color_prev.g;
		SpellAlertConfig.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("SADangerous_NormalTexture");
		SpellAlertConfig.color.dangerous.r = sa_color_prev.r;
		SpellAlertConfig.color.dangerous.g = sa_color_prev.g;
		SpellAlertConfig.color.dangerous.b = sa_color_prev.b;
		swatch:SetVertexColor(sa_color_prev.r, sa_color_prev.g, sa_color_prev.b);
	elseif (kind == 5) then
		swatch = getglobal("SATotem_NormalTexture");
		SpellAlertConfig.color.totem.r = sa_color_prev.r;
		SpellAlertConfig.color.totem.g = sa_color_prev.g;
		SpellAlertConfig.color.totem.b = sa_color_prev.b;
		swatch:SetVertexColor(sa_color_prev.r, sa_color_prev.g, sa_color_prev.b);
	end
	SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b);
end

function SpellAlert_OptionsClose()
	SpellAlertConfig.on = SpellAlert_Bool2Int(SAOptionsEnable:GetChecked());
	SpellAlertConfig.emote_on = SpellAlert_Bool2Int(SAOptionsEmote:GetChecked());
	SpellAlertConfig.NPCs_on = SpellAlert_Bool2Int(SAOptionsNPCs:GetChecked());
	SpellAlertConfig.CCFriendly_on = SpellAlert_Bool2Int(SAOptionsCCFriendly:GetChecked());
	SpellAlertConfig.totems = SpellAlert_Bool2Int(SAOptionsTotems:GetChecked());
	SpellAlertConfig.targetonly = SpellAlert_Bool2Int(SAOptionsTargetOnly:GetChecked());
	SpellAlertConfig.heals = SpellAlert_Bool2Int(SAOptionsHeals:GetChecked());
	SpellAlertConfig.cc = SpellAlert_Bool2Int(SAOptionsCC:GetChecked());
	SpellAlertConfig.warning = SpellAlert_Bool2Int(SAOptionsWarning:GetChecked());
	SpellAlertConfig.misc = SpellAlert_Bool2Int(SAOptionsMisc:GetChecked());
	SpellAlertConfig.damage = SpellAlert_Bool2Int(SAOptionsDamage:GetChecked());
	SA_AlertFrameStatusBar1String1:SetText(" ");
	SA_AlertFrameStatusBar2String2:SetText(" ");
	sa_options = 0;
	SAOptions:Hide();
	SA_AlertFrame:ClearAllPoints();
	getX = SA_AlertFrame:GetLeft();
	getY = SA_AlertFrame:GetTop();

	SA_AlertFrameStatusBar1:SetBackdropColor(0, 0, 0, 0);
	SA_AlertFrameStatusBar2:SetBackdropColor(0, 0, 0, 0);
	SA_AlertFrameStatusBar1:SetBackdropBorderColor(0, 0, 0, 0);
	SA_AlertFrameStatusBar2:SetBackdropBorderColor(0, 0, 0, 0);
	SA_AlertFrameStatusBar1:SetStatusBarColor(0, 0, 0, 0);
	SA_AlertFrameStatusBar2:SetStatusBarColor(0, 0, 0, 0);

end

function SpellAlert_OptionsLoad()
	local boxColor;
	SAOptionsEnable:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.on));
	SAOptionsEmote:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.emote_on));
	SAOptionsNPCs:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.NPCs_on));
	SAOptionsCCFriendly:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.CCFriendly_on));
	SAOptionsTotems:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.totems));
	SAOptionsTargetOnly:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.targetonly));
	SAOptionsHeals:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.heals));
	SAOptionsCC:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.cc));
	SAOptionsWarning:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.warning));
	SAOptionsMisc:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.misc));
	SAOptionsDamage:SetChecked(SpellAlert_Int2Bool(SpellAlertConfig.damage));
	SASizeSlider:SetValue(SpellAlertConfig.zoom);
	SAPosSlider:SetValue(SpellAlertConfig.pos);
	SAShadowSlider:SetValue(SpellAlertConfig.shadow);
	SADelaySlider:SetValue(SpellAlertConfig.holdTime);
	boxColor = getglobal("SAat_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b);
	boxColor = getglobal("SAhe_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.he.r,SpellAlertConfig.color.he.g,SpellAlertConfig.color.he.b);
	boxColor = getglobal("SAbu_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.bu.r,SpellAlertConfig.color.bu.g,SpellAlertConfig.color.bu.b);
	boxColor = getglobal("SAem_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.em.r,SpellAlertConfig.color.em.g,SpellAlertConfig.color.em.b);
	boxColor = getglobal("SADangerous_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.dangerous.r,SpellAlertConfig.color.dangerous.g,SpellAlertConfig.color.dangerous.b);
	boxColor = getglobal("SATotem_NormalTexture");
	boxColor:SetVertexColor(SpellAlertConfig.color.totem.r,SpellAlertConfig.color.totem.g,SpellAlertConfig.color.totem.b);

	SAOptionsRadio1:SetChecked(0);
	SAOptionsRadio2:SetChecked(0);
	SAOptionsRadio3:SetChecked(0);

	sa_options = 1;
	SA_MessageFrame:Show();
	SA_AlertFrame:Show();
	SA_AlertFrameStatusBar1:Show();
	SA_AlertFrameStatusBar2:Show();

	SA_AlertFrameStatusBar1:SetBackdropColor(1, 1, 1, 1);
	SA_AlertFrameStatusBar2:SetBackdropColor(1, 1, 1, 1);
	SA_AlertFrameStatusBar1:SetBackdropBorderColor(1, 1, 1, 1);
	SA_AlertFrameStatusBar2:SetBackdropBorderColor(1, 1, 1, 1);
	SA_AlertFrameStatusBar1:SetStatusBarColor(0.5, 0.5, 0.5, 1);
	SA_AlertFrameStatusBar2:SetStatusBarColor(0.5, 0.5, 0.5, 1);

	SA_AlertFrameStatusBar1String1:SetAlpha(1);
	SA_AlertFrameStatusBar1String1:Show();
	SA_AlertFrameStatusBar1String1:SetTextColor(SpellAlertConfig.color.at.r,SpellAlertConfig.color.at.g,SpellAlertConfig.color.at.b);
	SA_AlertFrameStatusBar2String2:SetAlpha(1);
	SA_AlertFrameStatusBar2String2:Show();
	SA_AlertFrameStatusBar2String2:SetTextColor(SpellAlertConfig.color.dangerous.r,SpellAlertConfig.color.dangerous.g,SpellAlertConfig.color.dangerous.b);

	if (SpellAlertConfig.radio == 1) then
		SAOptionsRadio1:SetChecked(1);
		SA_AlertFrameStatusBar1String1:SetText("<적대적 대상>이 <주문>을 시전합니다!");
		SA_AlertFrameStatusBar2String2:SetText("<적대적 대상>이 <위험 주문>을 시전합니다!");
	elseif (SpellAlertConfig.radio == 2) then
		SAOptionsRadio2:SetChecked(1);
		SA_AlertFrameStatusBar1String1:SetText("<적대적 대상>이 |cffffffff<주문>|r을 시전합니다!");
		SA_AlertFrameStatusBar2String2:SetText("<적대적 대상>이 |cffffffff<위험 주문>|r을 시전합니다!");
	else
		SAOptionsRadio3:SetChecked(1);
		SA_AlertFrameStatusBar1String1:SetText("|cffffffff<적대적 대상>|r이 <주문>을 시전합니다!");
		SA_AlertFrameStatusBar2String2:SetText("|cffffffff<적대적 대상>|r이 <위험 주문>을 시전합니다!");
	end

	SpellAlert_Toggle();

end

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

function SA_Radio_Update()
	SAOptionsRadio1:SetChecked(0);
	SAOptionsRadio2:SetChecked(0);
	SAOptionsRadio3:SetChecked(0);


	if (SpellAlertConfig.radio == 1) then
		SAOptionsRadio1:SetChecked(1);
		SA_AlertFrameStatusBar1String1:SetText("<적대적 대상>이 <주문>을 시전합니다!");
		SA_AlertFrameStatusBar2String2:SetText("<적대적 대상>이 <위험 주문>을 시전합니다!");
	elseif (SpellAlertConfig.radio == 2) then
		SAOptionsRadio2:SetChecked(1);
		SA_AlertFrameStatusBar1String1:SetText("<적대적 대상>이 |cffffffff<주문>|r을 시전합니다!");
		SA_AlertFrameStatusBar2String2:SetText("<적대적 대상>이 |cffffffff<위험 주문>|r을 시전합니다!");
	else
		SAOptionsRadio3:SetChecked(1);
		SA_AlertFrameStatusBar1String1:SetText("|cffffffff<적대적 대상>|r이 <주문>을 시전합니다!");
		SA_AlertFrameStatusBar2String2:SetText("|cffffffff<적대적 대상>|r이 <위험 주문>을 시전합니다!");
	end


	SpellAlert_Toggle();
	SpellAlert_SetStringColors();

end

function SpellAlert_SetStringColors()

	if (SpellAlertConfig.radio == 1) then
		saTextColor = "";
		saTextColorEnd = "";
		saSpellColor = "";
		saSpellColorEnd = "";
	elseif (SpellAlertConfig.radio == 2) then
		saTextColor = "";
		saTextColorEnd = "";
		saSpellColor = "|cffffffff";
		saSpellColorEnd = "|r";
	else
		saTextColor = "|cffffffff";
		saTextColorEnd = "|r";
		saSpellColor = "";
		saSpellColorEnd = "";
	end

end

function SpellAlert_Toggle()

--	if (SpellAlertConfig.drag == 5) then
--		SA_AlertFrame:ClearAllPoints();
	SA_AlertFrame:SetPoint("TOP", 0, SpellAlertConfig.pos);
--		SA_AlertFrameText:ClearAllPoints();
--		SA_AlertFrameText:SetPoint("CENTER", "AlertFrame", "CENTER");
--		SA_AlertFrameText:SetJustifyH("CENTER");
--		SADrag:Hide();
--	else

--		if (sa_options == 1) then
--			SADrag:Show();
--		end

--		SA_AlertFrame:ClearAllPoints();

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

--		:SetMovable(true);
--	end

end


function SpellAlert_UpdateLook()

	if(SpellAlertConfig.shadow == 0) then
		shadow = "";
	elseif (SpellAlertConfig.shadow == 1) then
		shadow = "OUTLINE";
	elseif (SpellAlertConfig.shadow == 2) then
		shadow = "THICKOUTLINE";
	end

	SA_AlertFrameStatusBar1String1:SetFont("Fonts\\2002.TTF", SpellAlertConfig.zoom, shadow);
	SA_AlertFrameStatusBar2String2:SetFont("Fonts\\2002.TTF", SpellAlertConfig.zoom, shadow);

end


function SpellAlert_UpdatePosition()

	SA_AlertFrame:ClearAllPoints();
	SA_AlertFrame:SetPoint("TOP", 0, SpellAlertConfig.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(self)

	if (saAlertFrame == 1) then
		startTime = GetTime();
		SA_AlertFrameStatusBar1String1:Show();
	else
		startTime2 = GetTime();
		SA_AlertFrameStatusBar2String2:Show();
	end

end

function SA_IsLocked()

  return false;

end

function SpellAlert_OnUpdate(self, elapsed)

	if (sa_options == 0) then

		saFrameHide = true;
		local elapsed = GetTime() - startTime;
		if ( elapsed < fadeInTime ) then
			local alpha = (elapsed / fadeInTime);
			SA_AlertFrameStatusBar1String1:SetAlpha(alpha);
			saFrameHide = false;
		end
		if ( elapsed < (fadeInTime + SpellAlertConfig.holdTime) ) and (saFrameHide == true) then
			SA_AlertFrameStatusBar1String1:SetAlpha(1.0);
			saFrameHide = false;
		end
		if ( elapsed < (fadeInTime + SpellAlertConfig.holdTime + fadeOutTime) ) and (saFrameHide == true) then
			local alpha = 1.0 - ((elapsed - SpellAlertConfig.holdTime - fadeInTime) / fadeOutTime);
			SA_AlertFrameStatusBar1String1:SetAlpha(alpha);
			saFrameHide = false;
		end
		if (saFrameHide == true) then
			SA_AlertFrameStatusBar1String1:Hide();
		end

		saFrameHide2 = true;
		local elapsed2 = GetTime() - startTime2;
		if ( elapsed2 < fadeInTime ) then
			local alpha2 = (elapsed2 / fadeInTime);
			SA_AlertFrameStatusBar2String2:SetAlpha(alpha2);
			saFrameHide2 = false;
		end
		if ( elapsed2 < (fadeInTime + SpellAlertConfig.holdTime) ) and (saFrameHide2 == true) then
			SA_AlertFrameStatusBar2String2:SetAlpha(1.0);
			saFrameHide2 = false;
		end
		if ( elapsed2 < (fadeInTime + SpellAlertConfig.holdTime + fadeOutTime) ) and (saFrameHide2 == true) then
			local alpha2 = 1.0 - ((elapsed2 - SpellAlertConfig.holdTime - fadeInTime) / fadeOutTime);
			SA_AlertFrameStatusBar2String2:SetAlpha(alpha2);
			saFrameHide2 = false;
		end
		if (saFrameHide2 == true) then
			SA_AlertFrameStatusBar2String2:Hide();
		end

	end
end
