local HPHeight=1.0
local FontSize=16
local TrinketFont=24
local ChildrenFrames=3
local DecayTime=30
local HPFont=25
local DB1=false-- For debugging frame removal / additions
local DB2=false --For debugging creating combat versus non combat frames
local DB3=false --For debugging adding buttons to click frames

function MainFrameCreate()
	mainframe=CreateFrame("Frame", "mainframe", UIParent)
	mainframe:SetWidth(XSize)
	mainframe:SetHeight(YSize)
	mainframe:ClearAllPoints()
	mainframe:SetPoint("CENTER",UIParent)
	mainframe:Show()
	mainframe:EnableMouse(true)
	mainframe:SetMovable(true)
	mainframe:RegisterForDrag("LeftButton")
	mainframe:SetScript("OnDragStart", mainframe.StartMoving)
	mainframe:SetScript("OnDragStop", mainframe.StopMovingOrSizing)
	
	local tex = mainframe:CreateTexture("ARTWORK");
	tex:SetAllPoints();
	tex:SetTexture(0, 0, 0); tex:SetAlpha(0.7);
	--[[
	button=CreateFrame("Button", "testbutton", mainframe, "SecureActionButtonTemplate")
	button:SetPoint("TOPLEFT", 50, 50)
	button:SetHeight(20)
	button:SetWidth(50)
	button:Show()
	button:SetText("Test")
        button:SetNormalTexture("Interface/Buttons/UI-Panel-Button-Up")
        button:SetHighlightTexture("Interface/Buttons/UI-Panel-Button-Highlight")
        button:SetPushedTexture("Interface/Buttons/UI-Panel-Button-Down")
	button:SetAttribute("type", "target")
	button:SetAttribute("unit", targetname)
	--]]
end

