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

function SmartFocusToT_BuffDebuffInitialize()
	-- 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 SmartFocusToT_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 = self;
	end
	if not UnitExists(smartFrame.unit) then
		smartFrame.current = nil;
		return;
	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);
		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(self, elapsed)
	self.timeleft = self.timeleft - elapsed;
	if self.timeleft < 0 then
		self.timeleft = 0.25;
		if UnitExists(self.unit) and self:IsVisible() then
				if (self.current == nil) or (self.current ~= UnitName(self.unit)) then
					self.current = UnitName(self.unit);
					SetPortraitTexture(self.portrait, self.unit);
					SMART_ColorName(self.name, self.unit);
					SMART_ClassIcons(self.classicon, self.unit);
					SMART_CheckLevel(self.leveltext, self.unit);
					SMART_CheckElite(self.elitetext, self.unit);
					SMART_FactionGroupCheck(self.factiongroup, self.unit);
				end
				UnitFrameHealthBar_Update(self.healthbar, self.unit);
				UnitFrameManaBar_Update(self.manabar, self.unit);
				SmartFocusToT_StatusCheck(self);
				SmartFocusToT_HealthUpdate(self);
				SmartFocusToT_ManaUpdate(self);
				SmartFocusToT_RefreshBuffs(self, SMART_TOT_Config.ChkBuff, SMART_TOT_Config.ChkDebuff);
		end
	end
end

function SmartFocusToT_OnHide(self)
	self.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.statustext:SetText(deadStatus);
			smartFrame.statustext:Show();
		elseif UnitIsGhost(smartFrame.unit) then
			smartFrame.portrait:SetVertexColor(0.2, 0.2, 0.75);
			smartFrame.statustext:SetText("유령");
			smartFrame.statustext:Show();
		else
			SMART_PortraitColor(smartFrame.portrait,smartFrame.unit);
			smartFrame.statustext:Hide();
		end
	else
		smartFrame.portrait:SetVertexColor(0.35, 0.35, 0.35);
		smartFrame.statustext:SetText("오프라인");
		smartFrame.statustext:Show();
	end
end

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

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

	self:RegisterEvent("VARIABLES_LOADED");
	self:RegisterEvent("PLAYER_FOCUS_CHANGED");
	self:RegisterEvent("UNIT_TARGET");
	self:RegisterEvent("UNIT_CLASSIFICATION_CHANGED");
	self:RegisterEvent("UNIT_LEVEL");
	self:RegisterEvent("UNIT_FACTION");
	self:RegisterEvent("RAID_TARGET_UPDATE");

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

	self.raidtargeticon = SmartFocusToTRaidTargetIcon;
	self.healthloss = SmartFocusToTHealthLoss;
	self.healthtext = SmartFocusToTHealthBarText;
	self.manatext = SmartFocusToTManaBarText;
	self.healthpercent = SmartFocusToTHealthPercent;
	self.manapercent = SmartFocusToTManaPercent;
	self.factiongroup = SmartFocusToTFactionGroupIcon;
	self.classicon = SmartFocusToTClassIcon;
	self.leveltext = SmartFocusToTLevelText;
	self.elitetext = SmartFocusToTEliteText;
	self.statustext = SmartFocusToTStatusText;

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

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

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

