--------------------------------------------
--
-- 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_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("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
		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 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_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 NewTargetFrame_OnUpdate()
end
function NewTargetofTarget_Update()
end
function NewTargetofTarget_OnUpdate(elapsed)
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(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
--			SmartToT_RegisterUnitWatch(smartFrame,false);
--		end
--		smartFrame.current = nil;
--		return;
--	else
--		if smartFrame.enable and not smartFrame.show then
--			SmartToT_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);
		SmartToT_RefreshBuffs(smartFrame, SMART_TOT_Config.ChkBuff, SMART_TOT_Config.ChkDebuff);
		SmartToT_RaidTargetCheck(smartFrame);
--		UnitFrame_UpdateManaType(smartFrame);
		UnitFrameHealthBar_Update(smartFrame.healthbar,smartFrame.unit);
		UnitFrameManaBar_Update(smartFrame.manabar,smartFrame.unit);
		SmartToT_StatusCheck(smartFrame);
		SmartToT_HealthUpdate(smartFrame);
		SmartToT_ManaUpdate(smartFrame);
	end
end

function SmartToT_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);
				SmartToT_StatusCheck(this);
				SmartToT_HealthUpdate(this);
				SmartToT_ManaUpdate(this);
				SmartToT_RefreshBuffs(this, SMART_TOT_Config.ChkBuff, SMART_TOT_Config.ChkDebuff);
		end
	end
end

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

function SmartToT_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 SmartToT_HealthCheck()
	local unitMinHP, unitMaxHP, unitCurrHP;
	unitHPMin, unitHPMax = this:GetMinMaxValues();
	unitCurrHP = this:GetValue();
	this:GetParent().unitHPPercent = unitCurrHP / unitHPMax;
end

function SmartToT_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 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()

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

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

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

	this.raidtargeticon = SmartToTRaidTargetIcon;
	this.healthloss = SmartToTHealthLoss;
	this.healthtext = SmartToTHealthBarText;
	this.manatext = SmartToTManaBarText;
	this.healthpercent = SmartToTHealthPercent;
	this.manapercent = SmartToTManaPercent;
	this.factiongroup = SmartToTFactionGroupIcon;
	this.classicon = SmartToTClassIcon;
	this.leveltext = SmartToTLevelText;
	this.elitetext = SmartToTEliteText;
	this.deadtext = SmartToTDeadText;

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

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

	this:SetFrameLevel(TargetFrame: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 SmartToTFrameDropDown_OnLoad()
	UIDropDownMenu_Initialize(this, SmartToTFrameDropDown_Initialize, "MENU");
end

function SmartToTFrameDropDown_Initialize()
	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(SmartToTFrameDropDown, menu, "targettarget", name, id);
	end
end

function SmartToTFrame_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		CheckWoWToTOnOff();
		this.enable = SMART_TOT_Config.enable;
		SmartToT_RegisterUnitWatch(this,this.enable);
	end
	if SMART_TOT_Config.enable then
		if (event == "PLAYER_TARGET_CHANGED") then
			SmartToT_Update(this);
		elseif (event == "UNIT_TARGET") then
			if (arg1 == "target") then
				SmartToT_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
				SmartToT_RaidTargetCheck(this);
			end
		end
	end
end

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

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

	this.raidtargeticon = SmartToTTRaidTargetIcon;
	this.healthloss = SmartToTTHealthLoss;
	this.healthtext = SmartToTTHealthBarText;
	this.manatext = SmartToTTManaBarText;
	this.healthpercent = SmartToTTHealthPercent;
	this.manapercent = SmartToTTManaPercent;
	this.factiongroup = SmartToTTFactionGroupIcon;
	this.classicon = SmartToTTClassIcon;
	this.leveltext = SmartToTTLevelText;
	this.elitetext = SmartToTTEliteText;
	this.deadtext = SmartToTTDeadText;

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

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

	this.lockframe = true;

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

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

function SmartToTTFrameDropDown_Initialize()
	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(SmartToTTFrameDropDown, menu, "targettargettarget", name, id);
	end
end

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