function AddFrame(sourceName)
	PlayerNamesDisplayed[sourceName]=true
	--Finding the current last frame in the chain
	if DB2 then print("We are", UnitAffectingCombat("player")," in combat.", sourceName, "is", PlayerTargetable[sourceName], "targetable. Frame existance is", _G["PlayerFrame" .. sourceName]) end
	CurrentParent=mainframe
	if DB1 then print("I see", CurrentParent:GetNumChildren(), "frames off of mainframe") end
	if CurrentParent:GetNumChildren()>1 then --There are player frames in existance
		CurrentParent=select(2, CurrentParent:GetChildren()) --frame is now the first player frame
		local health=select(1, CurrentParent:GetChildren())
		local name=health.name:GetText()
		if DB1 then print("I set the parent frame to be the frame named", name) end
		repeat 
			local NextFrameExists
			if CurrentParent:GetNumChildren()>ChildrenFrames then 
				CurrentParent=select(ChildrenFrames+1, CurrentParent:GetChildren()) 
				health=select(1, CurrentParent:GetChildren())
				name=health.name:GetText()
				if DB1 then print("I went to the next player frame, named", name) end
				NextFrameExists=true
			else
				NextFrameExists=false
			end
		until not NextFrameExists
	end 
	if _G["PlayerFrame" .. sourceName] then --Frame exists 
		if not _G["PlayerFrame" .. sourceName]:GetParent() then 
			NumberOfFramesActive=NumberOfFramesActive+1
			local health
			local name
			if NumberOfFramesActive==1 then
				name="mainframe"
			else
				health=select(1, CurrentParent:GetChildren())
				if health then name=health.name:GetText() else name="?" end
			end
			if DB1 then print("There should be", NumberOfFramesActive, "frames active.") end
			if DB1 then print("frame for", sourceName, "exists and will show now, attached to parent, named", name) end
			_G["PlayerFrame" .. sourceName] :Show()
			_G["PlayerFrame" .. sourceName] :SetParent(CurrentParent)
			if NumberOfFramesActive==1 then
				_G["PlayerFrame" .. sourceName]:SetPoint("TOP", CurrentParent, "TOP")
				if DB1 then print("anchoring frame to top of parent frame") end
			else
				_G["PlayerFrame" .. sourceName]:SetPoint("TOP", CurrentParent, "BOTTOM")
				if DB1 then print("anchoring frame to bottom of parent frame") end
			end
			CurrentParent=_G["PlayerFrame" .. sourceName] 
		else --Frame exists and has parent.
			if DB1 then print("frame for", sourceName, "exists and is already showing.") end
		end
	else --No frame exists for this player yet, shown or hidden
		--PlayerNamesList[table.maxn(PlayerNamesList)+1]=sourceName
		--print("added", sourceName,"to the names list.")
		NumberOfFramesActive=NumberOfFramesActive+1
		if DB1 then print("Creating a new frame for", sourceName) end
		if DB1 then print("There should be", NumberOfFramesActive, "frames active.") end
		_G["PlayerFrame" .. sourceName]=CreateFrame("Frame", nil, CurrentParent)
		PlayerTargetable[sourceName]=false
		_G["PlayerFrame" .. sourceName]:SetWidth(XSize)
		_G["PlayerFrame" .. sourceName]:SetHeight(CurrentFrameHeight)
		if NumberOfFramesActive==1 then
			_G["PlayerFrame" .. sourceName]:SetPoint("TOP", CurrentParent, "TOP")
		else
			_G["PlayerFrame" .. sourceName]:SetPoint("TOP", CurrentParent, "BOTTOM")
		end
		_G["PlayerFrame" .. sourceName]:Show()
		_G["PlayerFrame" .. sourceName]:SetBackdrop({
	       bgFile="Interface\\DialogFrame\\UI-DialogBox-Background", tile=false})
		CurrentParent=_G["PlayerFrame" .. sourceName]
		
		--Creation of Health Bar, 1st child of player frame
		_G["PlayerFrame" .. sourceName .. "Health"] = CreateFrame("StatusBar", nil, _G["PlayerFrame" .. sourceName])
		_G["PlayerFrame" .. sourceName .. "Health"]:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
		_G["PlayerFrame" .. sourceName .. "Health"]:GetStatusBarTexture():SetHorizTile(false)
		_G["PlayerFrame" .. sourceName .. "Health"]:SetPoint("TOP",_G["PlayerFrame" .. sourceName],"TOP")
		_G["PlayerFrame" .. sourceName .. "Health"]:SetMinMaxValues(0,100)
		--Creation of name on health bar, child of health bar
		_G["PlayerFrame" .. sourceName .. "Health"].name=_G["PlayerFrame" .. sourceName .. "Health"]:CreateFontString(nil, "OVERLAY")
		_G["PlayerFrame" .. sourceName .. "Health"].name:SetPoint("LEFT", _G["PlayerFrame" .. sourceName .. "Health"], "LEFT")
		_G["PlayerFrame" .. sourceName .. "Health"].name:SetFont("Fonts\\FRIZQT__.TTF", FontSize, "OUTLINE")
		_G["PlayerFrame" .. sourceName .. "Health"].name:SetJustifyH("LEFT")
		_G["PlayerFrame" .. sourceName .. "Health"].name:SetText(sourceName)
		--Creation of hp text
		_G["PlayerFrame" .. sourceName .. "Health"].value=_G["PlayerFrame" .. sourceName .. "Health"]:CreateFontString(nil, "OVERLAY")
		_G["PlayerFrame" .. sourceName .. "Health"].value:SetPoint("RIGHT", _G["PlayerFrame" .. sourceName .. "Health"], "RIGHT")
		_G["PlayerFrame" .. sourceName .. "Health"].value:SetFont("Fonts\\FRIZQT__.TTF", HPFont, "OUTLINE")
		_G["PlayerFrame" .. sourceName .. "Health"].value:SetJustifyH("LEFT")
		_G["PlayerFrame" .. sourceName .. "Health"].value:SetShadowOffset(1, -1)
		_G["PlayerFrame" .. sourceName .. "Health"].value:SetTextColor(1,1, 1)
		_G["PlayerFrame" .. sourceName .. "Health"].value:SetText("??%")
		--Creation of pvp trinket, 2nd child of player frame
		_G["PlayerFrame" .. sourceName .. "Trinket"] = CreateFrame("Frame", nil, _G["PlayerFrame" .. sourceName])
		_G["PlayerFrame" .. sourceName .. "Trinket"]:SetPoint("TOPRIGHT",_G["PlayerFrame" .. sourceName],"TOPRIGHT")
		_G["PlayerFrame" .. sourceName .. "Trinket"]:SetBackdrop({bgFile = "Interface/AddOns/HelloAce/Icons/Trinket",
			edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
			tile = false, tileSize = 16, edgeSize = 16, 
			insets = { left = 4, right = 4, top = 4, bottom = 4 }});
		--Creation of "cooldown frame" on top of the frame.
		_G["PlayerFrame" .. sourceName .. "Trinket"].cooldown=CreateFrame("Cooldown", nil, _G["PlayerFrame" .. sourceName .. "Trinket"], "CooldownFrameTemplate")
		_G["PlayerFrame" .. sourceName .. "Trinket"].cooldown:SetAllPoints()
		if PlayerTrinketTimes[sourceName] then
			if GetTime()-PlayerTrinketTimes[sourceName]<120 then
				_G["PlayerFrame" .. sourceName .. "Trinket"].cooldown:SetCooldown(PlayerTrinketTimes[sourceName], 120)
			end
		end
		--Creation of text on PvP trinket
		_G["PlayerFrame" .. sourceName .. "Trinket"].number=_G["PlayerFrame" .. sourceName .. "Trinket"]:CreateFontString(nil, "OVERLAY")
		_G["PlayerFrame" .. sourceName .. "Trinket"].number:SetPoint("Center", _G["PlayerFrame" .. sourceName .. "Trinket"], "CENTER")
		_G["PlayerFrame" .. sourceName .. "Trinket"].number:SetFont("Fonts\\FRIZQT__.TTF", TrinketFont, "OUTLINE")
		_G["PlayerFrame" .. sourceName .. "Trinket"].number:SetJustifyH("CENTER")
		--Creation of Class/Spec icon, 3rd child of prayer frame
		_G["PlayerFrame" .. sourceName .. "Class"] = CreateFrame("Frame", nil, _G["PlayerFrame" .. sourceName])
		_G["PlayerFrame" .. sourceName .. "Class"]:SetPoint("TOPLEFT",_G["PlayerFrame" .. sourceName],"TOPLEFT")
		_G["PlayerFrame" .. sourceName .. "Class"]:SetBackdrop({bgFile = "Interface/AddOns/HelloAce/Icons/Unknown", tile=false});
		--4th parent will be the next player frame
	end
	AdjustFrameHeights()
