--------------------------------------------
--
-- SMART - FocusToT, FocusToTT
--
--------------------------------------------
SMART_FOCUSTOT_ENABLE = true;

function SmartFocusToT_BuffDebuffInitial()
	-- BUFF initial
	if SMART_TOT_Config.ChkBuff < SMART_TOT_Config.MaxBuff then
		for i = (SMART_TOT_Config.ChkBuff+1), SMART_TOT_Config.MaxBuff do
				getglobal("SmartFocusToTFrameBuff"..i):Hide();
				getglobal("SmartFocusToTTFrameBuff"..i):Hide();
		end
	end
	-- DEBUFF initial
	if SMART_TOT_Config.ChkDebuff < SMART_TOT_Config.MaxDebuff then
		for i = (SMART_TOT_Config.ChkDebuff+1), SMART_TOT_Config.MaxDebuff do
			getglobal("SmartFocusToTFrameDebuff"..i):Hide();
			getglobal("SmartFocusToTTFrameDebuff"..i):Hide();
		end
	end
end

function SmartFocusToT_BuffDebuffLayout()
	if SMART_TOT_Config.buffswap then
		SmartFocusToTFrameDebuff1:ClearAllPoints();
		SmartFocusToTFrameDebuff1:SetPoint("BOTTOMLEFT", "SmartFocusToTFrame", "BOTTOMLEFT", 50, -15);
		SmartFocusToTFrameDebuff9:ClearAllPoints();
		SmartFocusToTFrameDebuff9:SetPoint("LEFT", "SmartFocusToTFrameDebuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartFocusToTFrameBuff1:ClearAllPoints();
			SmartFocusToTFrameBuff1:SetPoint("LEFT", "SmartFocusToTFrame", "LEFT", 162, 0);
		else
			SmartFocusToTFrameBuff1:ClearAllPoints();
			SmartFocusToTFrameBuff1:SetPoint("LEFT", "SmartFocusToTFrame", "LEFT", 100, 0);
		end
		SmartFocusToTFrameBuff9:ClearAllPoints();
		SmartFocusToTFrameBuff9:SetPoint("TOPLEFT", "SmartFocusToTFrameBuff1", "BOTTOMLEFT", 0, -2);

		SmartFocusToTTFrameDebuff1:ClearAllPoints();
		SmartFocusToTTFrameDebuff1:SetPoint("BOTTOMLEFT", "SmartFocusToTTFrame", "BOTTOMLEFT", 50, -15);
		SmartFocusToTTFrameDebuff9:ClearAllPoints();
		SmartFocusToTTFrameDebuff9:SetPoint("LEFT", "SmartFocusToTTFrameDebuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartFocusToTTFrameBuff1:ClearAllPoints();
			SmartFocusToTTFrameBuff1:SetPoint("LEFT", "SmartFocusToTTFrame", "LEFT", 162, 0);
		else
			SmartFocusToTTFrameBuff1:ClearAllPoints();
			SmartFocusToTTFrameBuff1:SetPoint("LEFT", "SmartFocusToTTFrame", "LEFT", 100, 0);
		end
		SmartFocusToTTFrameBuff9:ClearAllPoints();
		SmartFocusToTTFrameBuff9:SetPoint("TOPLEFT", "SmartFocusToTTFrameBuff1", "BOTTOMLEFT", 0, -2);
	else
		SmartFocusToTFrameBuff1:ClearAllPoints();
		SmartFocusToTFrameBuff1:SetPoint("BOTTOMLEFT", "SmartFocusToTFrame", "BOTTOMLEFT", 50, -15);
		SmartFocusToTFrameBuff9:ClearAllPoints();
		SmartFocusToTFrameBuff9:SetPoint("LEFT", "SmartFocusToTFrameBuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartFocusToTFrameDebuff1:ClearAllPoints();
			SmartFocusToTFrameDebuff1:SetPoint("LEFT", "SmartFocusToTFrame", "LEFT", 162, 0);
		else
			SmartFocusToTFrameDebuff1:ClearAllPoints();
			SmartFocusToTFrameDebuff1:SetPoint("LEFT", "SmartFocusToTFrame", "LEFT", 100, 0);
		end
		SmartFocusToTFrameDebuff9:ClearAllPoints();
		SmartFocusToTFrameDebuff9:SetPoint("TOPLEFT", "SmartFocusToTFrameDebuff1", "BOTTOMLEFT", 0, -2);

		SmartFocusToTTFrameBuff1:ClearAllPoints();
		SmartFocusToTTFrameBuff1:SetPoint("BOTTOMLEFT", "SmartFocusToTTFrame", "BOTTOMLEFT", 50, -15);
		SmartFocusToTTFrameBuff9:ClearAllPoints();
		SmartFocusToTTFrameBuff9:SetPoint("LEFT", "SmartFocusToTTFrameBuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartFocusToTTFrameDebuff1:ClearAllPoints();
			SmartFocusToTTFrameDebuff1:SetPoint("LEFT", "SmartFocusToTTFrame", "LEFT", 162, 0);
		else
			SmartFocusToTTFrameDebuff1:ClearAllPoints();
			SmartFocusToTTFrameDebuff1:SetPoint("LEFT", "SmartFocusToTTFrame", "LEFT", 100, 0);
		end
		SmartFocusToTTFrameDebuff9:ClearAllPoints();
		SmartFocusToTTFrameDebuff9:SetPoint("TOPLEFT", "SmartFocusToTTFrameDebuff1", "BOTTOMLEFT", 0, -2);
	end
end

function SmartFocusToT_ResetPosition()
	SmartFocusToTFrameResetPosition();
	SmartFocusToTTFrameResetPosition();
end

function SmartFocusToTFrameResetPosition()
	if SMART_TOT_Config.WoWToTOnOff then
		SmartFocusToTFrame:ClearAllPoints();
		SmartFocusToTFrame:SetPoint("TOPLEFT", "SmartFocusFrame", "BOTTOMRIGHT", -128, 35);
		SmartFocusToTFrame:SetUserPlaced(false);
	else
		SmartFocusToTFrame:ClearAllPoints();
		SmartFocusToTFrame:SetPoint("LEFT", "SmartFocusFrame", "RIGHT", 0, 0);
		SmartFocusToTFrame:SetUserPlaced(true);
	end
end

function SmartFocusToTTFrameResetPosition()
	if SMART_TOT_Config.WoWToTOnOff then
		SmartFocusToTTFrame:ClearAllPoints();
		SmartFocusToTTFrame:SetPoint("TOPLEFT", "SmartFocusFrame", "BOTTOMRIGHT", -128, -15);
		SmartFocusToTTFrame:SetUserPlaced(false);
	else
		SmartFocusToTTFrame:ClearAllPoints();
		SmartFocusToTTFrame:SetPoint("LEFT", "SmartFocusFrame", "RIGHT", 0, -50);
		SmartFocusToTTFrame:SetUserPlaced(true);
	end
end

function CheckWoWFocusToTOnOff()
	if SMART_TOT_Config.WoWToTOnOff then
		SmartFocusToTFrame.lockframe = true;
	else
		SmartFocusToTFrame.lockframe = false;
	end
	SmartFocusToTTFrame.lockframe = false;
	if SMART_TOT_FIXED == nil then
		SMART_TOT_FIXED = SMART_TOT_Config.WoWToTOnOff;
	else
		if SMART_TOT_FIXED ~= SMART_TOT_Config.WoWToTOnOff then
			SMART_TOT_FIXED = SMART_TOT_Config.WoWToTOnOff;
			SmartFocusToT_ResetPosition();
		end
	end
end

function SmartFocusToT_RefreshBuffs(smartFrame, chkBuff, chkDebuff)
	local smartUnit = smartFrame.unit;
-- Buff Refresh Check
	local idxBuff = 0;
	if SMART_TOT_Config.buff and (chkBuff > 0) then
		local buffImage, buffButton, buffIcon, buffCount, buffStack;
		for i=1, chkBuff do
			_, _, buffImage, buffStack, _, _ = UnitBuff(smartUnit, i);
			if ( buffImage ) then
				idxBuff = idxBuff + 1;
				buffButton = getglobal(smartFrame:GetName().."Buff"..idxBuff);
				buffIcon = getglobal(smartFrame:GetName().."Buff"..idxBuff.."Icon");
				buffCount = getglobal(smartFrame:GetName().."Buff"..idxBuff.."Count");
				buffIcon:SetTexture(buffImage);
				buffButton:SetID(idxBuff);
				buffButton:Show();
				if ( buffStack > 1 ) then
					buffCount:SetText(buffStack);
					buffCount:Show();
				else
					buffCount:Hide();
				end
			else
				break;
			end
		end
	end
	if idxBuff < smartFrame.numbuffs then
		for i = (idxBuff+1), smartFrame.numbuffs do
			getglobal(smartFrame:GetName().."Buff"..i):Hide();
		end
	end
	smartFrame.numbuffs = idxBuff;

-- Debuff Refresh Check
	local idxDebuff = 0;
	if SMART_TOT_Config.debuff and (chkDebuff > 0)  then
		local debuffImage, debuffStack, debuffType;
		local debuffButton, debuffIcon, debuffBorder, debuffCount, debuffColor;
		for i=1, chkDebuff do
			_, _, debuffImage, debuffStack, debuffType, _, _ = UnitDebuff(smartUnit, i);
			if ( debuffImage ) then
				idxDebuff = idxDebuff + 1;
				debuffButton = getglobal(smartFrame:GetName().."Debuff"..idxDebuff);
				debuffIcon = getglobal(smartFrame:GetName().."Debuff"..idxDebuff.."Icon");
				debuffBorder = getglobal(smartFrame:GetName().."Debuff"..idxDebuff.."Border");
				debuffCount = getglobal(smartFrame:GetName().."Debuff"..idxDebuff.."Count");
				if ( debuffType ) then
					debuffColor = DebuffTypeColor[debuffType];
				else
					debuffColor = DebuffTypeColor["none"];
				end
				debuffBorder:SetVertexColor(debuffColor.r, debuffColor.g, debuffColor.b);
				if ( debuffStack > 1 ) then
					debuffCount:SetText(debuffStack);
					debuffCount:Show();
				else
					debuffCount:Hide();
				end
				debuffIcon:SetTexture(debuffImage);
				debuffButton:SetAlpha(1);
				debuffButton:SetID(idxDebuff);
				debuffButton:Show();
			else
				break;
			end
		end
	end
	if idxDebuff < smartFrame.numdebuffs then
		for i = (idxDebuff+1), smartFrame.numdebuffs do
			getglobal(smartFrame:GetName().."Debuff"..i):Hide();
		end
	end
	smartFrame.numdebuffs = idxDebuff;
end

function SmartFocusToT_HealthUpdate(smartFrame)
	if SMART_TOT_Config.statusbartext then
		if SMART_TOT_Config.percent then
			smartFrame.healthtext:Hide();
			SMART_HealthPercent(smartFrame.healthpercent,smartFrame.unit);
		else
			smartFrame.healthpercent:Hide();
			SMART_HealthUpdate(smartFrame.healthtext,smartFrame.unit);
			-- 몹헬쓰로 보기를 원할 경우.. 아래 주석부분을 활성화 (윗줄은 주석처리로...)
			-- SMART_MH3HealthUpdate(smartFrame.healthtext,smartFrame.unit);
		end
	else
		smartFrame.healthtext:Hide();
		smartFrame.healthpercent:Hide();
	end
	if SMART_TOT_Config.loss then
		if SMART_TOT_Config.losstopercent then
			SMART_HealthPercent(smartFrame.healthloss,smartFrame.unit);
		else
			SMART_HealthLoss(smartFrame.healthloss,smartFrame.unit);
		end
	else
		smartFrame.healthloss:Hide();
	end
end

function SmartFocusToT_ManaUpdate(smartFrame)
	if SMART_TOT_Config.statusbartext then
		if SMART_TOT_Config.percent then
			smartFrame.manatext:Hide();
			SMART_ManaPercent(smartFrame.manapercent,smartFrame.unit);
		else
			smartFrame.manapercent:Hide();
			SMART_ManaUpdate(smartFrame.manatext,smartFrame.unit);
		end
	else
		smartFrame.manatext:Hide();
		smartFrame.manapercent:Hide();
	end
end

function SmartFocusToT_RaidTargetCheck(smartFrame)
	if SMART_TOT_Config.raid and UnitExists(smartFrame.unit) then
		SMART_RaidTargetCheck(smartFrame.raidtargeticon,smartFrame.unit);
	else
		smartFrame.raidtargeticon:Hide();
	end
end

function SmartFocusToT_Update(smartFrame)
	if not smartFrame then
		smartFrame = this;
	end
	if not UnitExists(smartFrame.unit) then
		smartFrame.current = nil;
		return;
	end
--	if UnitIsUnit("target","player") then
--		if smartFrame.enable and smartFrame.show then
--			SmartFocusToT_RegisterUnitWatch(smartFrame,false);
--		end
--		smartFrame.current = nil;
--		return;
--	else
--		if smartFrame.enable and not smartFrame.show then
--			SmartFocusToT_RegisterUnitWatch(smartFrame,true);
--		end
--	end
	if smartFrame.show then
		smartFrame.current = UnitName(smartFrame.unit);
		SetPortraitTexture(smartFrame.portrait, smartFrame.unit);
		SMART_ColorName(smartFrame.name, smartFrame.unit);
		SMART_ClassIcons(smartFrame.classicon, smartFrame.unit);
		SMART_CheckLevel(smartFrame.leveltext, smartFrame.unit);
		SMART_CheckElite(smartFrame.elitetext, smartFrame.unit);
		SMART_FactionGroupCheck(smartFrame.factiongroup, smartFrame.unit);
		SmartFocusToT_RefreshBuffs(smartFrame, SMART_TOT_Config.ChkBuff, SMART_TOT_Config.ChkDebuff);
		SmartFocusToT_RaidTargetCheck(smartFrame);
--		UnitFrame_UpdateManaType(smartFrame);
		UnitFrameHealthBar_Update(smartFrame.healthbar,smartFrame.unit);
		UnitFrameManaBar_Update(smartFrame.manabar,smartFrame.unit);
		SmartFocusToT_StatusCheck(smartFrame);
		SmartFocusToT_HealthUpdate(smartFrame);
		SmartFocusToT_ManaUpdate(smartFrame);
	end
end

function SmartFocusToT_OnUpdate(elapsed)
	this.timeleft = this.timeleft - elapsed;
	if this.timeleft < 0 then
		this.timeleft = 0.25;
		if UnitExists(this.unit) and this:IsVisible() then
				if (this.current == nil) or (this.current ~= UnitName(this.unit)) then
					this.current = UnitName(this.unit);
					SetPortraitTexture(this.portrait, this.unit);
					SMART_ColorName(this.name, this.unit);
					SMART_ClassIcons(this.classicon, this.unit);
					SMART_CheckLevel(this.leveltext, this.unit);
					SMART_CheckElite(this.elitetext, this.unit);
					SMART_FactionGroupCheck(this.factiongroup, this.unit);
				end
--				UnitFrame_UpdateManaType(this);
				UnitFrameHealthBar_Update(this.healthbar,this.unit);
				UnitFrameManaBar_Update(this.manabar,this.unit);
				SmartFocusToT_StatusCheck(this);
				SmartFocusToT_HealthUpdate(this);
				SmartFocusToT_ManaUpdate(this);
				SmartFocusToT_RefreshBuffs(this, SMART_TOT_Config.ChkBuff, SMART_TOT_Config.ChkDebuff);
		end
	end
end

function SmartFocusToT_OnHide()
	this.current = nil;
--	CloseDropDownMenus();
end

function SmartFocusToT_StatusCheck(smartFrame)
	if UnitIsConnected(smartFrame.unit) then
		if UnitIsDead(smartFrame.unit) then
			local deadStatus = "죽음";
			if SMART_CheckFeign(smartFrame.unit) then
				deadStatus = "죽은척하기";
			end
			smartFrame.portrait:SetVertexColor(0.35, 0.35, 0.35);
			smartFrame.deadtext:SetText(deadStatus);
			smartFrame.deadtext:Show();
		elseif UnitIsGhost(smartFrame.unit) then
			smartFrame.portrait:SetVertexColor(0.2, 0.2, 0.75);
			smartFrame.deadtext:SetText("유령");
			smartFrame.deadtext:Show();
		else
			SMART_PortraitColor(smartFrame.portrait,smartFrame.unit);
			smartFrame.deadtext:Hide();
		end
	else
		smartFrame.portrait:SetVertexColor(0.35, 0.35, 0.35);
		smartFrame.deadtext:SetText("오프라인");
		smartFrame.deadtext:Show();
	end
end

function SmartFocusToT_HealthCheck()
	local unitMinHP, unitMaxHP, unitCurrHP;
	unitHPMin, unitHPMax = this:GetMinMaxValues();
	unitCurrHP = this:GetValue();
	this:GetParent().unitHPPercent = unitCurrHP / unitHPMax;
end

function SmartFocusToT_StatusUpdate(elapsed)
	if UnitExists(this.unit) and this:IsVisible() then
		local alpha = 255;
		local counter = this.statusCounter + elapsed;
		local sign    = this.statusSign;
	
		if ( counter > 0.5 ) then
			sign = -sign;
			this.statusSign = sign;
		end
		counter = mod(counter, 0.5);
		this.statusCounter = counter;
	
		if ( sign == 1 ) then
			alpha = (127  + (counter * 256)) / 255;
		else
			alpha = (255 - (counter * 256)) / 255;
		end
		
		if UnitExists(this.unit) and UnitAffectingCombat(this.unit) then
			if not this.statusflash:IsShown() then
				this.statusflash:Show();
			end
			this.statusflash:SetAlpha(alpha);
		else
			if this.statusflash:IsShown() then
				this.statusflash:Hide();
			end
		end
	
		if UnitIsGhost("focus") or ( (this.unitHPPercent > 0) and (this.unitHPPercent < 0.2) ) then
			this.portrait:SetAlpha(alpha);
		else
			this.portrait:SetAlpha(1);
		end
	end
end

function SmartFocusToT_RegisterUnitWatch(smartFrame,smartCheck)
	if smartCheck then
		RegisterUnitWatch(smartFrame);
		smartFrame.show = true;
	else
		UnregisterUnitWatch(smartFrame);
		smartFrame.show = false;
		if smartFrame:IsShown() then
			smartFrame:Hide();
		end
	end
end
--------------------------------------------
--
-- Target of Target Funtions
--
--------------------------------------------
function SmartFocusToTFrame_OnLoad()

	this.timeleft = 0.25;
	this.statusCounter = 0;
	this.statusSign = -1;
	this.unitHPPercent = 1;

	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("PLAYER_FOCUS_CHANGED");
	this:RegisterEvent("UNIT_TARGET");
	this:RegisterEvent("UNIT_CLASSIFICATION_CHANGED");
	this:RegisterEvent("UNIT_LEVEL");
	this:RegisterEvent("UNIT_FACTION");
--	this:RegisterEvent("UNIT_AURA");
	this:RegisterEvent("RAID_TARGET_UPDATE");

	UnitFrame_Initialize("focustarget", SmartFocusToTName, SmartFocusToTPortrait, SmartFocusToTHealthBar, SmartFocusToTHealthText, SmartFocusToTManaBar, SmartFocusToTManaText);

	this.raidtargeticon = SmartFocusToTRaidTargetIcon;
	this.healthloss = SmartFocusToTHealthLoss;
	this.healthtext = SmartFocusToTHealthBarText;
	this.manatext = SmartFocusToTManaBarText;
	this.healthpercent = SmartFocusToTHealthPercent;
	this.manapercent = SmartFocusToTManaPercent;
	this.factiongroup = SmartFocusToTFactionGroupIcon;
	this.classicon = SmartFocusToTClassIcon;
	this.leveltext = SmartFocusToTLevelText;
	this.elitetext = SmartFocusToTEliteText;
	this.deadtext = SmartFocusToTDeadText;

	this.current = nil;
	this.currentportrait = nil;
	
	this.numbuffs = 0;
	this.numdebuffs = 0;
	
	this.lockframe = true;

	SmartFocusToTTextureFrame:CreateTexture("SmartFocusToTStatusTexture","ARTWORK");
	SmartFocusToTStatusTexture:SetTexture("Interface\\AddOns\\SmartUnitFrame\\CircleFlash.tga");
	SmartFocusToTStatusTexture:SetWidth(this.portrait:GetWidth());
	SmartFocusToTStatusTexture:SetHeight(this.portrait:GetHeight());
	SmartFocusToTStatusTexture:SetTexCoordModifiesRect(false);
	SmartFocusToTStatusTexture:SetTexCoord(0,1,0,1);
	SmartFocusToTStatusTexture:SetPoint("CENTER",this.portrait,"CENTER",0,0);
	SmartFocusToTStatusTexture:SetBlendMode("ADD");
	SmartFocusToTStatusTexture:SetVertexColor(1,0.5,0);
	
	this.statusflash = SmartFocusToTStatusTexture;

	this:SetFrameLevel(SmartFocusFrame:GetFrameLevel()+2);
	local showmenu = function()
		ToggleDropDownMenu(1, nil, getglobal(this:GetName().."DropDown"), this:GetName(), 60, 10);
	end
	SecureUnitButton_OnLoad(this, this.unit, showmenu);
end

function SmartFocusToTFrameDropDown_OnLoad()
	UIDropDownMenu_Initialize(this, SmartFocusToTFrameDropDown_Initialize, "MENU");
end

function SmartFocusToTFrameDropDown_Initialize()
	local menu;
	local name;
	local id = nil;
	if ( UnitIsUnit("focustarget", "player") ) then
		menu = "SELF";
	elseif ( UnitIsUnit("focustarget", "pet") ) then
		menu = "PET";
	elseif ( UnitIsPlayer("focustarget") ) then
		id = UnitInRaid("focustarget");
		if ( id ) then
			menu = "RAID_PLAYER";
		elseif ( UnitInParty("focustarget") ) then
			menu = "PARTY";
		else
			menu = "PLAYER";
		end
	else
		menu = "RAID_TARGET_ICON";
		name = RAID_TARGET_ICON;
	end
	if ( menu ) then
		UnitPopup_ShowMenu(SmartFocusToTFrameDropDown, menu, "focustarget", name, id);
	end
end

function SmartFocusToTFrame_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		CheckWoWFocusToTOnOff();
		this.enable = SMART_TOT_Config.enable;
		SmartFocusToT_RegisterUnitWatch(this,this.enable);
	end
	if SMART_TOT_Config.enable then
		if (event == "PLAYER_FOCUS_CHANGED") then
			SmartFocusToT_Update(this);
		elseif (event == "UNIT_TARGET") then
			if (arg1 == "focus") then
				SmartFocusToT_Update(this);
			end
		end
		if UnitExists(this.unit) then
			if (event == "UNIT_NAME_UPDATE") or (event == "UNIT_CLASSIFICATION_CHANGED") or (event == "UNIT_LEVEL") then
				if (arg1 == this.unit) then
					SMART_ColorName(this.name, this.unit);
					SMART_CheckLevel(this.leveltext, this.unit);
					SMART_FactionGroupCheck(this.factiongroup, this.unit);
				end
			elseif (event == "UNIT_FACTION") then
				if (arg1 == this.unit) or (arg1 == "player")then
					SMART_ColorName(this.name, this.unit);
					SMART_CheckLevel(this.leveltext, this.unit);
					SMART_FactionGroupCheck(this.factiongroup, this.unit);
				end
			elseif (event == "UNIT_PORTRAIT_UPDATE") then
				SetPortraitTexture(this.portrait, this.unit);
			elseif (event == "RAID_TARGET_UPDATE") then
				SmartFocusToT_RaidTargetCheck(this);
			end
		end
	end
end

--------------------------------------------
--
-- Target of Target's Target Funtions
--
--------------------------------------------
function SmartFocusToTTFrame_OnLoad()
	this.timeleft = 0.25;
	this.statusCounter = 0;
	this.statusSign = -1;
	this.unitHPPercent = 1;

	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("PLAYER_FOCUS_CHANGED");
	this:RegisterEvent("UNIT_TARGET");
	this:RegisterEvent("UNIT_CLASSIFICATION_CHANGED");
	this:RegisterEvent("UNIT_LEVEL");
	this:RegisterEvent("UNIT_FACTION");
--	this:RegisterEvent("UNIT_AURA");
	this:RegisterEvent("RAID_TARGET_UPDATE");

	UnitFrame_Initialize("focustargettarget", SmartFocusToTTName, SmartFocusToTTPortrait, SmartFocusToTTHealthBar, SmartFocusToTTHealthText, SmartFocusToTTManaBar, SmartFocusToTTManaText);

	this.raidtargeticon = SmartFocusToTTRaidTargetIcon;
	this.healthloss = SmartFocusToTTHealthLoss;
	this.healthtext = SmartFocusToTTHealthBarText;
	this.manatext = SmartFocusToTTManaBarText;
	this.healthpercent = SmartFocusToTTHealthPercent;
	this.manapercent = SmartFocusToTTManaPercent;
	this.factiongroup = SmartFocusToTTFactionGroupIcon;
	this.classicon = SmartFocusToTTClassIcon;
	this.leveltext = SmartFocusToTTLevelText;
	this.elitetext = SmartFocusToTTEliteText;
	this.deadtext = SmartFocusToTTDeadText;

	this.current = nil;
	this.currentportrait = nil;

	this.numbuffs = 0;
	this.numdebuffs = 0;

	this.lockframe = true;

	SmartFocusToTTTextureFrame:CreateTexture("SmartFocusToTTStatusTexture","ARTWORK");
	SmartFocusToTTStatusTexture:SetTexture("Interface\\AddOns\\SmartUnitFrame\\CircleFlash.tga");
	SmartFocusToTTStatusTexture:SetWidth(this.portrait:GetWidth());
	SmartFocusToTTStatusTexture:SetHeight(this.portrait:GetHeight());
	SmartFocusToTTStatusTexture:SetTexCoordModifiesRect(false);
	SmartFocusToTTStatusTexture:SetTexCoord(0,1,0,1);
	SmartFocusToTTStatusTexture:SetPoint("CENTER",this.portrait,"CENTER",0,0);
	SmartFocusToTTStatusTexture:SetBlendMode("ADD");
	SmartFocusToTTStatusTexture:SetVertexColor(1,0.5,0);
	
	this.statusflash = SmartFocusToTTStatusTexture;

	this:SetFrameLevel(SmartFocusFrame:GetFrameLevel()+2);
	local showmenu = function()
		ToggleDropDownMenu(1, nil, getglobal(this:GetName().."DropDown"), this:GetName(), 60, 10);
	end
	SecureUnitButton_OnLoad(this, this.unit, showmenu);
end

function SmartFocusToTTFrameDropDown_OnLoad()
	UIDropDownMenu_Initialize(this, SmartFocusToTTFrameDropDown_Initialize, "MENU");
end

function SmartFocusToTTFrameDropDown_Initialize()
	local menu;
	local name;
	local id = nil;
	if ( UnitIsUnit("focustargettarget", "player") ) then
		menu = "SELF";
	elseif ( UnitIsUnit("focustargettarget", "pet") ) then
		menu = "PET";
	elseif ( UnitIsPlayer("focustargettarget") ) then
		id = UnitInRaid("focustargettarget");
		if ( id ) then
			menu = "RAID_PLAYER";
		elseif ( UnitInParty("focustargettarget") ) then
			menu = "PARTY";
		else
			menu = "PLAYER";
		end
	else
		menu = "RAID_TARGET_ICON";
		name = RAID_TARGET_ICON;
	end
	if ( menu ) then
		UnitPopup_ShowMenu(SmartFocusToTTFrameDropDown, menu, "focustargettarget", name, id);
	end
end

function SmartFocusToTTFrame_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		this.enable = SMART_TOT_Config.enable and SMART_TOT_Config.tott;
		SmartFocusToT_RegisterUnitWatch(this,this.enable);
	end
	if SMART_TOT_Config.tott and SMART_TOT_Config.enable then
		if (event == "PLAYER_FOCUS_CHANGED") then
			SmartFocusToT_Update(this);
		elseif (event == "UNIT_TARGET") then
			if (arg1 == "focus") or (arg1 == "focustarget") then
				SmartFocusToT_Update(this);
			end
		end
		if UnitExists(this.unit) then
			if (event == "UNIT_NAME_UPDATE") or (event == "UNIT_CLASSIFICATION_CHANGED") or (event == "UNIT_LEVEL") then
				if (arg1 == this.unit) then
					SMART_ColorName(this.name, this.unit);
					SMART_CheckLevel(this.leveltext, this.unit);
					SMART_FactionGroupCheck(this.factiongroup, this.unit);
				end
			elseif (event == "UNIT_FACTION") then
				if (arg1 == this.unit) or (arg1 == "player")then
					SMART_ColorName(this.name, this.unit);
					SMART_CheckLevel(this.leveltext, this.unit);
					SMART_FactionGroupCheck(this.factiongroup, this.unit);
				end
			elseif (event == "UNIT_PORTRAIT_UPDATE") then
				SetPortraitTexture(this.portrait, this.unit);
			elseif (event == "RAID_TARGET_UPDATE") then
				SmartFocusToT_RaidTargetCheck(this);
			end
		end
	end
end
