Global("Tracked", {})
Global("Container", mainForm:GetChildChecked("MainPanel", false))
Global("ContInternal", mainForm:GetChildChecked("ContInternal", false))
Global("ContIntDesc", ContInternal:GetWidgetDesc())
ContInternal:DestroyWidget()
Global("DragPanel", Container:GetChildChecked("DragPanel", false))
local TRACK = false
local FORCETRACK = false
local TimerTracker = {}
local StackTracker = {}
local TrackColors = {
	["Buff"] = {r=0;g=.5;b=.5;a=1},
	["Attack"] = {r=.8;g=.4;b=0;a=1},
	["PlayerDebuff"] = {r=.7;g=0;b=0;a=1},
	["Spawn"] = {r=.5;g=0;b=.5;a=1},
	["Health"] = {r=0;g=.7;b=0;a=1},
	["Mana"] = {r=0;g=0;b=.7;a=1},
}
local C2TDesc = mainForm:GetChildChecked("C2T", false):GetWidgetDesc()
mainForm:GetChildChecked("C2T", false):DestroyWidget()

if not unit.IsInCombat then
	unit.IsInCombat = object.IsInCombat
end

-------------------------------------------------------------------------------
-- FUNCTION
-------------------------------------------------------------------------------
local function CheckIfTracked(sourceId)
	local t
	for i, v in Tracked do
		if v.ID == sourceId then
			t = i
		end
	end
	if not t then
		table.insert(Tracked, {["Name"] = userMods.FromWString(object.GetName(sourceId)), ["Time"] = 0, ["ID"] = sourceId})
	end
	for i, v in Tracked do
		if v.ID == sourceId then
			t = i
		end
	end
	if not TimerTracker[t] then
		TimerTracker[t] = {}
	end
	return t
end

function Tracker(p)	
	if BossCheck(p.source) then
		if p.spellId then
			local spellInfo = avatar.GetSpellInfo(p.spellId)
			local t = CheckIfTracked(p.source)
			local spellName = userMods.FromWString(spellInfo.name)
			if string.find(spellName, "<html>") then
				spellName = string.sub(spellName, 7, string.len(spellName)-7)
			end
			if not Tracked[t][spellName] then
				Tracked[t][spellName] = {["TimeUsed"] = "", ["Widget"] = nil,}
				Tracked[t][spellName]["SpellType"] = (p.unitId and "Buff") or "Attack"
				Tracked[t][spellName]["Widget"] = mainForm:CreateWidgetByDesc(ContIntDesc)
				Container:AddChild(Tracked[t][spellName]["Widget"])
				Tracked[t][spellName]["Widget"]:GetChildChecked("Bar", false):SetBackgroundColor(TrackColors[(Tracked[t][spellName]["SpellType"])])
			end
			if Tracked[t][spellName]["SpellType"] == "Attack" or (p.unitId and Tracked[t][spellName]["SpellType"] == "Buff") then
				if BossTable[userMods.FromWString(object.GetName(p.source))] then
					TrackCreate(spellInfo, Tracked[t][spellName]["Widget"], BossTable[userMods.FromWString(object.GetName(p.source))], object.GetName(p.source), t)
					TrackSort()
				end
				Tracked[t][spellName]["TimeUsed"] = Tracked[t][spellName]["TimeUsed"].." ["..Tracked[t]["Time"].."s"
				if object.IsExist(p.source) then
					Tracked[t][spellName]["TimeUsed"] = Tracked[t][spellName]["TimeUsed"].." H"..(unit.GetHealthPercentage(p.source)).."%"
					Tracked[t][spellName]["TimeUsed"] = Tracked[t][spellName]["TimeUsed"].." M"..(unit.GetManaPercentage(p.source)).."%"
				end
				Tracked[t][spellName]["TimeUsed"] = Tracked[t][spellName]["TimeUsed"].."]"
			end
		end
	end
end