end

function AdjustFrameHeights()
	if not CombatStatus then CalculateFrameHeights() end
	local frame=mainframe
	if frame:GetNumChildren()>1  then --There are player frames in existance and we arent in combat.
		frame=select(2, frame:GetChildren()) --frame is now the first player frame
		repeat 
			local RealFrameHeight=CurrentFrameHeight
			frame:SetHeight(RealFrameHeight)
			frame:SetWidth(XSize)
			health=select(1, frame:GetChildren())
			trinket=select(2, frame:GetChildren())
			class=select(3, frame:GetChildren())
			if health then 
				health:SetHeight(HPHeight*RealFrameHeight)
				health:SetWidth(math.min(XSize-(1+HPHeight)*RealFrameHeight))
				health:SetPoint("TOPLEFT", class, "TOPRIGHT")
				if health:GetNumChildren()==1 then
					local button=select(1, health:GetChildren())
					button:SetHeight(HPHeight*RealFrameHeight)
					button:SetWidth(math.min(XSize-(1+HPHeight)*RealFrameHeight))
				end
				if trinket then
					trinket:SetHeight(RealFrameHeight)
					trinket:SetWidth(RealFrameHeight)
					trinket:SetBackdrop({bgFile = "Interface/AddOns/HelloAce/Icons/Trinket",
						edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
						tileSize = 16*RealFrameHeight/50, edgeSize = 16*RealFrameHeight/50, 
						insets = { left = 4*RealFrameHeight/50, right = 4*RealFrameHeight/50,
						top = 4*RealFrameHeight/50, bottom = 4*RealFrameHeight/50 }});
					trinket.number:SetFont("Fonts\\FRIZQT__.TTF", TrinketFont, "OUTLINE")
					if trinket.number then
						if (0.5*RealFrameHeight)<TrinketFont then
							trinket.number:SetTextHeight(math.max((0.5*RealFrameHeight), 1))
						else
							trinket.number:SetTextHeight(TrinketFont)
						end
					end
				end
				if class then
					class:SetHeight(HPHeight*RealFrameHeight)
					class:SetWidth(HPHeight*RealFrameHeight)
				end
				if health then
					if health.name then
						health.name:SetFont("Fonts\\FRIZQT__.TTF", FontSize, "OUTLINE")
						if RealFrameHeight<FontSize then 
							health.name:SetTextHeight(math.max(RealFrameHeight, 1))
						else
							health.name:SetTextHeight(FontSize)
						end
					end
					if health.value then
						health.value:SetFont("Fonts\\FRIZQT__.TTF", HPFont, "OUTLINE")
					end
					if health.value then
						if RealFrameHeight<HPFont then
							health.value:SetTextHeight(math.max(RealFrameHeight, 1))
						else
							health.value:SetTextHeight(HPFont)
						end
					end
				end
			end
			if select(ChildrenFrames+1, frame:GetChildren()) then 
				frame=select(ChildrenFrames+1, frame:GetChildren()) 
			else
				frame=nil
			end
		until not frame
	end
	UpgradeFrames()
