--------------------------------------------
--
-- SMART - ToT, ToTT
--
--------------------------------------------
SMART_TOT_ENABLE = true;
SMART_TOT_FIXED = nil;
SMART_TOT_Config = {enable=true,statusbartext=true,loss=false,losstopercent=false,percent=true,tott=false,WoWToTOnOff=true,raid=true,buff=true,debuff=true,buffswap=false,ChkBuff=16,MaxBuff=16,ChkDebuff=16,MaxDebuff=16};

function SmartToT_CheckWoWToTOnOff()
	if SMART_TOT_Config.WoWToTOnOff then
		SmartToTFrame.lockframe = true
	else
		SmartToTFrame.lockframe = false
	end

	SmartToTTFrame.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
			SmartToT_ResetPosition()
		end
	end
end

function SmartToT_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("SmartToTFrameBuff"..i):Hide();
				getglobal("SmartToTTFrameBuff"..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("SmartToTFrameDebuff"..i):Hide();
			getglobal("SmartToTTFrameDebuff"..i):Hide();
		end
	end
end

function SmartToT_BuffDebuffLayout()
	if SMART_TOT_Config.buffswap then
		SmartToTFrameDebuff1:ClearAllPoints();
		SmartToTFrameDebuff1:SetPoint("BOTTOMLEFT", "SmartToTFrame", "BOTTOMLEFT", 50, -15);
		SmartToTFrameDebuff9:ClearAllPoints();
		SmartToTFrameDebuff9:SetPoint("LEFT", "SmartToTFrameDebuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartToTFrameBuff1:ClearAllPoints();
			SmartToTFrameBuff1:SetPoint("LEFT", "SmartToTFrame", "LEFT", 162, 0);
		else
			SmartToTFrameBuff1:ClearAllPoints();
			SmartToTFrameBuff1:SetPoint("LEFT", "SmartToTFrame", "LEFT", 100, 0);
		end
		SmartToTFrameBuff9:ClearAllPoints();
		SmartToTFrameBuff9:SetPoint("TOPLEFT", "SmartToTFrameBuff1", "BOTTOMLEFT", 0, -2);

		SmartToTTFrameDebuff1:ClearAllPoints();
		SmartToTTFrameDebuff1:SetPoint("BOTTOMLEFT", "SmartToTTFrame", "BOTTOMLEFT", 50, -15);
		SmartToTTFrameDebuff9:ClearAllPoints();
		SmartToTTFrameDebuff9:SetPoint("LEFT", "SmartToTTFrameDebuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartToTTFrameBuff1:ClearAllPoints();
			SmartToTTFrameBuff1:SetPoint("LEFT", "SmartToTTFrame", "LEFT", 162, 0);
		else
			SmartToTTFrameBuff1:ClearAllPoints();
			SmartToTTFrameBuff1:SetPoint("LEFT", "SmartToTTFrame", "LEFT", 100, 0);
		end
		SmartToTTFrameBuff9:ClearAllPoints();
		SmartToTTFrameBuff9:SetPoint("TOPLEFT", "SmartToTTFrameBuff1", "BOTTOMLEFT", 0, -2);
	else
		SmartToTFrameBuff1:ClearAllPoints();
		SmartToTFrameBuff1:SetPoint("BOTTOMLEFT", "SmartToTFrame", "BOTTOMLEFT", 50, -15);
		SmartToTFrameBuff9:ClearAllPoints();
		SmartToTFrameBuff9:SetPoint("LEFT", "SmartToTFrameBuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartToTFrameDebuff1:ClearAllPoints();
			SmartToTFrameDebuff1:SetPoint("LEFT", "SmartToTFrame", "LEFT", 162, 0);
		else
			SmartToTFrameDebuff1:ClearAllPoints();
			SmartToTFrameDebuff1:SetPoint("LEFT", "SmartToTFrame", "LEFT", 100, 0);
		end
		SmartToTFrameDebuff9:ClearAllPoints();
		SmartToTFrameDebuff9:SetPoint("TOPLEFT", "SmartToTFrameDebuff1", "BOTTOMLEFT", 0, -2);

		SmartToTTFrameBuff1:ClearAllPoints();
		SmartToTTFrameBuff1:SetPoint("BOTTOMLEFT", "SmartToTTFrame", "BOTTOMLEFT", 50, -15);
		SmartToTTFrameBuff9:ClearAllPoints();
		SmartToTTFrameBuff9:SetPoint("LEFT", "SmartToTTFrameBuff8", "RIGHT", 2, 0);
		if SMART_TOT_Config.statusbartext then
			SmartToTTFrameDebuff1:ClearAllPoints();
			SmartToTTFrameDebuff1:SetPoint("LEFT", "SmartToTTFrame", "LEFT", 162, 0);
		else
			SmartToTTFrameDebuff1:ClearAllPoints();
			SmartToTTFrameDebuff1:SetPoint("LEFT", "SmartToTTFrame", "LEFT", 100, 0);
		end
		SmartToTTFrameDebuff9:ClearAllPoints();
		SmartToTTFrameDebuff9:SetPoint("TOPLEFT", "SmartToTTFrameDebuff1", "BOTTOMLEFT", 0, -2);
	end
end

function SmartToT_CommandHandler(msg)
	if (strlower(msg) == strlower("reset")) then
		SmartToT_CheckWoWToTOnOff()
		SmartToT_ResetPosition();
	else
		DEFAULT_CHAT_FRAME:AddMessage("SmartToT: /stot [reset]");
		DEFAULT_CHAT_FRAME:AddMessage("SmartToT: [reset] -  위치초기화");
	end
end

function SmartToT_ResetPosition()
	SmartToTFrameResetPosition();
	SmartToTTFrameResetPosition();
end

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

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

function SmartToT_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 SmartToT_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 SmartToT_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 SmartToT_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 SmartToT_Update(self)
	if not UnitExists(self.unit) then
		self.current = nil;
	else
		if self.show then
			self.current = UnitName(self.unit)

			UnitFrame_Update(self)
			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)
			SmartToT_RefreshBuffs(self, SMART_TOT_Config.ChkBuff, SMART_TOT_Config.ChkDebuff)
			SmartToT_RaidTargetCheck(self)

			UnitFrameHealthBar_Update(self.healthbar, self.unit)
			UnitFrameManaBar_Update(self.manabar, self.unit)

			SmartToT_SetStatusText(self)
			SmartToT_HealthUpdate(self)
			SmartToT_ManaUpdate(self)
		end
	end
end

function SmartToT_OnUpdate(self, elapsed)
	self.elapsed = (self.elapsed or 0) + elapsed

	if self.elapsed > 0.25 then
		self.elapsed = 0;

		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)

			SmartToT_SetStatusText(self)
			SmartToT_HealthUpdate(self)
			SmartToT_ManaUpdate(self)
			SmartToT_RefreshBuffs(self, SMART_TOT_Config.ChkBuff, SMART_TOT_Config.ChkDebuff)
		end
	end
end

function SmartToT_OnHide(self)
	self.current = nil
	--CloseDropDownMenus()
end

function SmartToT_SetStatusText(smartFrame)
	local status

	if UnitIsConnected(smartFrame.unit) then
		if UnitIsDead(smartFrame.unit) then
			if SMART_CheckFeign(smartFrame.unit) then
				status = "죽은척하기"
			else
				status = "죽음"
			end
			smartFrame.portrait:SetVertexColor(0.35, 0.35, 0.35);
			smartFrame.statustext:SetText(status);
			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
		if UnitIsDead(smartFrame.unit) then
			if SMART_CheckFeign(smartFrame.unit) then
				status = "죽은척하기"
			else
				status = "죽음"
			end
		else
			status = "오프라인"
		end

		smartFrame.portrait:SetVertexColor(0.35, 0.35, 0.35);
		smartFrame.statustext:SetText(status)
		smartFrame.statustext:Show();
	end
end

function SmartToT_HealthCheck(self, value)
	if (UnitIsPlayer(self.unit)) then
		local unitMinHP, unitMaxHP, unitCurrHP;
		unitHPMin, unitHPMax = self:GetMinMaxValues();
		unitCurrHP = value;
		self:GetParent().unitHPPercent = unitCurrHP / unitHPMax;
	end
end

function SmartToT_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 SmartToT_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 SmartToTFrame_OnLoad(self)
	UnregisterUnitWatch(TargetofTargetFrame);

	TargetofTargetFrame:SetAlpha(0);
	TargetofTargetFrame:Hide();

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

	self:RegisterEvent("VARIABLES_LOADED");
	self:RegisterEvent("PLAYER_TARGET_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, "targettarget", SmartToTName, SmartToTPortrait,
			SmartToTHealthBar, SmartToTHealthText,
			SmartToTManaBar, SmartToTManaText,
			SmartToTThreatIndicator, "player");

	self.raidtargeticon = SmartToTRaidTargetIcon;
	self.healthloss = SmartToTHealthLoss;
	self.healthtext = SmartToTHealthBarText;
	self.manatext = SmartToTManaBarText;
	self.healthpercent = SmartToTHealthPercent;
	self.manapercent = SmartToTManaPercent;
	self.factiongroup = SmartToTFactionGroupIcon;
	self.classicon = SmartToTClassIcon;
	self.leveltext = SmartToTLevelText;
	self.elitetext = SmartToTEliteText;
	self.statustext = SmartToTStatusText;

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

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

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

	SecureUnitButton_OnLoad(self, "targettarget", showmenu);
end

function SmartToTFrameDropDown_OnLoad(self)
	UIDropDownMenu_Initialize(self, SmartToTFrameDropDown_Initialize, "MENU");
end

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

function SmartToTFrame_OnEvent(self, event, ...)
	UnitFrame_OnEvent(self, event, ...);

	local arg1 = ...;

	if (event == "VARIABLES_LOADED") then
		SmartToT_CheckWoWToTOnOff()
		self.enable = SMART_TOT_Config.enable
		SmartToT_RegisterUnitWatch(self, self.enable)
	end

	if SMART_TOT_Config.enable then
		if (event == "PLAYER_TARGET_CHANGED") then
			SmartToT_Update(self);
		elseif (event == "UNIT_TARGET") then
			if (arg1 == "target") then
				SmartToT_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
				SmartToT_RaidTargetCheck(self);
			end
		end
	end
end

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

	self:RegisterEvent("VARIABLES_LOADED");
	self:RegisterEvent("PLAYER_TARGET_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, self.unit, SmartToTTName, SmartToTTPortrait, SmartToTTHealthBar, SmartToTTHealthText, SmartToTTManaBar, SmartToTTManaText);

	self.raidtargeticon = SmartToTTRaidTargetIcon;
	self.healthloss = SmartToTTHealthLoss;
	self.healthtext = SmartToTTHealthBarText;
	self.manatext = SmartToTTManaBarText;
	self.healthpercent = SmartToTTHealthPercent;
	self.manapercent = SmartToTTManaPercent;
	self.factiongroup = SmartToTTFactionGroupIcon;
	self.classicon = SmartToTTClassIcon;
	self.leveltext = SmartToTTLevelText;
	self.elitetext = SmartToTTEliteText;
	self.statustext = SmartToTTStatusText;

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

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

	self.lockframe = true;

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

	self:SetFrameLevel(TargetFrame: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 SmartToTTFrameDropDown_OnLoad(self)
	UIDropDownMenu_Initialize(self, SmartToTTFrameDropDown_Initialize, "MENU");
end

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

function SmartToTTFrame_OnEvent(self, event)
	if (event == "VARIABLES_LOADED") then
		self.enable = SMART_TOT_Config.enable and SMART_TOT_Config.tott;
		SmartToT_RegisterUnitWatch(self, self.enable);
	end

	if SMART_TOT_Config.tott and SMART_TOT_Config.enable then
		if (event == "PLAYER_TARGET_CHANGED") then
			SmartToT_Update(self);
		elseif (event == "UNIT_TARGET") then
			if (arg1 == "target") or (arg1 == "targettarget") then
				SmartToT_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
				SmartToT_RaidTargetCheck(self);
			end
		end
	end
end