function BuffTrackingAssist(p)
	if p.objectId then
		p.unitId = p.objectId
	end
	if object.IsUnit(p.unitId) and not object.IsAstralUnit(p.unitId) then
		local buffInfo
		for i, v in object.GetBuffs(p.unitId) do
			if tonumber(v) == tonumber(p.buffId) then
				buffInfo = object.GetBuffInfo(v)
			end
		end
		local buffName = userMods.FromWString(buffInfo.name)
		p.source = buffInfo.producer.casterId
		if p.source == nil and buffInfo.producer.buffId then
			p.source = object.GetBuffInfo(buffInfo.producer.buffId).producer.casterId
		end
		if BossCheck(p.source) then
			p.spellId = buffInfo.producer.spellId
			Tracker(p)
			if object.IsExist(p.unitId) and (((raid.IsPlayerInAvatarsRaid(object.GetName(p.unitId)) or group.IsCreatureInGroup(p.unitId) or p.unitId == avatar.GetId())) or p.unitId == p.source) then
				BuffTrackCreate(p, buffInfo)
			end
		end
	end
end

function BCTrack(p)
	if p.objects then
		p.units = p.objects
	end
	for i, v in p.units do
		if object.IsUnit(i) and not object.IsAstralUnit(i) then
			for j, x in v do
				if x then
					local c = {}
					local buffInfo
					for k, y in object.GetBuffs(i) do
						if tonumber(y) == tonumber(j) then
							buffInfo = object.GetBuffInfo(y)
						end
					end
					if buffInfo then
						local buffName = userMods.FromWString(buffInfo.name)
						if string.find(buffName, "<html>") then
							buffName = string.sub(buffName, 7, string.len(buffName)-7)
						end
						if buffInfo.stackCount and buffInfo.stackCount > 1 then
							c.unitId = i
							c.source = buffInfo.producer.casterId
							if BossCheck(c.source) then
								c.spellId = buffInfo.producer.spellId
								if c.spellId then
									if not StackTracker[i] then
										StackTracker[i] = {}
									end
									if not StackTracker[i][buffName] then
										StackTracker[i][buffName] = 1
									end
									if StackTracker[i][buffName] ~= buffInfo.stackCount then
										StackTracker[i][buffName] = buffInfo.stackCount
										--Tracker(c)
										if object.IsExist(c.unitId) and (((raid.IsPlayerInAvatarsRaid(object.GetName(c.unitId)) or group.IsCreatureInGroup(c.unitId) or c.unitId == avatar.GetId())) or c.unitId == c.source) then
											BuffTrackCreate(c, buffInfo)
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

function TrackSpawn(p)
	local bFP
	if avatar.GetUnitList then
		for i, v in avatar.GetUnitList() do
			if BossCheck(v) and unit.IsInCombat(v) then
				bFP = v
			end
		end
	end
	if bFP ~= nil then
		local t = CheckIfTracked(bFP)
		local spawnName = userMods.FromWString(object.GetName(p.unitId))
		if not Tracked[t][spawnName] then
			Tracked[t][spawnName] = {["TimeUsed"] = "", ["Widget"] = nil,}
			Tracked[t][spawnName]["SpellType"] = "Spawn"
			Tracked[t][spawnName]["Widget"] = mainForm:CreateWidgetByDesc(ContIntDesc)
			Container:AddChild(Tracked[t][spawnName]["Widget"])
			Tracked[t][spawnName]["Widget"]:GetChildChecked("Bar", false):SetBackgroundColor(TrackColors[(Tracked[t][spawnName]["SpellType"])])
		end
		if BossTable[userMods.FromWString(object.GetName(bFP))] then
			TrackCreate({["name"] = userMods.ToWString(spawnName),}, Tracked[t][spawnName]["Widget"], BossTable[userMods.FromWString(object.GetName(bFP))], object.GetName(bFP), t)
			TrackSort()
		end
		Tracked[t][spawnName]["TimeUsed"] = Tracked[t][spawnName]["TimeUsed"].." ["..Tracked[t]["Time"].."s"
		if object.IsExist(bFP) then
			Tracked[t][spawnName]["TimeUsed"] = Tracked[t][spawnName]["TimeUsed"].." H"..unit.GetHealthPercentage(bFP).."%"
			Tracked[t][spawnName]["TimeUsed"] = Tracked[t][spawnName]["TimeUsed"].." M"..unit.GetManaPercentage(bFP).."%"
		end
		Tracked[t][spawnName]["TimeUsed"] = Tracked[t][spawnName]["TimeUsed"].."]"
	end
end