end

function SetFrameColors(playername)
	if _G["PlayerFrame" .. playername .. "Health"] then
		local T
		if PlayerLastActive[playername] then
			T=1-math.min((math.min(GetTime()-PlayerLastActive[playername], DecayTime))/DecayTime), 0 --Will range from 1 to 0 as time past goes from 0 to DecayTime
		else
			T=1
		end
		T=math.min(1, T*1.25)
		if playername=="Predatoria" then --Special color for myself
			_G["PlayerFrame" .. playername .. "Health"]:SetStatusBarColor(T*187/255, T*230/255, T*237/255) 
			--SetStatusBarColor(212/255, 249/255, 255/255) 
		elseif ClassByName[playername] then --Know or suspect a class for this player
			_G["PlayerFrame" .. playername .. "Health"]:SetStatusBarColor(T*(ClassColors[(ClassByName[playername])[1]])[1], T*(ClassColors[(ClassByName[playername])[1]])[2], T*(ClassColors[(ClassByName[playername])[1]])[3])
		else                                             --Don't know class for this player
			_G["PlayerFrame" .. playername .. "Health"]:SetStatusBarColor(T*0.5, T*0.5, T*0.5) 
		end
	end
end

function SetClassFrame(playername)
	if _G["PlayerFrame" .. playername .. "Class"] then --the frame exists
		if ClassByName[playername] then
			if playername=="Predatoria" then --Giving myself a special icon
				_G["PlayerFrame" .. playername .. "Class"]:SetBackdrop({bgFile = "Interface/AddOns/HelloAce/Icons/Predatoria", tile=false})
			elseif (ClassByName[playername])[3] then --We know the spec
				local filepath="Interface/AddOns/HelloAce/Icons/" .. (ClassByName[playername])[3]
				_G["PlayerFrame" .. playername .. "Class"]:SetBackdrop({bgFile = filepath, tile=false})
			elseif (ClassByName[playername])[1] then  --We don't know the spec but know the class
				local filepath="Interface/AddOns/HelloAce/Icons/" .. (ClassByName[playername])[1]
				_G["PlayerFrame" .. playername .. "Class"]:SetBackdrop({bgFile = filepath, tile=false})
			else --We don't know the spec or class
				_G["PlayerFrame" .. playername .. "Class"]:SetBackdrop({bgFile = "Interface/AddOns/HelloAce/Icons/Unknown", tile=false})
			end
		end
	end
end

function RemoveFrame(name)
	--print("Status of player frame", PlayerNamesDisplayed[name], "Status of frame of player", _G["PlayerFrame" .. name])
	if _G["PlayerFrame" .. name]:IsShown() then --Frame exists.  Must attach children to the parent and remove frame from the chain
		NumberOfFramesActive=NumberOfFramesActive-1
		if DB1 then print("attempting to remove frame for", name, ". ", NumberOfFramesActive, "will be active.") end
		
		
		parent=_G["PlayerFrame" .. name]:GetParent()
		local parentname
		if parent then
			if parent:GetNumChildren()<=1 then --The parent is the main frame
				parentname="mainframe"
			else
				local health=select(1, parent:GetChildren())
				if health then
					if health.name then 
						parentname=health.name:GetText() 
					else 
						parentname=nil
					end
				else
					parentname=nil
				end
			end
		else
			if DB1 then print("Found parent to be nil for this frame.") end
		end
		if DB1 then print("Parent is", parentname) end
		_G["PlayerFrame" .. name]:SetParent(mainframe) --Changing this parent removes a child from the previous frame
		if _G["PlayerFrame" .. name]:GetNumChildren()==ChildrenFrames+1 then --Children player frames exist
			if DB1 then print("Frame has frames afterwards.  Need to move them.") end
			playerchild=select(ChildrenFrames+1, _G["PlayerFrame" .. name]:GetChildren())
			playerchild:SetParent(parent)
			if parent==mainframe then
				playerchild:SetPoint("TOP", parent, "TOP")
				if DB1 then print("Frame was the first one.  Attaching to top of mainframe.") end
			else
				playerchild:SetPoint("TOP", parent, "BOTTOM")
				if DB1 then print("Parent was not mainframe.  Attaching to bottom of previous.") end
			end
		else
			CurrentParent=parent
			if DB1 then print("Frame was last in chain.  No need to move children.") end
		end
		PlayerNamesDisplayed[name]=nil
		_G["PlayerFrame" .. name]:Hide()
		_G["PlayerFrame" .. name]:SetParent(nil)
		PlayerHealth[name]=nil
		PlayerMaxHealth[name]=nil
	end
	UpdateTrinketDisplay() --This is to get it from momentarily showing frames at the bottom.  Fixes a bug, unsure why it works.
