
if (not Nurfed_UnitFrames) then

	local unitlib = Nurfed_Units:New();
	local utility = Nurfed_Utility:New();

	Nurfed_UnitFrames = {};

	Nurfed_UnitFrames.Default = {
		["preservetarget"] = 1,
		["healthflash"] = 20,
		["outlinetext"] = 1,
		["bartexture"] = 6,
		["textformat"] = "$c ($m)",
		["textsize"] = 9,
		["border"] = 2,
		["hptextalign"] = 1,
		["mptextalign"] = 1,
		["hpmissing"] = 1,
		["blizzplayer"] = 0,
		["player"] = 1,
		["playerbg"] = { r = 0, g = 0, b = 0, a = 0.75 },
		["playerborder"] = { r = 1, g = 1, b = 1, a = 1 },
		["playerscale"] = 1,
		["playerhptext"] = 1,
		["playermptext"] = 1,
		["playerhpmissing"] = 1,
		["playermodel"] = 1,
		["playermodelrecolor"] = 1,
		["blizztarget"] = 0,
		["target"] = 1,
		["targetbg"] = { r = 0, g = 0, b = 0, a = 0.75 },
		["targetborder"] = { r = 1, g = 1, b = 1, a = 1 },
		["targetscale"] = 1,
		["targethptext"] = 1,
		["targetmptext"] = 1,
		["targethpmissing"] = 1,
		["targetclass"] = 1,
		["targettarget"] = 1,
		["targettargettarget"] = 1,
		["blizzpet"] = 0,
		["pet"] = 1,
		["petbg"] = { r = 0, g = 0, b = 0, a = 0.75 },
		["petborder"] = { r = 1, g = 1, b = 1, a = 1 },
		["petscale"] = 1,
		["pethptext"] = 1,
		["petmptext"] = 1,
		["pethpmissing"] = 1,
		["blizzparty"] = 0,
		["party"] = 1,
		["partybg"] = { r = 0, g = 0, b = 0, a = 0.75 },
		["partyborder"] = { r = 1, g = 1, b = 1, a = 1 },
		["partyscale"] = 1,
		["partyhptext"] = 1,
		["partymptext"] = 0,
		["partyhpmissing"] = 1,
		["partylink"] = 0,
		["partyname"] = "[$hk] $n",
	};

	Nurfed_UnitFrames.Events = {
		["blizzplayer"] = {
			["unit"] = "player",
			["frame"] = "PlayerFrame",
			["events"] = {
				"UNIT_LEVEL",
				"UNIT_COMBAT",
				"UNIT_PVP_UPDATE",
				"UNIT_MAXMANA",
				"PLAYER_ENTERING_WORLD",
				"PLAYER_ENTER_COMBAT",
				"PLAYER_LEAVE_COMBAT",
				"PLAYER_REGEN_DISABLED",
				"PLAYER_REGEN_ENABLED",
				"PLAYER_UPDATE_RESTING",
				"PARTY_MEMBERS_CHANGED",
				"PARTY_LEADER_CHANGED",
				"PARTY_LOOT_METHOD_CHANGED",
				"RAID_ROSTER_UPDATE",
				"UNIT_NAME_UPDATE",
				"UNIT_PORTRAIT_UPDATE",
				"UNIT_DISPLAYPOWER",
			},
		},
		["player"] = {
			["unit"] = "player",
			["frame"] = "Nurfed_player",
			["events"] = {
				"PLAYER_ENTER_COMBAT",
				"PLAYER_LEAVE_COMBAT",
				"PLAYER_REGEN_DISABLED",
				"PLAYER_REGEN_ENABLED",
				"PLAYER_UPDATE_RESTING",
				"PLAYER_XP_UPDATE",
				"PARTY_MEMBERS_CHANGED",
				"RAID_ROSTER_UPDATE",
				"PARTY_LEADER_CHANGED",
				"PARTY_LOOT_METHOD_CHANGED",
				"UPDATE_EXHAUSTION",
				"UNIT_LEVEL",
				"UNIT_HEALTH",
				"UNIT_MANA",
				"UNIT_ENERGY",
				"UNIT_RAGE",
				"UNIT_MAXHEALTH",
				"UNIT_MAXMANA",
				"UNIT_MAXRAGE",
				"UNIT_MAXENERGY",
				"UNIT_DISPLAYPOWER",
				"UNIT_PVP_UPDATE",
			},
		},

		["blizztarget"] = {
			["unit"] = "target",
			["frame"] = "TargetFrame",
			["events"] = {
				"PLAYER_TARGET_CHANGED",
				"UNIT_HEALTH",
				"UNIT_LEVEL",
				"UNIT_FACTION",
				"UNIT_CLASSIFICATION_CHANGED",
				"UNIT_AURA",
				"PLAYER_FLAGS_CHANGED",
				"PARTY_MEMBERS_CHANGED",
				"UNIT_NAME_UPDATE",
				"UNIT_PORTRAIT_UPDATE",
				"UNIT_DISPLAYPOWER",
			}
		},
		["target"] = {
			["unit"] = "target",
			["frame"] = "Nurfed_target",
			["events"] = {
				"PLAYER_TARGET_CHANGED",
				"PLAYER_FLAGS_CHANGED",
				"PLAYER_COMBO_POINTS",
				"UNIT_NAME_UPDATE",
				"UNIT_LEVEL",
				"UNIT_HEALTH",
				"UNIT_MANA",
				"UNIT_ENERGY",
				"UNIT_RAGE",
				"UNIT_FOCUS",
				"UNIT_MAXHEALTH",
				"UNIT_MAXMANA",
				"UNIT_MAXRAGE",
				"UNIT_MAXENERGY",
				"UNIT_DISPLAYPOWER",
				"UNIT_PVP_UPDATE",
				"UNIT_FACTION",
				"UNIT_CLASSIFICATION_CHANGED",
				"UNIT_AURA",
				"UNIT_DYNAMIC_FLAGS",
			},
		},

		["blizzpet"] = {
			["unit"] = "pet",
			["frame"] = "PetFrame",
			["events"] = {
				"UNIT_PET",
				"UNIT_COMBAT",
				"UNIT_AURA",
				"PET_ATTACK_START",
				"PET_ATTACK_STOP",
				"UNIT_HAPPINESS",
				"UNIT_NAME_UPDATE",
				"UNIT_PORTRAIT_UPDATE",
				"UNIT_DISPLAYPOWER",
			}
		},
		["pet"] = {
			["unit"] = "pet",
			["frame"] = "Nurfed_pet",
			["events"] = {
				"PET_ATTACK_START",
				"PET_ATTACK_STOP",
				"UNIT_PET",
				"UNIT_NAME_UPDATE",
				"UNIT_LEVEL",
				"UNIT_HEALTH",
				"UNIT_MANA",
				"UNIT_ENERGY",
				"UNIT_RAGE",
				"UNIT_FOCUS",
				"UNIT_MAXHEALTH",
				"UNIT_MAXMANA",
				"UNIT_MAXRAGE",
				"UNIT_MAXENERGY",
				"UNIT_DISPLAYPOWER",
				"UNIT_AURA",
				"UNIT_HAPPINESS",
			},
		},

		["blizzparty"] = {
			["unit"] = "party",
			["frame"] = "PartyMemberFrame",
			["events"] = {
				"PARTY_MEMBERS_CHANGED",
				"PARTY_LEADER_CHANGED",
				"PARTY_MEMBER_ENABLE",
				"PARTY_MEMBER_DISABLE",
				"PARTY_LOOT_METHOD_CHANGED",
				"UNIT_PVP_UPDATE",
				"UNIT_AURA",
				"UNIT_PET",
				"VARIABLES_LOADED",
				"UNIT_NAME_UPDATE",
				"UNIT_PORTRAIT_UPDATE",
				"UNIT_DISPLAYPOWER",
			}
		},
		["party"] = {
			["unit"] = "party",
			["frame"] = "Nurfed_party",
			["petframe"] = "Nurfed_partypet",
			["events"] = {
				"PLAYER_TARGET_CHANGED",
				"PARTY_MEMBERS_CHANGED",
				"PARTY_LEADER_CHANGED",
				"PARTY_LOOT_METHOD_CHANGED",
				"UNIT_NAME_UPDATE",
				"UNIT_LEVEL",
				"UNIT_HEALTH",
				"UNIT_MANA",
				"UNIT_ENERGY",
				"UNIT_RAGE",
				"UNIT_MAXHEALTH",
				"UNIT_MAXMANA",
				"UNIT_MAXRAGE",
				"UNIT_MAXENERGY",
				"UNIT_DISPLAYPOWER",
				"UNIT_AURA",
				"UNIT_PET",
			},
		},
	};

	Nurfed_UnitFrames.Effects = {
		["Poison"] = "Interface\\Icons\\Spell_Nature_NullifyPoison_02",
		["Magic"] = "Interface\\Icons\\Spell_Holy_DispelMagic",
		["Disease"] = "Interface\\Icons\\Spell_Nature_NullifyDisease",
		["Curse"] = "Interface\\Icons\\Spell_Nature_RemoveCurse",
	};

	Nurfed_UnitFrames.Classification = {
		["worldboss"] = BOSS,
		["rareelite"] = ITEM_QUALITY3_DESC.."-"..ELITE,
		["rare"] = ITEM_QUALITY3_DESC,
		["elite"] = ELITE,
	};

	function Nurfed_UnitFrames:New()
		local object = {};
		setmetatable(object, self);
		self.__index = self;
		return object;
	end

	function Nurfed_UnitFrames:GetOption(option)
		local player = UnitName("player").." - "..GetRealmName();
		if (NURFED_SAVED_UNITFRAMES[player][option]) then
			return NURFED_SAVED_UNITFRAMES[player][option];
		end
	end

	function Nurfed_UnitFrames:UnitInit(unit)
		local frame = getglobal(self.Events[unit]["frame"]);
		if (self:GetOption(unit) == 1) then
			for k, v in self.Events[unit]["events"] do
				frame:RegisterEvent(v);
			end
			if (UnitExists(self.Events[unit]["unit"])) then
				frame:Show();
			end
			local func = getglobal(self.Events[unit]["frame"].."_Update");
			if (func) then
				func();
			end
			if (unit == "target") then
				Nurfed_target:SetScript("OnUpdate", function() Nurfed_ToT_OnUpdate(arg1) end);
			end
			if (unit == "pet") then
				Nurfed_pet:SetScript("OnUpdate", function() Nurfed_ToP_OnUpdate(arg1) end);
			end
		else
			for k, v in self.Events[unit]["events"] do
				frame:UnregisterEvent(v);
			end
			if (frame:IsVisible()) then
				frame:Hide();
			end
			if (unit == "target") then
				Nurfed_target:SetScript("OnUpdate", nil);
			end
			if (unit == "pet") then
				Nurfed_pet:SetScript("OnUpdate", nil);
			end
		end

		if (string.find(frame:GetName(), "Nurfed")) then
			frame:EnableMouse(true);
			frame:RegisterForDrag("LeftButton");
			frame:RegisterForClicks("LeftButtonUp", "RightButtonUp");
			frame.unit = unit;
			if (unit == "target") then
				Nurfed_target.update = 0;
				Nurfed_targettarget.unit = "targettarget";
				Nurfed_targettargettarget.unit = "targettargettarget";
			end
			if (unit == "pet") then
				Nurfed_pet.update = 0;
				Nurfed_pettarget.unit = "pettarget";
			end
		end
	end

	function Nurfed_UnitFrames:PartyInit(unit)
		local i, frame, petframe;
		if (self:GetOption(unit) == 1) then
			for i = 1, 4 do
				frame = getglobal(self.Events[unit]["frame"]..i);
				for k, v in self.Events[unit]["events"] do
					frame:RegisterEvent(v);
				end
				frame.unit = "party"..i;
				if (UnitExists(self.Events[unit]["unit"]..i)) then
					frame:Show();
				end
				if (unit == "party") then
					petframe = getglobal(self.Events[unit]["petframe"]..i);
					local func = getglobal(self.Events[unit]["frame"].."_UpdateMember");
					if (func) then
						func(i);
					end
					petframe:RegisterEvent("UNIT_HEALTH");
					petframe:RegisterEvent("UNIT_NAME_UPDATE");
					petframe:SetScript("OnEvent", function() Nurfed_Party_OnEvent(event) end);
					getglobal("Nurfed_partypet"..i.."HealthBarBG"):SetVertexColor(1, 0, 0, 0.3);
					frame:EnableMouse(true);
					frame:RegisterForDrag("LeftButton");
					frame:RegisterForClicks("LeftButtonUp", "RightButtonUp");
				end
			end
			if (unit == "blizzparty") then
				HIDE_PARTY_INTERFACE = "0";
			end
		else
			for i = 1, 4 do
				frame = getglobal(self.Events[unit]["frame"]..i);
				for k, v in self.Events[unit]["events"] do
					frame:UnregisterEvent(v);
				end
				if (frame:IsVisible()) then
					frame:Hide();
				end
			end
			if (unit == "blizzparty") then
				HIDE_PARTY_INTERFACE = "1";
			end
		end
	end

	function Nurfed_UnitFrames:InitAll()
		self:UpdateTextFormat();
		self:UnitInit("player");
		self:StatusText("player");
		self:UnitInit("blizzplayer");
		self:UnitInit("target");
		self:StatusText("target");
		self:UnitInit("blizztarget");
		self:UnitInit("pet");
		self:StatusText("pet");
		self:UnitInit("blizzpet");
		self:PartyInit("party");
		self:StatusText("party");
		self:PartyInit("blizzparty");
		self:InitText();
		self:UpdateBarTexture();
		self:UpdateBorder();
		self:UpdatePlayerModel();
		self:LinkParty();
	end

	function Nurfed_UnitFrames:UpdateAll()
		if (self:GetOption("player") == 1) then
			Nurfed_player_Update();
		end
		if (self:GetOption("target") == 1) then
			Nurfed_target_Update();
		end
		self:ScaleAll();
	end

	function Nurfed_UnitFrames:Status(unit)
		local icon = getglobal("Nurfed_"..unit.."StatusIcon");
		local model = getglobal("Nurfed_"..unit.."Model");
		local text = getglobal("Nurfed_"..unit.."Level");
		if (this.inCombat) then
			icon:Show();
			icon:SetTexCoord(.5625, .9375, .0625, .4375);
			if (text) then
				text:SetTextColor(1, 0, 0);
			end
			if (model and self:GetOption("playermodelrecolor") == 1) then
				model:SetLight(1, 0, 0, -0.707, -0.707, 1.0, 1.0, 0.0, 0, 0.8, 1.0, 1.0, 0.8);
			end
		elseif (IsResting()) then
			icon:Show();
			icon:SetTexCoord(.0625, .4375, .0625, .4375);
			if (text) then
				text:SetTextColor(1, 1, 0);
			end
			if (model and self:GetOption("playermodelrecolor") == 1) then
				model:SetLight(1, 0, 0, -0.707, -0.707, 1.0, 1.0, 1.0, 1.0, 0.8, 1.0, 1.0, 1.0);
			end
		else
			icon:Hide();
			if (text) then
				text:SetTextColor(1, 1, 1);
			end
			if (model) then
				model:SetLight(1, 0, 0, -0.707, -0.707, 1.0, 1.0, 1.0, 1.0, 0.8, 1.0, 1.0, 1.0);
			end
		end
	end

	function Nurfed_UnitFrames:PvP(unit)
		local icon = getglobal("Nurfed_"..unit.."PvPIcon");
		local factionGroup = UnitFactionGroup(unit);
		if (UnitIsPVPFreeForAll(unit)) then
			icon:SetTexture("Interface\\TargetingFrame\\UI-PVP-FFA");
			icon:Show();
		elseif (factionGroup and UnitIsPVP(unit)) then
			icon:SetTexture("Interface\\TargetingFrame\\UI-PVP-"..factionGroup);
			icon:Show();
		else
			icon:Hide();
		end
	end

	function Nurfed_UnitFrames:Rank(unit)
		local rankname, ranknumber = GetPVPRankInfo(UnitPVPRank(unit));
		if (ranknumber) then
			local icon = getglobal("Nurfed_"..unit.."RankIcon");
			icon:Show();
			if (ranknumber==0) then
				icon:Hide();
			elseif (ranknumber>9) then
				icon:SetTexture("Interface\\PVPRankBadges\\PVPRank"..ranknumber);
			else
				icon:SetTexture("Interface\\PVPRankBadges\\PVPRank0"..ranknumber);
			end
		else
			icon:Hide();
		end
	end

	function Nurfed_UnitFrames:Leader()
		local id = this:GetID();
		if (IsPartyLeader()) then
			Nurfed_playerLeaderIcon:Show();
		else
			Nurfed_playerLeaderIcon:Hide();
		end
		if (id > 0) then
			if (GetPartyLeaderIndex() == id) then
				getglobal("Nurfed_party"..id.."LeaderIcon"):Show();
			else
				getglobal("Nurfed_party"..id.."LeaderIcon"):Hide();
			end
		end
	end

	function Nurfed_UnitFrames:Loot()
		local id = this:GetID();
		local lootMethod, lootMaster = GetLootMethod();

		if (lootMaster == 0) then
			Nurfed_playerMasterIcon:Show();
		else
			Nurfed_playerMasterIcon:Hide();
		end
		if (id > 0) then
			local icon = getglobal("Nurfed_party"..id.."MasterIcon");
			if (id == lootMaster) then
				icon:Show();
			else
				icon:Hide();
			end
		end
	end

	function Nurfed_UnitFrames:UpdateHealth(unit)
		local frame = getglobal("Nurfed_"..unit);
		local bar = getglobal("Nurfed_"..unit.."HealthBar");
		local currhp, maxhp, perc, color = unitlib:Health(unit);
		local text = getglobal("Nurfed_"..unit.."HealthBarMissingText");
		local textformat = self:GetOption("textformat");
		local showmissing;
		frame:SetAlpha(1);
		if (string.find(unit, "party[1-4]")) then
			showmissing = self:GetOption("partyhpmissing");
		else
			showmissing = self:GetOption(unit.."hpmissing");
		end
		if (showmissing == 1 and (UnitIsPlayer(unit) or unit == "pet")) then
			text:SetText(currhp - maxhp);
		else
			text:SetText(nil);
		end
		bar:SetMinMaxValues(0, maxhp);
		bar:SetValue(currhp);
		bar:SetStatusBarColor(color.r, color.g, color.b);

		local perctext = getglobal("Nurfed_"..unit.."HPPerc");
		if (perctext) then
			perctext:SetText(format("%.0f", perc).."%");
			perctext:SetTextColor(color.r, color.g, color.b);
		end
		local text = getglobal("Nurfed_"..unit.."HealthBarText");
		if (UnitIsDead(unit) and not UnitIsGhost(unit)) then
			text:SetText(DEAD);
		elseif (UnitIsGhost(unit)) then
			text:SetText(NURFEDGHOST);
		elseif (not UnitIsConnected(unit)) then
			text:SetText(PLAYER_OFFLINE);
			frame:SetAlpha(0.5);
			bar:SetValue(0);
		else
			textformat = string.gsub(textformat, "$c", currhp);
			textformat = string.gsub(textformat, "$m", maxhp);
			text:SetText(textformat);
		end

		if (perc < self:GetOption("healthflash") and UnitIsConnected(unit) and not (UnitIsDead(unit) or UnitIsGhost(unit))) then
			if (not bar:GetScript("OnUpdate")) then
				bar.flashtime = GetTime();
				bar.update = 0;
				bar.flashdct = 1;
				bar:SetScript("OnUpdate", function() Nurfed_HPBar_OnUpdate(arg1) end);
			end
		else
			if (bar:GetScript("OnUpdate")) then
				bar:SetScript("OnUpdate", nil);
				bar:SetAlpha(1);
			end
		end

		if (IsAddOnLoaded("MobHealth") and MobHealthFrame:IsVisible()) then
			MobHealthFrame:Hide();
		end
	end

	function Nurfed_UnitFrames:UpdateMana(unit)
		local bar = getglobal("Nurfed_"..unit.."ManaBar");
		local text = getglobal("Nurfed_"..unit.."ManaBarText");
		local currmp, maxmp, perc = unitlib:Mana(unit);
		if (maxmp > 0) then
			local textformat = self:GetOption("textformat");
			textformat = string.gsub(textformat, "$c", currmp);
			textformat = string.gsub(textformat, "$m", maxmp);
			text:SetText(textformat);
		else
			text:SetText(nil);
		end
		bar:SetMinMaxValues(0, maxmp);
		bar:SetValue(currmp);
	end

	function Nurfed_UnitFrames:UpdateManaColor(unit)
		local bar = getglobal("Nurfed_"..unit.."ManaBar");
		local color = ManaBarColor[UnitPowerType(unit)];
		bar:SetStatusBarColor(color.r, color.g, color.b);
		bar = getglobal("Nurfed_"..unit.."ManaBarBG");
		bar:SetVertexColor(color.r, color.g, color.b, 0.3);
		bar = getglobal("Nurfed_"..unit.."HealthBarBG");
		bar:SetVertexColor(1, 0, 0, 0.3);
		self:UpdateMana(unit);
	end

	function Nurfed_UnitFrames:UpdateLevel(unit)
		local text = getglobal("Nurfed_"..unit.."Level");
		text:SetText(UnitLevel(unit));
	end

	function Nurfed_UnitFrames:UpdateGroupNum()
		local subgroup, class = unitlib:Raid(UnitName("player"));
		if (subgroup) then
			Nurfed_playerGroup:SetText("|cffffffffGroup:|r "..subgroup);
			Nurfed_playerGroup:Show();
		else
			Nurfed_playerGroup:Hide();
		end
	end

	function Nurfed_UnitFrames:Background(unit)
		local color = self:GetOption(unit.."bg");
		local i, frame;
		if (unit == "party") then
			for i = 1, 4 do
				frame = getglobal("Nurfed_"..unit..i);
				frame:SetBackdropColor(color.r, color.g, color.b, color.a);
			end
			Nurfed_party:SetBackdropColor(color.r, color.g, color.b, color.a);

			color = self:GetOption(unit.."border");
			for i = 1, 4 do
				frame = getglobal("Nurfed_"..unit..i);
				frame:SetBackdropBorderColor(color.r, color.g, color.b, color.a);
			end
			Nurfed_party:SetBackdropBorderColor(color.r, color.g, color.b, color.a);
			self:LinkParty(true);
		else
			frame = getglobal("Nurfed_"..unit);
			frame:SetBackdropColor(color.r, color.g, color.b, color.a);
			if (unit == "target") then
				local tot = getglobal("Nurfed_"..unit.."target");
				tot:SetBackdropColor(color.r, color.g, color.b, color.a);
				local tott = getglobal("Nurfed_"..unit.."targettarget");
				tott:SetBackdropColor(color.r, color.g, color.b, color.a);
			end
			color = self:GetOption(unit.."border");
			frame:SetBackdropBorderColor(color.r, color.g, color.b, color.a);
			if (unit == "target") then
				local tot = getglobal("Nurfed_"..unit.."target");
				tot:SetBackdropBorderColor(color.r, color.g, color.b, color.a);
				local tott = getglobal("Nurfed_"..unit.."targettarget");
				tott:SetBackdropBorderColor(color.r, color.g, color.b, color.a);
			end
		end
	end

	function Nurfed_UnitFrames:UpdateBorder()
		local border = self:GetOption("border");
		local background, edge, size, inset;
		if (border == 1) then
			background = "Interface\\Tooltips\\UI-Tooltip-Background";
			edge = "Interface\\Tooltips\\UI-Tooltip-Border";
			size = 1;
			inset = 0;
		elseif (border == 2) then
			background = "Interface\\Tooltips\\UI-Tooltip-Background";
			edge = "Interface\\Tooltips\\UI-Tooltip-Border";
			size = 16;
			inset = 5;
		elseif (border == 3) then
			background = "Interface\\Tooltips\\UI-Tooltip-Background";
			edge = "Interface\\DialogFrame\\UI-DialogBox-Border";
			size = 16;
			inset = 5;
		end
		Nurfed_party:SetBackdrop({bgFile = background, edgeFile = edge, tile = true, tileSize = 16, edgeSize = size, insets = { left = inset, right = inset, top = inset, bottom = inset }});
		for k, v in unitlib.UnitNames do
			getglobal("Nurfed_"..v):SetBackdrop({bgFile = background, edgeFile = edge, tile = true, tileSize = 16, edgeSize = size, insets = { left = inset, right = inset, top = inset, bottom = inset }});
			if (string.find(v, "party[1-4]")) then
				self:Background("party");
			else
				self:Background(v);
			end
		end
	end

	function Nurfed_UnitFrames:UpdatePartyNames()
		local i;
		for i = 1, 4 do
			self:UpdateName("party"..i);
		end
	end

	function Nurfed_UnitFrames:UpdateName(unit)
		local text = getglobal("Nurfed_"..unit.."Name");
		local icon = getglobal("Nurfed_"..unit.."ClassIcon");
		local name = UnitName(unit);
		local level = UnitLevel(unit);
		if (UnitIsPlayer(unit)) then
			local info = unitlib.Class[UnitClass(unit)];
			if (info) then
				name = "|cff"..info.color..name.."|r";
				if (icon) then
					icon:SetTexCoord(info.right, info.left, info.top, info.bottom);
					if (unit == "target") then
						if (self:GetOption("targetclass") == 1) then
							icon:Show();
						else
							icon:Hide();
						end
					else
						icon:Show();
					end
				end
			end

			if (string.find(unit, "party[1-4]")) then
				local id = this:GetID();
				if (not id or id < 1) then
					id = string.gsub(unit, "party", "");
					id = tonumber(id);
				end
				local binding = GetBindingText(GetBindingKey("TARGETPARTYMEMBER"..id), "KEY_");

				binding = string.gsub(binding, "CTRL%-", "C-");
				binding = string.gsub(binding, "ALT%-", "A-");
				binding = string.gsub(binding, "SHIFT%-", "S-");
				binding = string.gsub(binding, "Num Pad", "NP");
				binding = string.gsub(binding, "Backspace", "Bksp");
				binding = string.gsub(binding, "Spacebar", "Space");
				binding = string.gsub(binding, "Page", "Pg");
				binding = string.gsub(binding, "Down", "Dn");
				binding = string.gsub(binding, "Arrow", "");
				binding = string.gsub(binding, "Insert", "Ins");
				binding = string.gsub(binding, "Delete", "Del");

				local textformat = self:GetOption("partyname");
				textformat = string.gsub(textformat, "$hk", binding);
				textformat = string.gsub(textformat, "$n", name);
				textformat = string.gsub(textformat, "$lv", level);
				name = textformat;
			end
		elseif (icon) then
			icon:Hide();
		end

		if (unit == "pet") then
			name = UnitLevel("pet").." "..name;
		end

		text:SetText(name);

		if (unit == "target") then
			local info = unitlib:Reaction("target");
			text:SetTextColor(info.r, info.g, info.b);
		end
	end

	function Nurfed_UnitFrames:UpdateTargetLevel()
		local text = getglobal("Nurfed_targetLevel");
		local level, display, classification, color, display;

		level = UnitLevel("target");
		color = GetDifficultyColor(level);

		if (UnitIsPlusMob("target")) then
			level = level.."+";
		elseif (level == 0) then
			level = "";
		elseif (level < 0) then
			level = "??";
		end

		if (self.Classification[UnitClassification("target")]) then
			classification = self.Classification[UnitClassification("target")];
		end

		if (classification) then
			if (classification == BOSS) then
				display = classification;
			elseif (UnitCreatureType("target")) then
				display = level.." "..classification.." "..UnitCreatureType("target");
			else
				display = level.." "..classification;
			end
		else
			if (UnitIsPlayer("target")) then
				local class = UnitClass("target");
				local color = unitlib.Class[class].color;
				class = "|cff"..color..class.."|r";
				display = level.." "..class;
			elseif (UnitCreatureType("target") == "Humanoid" and UnitIsFriend("player", "target")) then
				display = level.." NPC";
			elseif (UnitCreatureType("target")) then
				display = level.." "..UnitCreatureType("target");
			else
				display = level;
			end
		end

		if ( UnitIsCorpse("target") ) then
			display = "Corpse";
		end
		text:SetText(display);
		text:SetTextColor(color.r, color.g, color.b);
	end

	function Nurfed_UnitFrames:UpdateAuras(unit)
		local i, button, icon, buff, debuff, border, debuffcount, size;
		local num = 0;
		for i = 1, 16 do
			buff = UnitBuff(unit, i);
			button = getglobal("Nurfed_"..unit.."Buff"..i);
			if (buff and button) then
				icon = getglobal(button:GetName().."Icon");
				icon:SetTexture(buff);
				button:Show();
				num = num + 1;
			elseif (button) then
				button:Hide();
			end
		end

		if (num > 10) then
			size = 12;
		else
			size = 16;
		end

		for i = 1, 10 do
			button = getglobal("Nurfed_"..unit.."Buff"..i);
			if (button) then
				button:SetWidth(size);
				button:SetHeight(size);
			end
		end

		if (string.find(unit, "party[1-4]") and self:GetOption("partylink") == 1) then
			self:UpdatePartyBackground();
		end

		num = 0;
		for i = 1, 16 do
			debuff, debuffApplications = UnitDebuff(unit, i);
			button = getglobal("Nurfed_"..unit.."DeBuff"..i);
			if (debuff and button) then
				debuffcount = getglobal(button:GetName().."Count");
				border = getglobal(button:GetName().."Border");
				icon = getglobal(button:GetName().."Icon");
				icon:SetTexture(debuff);
				button.isdebuff = 1;
				button:Show();
				border:Show();
				num = num + 1;
				if (debuffApplications > 1) then
					debuffcount:SetText(debuffApplications);
					debuffcount:Show();
				else
					debuffcount:Hide();
				end
				if (string.find(unit, "party[1-4]")) then
					if (UnitDebuff(unit, i, 1)) then
						if (not button:GetScript("OnUpdate")) then
							button.flashtime = GetTime();
							button.update = 0;
							button.flashdct = 1;
							button:SetScript("OnUpdate", function() Nurfed_HPBar_OnUpdate(arg1) end);
						end
					else
						button:SetScript("OnUpdate", nil);
						button:SetAlpha(1);
					end
				end
			elseif (button) then
				button:Hide();
				button:SetScript("OnUpdate", nil);
			end
		end

		if (num > 10) then
			size = 12;
		else
			size = 16;
		end

		for i = 1, 10 do
			button = getglobal("Nurfed_"..unit.."DeBuff"..i);
			if (button) then
				button:SetWidth(size);
				button:SetHeight(size);
			end
		end
	end

	function Nurfed_UnitFrames:UpdatePartyBG(unit)
		local frame = getglobal("Nurfed_"..unit.."Highlight");
		if (UnitName("target") == UnitName(unit)) then
			frame:Show();
		else
			frame:Hide();
		end
	end

	function Nurfed_UnitFrames:ScaleFrame(unit)
		if (not unit) then
			return;
		end
		local frame;

		if (unit == "party") then
			for i = 1, 4 do
				frame = getglobal("Nurfed_"..unit..i);
				frame:SetScale(self:GetOption(unit.."scale"));
				utility:ValidatePosition(frame);
			end
			Nurfed_party:SetScale(self:GetOption(unit.."scale"));
		else
			frame = getglobal("Nurfed_"..unit);
			frame:SetScale(self:GetOption(unit.."scale"));
			utility:ValidatePosition(frame);
		end
	end

	function Nurfed_UnitFrames:ScaleAll()
		self:ScaleFrame("player");
		self:ScaleFrame("target");
		self:ScaleFrame("pet");
		self:ScaleFrame("party");
	end

	function Nurfed_UnitFrames:UpdateToTInfo(unit)
		local frame = getglobal("Nurfed_"..unit);
		if (UnitExists(unit) and self:GetOption(unit) == 1) then
			local name = getglobal("Nurfed_"..unit.."Name");
			local hpbar = getglobal("Nurfed_"..unit.."HealthBar");
			local hptext = getglobal("Nurfed_"..unit.."HealthBarText");
			local info = unitlib:Reaction(unit);
			local currhp, maxhp, perc, color = unitlib:Health(unit);
			name:SetText(UnitName(unit));
			name:SetTextColor(info.r, info.g, info.b);
			hpbar:SetMinMaxValues(0, maxhp);
			hpbar:SetValue(currhp);
			hpbar:SetStatusBarColor(color.r, color.g, color.b);
			hptext:SetText(format("%.0f", perc).."%");
			frame:Show();
		else
			frame:Hide();
		end
	end

	function Nurfed_UnitFrames:UpdateCombo()
		local points = GetComboPoints();

		if (points > 0) then
			Nurfed_targetComboText:SetText(points);
			Nurfed_targetComboText:SetTextColor(0 + (0.2 * points), 1 - (0.2 * points), 0);
			Nurfed_targetComboText:Show();
		else
			Nurfed_targetComboText:Hide();
		end
	end

	function Nurfed_UnitFrames:UpdateTargetAuras()
		if (UnitIsFriend("player", "target")) then
			Nurfed_targetBuff1:SetPoint("TOPLEFT", "Nurfed_target", "BOTTOMLEFT", 5, 2);
			Nurfed_targetDeBuff1:SetPoint("TOPLEFT", "Nurfed_targetBuff1", "BOTTOMLEFT", 0, 0);
		else
			Nurfed_targetDeBuff1:SetPoint("TOPLEFT", "Nurfed_target", "BOTTOMLEFT", 5, 2);
			Nurfed_targetBuff1:SetPoint("TOPLEFT", "Nurfed_targetDeBuff1", "BOTTOMLEFT", 0, 0);
		end
	end

	function Nurfed_UnitFrames:UpdateBarTexture()
		local texture, health, mana;
		if (self:GetOption("bartexture") == 1) then
			texture = "Interface\\TargetingFrame\\UI-StatusBar";
		else
			texture = "Interface\\AddOns\\Nurfed_UnitFrames\\images\\statusbar"..self:GetOption("bartexture")..".tga";
		end
		for k, v in unitlib.UnitNames do
			health = getglobal("Nurfed_"..v.."HealthBarTex");
			if (health) then
				health:SetTexture(texture);
			end
			mana = getglobal("Nurfed_"..v.."ManaBarTex");
			if (mana) then
				mana:SetTexture(texture);
			end
		end
	end

	function Nurfed_UnitFrames:UpdateTextFormat()
		if (self:GetOption("textformat") == 1) then
			self.TextFormat = "%d / %d";
		elseif (self:GetOption("textformat") == 2) then
			self.TextFormat = "%d | %d";
		elseif (self:GetOption("textformat") == 3) then
			self.TextFormat = "%d (%d)";
		elseif (self:GetOption("textformat") == 4) then
			self.TextFormat = "%d %d";
		end
		for k, v in unitlib.UnitNames do
			self:UpdateHealth(v);
			self:UpdateMana(v);
		end
	end

	function Nurfed_UnitFrames:InitText()
		local hpalign, mpalign, missing, outline, size;

		if (self:GetOption("hptextalign") == 1) then
			hpalign = "LEFT";
		elseif(self:GetOption("hptextalign") == 2) then
			hpalign = "CENTER";
		elseif(self:GetOption("hptextalign") == 3) then
			hpalign = "RIGHT";
		end

		if (self:GetOption("mptextalign") == 1) then
			mpalign = "LEFT";
		elseif(self:GetOption("mptextalign") == 2) then
			mpalign = "CENTER";
		elseif(self:GetOption("mptextalign") == 3) then
			mpalign = "RIGHT";
		end

		if (self:GetOption("hptextalign") == 1 or self:GetOption("hptextalign") == 2) then
			missing = "RIGHT";
		elseif(self:GetOption("hptextalign") == 3) then
			missing = "LEFT";
		end

		if (self:GetOption("outlinetext") == 1) then
			outline = "OUTLINE";
		else
			outline = "NONE";
		end

		size = self:GetOption("textsize");

		for k, v in unitlib.UnitNames do
			getglobal("Nurfed_"..v.."HealthBarText"):SetFont("Fonts\\FRIZQT__.TTF", size, outline);
			getglobal("Nurfed_"..v.."HealthBarText"):SetJustifyH(hpalign);
			getglobal("Nurfed_"..v.."HealthBarText"):SetText(nil);

			getglobal("Nurfed_"..v.."HealthBarMissingText"):SetFont("Fonts\\FRIZQT__.TTF", size, outline);
			getglobal("Nurfed_"..v.."HealthBarMissingText"):SetJustifyH(missing);
			getglobal("Nurfed_"..v.."HealthBarMissingText"):SetText(nil);

			getglobal("Nurfed_"..v.."ManaBarText"):SetFont("Fonts\\FRIZQT__.TTF", size, outline);
			getglobal("Nurfed_"..v.."ManaBarText"):SetJustifyH(mpalign);
			getglobal("Nurfed_"..v.."ManaBarText"):SetText(nil);
			self:UpdateHealth(v);
			self:UpdateMana(v);
		end
		getglobal("Nurfed_targettargetHealthBarText"):SetJustifyH("RIGHT");
		getglobal("Nurfed_targettargetHealthBarText"):SetTextColor(1, 0, 0);
		getglobal("Nurfed_targettargettargetHealthBarText"):SetJustifyH("RIGHT");
		getglobal("Nurfed_targettargettargetHealthBarText"):SetTextColor(1, 0, 0);
	end

	function Nurfed_UnitFrames:StatusText(unit)
		if (self:GetOption(unit.."hptext") == 1) then
			if (unit == "party") then
				for i = 1, 4 do
					getglobal("Nurfed_"..unit..i.."HealthBarText"):Show();
				end
			else
				getglobal("Nurfed_"..unit.."HealthBarText"):Show();
			end
		else
			if (unit == "party") then
				for i = 1, 4 do
					getglobal("Nurfed_"..unit..i.."HealthBarText"):Hide();
				end
			else
				getglobal("Nurfed_"..unit.."HealthBarText"):Hide();
			end
		end

		if (self:GetOption(unit.."mptext") == 1) then
			if (unit == "party") then
				for i = 1, 4 do
					getglobal("Nurfed_"..unit..i.."ManaBarText"):Show();
				end
			else
				getglobal("Nurfed_"..unit.."ManaBarText"):Show();
			end
		else
			if (unit == "party") then
				for i = 1, 4 do
					getglobal("Nurfed_"..unit..i.."ManaBarText"):Hide();
				end
			else
				getglobal("Nurfed_"..unit.."ManaBarText"):Hide();
			end
		end
	end

	function Nurfed_UnitFrames:UpdatePlayerModel()
		if (self:GetOption("playermodel") == 1) then
			Nurfed_playerModel:ClearAllPoints();
			Nurfed_playerModel:SetPoint("BOTTOMRIGHT", "Nurfed_player", "BOTTOMRIGHT", -5, 4);
			Nurfed_playerModel:Show();
			Nurfed_playerHealthBar:ClearAllPoints();
			Nurfed_playerHealthBar:SetPoint("LEFT", "Nurfed_player", "LEFT", 5, -1);
			Nurfed_playerPvPIcon:ClearAllPoints();
			Nurfed_playerPvPIcon:SetPoint("TOPRIGHT", "Nurfed_player", "TOPRIGHT", -35, -5);
			Nurfed_playerStatusIcon:ClearAllPoints();
			Nurfed_playerStatusIcon:SetPoint("TOPLEFT", "Nurfed_player", "TOPLEFT", 4, -3);
			Nurfed_playerHealthBar:SetWidth(128);
			Nurfed_playerManaBar:SetWidth(128);
		elseif (self:GetOption("playermodel") == 2) then
			Nurfed_playerModel:ClearAllPoints();
			Nurfed_playerModel:SetPoint("BOTTOMLEFT", "Nurfed_player", "BOTTOMLEFT", 6, 4);
			Nurfed_playerModel:Show();
			Nurfed_playerHealthBar:ClearAllPoints();
			Nurfed_playerHealthBar:SetPoint("RIGHT", "Nurfed_player", "RIGHT", -5, -1);
			Nurfed_playerPvPIcon:ClearAllPoints();
			Nurfed_playerPvPIcon:SetPoint("TOPRIGHT", "Nurfed_player", "TOPRIGHT", 0, -5);
			Nurfed_playerStatusIcon:ClearAllPoints();
			Nurfed_playerStatusIcon:SetPoint("TOPLEFT", "Nurfed_player", "TOPLEFT", 40, -3);
			Nurfed_playerHealthBar:SetWidth(128);
			Nurfed_playerManaBar:SetWidth(128);
		else
			Nurfed_playerModel:Hide();
			Nurfed_playerPvPIcon:ClearAllPoints();
			Nurfed_playerPvPIcon:SetPoint("TOPRIGHT", "Nurfed_player", "TOPRIGHT", 0, -5);
			Nurfed_playerStatusIcon:ClearAllPoints();
			Nurfed_playerStatusIcon:SetPoint("TOPLEFT", "Nurfed_player", "TOPLEFT", 4, -3);
			Nurfed_playerHealthBar:SetWidth(165);
			Nurfed_playerManaBar:SetWidth(165);
		end
	end

	function Nurfed_UnitFrames:UpdateHappiness()
		local happiness, damagePercentage, loyaltyRate = GetPetHappiness();
		local hasPetUI, isHunterPet = HasPetUI();
		if (happiness or isHunterPet) then
			local name = UnitLevel("pet").." "..UnitName("pet");
			Nurfed_petHappinessIcon:Show();
			if ( happiness == 1 ) then
				Nurfed_petName:SetTextColor(1, 0.5, 0);
				Nurfed_petHappinessIcon:SetTexCoord(0.375, 0.5625, 0, 0.359375);
			elseif ( happiness == 2 ) then
				Nurfed_petName:SetTextColor(1, 1, 0);
				Nurfed_petHappinessIcon:SetTexCoord(0.1875, 0.375, 0, 0.359375);
			elseif ( happiness == 3 ) then
				Nurfed_petName:SetTextColor(0, 1, 0);
				Nurfed_petHappinessIcon:SetTexCoord(0, 0.1875, 0, 0.359375);
			end
			if ( loyaltyRate < 0 ) then
				Nurfed_petName:SetText("-"..name.."-");
			elseif ( loyaltyRate > 0 ) then
				Nurfed_petName:SetText("+"..name.."+");
			else
				Nurfed_petName:SetText(name);
			end
		end
	end

	function Nurfed_UnitFrames:LinkParty(bg)
		local i, frame, buff;
		if (self:GetOption("partylink") == 1) then
			for i = 1, 4 do
				frame = getglobal("Nurfed_party"..i);
				buff = getglobal("Nurfed_party"..i.."Buff1");
				buff:ClearAllPoints();
				buff:SetPoint("TOPLEFT", frame, "BOTTOMLEFT", 3, 3);
				frame:SetBackdropColor(0, 0, 0, 0);
				frame:SetBackdropBorderColor(0, 0, 0, 0);
				if (i > 1) then
					frame:ClearAllPoints();
					frame:SetPoint("TOPLEFT", getglobal("Nurfed_party"..(i-1).."Buff1"), "BOTTOMLEFT", -3, 3);
					if (frame:IsMovable()) then
						frame:SetUserPlaced(1);
						frame:SetMovable(0);
					end
				end
			end
			Nurfed_party:SetPoint("TOPLEFT", Nurfed_party1, "TOPLEFT", 0, 0);
			self:UpdatePartyBackground();
		else
			for i = 1, 4 do
				frame = getglobal("Nurfed_party"..i);
				buff = getglobal("Nurfed_party"..i.."Buff1");
				buff:ClearAllPoints();
				buff:SetPoint("TOPLEFT", frame, "BOTTOMLEFT", 3, 2);
				frame:SetMovable(1);
				frame:SetUserPlaced(1);
			end
			if (not bg) then
				self:Background("party");
			end
			Nurfed_party:Hide();
		end
	end

	function Nurfed_UnitFrames:UpdatePartyBackground()
		local num = GetNumPartyMembers();
		local height = 0;
		local i, frame, buff;
		if (self:GetOption("partylink") == 1 and num > 0) then
			for i = 1, num do
				frame = getglobal("Nurfed_party"..i);
				buff = getglobal("Nurfed_party"..i.."Buff1");
				petframe = getglobal("Nurfed_partypet"..i);
				if (UnitExists("partypet"..i)) then
					height = height + ((frame:GetHeight() + buff:GetHeight() + petframe:GetHeight()) * frame:GetEffectiveScale()) + 3;
				else
					height = height + ((frame:GetHeight() + buff:GetHeight()) * frame:GetEffectiveScale()) + 3;
				end
			end
			Nurfed_party:Show();
			Nurfed_party:SetHeight(height);
		else
			Nurfed_party:Hide();
		end
	end

	function Nurfed_UnitFrames:UpdatePartyPet()
		local id = this:GetID();
		if (UnitExists("partypet"..id)) then
			getglobal("Nurfed_party"..id):SetHeight(48);
			getglobal("Nurfed_partypet"..id):Show();
			self:UpdateName("partypet"..id);
			self:UpdateHealth("partypet"..id);
		else
			getglobal("Nurfed_party"..id):SetHeight(39);
			getglobal("Nurfed_partypet"..id):Hide();
		end
		if (self:GetOption("partylink") == 1) then
			self:UpdatePartyBackground();
		end
	end
end