function BossCheck(p)
	if p and object.IsUnit(p) and unit.IsPlayer(p) == false and unit.IsPet(p) == false and (unit.GetQuality(p) == UNIT_QUALITY_RAID_BOSS or unit.GetQuality(p) == UNIT_QUALITY_BOSS) and unit.IsInCombat(p) then
		return true
	end
end

function HealthTrack(p)
	if BossCheck(p.unitId) then
		if BossTable[userMods.FromWString(object.GetName(p.unitId))] then
			local highestHealth = 0
			local highestHealth2 = 0
			for i, v in BossTable[userMods.FromWString(object.GetName(p.unitId))] do
				if string.find(i, "H!") then
					if highestHealth < v then
						highestHealth = v
					elseif highestHealth == v then
						highestHealth2 = v
					end
				end
			end
			for i, v in BossTable[userMods.FromWString(object.GetName(p.unitId))] do
				local t = CheckIfTracked(p.unitId)
				if string.find(i, "H!") and (v == highestHealth or v == highestHealth2) then
					if not Tracked[t][i] then
						Tracked[t][i] = {["TimeUsed"] = "", ["Widget"] = nil,}
						Tracked[t][i]["SpellType"] = "Health"
						Tracked[t][i]["Widget"] = mainForm:CreateWidgetByDesc(ContIntDesc)
						Container:AddChild(Tracked[t][i]["Widget"])
						Tracked[t][i]["Widget"]:GetChildChecked("Bar", false):SetBackgroundColor(TrackColors[(Tracked[t][i]["SpellType"])])
					end
					local P = Tracked[t][i]["Widget"]:GetPlacementPlain()
					if P.alignX == WIDGET_ALIGN_CENTER then
						Container:AddChild(Tracked[t][i]["Widget"])
						P.alignX = WIDGET_ALIGN_LOW
						P.alignY = WIDGET_ALIGN_LOW
						P.sizeX = 200
						P.sizeY = 15
						Tracked[t][i]["Widget"]:SetPlacementPlain()
					end
					local currentHealth = unit.GetHealthPercentage(p.unitId)
					if currentHealth >= v then
						PercTrackCreate(i, Tracked[t][i]["Widget"], BossTable[userMods.FromWString(object.GetName(p.unitId))][i], currentHealth, "H")
						TrackSort()
					elseif Tracked[t][i]["Widget"]:IsVisible() then
						Tracked[t][i]["Widget"]:PlayFadeEffect(1.0, 0, 1000, EA_MONOTONOUS_INCREASE)
					end
				elseif string.find(i, "H!") and Tracked[t][i] and Tracked[t][i]["Widget"]:IsVisible() then
					Tracked[t][i]["Widget"]:PlayFadeEffect(1.0, 0, 3000, EA_MONOTONOUS_INCREASE)
					mainForm:AddChild(Tracked[t][i]["Widget"])
					local P = Tracked[t][i]["Widget"]:GetPlacementPlain()
					P.alignX = WIDGET_ALIGN_CENTER
					P.alignY = WIDGET_ALIGN_CENTER
					Tracked[t][i]["Widget"]:SetPlacementPlain(P)
					local toP = {}
					for j, x in P do
						toP[j] = x
					end
					toP.sizeX = 200*1.5
					toP.sizeY = 15*1.5
					Tracked[t][i]["Widget"]:PlayResizeEffect(P, toP, 2000, EA_MONOTONOUS_INCREASE)
				end
			end
		end
	end
end

function ManaTrack(p)
	if BossCheck(p.unitId) then
		if BossTable[userMods.FromWString(object.GetName(p.unitId))] then
			for i, v in BossTable[userMods.FromWString(object.GetName(p.unitId))] do
				if string.find(i, "M!") then
					local t = CheckIfTracked(p.unitId)
					if not Tracked[t][i] then
						Tracked[t][i] = {["TimeUsed"] = "", ["Widget"] = nil,}
						Tracked[t][i]["SpellType"] = "Mana"
						Tracked[t][i]["Widget"] = mainForm:CreateWidgetByDesc(ContIntDesc)
						Container:AddChild(Tracked[t][i]["Widget"])
						Tracked[t][i]["Widget"]:GetChildChecked("Bar", false):SetBackgroundColor(TrackColors[(Tracked[t][i]["SpellType"])])
					end
					local currentMana = unit.GetManaPercentage(p.unitId)
					if currentMana >= BossTable[userMods.FromWString(object.GetName(p.unitId))][i] then
						PercTrackCreate(i, Tracked[t][i]["Widget"], BossTable[userMods.FromWString(object.GetName(p.unitId))][i], currentMana, "M")
						TrackSort()
					elseif Tracked[t][i]["Widget"]:IsVisible() then
						Tracked[t][i]["Widget"]:PlayFadeEffect(1.0, 0, 1000, EA_MONOTONOUS_INCREASE)
					end
				end
			end
		end
	end