end

function UpdateTrinketDisplay()
	local frame=mainframe
	local NumberOfFramesActive=0
	if frame:GetNumChildren()>1 then --There are player frames in existance
		frame=select(2, frame:GetChildren()) --frame is now the first player frame
		frame:SetPoint("TOP", mainframe, "TOP") --Fixing a bug that appeared 
		NumberOfFramesActive=1
		repeat 
			health=select(1, frame:GetChildren())
			local name
			if health.name then 
				name=health.name:GetText()
			else
				name="??"
			end
			PlayerNamesDisplayed[name]=true
			trinket=select(2, frame:GetChildren())
			if trinket then
				if PlayerTrinketTimes[name] then
					if GetTime()-PlayerTrinketTimes[name]<120 then
						local time=120-(GetTime()-PlayerTrinketTimes[name])
						local text=string.format("%.0f", time)
						trinket.number:SetText(text)
						trinket.number:SetTextColor(2*time/120, -2*time/120+2, 0)
					else
						trinket.number:SetText("")
						trinket.number:SetTextColor(0,1,0)
					end
				else
					trinket.number:SetText("")
					trinket.number:SetTextColor(0,1,0)
				end
			end
			if select(ChildrenFrames+1, frame:GetChildren()) then 
				frame=select(ChildrenFrames+1, frame:GetChildren()) 
				NumberOfFramesActive=NumberOfFramesActive+1
			else
				frame=nil
			end
		until not frame
	end
end

function RemoveOldFrames()
	local frame=mainframe
	if frame:GetNumChildren()>1 then --There are player frames in existance
		frame=select(2, frame:GetChildren()) --frame is now the first player frame
		repeat
			health=select(1, frame:GetChildren())
			local name=health.name:GetText()
			if frame:GetNumChildren()==ChildrenFrames+1 then
			--if select(ChildrenFrames+1, frame:GetChildren()) then
				frame=select(ChildrenFrames+1, frame:GetChildren())
			else
				frame=nil
			end
			if PlayerLastActive[name] then
				if GetTime()-PlayerLastActive[name]>DecayTime then
					if PlayerNamesDisplayed[name] then
						RemoveFrame(name)
					end
				end
			end
		until not frame
	end
	AdjustFrameHeights()
end

function SetHealthBars()
	local frame=mainframe
	if frame:GetNumChildren()>1 then --There are player frames in existance
		frame=select(2, frame:GetChildren()) --frame is now the first player frame
		repeat
			health=select(1, frame:GetChildren())
			local name
				if health then
					if health.name then
						name=health.name:GetText()
					else
						name="??"
					end
				else
					name="??"
				end
			if PlayerHealth[name] then
				local percent=PlayerHealth[name]/PlayerMaxHealth[name]*100
				local hp=string.format("%.0f%%", percent)
				health.value:SetText(hp)
				local r
				local g
				local b
				local P=percent/100
				if P>0.67 then
					r=1
					g=(255-(255-196)*(1-P)/.33)/255
					b=(255-255*(1-P)/.33)/255
				elseif P>0.33 then
					r=255/255
					g=(196-(196-111)*(0.67-P)/.34)/255
					b=0
				else
					r=(255-(255-192)*(0.33-P)/.33)/255
					g=(111-111*(0.33-P)/0.33)/255
					b=0
				end
				if health then
					if health:GetObjectType()=="StatusBar" then
						health:SetValue(percent)
						if health.value then
							health.value:SetTextColor(r,g,b)
						end
					end
				end
			else
				if health then
					if health:GetObjectType()=="StatusBar" then
						health:SetValue(100)
						if health.value then
							health.value:SetText("??%")
							health.value:SetTextColor(1,1, 1)
						end
					end
				end
			end
			if select(ChildrenFrames+1, frame:GetChildren()) then
				frame=select(ChildrenFrames+1, frame:GetChildren())
			else
				frame=nil
			end
		until not frame
	end