function SmartFocusToTFrameDropDown_OnLoad(self)
	UIDropDownMenu_Initialize(self, 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(self, event)
	if (event == "VARIABLES_LOADED") then
		SmartFocusToT_CheckWoWFocusToTOnOff();
		self.enable = SMART_TOT_Config.enable;
		SmartFocusToT_RegisterUnitWatch(self,self.enable);
	end
	if SMART_TOT_Config.enable then
		if (event == "PLAYER_FOCUS_CHANGED") then
			SmartFocusToT_Update(self);
		elseif (event == "UNIT_TARGET") then
			if (arg1 == "focus") then
				SmartFocusToT_Update(self);
			end
		end
		if UnitExists(self.unit) then
			if (event == "UNIT_NAME_UPDATE") or (event == "UNIT_CLASSIFICATION_CHANGED") or (event == "UNIT_LEVEL") then
				if (arg1 == self.unit) then
					SMART_ColorName(self.name, self.unit);
					SMART_CheckLevel(self.leveltext, self.unit);
					SMART_FactionGroupCheck(self.factiongroup, self.unit);
				end
			elseif (event == "UNIT_FACTION") then
				if (arg1 == self.unit) or (arg1 == "player")then
					SMART_ColorName(self.name, self.unit);
					SMART_CheckLevel(self.leveltext, self.unit);
					SMART_FactionGroupCheck(self.factiongroup, self.unit);
				end
			elseif (event == "UNIT_PORTRAIT_UPDATE") then
				SetPortraitTexture(self.portrait, self.unit);
			elseif (event == "RAID_TARGET_UPDATE") then
				SmartFocusToT_RaidTargetCheck(self);
			end
		end
	end
end

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

	self:RegisterEvent("VARIABLES_LOADED");
	self:RegisterEvent("PLAYER_FOCUS_CHANGED");
	self:RegisterEvent("UNIT_TARGET");
	self:RegisterEvent("UNIT_CLASSIFICATION_CHANGED");
	self:RegisterEvent("UNIT_LEVEL");
	self:RegisterEvent("UNIT_FACTION");
	self:RegisterEvent("RAID_TARGET_UPDATE");

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

	self.raidtargeticon = SmartFocusToTTRaidTargetIcon;
	self.healthloss = SmartFocusToTTHealthLoss;
	self.healthtext = SmartFocusToTTHealthBarText;
	self.manatext = SmartFocusToTTManaBarText;
	self.healthpercent = SmartFocusToTTHealthPercent;
	self.manapercent = SmartFocusToTTManaPercent;
	self.factiongroup = SmartFocusToTTFactionGroupIcon;
	self.classicon = SmartFocusToTTClassIcon;
	self.leveltext = SmartFocusToTTLevelText;
	self.elitetext = SmartFocusToTTEliteText;
	self.statustext = SmartFocusToTTStatusText;

	self.current = nil;
	self.currentportrait = nil;

	self.numbuffs = 0;
	self.numdebuffs = 0;

	self.lockframe = true;

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

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

function SmartFocusToTTFrameDropDown_OnLoad(self)
	UIDropDownMenu_Initialize(self, 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(self, event)
	if (event == "VARIABLES_LOADED") then
		self.enable = SMART_TOT_Config.enable and SMART_TOT_Config.tott;
		SmartFocusToT_RegisterUnitWatch(self,self.enable);
	end
	if SMART_TOT_Config.tott and SMART_TOT_Config.enable then
		if (event == "PLAYER_FOCUS_CHANGED") then
			SmartFocusToT_Update(self);
		elseif (event == "UNIT_TARGET") then
			if (arg1 == "focus") or (arg1 == "focustarget") then
				SmartFocusToT_Update(self);
			end
		end
		if UnitExists(self.unit) then
			if (event == "UNIT_NAME_UPDATE") or (event == "UNIT_CLASSIFICATION_CHANGED") or (event == "UNIT_LEVEL") then
				if (arg1 == self.unit) then
					SMART_ColorName(self.name, self.unit);
					SMART_CheckLevel(self.leveltext, self.unit);
					SMART_FactionGroupCheck(self.factiongroup, self.unit);
				end
			elseif (event == "UNIT_FACTION") then
				if (arg1 == self.unit) or (arg1 == "player")then
					SMART_ColorName(self.name, self.unit);
					SMART_CheckLevel(self.leveltext, self.unit);
					SMART_FactionGroupCheck(self.factiongroup, self.unit);
				end
			elseif (event == "UNIT_PORTRAIT_UPDATE") then
				SetPortraitTexture(self.portrait, self.unit);
			elseif (event == "RAID_TARGET_UPDATE") then
				SmartFocusToT_RaidTargetCheck(self);
			end
		end
	end
end