end

function PercTrackCreate(p, w, aInfo, currentPerc, percType)
	if w then
		w:SetBackgroundColor({r=0;b=0;g=0;a=.5})
		local msg = ""
		if p then
			msg = msg..(string.sub(p, 3))
		end
		w:GetChildChecked("Name", false):SetFormat(userMods.ToWString("<html fontsize='13' outline='1'><r name='name'/></html>"))
		w:GetChildChecked("Name", false):SetVal("name", userMods.ToWString(msg))
		w:GetChildChecked("BuffTimer", false):SetFormat(userMods.ToWString("<html fontsize='13' outline='1'><r name='timer'/></html>"))
		w:GetChildChecked("BuffTimer", false):SetVal("timer", userMods.ToWString(percType..currentPerc.."/"..aInfo..""))
		local P = w:GetChildChecked("Bar", false):GetPlacementPlain()
		if currentPerc > aInfo then
			P.sizeX = (1 - (100-currentPerc)/(100-aInfo))*180
		else
			P.sizeX = 1
		end
		w:GetChildChecked("Bar", false):SetPlacementPlain(P)
		w:Show(true)
	end
end

function BuffTrackCreate(p, buffInfo)
	local t = CheckIfTracked(p.source)
	local buffName = userMods.FromWString(buffInfo.name)
	if string.find(buffName, "<html>") then
		buffName = string.sub(buffName, 7, string.len(buffName)-7)
	end
	if userMods.FromWString(object.GetName(p.source)) == "Nogrom" and buffName ~= "Concussive Strike" then
	else
		local naming = buffName..userMods.FromWString(object.GetName(p.unitId))
		if not Tracked[t][naming] then
			Tracked[t][naming] = {["Widget"] = nil, ["UnitId"] = p.unitId, ["BuffName"] = buffName}
			Tracked[t][naming]["SpellType"] = "PlayerDebuff"
			Tracked[t][naming]["Widget"] = mainForm:CreateWidgetByDesc(ContIntDesc)
			Container:AddChild(Tracked[t][naming]["Widget"])
			Tracked[t][naming]["Widget"]:GetChildChecked("Bar", false):SetBackgroundColor(TrackColors[(Tracked[t][naming]["SpellType"])])
			local C2T = mainForm:CreateWidgetByDesc(C2TDesc)
			Tracked[t][naming]["Widget"]:AddChild(C2T)
			C2T:SetName(""..p.unitId)
			C2T:Show(true)
		end
		Tracked[t][naming]["Widget"]:FinishFadeEffect()
		Tracked[t][naming]["Widget"]:SetFade(1)
		if buffInfo.texture then
			Tracked[t][naming]["Widget"]:GetChildChecked("Texture", false):SetBackgroundTexture(buffInfo.texture)
		end
		Tracked[t][naming]["Widget"]:SetBackgroundColor({r=0;b=0;g=0;a=.5})
		local msg = ""
		if object.GetName(p.unitId)then
			if string.len(userMods.FromWString(object.GetName(p.unitId))) >= 15 then
				msg = msg..string.sub(userMods.FromWString(object.GetName(p.unitId)), 0, 15).."..."
			else
				msg = msg..userMods.FromWString(object.GetName(p.unitId))
			end
		end
		Tracked[t][naming]["Widget"]:GetChildChecked("Name", false):SetFormat(userMods.ToWString("<html fontsize='13' outline='1'><r name='name'/></html>"))
		Tracked[t][naming]["Widget"]:GetChildChecked("Name", false):SetVal("name", userMods.ToWString(msg))
		if buffInfo.remainingMs > 0 then
			local times = math.ceil(buffInfo.remainingMs/1000)
			TimerTracker[t][naming] = times
			local time2 = buffInfo.remainingMs
			local timetype = "s"
			if times >= 60 then
				times = math.ceil( times / 60 )
				timetype = "m"
				if times >= 60 then
					times = math.ceil( times / 60 )
					timetype = "h"
				end
			end
			Tracked[t][naming]["Widget"]:GetChildChecked("BuffTimer", false):SetFormat(userMods.ToWString("<html fontsize='13' outline='1'><r name='stack'/> <r name='timer'/></html>"))
			if buffInfo.stackCount and buffInfo.stackCount > 1 then
				Tracked[t][naming]["Widget"]:GetChildChecked("BuffTimer", false):SetVal("stack", userMods.ToWString(""..buffInfo.stackCount))
			end
			Tracked[t][naming]["Widget"]:GetChildChecked("BuffTimer", false):SetVal("timer", userMods.ToWString(times.." "..timetype))
			local fromPlacement = Tracked[t][naming]["Widget"]:GetChildChecked("Bar", false):GetPlacementPlain()
			fromPlacement.sizeX = 180
			local toPlacement = Tracked[t][naming]["Widget"]:GetChildChecked("Bar", false):GetPlacementPlain()
			toPlacement.sizeX = 0
			Tracked[t][naming]["Widget"]:GetChildChecked("Bar", false):PlayResizeEffect( fromPlacement, toPlacement, time2, EA_MONOTONOUS_INCREASE)
			Tracked[t][naming]["Widget"]:Show(true)
		elseif buffInfo.stackCount and buffInfo.stackCount > 1 then
			Tracked[t][naming]["Widget"]:GetChildChecked("BuffTimer", false):SetFormat(userMods.ToWString("<html fontsize='13' outline='1'><r name='timer'/></html>"))
			Tracked[t][naming]["Widget"]:GetChildChecked("BuffTimer", false):SetVal("timer", userMods.ToWString(""..buffInfo.stackCount))
			local P = Tracked[t][naming]["Widget"]:GetChildChecked("Bar", false):GetPlacementPlain()
			P.sizeX = buffInfo.stackCount/buffInfo.stackLimit*180
			Tracked[t][naming]["Widget"]:GetChildChecked("Bar", false):SetPlacementPlain(P)
			Tracked[t][naming]["Widget"]:Show(true)
		end
		TrackSort()
	end