end

function UpgradeFrames(safe)
	if not CombatStatus then
		--print("Making Frames Clickable of height and width", CurrentFrameHeight, XSize)
		local FrameIndex=0
		local frame=mainframe
		local currentparent=nil
		if frame:GetNumChildren()>1 then --There are player frames in existance
			frame=select(2, frame:GetChildren()) --frame is now the first player frame
			currentparent=mainframe
			repeat
				FrameIndex=FrameIndex+1
				health=select(1, frame:GetChildren())
				local name
				if health then 
					if health.name then
						name=health.name:GetText()
					else
						name="??"
					end
				else
					name="??"
				end
				if frame:GetNumChildren()==ChildrenFrames+1 then
				--if select(ChildrenFrames+1, frame:GetChildren()) then
					frame=select(ChildrenFrames+1, frame:GetChildren())
				else
					frame=nil
				end
				if PlayerLastActive[name] then
					if GetTime()-PlayerLastActive[name]>DecayTime then
						if PlayerNamesDisplayed[name] then
							RemoveFrame(name)
						end
					end
				end
				--print("The", FrameIndex, "th frame will target", name)
				if _G["PlayerButton" .. FrameIndex] then --Frame exists, set macro
					_G["PlayerButton" .. FrameIndex]:SetAttribute("type", "macro")
					_G["PlayerButton" .. FrameIndex]:SetAttribute("macrotext", "/target " .. name)
					_G["PlayerButton" .. FrameIndex]:SetWidth(XSize)
					_G["PlayerButton" .. FrameIndex]:SetHeight(CurrentFrameHeight)
				else --Frame doesnt exist, create frame
					_G["PlayerButton" .. FrameIndex]=CreateFrame("Button", nil, currentparent, "SecureActionButtonTemplate")
					_G["PlayerButton" .. FrameIndex]:SetAttribute("type", "macro")
					_G["PlayerButton" .. FrameIndex]:SetAttribute("macrotext", "/target " .. name)
					if currentparent==mainframe then 
						_G["PlayerButton" .. FrameIndex]:SetPoint("TOP",currentparent , "TOP")
					else
						_G["PlayerButton" .. FrameIndex]:SetPoint("TOP",currentparent , "BOTTOM")
					end
					_G["PlayerButton" .. FrameIndex]:SetWidth(XSize)
					_G["PlayerButton" .. FrameIndex]:SetHeight(CurrentFrameHeight)
				end
				_G["PlayerButton" .. FrameIndex]:Show()
				--local child=_G["PlayerButton" .. FrameIndex]:GetChildren()
				--if child then
				--	child:Hide()
				--end
				currentparent=_G["PlayerButton" .. FrameIndex]
			until not frame
		end
	end
end

function CalculateFrameHeights()
	if not CombatStatus then
		CurrentFrameHeight=math.min(YSize/NumberOfFramesActive, FrameHeight)
	end
	--print("Current frame height is", CurrentFrameHeight, "and combat status is", CombatStatus)
end

function SetFrameShades()
	local frame=mainframe
	local NumberOfFramesActive=0
	if frame:GetNumChildren()>1 then --There are player frames in existance
		frame=select(2, frame:GetChildren()) --frame is now the first player frame
		NumberOfFramesActive=1
		repeat 
			health=select(1, frame:GetChildren())
			local name=health.name:GetText()
			SetFrameColors(name)
			if select(ChildrenFrames+1, frame:GetChildren()) then 
				frame=select(ChildrenFrames+1, frame:GetChildren()) 
				NumberOfFramesActive=NumberOfFramesActive+1
			else
				frame=nil
			end
		until not frame
	end
end