end

function TrackCreate(p, w, aInfo, nameCheck, blah)
	if w then
		w:FinishFadeEffect()
		w:SetFade(1)
		if p.texture then
			w:GetChildChecked("Texture", false):SetBackgroundTexture(p.texture)
		end
		local spellName = userMods.FromWString(p.name)
		if string.find(spellName, "<html>") then
			spellName = string.sub(spellName, 7, string.len(spellName)-7)
		end
		w:SetBackgroundColor({r=0;b=0;g=0;a=.5})
		local msg = ""
		if p.name then
			msg = msg..spellName
		end
		w:GetChildChecked("Name", false):SetFormat(userMods.ToWString("<html fontsize='13' outline='1'><r name='name'/></html>"))
		w:GetChildChecked("Name", false):SetVal("name", userMods.ToWString(msg))
		local times = aInfo[spellName]
		if userMods.FromWString(nameCheck) == "Nogrom" and spellName == "Seismic Attack" then
			local t
			for i, v in Tracked do
				if v.Name == userMods.FromWString(nameCheck) then
					t = i
				end
			end
			for i = 0, unit.GetBuffCount(Tracked[t]["ID"]) - 1 do
				local buffInfo = unit.GetBuff(Tracked[t]["ID"], i)
				if buffInfo.isStackable and buffInfo.isPositive then
					for i = 0, buffInfo.stackCount - 1 do
						times = times*.9
					end
				end
			end
		end		
		if times then
			local time2 = math.ceil(times*1000)
			times = math.ceil(times * 10)/10
			TimerTracker[blah][spellName] = times
			local timetype = "s"
			if times >= 60 then
				times = math.ceil( times / 60 )
				timetype = "m"
				if times >= 60 then
					times = math.ceil( times / 60 )
					timetype = "h"
				end
			end
			w:GetChildChecked("BuffTimer", false):SetFormat(userMods.ToWString("<html fontsize='13' outline='1'><r name='timer'/></html>"))
			w:GetChildChecked("BuffTimer", false):SetVal("timer", userMods.ToWString(times.." "..timetype))
			local fromPlacement = w:GetChildChecked("Bar", false):GetPlacementPlain()
			fromPlacement.sizeX = 180
			local toPlacement = w:GetChildChecked("Bar", false):GetPlacementPlain()
			toPlacement.sizeX = 0
			w:GetChildChecked("Bar", false):PlayResizeEffect( fromPlacement, toPlacement, time2, EA_MONOTONOUS_INCREASE)
			w:Show(true)
		end
	end
end

function TrackSort()
	local num = 0
	for i, v in Tracked do
		if v then
			for j, x in v do
				if j ~= "Time" and j ~= "ID" and j ~= "Name" then
					if x["Widget"] and x["Widget"]:IsVisible() then
						local P = x["Widget"]:GetPlacementPlain()
						P.posY = P.sizeY * num
						x["Widget"]:SetPlacementPlain(P)
						num = num + 1
					end
				end
			end
		end
	end
end

function SecondTimer()
	for i, v in Tracked do
		if v then
			if (object.IsExist(v["ID"]) == false and unit.IsInCombat(avatar.GetId()) == false) or (object.IsExist(v["ID"]) and (object.IsDead(v["ID"]) == true or unit.IsInCombat(v["ID"]) == false)) then
				if TRACK == true and BossTable[v["Name"]] == nil or FORCETRACK == true then
					common.LogInfo("Blah", v["Name"])
					for j, x in v do
						if j ~= "Time" and j ~= "ID" and j ~= "Name" then
							if TRACK == true then
								if x["SpellType"] ~= "PlayerDebuff" and x["SpellType"] ~= "Health" and x["SpellType"] ~= "Mana" then
									common.LogInfo("Blah", j..": "..x["SpellType"].." used at: "..x["TimeUsed"])
								end
							end
						end
					end
				end
				for j, x in v do
					if j ~= "Time" and j ~= "ID" and j ~= "Name" then
						if x["Widget"] then
							x["Widget"]:DestroyWidget()
							x["Widget"] = nil
						end
					end
				end
				local newLoc
				for j, x in Tracked do
					if x then
						if x["ID"] == v["ID"] then
							newLoc = j
						end
					end
				end
				table.remove(Tracked, newLoc)
				table.remove(TimerTracker, newLoc)
				if StackTracker[v.Name] then
					StackTracker[v.Name] = nil
				end
			elseif object.IsExist(v["ID"]) and unit.IsInCombat(v["ID"]) or unit.IsInCombat(avatar.GetId()) then
				if not v["Time"] then
					v["Time"] = 0
				end
				v["Time"] = v["Time"] + 1
				for j, x in v do
					if j ~= "Time" and j ~= "ID" and j ~= "Name" then
						if x["Widget"] and TimerTracker[i][j] and TimerTracker[i][j] > 0 then
							if x["BuffName"] and object.IsExist(x["UnitId"]) then
								local dnExist = true
								for i = 0, unit.GetBuffCount(x["UnitId"]) - 1 do
									local buffInfo = unit.GetBuff(x["UnitId"], i)
									local buffName = userMods.FromWString(buffInfo.name)
									if string.find(buffName, "<html>") then
										buffName = string.sub(buffName, 7, string.len(buffName)-7)
									end
									if buffName == x["BuffName"] then
										dnExist = false
									end
								end
								if dnExist then
									x["Widget"]:Show(false)
								end
							end
							TimerTracker[i][j] = TimerTracker[i][j] - 1
							local times = TimerTracker[i][j]
							local timetype = "s"
							if times >= 60 then
								times = math.ceil( times / 60 )
								timetype = "m"
								if times >= 60 then
									times = math.ceil( times / 60 )
									timetype = "h"
								end
							end
							x["Widget"]:GetChildChecked("BuffTimer", false):SetVal("timer", userMods.ToWString(times.." "..timetype))
						end
					end
				end
			end
		end
	end
end

function EffectFinished(p)
	if p.effectType == ET_FADE then
		for i, v in Tracked do
			for j, x in v do
				if j ~= "Time" and j ~= "ID" and j ~= "Name" then
					if p.wtOwner:IsEqual(x["Widget"]) then
						p.wtOwner:Show(false)
						TrackSort()
					elseif p.wtOwner:GetParent() ~= nil and p.wtOwner:GetParent():IsEqual(x["Widget"]) then
						p.wtOwner:GetParent():Show(false)
						TrackSort()
					end
				end
			end
		end
	elseif p.effectType == ET_RESIZE then
		for i, v in Tracked do
			for j, x in v do
				if j ~= "Time" and j ~= "ID" and j ~= "Name" then
					if p.wtOwner:IsEqual(x["Widget"]) then
						p.wtOwner:PlayFadeEffect(1.0, 0.0, 1000, EA_SYMMETRIC_FLASH)
					elseif p.wtOwner:GetParent() ~= nil and p.wtOwner:GetParent():IsEqual(x["Widget"]) then
						p.wtOwner:GetParent():PlayFadeEffect(1.0, 0.0, 1000, EA_SYMMETRIC_FLASH)
					end
				end
			end
		end
	end
end

function Slash(p)
	if userMods.FromWString(p.text) == "/btdnd" then
		DragPanel:Show(not DragPanel:IsVisible())
	elseif userMods.FromWString(p.text) == "/bttrack on" then
		TRACK = true
	elseif userMods.FromWString(p.text) == "/bttrack off" then
		TRACK = false
	elseif userMods.FromWString(p.text) == "/bttforce" then
		FORCETRACK = true
	end
end

function ClickToTarget(r)
	if object.IsExist(tonumber(r.sender)) then
		avatar.SelectTarget(tonumber(r.sender))
	end
end

--ADDON MANAGER COOPERATION
function OnEventAMMemUsageRequest() --MEMORY
	userMods.SendEvent( "U_EVENT_ADDON_MEM_USAGE_RESPONSE", { sender = common.GetAddonName(), memUsage = gcinfo() } )
end

function OnScriptToggleUI( params ) --UI TOGGLE
	mainForm:Show( params.visible )
end
--------------------------------------------------------------------------------
-- INITIALIZATION
--------------------------------------------------------------------------------
function Init()
	common.RegisterEventHandler(Tracker, "EVENT_UNIT_DAMAGE_RECEIVED")
	common.RegisterEventHandler(BuffTrackingAssist, "EVENT_UNIT_BUFF_ADDED")
	common.RegisterEventHandler(BuffTrackingAssist, "EVENT_OBJECT_BUFF_ADDED")
	common.RegisterEventHandler(BCTrack, "EVENT_UNIT_BUFFS_ELEMENT_CHANGED")
	common.RegisterEventHandler(BCTrack, "EVENT_OBJECT_BUFFS_ELEMENT_CHANGED")
	common.RegisterEventHandler(HealthTrack, "EVENT_UNIT_HEALTH_PERCENTAGE_CHANGED")
	common.RegisterEventHandler(ManaTrack, "EVENT_UNIT_MANA_PERCENTAGE_CHANGED")
	common.RegisterEventHandler(SecondTimer, "EVENT_SECOND_TIMER")
	common.RegisterEventHandler(Slash, "EVENT_UNKNOWN_SLASH_COMMAND")
	common.RegisterEventHandler(TrackSpawn, "EVENT_UNIT_SPAWNED")
	common.RegisterEventHandler(EffectFinished, "EVENT_EFFECT_FINISHED")
	common.RegisterReactionHandler(ClickToTarget, "C2T")
-- ADDON MANAGER EVENTS (MEMORY, DND TOGGLE, UI TOGGLE)
	common.RegisterEventHandler( OnEventAMMemUsageRequest, "U_EVENT_ADDON_MEM_USAGE_REQUEST" )
	common.RegisterEventHandler( OnScriptToggleUI, "SCRIPT_TOGGLE_UI" )
	DnD:Init(612, DragPanel, Container, true)
	DragPanel:SetBackgroundColor({r=0;b=0;g=0;a=.4})
	DragPanel:Show(false)
end
--------------------------------------------------------------------------------
common.RegisterEventHandler( Init, "EVENT_AVATAR_CREATED")
if avatar.IsExist() then
Init()
end
--------------------------------------------------------------------------------