local HealBot_headerno=0;

local HealBot_ClassColR = {};
local HealBot_ClassColG = {};
local HealBot_ClassColB = {};

local HealBot_UnitStatus={};
local HealBot_PlayerDead=false;
local HealBot_CheckGroup=0;
local HealBot_Enabled={};
local HealBot_PetMaxH={};
local HealBot_PetMaxHcnt1={};
local HealBot_PetMaxHcnt2={};
local HealBot_AttribStatus={};
local HealBot_UnitRange={}
local HealBot_UnitRangeSpell={}
local HealBot_UnitRanger={}
local HealBot_UnitRangeg={}
local HealBot_UnitRangeb={}
local HealBot_UnitRangeitr={}
local HealBot_UnitRangeitg={}
local HealBot_UnitRangeitb={}
local HealBot_UnitRangeotr={}
local HealBot_UnitRangeotg={}
local HealBot_UnitRangeotb={}
local HealBot_curUnitHealth={}
local HealBot_UnitBarUpdate={}
local HealBot_TrackBars={}
local HealBot_ResetBars={}
local ceil=ceil;
local unit=nil
local b=nil
local btHBbarText=nil
local btname=nil
local bttextlen=0
local bthlthdelta=0
local floor=floor
local strlower=strlower
local strlen=strlen
local HealBot_ButtonStore={}
local HealBot_hSpell="Heal"
local HealBot_bSpell="Power Word: Fortitude"
local HealBot_dSpell="Cure Disease"
local HealBot_rSpell="Resurrection"

local HealBot_ButtonArray1={}
local HealBot_ButtonArray2={}
local HealBot_ButtonArray3={}
local HealBot_ButtonArray=1
local HealBot_Aggro={}
local HealBot_AggroBar4={}
local HealBot_AggroBar3={}
local HealBot_AggroBarA=0.5
local HealBot_AggroBarAup=true
local HealBot_Action_rButtonOC={}
local HealBot_MyTargets={}
local HB_Action_Timer1 = 2000

function HealBot_Action_AddDebug(msg)
  HealBot_AddDebug("Action: " .. msg);
end

local uaName=nil
local uaBar4=nil
function HealBot_Action_UpdateAggro(unit,status)
  uaName=HealBot_UnitName[unit]
  uaBar4=HealBot_Action_HealthBar4(HealBot_Unit_Button[unit])
  if not uaBar4 then return end
  if not status then
    if HealBot_AggroBar4[uaName] then
	  uaBar4=HealBot_AggroBar4[uaName]
	  uaBar4:SetValue(0)
	  HealBot_AggroBar4[uaName]=nil
	end
  elseif HealBot_Config.ShowAggroBars==1 then
    if not HealBot_AggroBar4[uaName] then
      HealBot_AggroBar4[uaName]=uaBar4
      uaBar4:SetValue(100)
	elseif HealBot_AggroBar4[uaName]~=uaBar4 then
	  uaBar4=HealBot_AggroBar4[uaName]
	  uaBar4:SetValue(0)
	  uaBar4=HealBot_Action_HealthBar4(HealBot_Unit_Button[unit])
      HealBot_AggroBar4[uaName]=uaBar4
      uaBar4:SetValue(100)
	end
  end
  HealBot_Aggro[uaName]=status
  HealBot_UnitRange[unit]=-1
  HealBot_UnitStatus[unit]=1
end

function HealBot_Action_SetrSpell()
  if UnitClass("player")==HEALBOT_DRUID then
    HealBot_hSpell=HEALBOT_REJUVENATION
	HealBot_bSpell=HEALBOT_MARK_OF_THE_WILD
	HealBot_dSpell=HEALBOT_CURE_POISON
	HealBot_rSpell=HEALBOT_REBIRTH
  elseif UnitClass("player")==HEALBOT_HUNTER then
    HealBot_hSpell=HEALBOT_MENDPET
  elseif UnitClass("player")==HEALBOT_MAGE then
    HealBot_hSpell=HEALBOT_ARCANE_INTELLECT
	HealBot_bSpell=HEALBOT_ARCANE_INTELLECT
	HealBot_dSpell=HEALBOT_REMOVE_LESSER_CURSE
  elseif UnitClass("player")==HEALBOT_PALADIN then
    HealBot_hSpell=HEALBOT_HOLY_LIGHT
	HealBot_bSpell=HEALBOT_BLESSING_OF_MIGHT
	HealBot_dSpell=HEALBOT_PURIFY
	HealBot_rSpell=HEALBOT_REDEMPTION
  elseif UnitClass("player")==HEALBOT_PRIEST then
    HealBot_hSpell=HEALBOT_LESSER_HEAL
	HealBot_bSpell=HEALBOT_POWER_WORD_FORTITUDE
	HealBot_dSpell=HEALBOT_CURE_DISEASE
	HealBot_rSpell=HEALBOT_RESURRECTION
  elseif UnitClass("player")==HEALBOT_ROGUE then
    HealBot_hSpell=HEALBOT_HEAVY_RUNECLOTH_BANDAGE
  elseif UnitClass("player")==HEALBOT_SHAMAN then
    HealBot_hSpell=HEALBOT_HEALING_WAVE
	HealBot_bSpell=HEALBOT_EARTH_SHIELD
	HealBot_dSpell=HEALBOT_CURE_DISEASE
	HealBot_rSpell=HEALBOT_ANCESTRALSPIRIT
  elseif UnitClass("player")==HEALBOT_WARLOCK then
    HealBot_hSpell=HEALBOT_UNENDING_BREATH
    HealBot_bSpell=HEALBOT_UNENDING_BREATH
  elseif UnitClass("player")==HEALBOT_WARRIOR then
    HealBot_hSpell=HEALBOT_HEAVY_RUNECLOTH_BANDAGE
  else
    HealBot_AddDebug("UnitClass(HealBot_PlayerName) NOT FOUND in HealBot_Action_SetrSpell()")
  end
  HealBot_SetrSpells(HealBot_hSpell,HealBot_bSpell,HealBot_dSpell,HealBot_rSpell)
end

local hcr=nil
local hcg=nil
local hcb=nil
local hca=nil
local hcpct=nil
local hrpct=100
function HealBot_HealthColor(unit,hlth,maxhlth,tooltipcol,Member_Name,UnitDead,Member_Buff,Member_Debuff,healin)

  hcpct = hlth+healin
  if hcpct<maxhlth then
    hcpct=hcpct/maxhlth
  else
    hcpct=1;
  end
  hrpct=floor((hlth/maxhlth)*100)
  
  if not tooltipcol then
    HealBot_UnitStatus[unit]=1
    if UnitDead and HealBot_HoT_Active_Button[Member_Name] then
      HealBot_HoT_RemoveIcon(Member_Name)
    elseif Member_Debuff then
        return HealBot_Config.CDCBarColour[Member_Debuff].R,
               HealBot_Config.CDCBarColour[Member_Debuff].G,
               HealBot_Config.CDCBarColour[Member_Debuff].B,
               HealBot_Config.Barcola[HealBot_Config.Current_Skin],
			   hrpct
    elseif Member_Buff then
		hcr,hcg,hcb=HealBot_Options_RetBuffRGB(Member_Buff)
        return hcr,hcg,hcb,HealBot_Config.Barcola[HealBot_Config.Current_Skin],hrpct
    elseif hlth>maxhlth*HealBot_Config.AlertLevel and healin==0 then
        HealBot_UnitStatus[unit]=0;
    end
  end

  hcr,hcg,hcb = 1.0, 1.0, 0.0;
  if hcpct>HealBot_Config.AlertLevel then
    hca=HealBot_Config.bardisa[HealBot_Config.Current_Skin]
  else
    hca=HealBot_Config.Barcola[HealBot_Config.Current_Skin]
  end

  if hcpct>=0.98 then hcr = 0.0;
  elseif hcpct<0.98 and hcpct>=0.65 then hcr=2.94-(hcpct*3);
  elseif hcpct<=0.64 and hcpct>0.31 then hcg=(hcpct-0.31)*3; 
  elseif hcpct<=0.31 then hcg = 0.0; 
  end
  return hcr,hcg,hcb,hca,hrpct
end

local barName=nil
function HealBot_Action_HealthBar(button)
  if not button then return; end
  barName = button:GetName();
  return getglobal(barName.."Bar");
end

function HealBot_Action_HealthBar2(button)
  if not button then return; end
  barName = button:GetName();
  return getglobal(barName.."Bar2");
end

function HealBot_Action_HealthBar3(button)
  if not button then return; end
  barName = button:GetName();
  return getglobal(barName.."Bar3");
end

function HealBot_Action_HealthBar4(button)
  if not button then return; end
  barName = button:GetName();
  return getglobal(barName.."Bar4");
end

function HealBot_Action_ShouldHealSome(unit)
  if HealBot_Enabled[unit] then 
    return true
  else
    for hbunit,button in pairs(HealBot_Unit_Button) do
  	  if HealBot_Enabled[UnitName(hbunit)] then
  	    return true
 	  end
    end
  end
end

function HealBot_MayHeal(unit, name)
  if name then
    if unit ~= 'target' then return true end
    if not HealBot_Config.TargetHeals or UnitCanAttack("player",unit) then return false end
    return true;
  else
    return false;
  end
end

function HealBot_Action_RefreshBar3(unit)
  if HealBot_Config.bar2size[HealBot_Config.Current_Skin]>0 and HealBot_UnitName[unit] then
    b=HealBot_Unit_Button[unit]
	HealBot_Action_SetBar3Value(b);
  end
end

local b3mana=nil
local b3maxmana=nil
local b3r=nil
local b3g=nil
local b3b=nil
function HealBot_Action_SetBar3Value(button)
  if not button then return end
  barName = HealBot_Action_HealthBar3(button); 
  b3mana = UnitMana(button.unit);
  b3maxmana = UnitManaMax(button.unit);
  b3r,b3g,b3b=HealBot_Action_GetManaBarCol(button.unit)
  barName:SetMinMaxValues(0,b3maxmana);
  barName:SetValue(b3mana);
  barName:SetStatusBarColor(b3r,b3g,b3b)  
end

local powertype=nil
function HealBot_Action_GetManaBarCol(unit)
  powertype=UnitPowerType(unit);
  if powertype==0 then
    return 0,0,0.8
  elseif powertype==1 then
    return 0.8,0,0
  elseif powertype==3 then
    return 0.8,0.8,0
  elseif powertype==4 then
    return 0.8,0,0.8
  end
  return 0.8,0.8,0
end

function HealBot_CorrectPetHealth(unit,hlth,maxhlth,Member_Name)
  if not HealBot_PetMaxH[Member_Name] then HealBot_PetMaxH[Member_Name]=hlth; end
  if hlth>HealBot_PetMaxH[Member_Name] then HealBot_PetMaxH[Member_Name]=hlth; end
  if HealBot_PetMaxH[Member_Name] and not HealBot_IsFighting then
    if not HealBot_PetMaxHcnt1[Member_Name] then 
      HealBot_PetMaxHcnt1[Member_Name]=1
      HealBot_PetMaxHcnt2[Member_Name]=HealBot_PetMaxH[Member_Name];
    else
      if HealBot_PetMaxHcnt2[Member_Name]~=hlth then
        HealBot_PetMaxHcnt2[Member_Name]=hlth;
        HealBot_PetMaxHcnt1[Member_Name]=1;
      else
        HealBot_PetMaxHcnt1[Member_Name]=HealBot_PetMaxHcnt1[Member_Name]+1;
        if HealBot_PetMaxHcnt1[Member_Name]>20 then
          HealBot_PetMaxH[Member_Name]=HealBot_PetMaxHcnt2[Member_Name];
        end
      end
    end
  end
  return HealBot_PetMaxH[Member_Name]
end

function HealBot_RetCorrectPetHealth(unit,hlth,maxhlth,Member_Name)
  if not HealBot_PetMaxH[Member_Name] then
    HealBot_CorrectPetHealth(unit,hlth,maxhlth,Member_Name)
  end
  return HealBot_PetMaxH[Member_Name]
end	

local ebuName=nil
local ebuhlth=nil
local ebumaxhlth=nil
local ebubar=nil
local ebubar2=nil
local ebusr=nil
local ebusg=nil
local ebusb=nil
local ebusa=nil
local ebur=nil
local ebug=nil
local ebub=nil
local ebua=nil
local ebpct=1
local ebipct=0
local ebtext=nil
local ebuhealin=nil
local ebub2=nil
local ebufastenable=nil
local ebuProcessThis=nil
local ebuUnitDead=nil
local ebuHealBot_UnitDebuff=nil
local ebuHealBot_UnitBuff=nil
local ebuUnit_BuffRange=false
local ebuUnit_HealRange=false
function HealBot_Action_EnableButton(button, ebuName)
  if not ebuName then 
    ebuName=HEALBOT_WORDS_UNKNOWN
  end
  
  ebUnit=button.unit
  ebuUnitDead = UnitIsDeadOrGhost(ebUnit)
  ebubar = HealBot_Action_HealthBar(button);
  ebubar2 = HealBot_Action_HealthBar2(button);  
  ebuhlth=UnitHealth(ebUnit) or 0;
  ebumaxhlth=UnitHealthMax(ebUnit) or 0;
  HealBot_UnitRangeSpell[ebUnit]=HealBot_hSpell

  if HealBot_Config.SetClassColourText==1 and HealBot_ClassColB[ebuName] then
    ebusr,ebusg,ebusb = HealBot_ClassColR[ebuName],HealBot_ClassColG[ebuName],HealBot_ClassColB[ebuName];
    ebusa = HealBot_Config.btextenabledcola[HealBot_Config.Current_Skin];
  elseif ebuHealBot_UnitDebuff then
    ebusr=HealBot_Config.btextcursecolr[HealBot_Config.Current_Skin];
    ebusg=HealBot_Config.btextcursecolg[HealBot_Config.Current_Skin];
    ebusb=HealBot_Config.btextcursecolb[HealBot_Config.Current_Skin];
    ebusa=HealBot_Config.btextcursecola[HealBot_Config.Current_Skin];
  else
    ebusr=HealBot_Config.btextenabledcolr[HealBot_Config.Current_Skin] or 0;
    ebusg=HealBot_Config.btextenabledcolg[HealBot_Config.Current_Skin] or 0;
    ebusb=HealBot_Config.btextenabledcolb[HealBot_Config.Current_Skin] or 0;
    ebusa=HealBot_Config.btextenabledcola[HealBot_Config.Current_Skin] or 0;
  end

  ebuhealin = HealBot_RetHealsIn(ebuName)
  if ebuhealin>0 then
    ebipct = ebuhlth+ebuhealin
    if ebipct<ebumaxhlth then
      ebipct=ebipct/ebumaxhlth
    else
      ebipct=1;
    end
    ebipct=floor(ebipct*100)
    ebubar2:SetValue(ebipct);
	HealBot_UnitRange[ebUnit]=-1
  elseif ebubar2:GetValue()>0 then
    ebubar2:SetValue(0)
  end
  
  if ebuName~=HEALBOT_WORDS_UNKNOWN then
  
    ebuHealBot_UnitDebuff=HealBot_UnitDebuff[ebuName]
    ebuHealBot_UnitBuff=HealBot_UnitBuff[ebuName]

    if ebuUnitDead then
      if UnitIsFeignDeath(ebUnit) then
        ebuUnitDead = nil
      else
	    if ebuhealin>0 then
	      ebuhealin=0
        end
        if HealBot_Aggro[ebuName] then
	      HealBot_Action_UpdateAggro(ebUnit,false)
	      HealBot_Aggro[ebuName]=nil
	    end
	    if HealBot_UnitDebuff[ebuName] then
		  HealBot_UnitDebuff[ebuName]=nil
        end
	  end
  	  if ebuName==HealBot_PlayerName and not HealBot_PlayerDead then
	    HealBot_Action_ResetActiveUnitStatus()
	    HealBot_PlayerDead=true
	  end
    elseif ebuName==HealBot_PlayerName and HealBot_PlayerDead then
	  HealBot_Action_ResetActiveUnitStatus()
	  HealBot_PlayerDead=false
    end

    
	ebur,ebug,ebub,ebua,ebpct = HealBot_HealthColor(ebUnit,ebuhlth,ebumaxhlth,false,ebuName,ebuUnitDead,ebuHealBot_UnitBuff,ebuHealBot_UnitDebuff,ebuhealin)
	
    if ebuName==HealBot_PlayerName then
      ebuUnit_BuffRange=true
      ebuUnit_HealRange=true
    elseif IsSpellInRange(HealBot_hSpell, ebUnit)==1 then
      ebuUnit_BuffRange=true
      ebuUnit_HealRange=true
    elseif IsSpellInRange(HealBot_bSpell, ebUnit)==1 then
      ebuUnit_BuffRange=true
      ebuUnit_HealRange=false
    else
      ebuUnit_BuffRange=false
      ebuUnit_HealRange=false
    end

    if ebumaxhlth<1 then ebuhlth=1 end
    if ebuhlth>ebumaxhlth then
       ebumaxhlth=HealBot_CorrectPetHealth(ebUnit,ebuhlth,ebumaxhlth,ebuName)
    end
  
    if HealBot_Config.UseFluidBars==0 then
      ebubar:SetValue(ebpct)
    else
      if HealBot_curUnitHealth[ebubar]~=ebpct then
        if ebuUnitDead then
	      ebubar:SetValue(0)
	    else
          HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
	    end
      end
      HealBot_curUnitHealth[ebubar]=ebpct
    end
	
    ebuProcessThis=true
	ebufastenable=false
    if HealBot_Config.ProtectPvP==1 then
      if UnitIsPVP(ebUnit) and not UnitIsPVP("player") then ebuProcessThis=false; end
    end
    if not ebuUnitDead and not HealBot_PlayerDead and ebuProcessThis then
      if ebuHealBot_UnitDebuff and ebuUnit_BuffRange then
	    HealBot_UnitRangeSpell[ebUnit]=HealBot_dSpell
		ebufastenable=true
      elseif ebuHealBot_UnitBuff and ebuUnit_BuffRange then
	    HealBot_UnitRangeSpell[ebUnit]=HealBot_bSpell
		ebufastenable=true
	  elseif (ebuhlth<=(ebumaxhlth*HealBot_Config.AlertLevel) or HealBot_Aggro[ebuName] or HealBot_MyTargets[ebuName]) and ebuUnit_HealRange then
	    ebufastenable=true
	  end
	end
    if ebufastenable then
	  HealBot_Enabled[ebuName]=true
	  ebua=HealBot_Config.Barcola[HealBot_Config.Current_Skin]
      ebubar:SetStatusBarColor(ebur,ebug,ebub,ebua);
      ebubar2:SetStatusBarColor(ebur,ebug,ebub,HealBot_Config.BarcolaInHeal[HealBot_Config.Current_Skin]);
      HealBot_UnitRangeitr[ebUnit]=ebusr
      HealBot_UnitRangeitg[ebUnit]=ebusg
      HealBot_UnitRangeitb[ebUnit]=ebusb
      HealBot_UnitRangeotr[ebUnit]=HealBot_Config.btextdisbledcolr[HealBot_Config.Current_Skin]
      HealBot_UnitRangeotg[ebUnit]=HealBot_Config.btextdisbledcolg[HealBot_Config.Current_Skin]
      HealBot_UnitRangeotb[ebUnit]=HealBot_Config.btextdisbledcolb[HealBot_Config.Current_Skin]
      if ebUnit==HealBot_Action_DisableTooltipUnit then
        HealBot_Action_TooltipUnit = ebUnit;
        HealBot_Action_DisableTooltipUnit = nil;
      end
      if ebUnit==HealBot_Action_TooltipUnit then
	    HealBot_Action_RefreshTooltip(ebUnit)
      end
	else
	  HealBot_Enabled[ebuName]=false
	  ebusa=HealBot_Config.btextdisbledcola[HealBot_Config.Current_Skin]
	  if HealBot_Config.SetClassColourText==0 then
        ebusr=HealBot_Config.btextdisbledcolr[HealBot_Config.Current_Skin]
        ebusg=HealBot_Config.btextdisbledcolg[HealBot_Config.Current_Skin]
        ebusb=HealBot_Config.btextdisbledcolb[HealBot_Config.Current_Skin]
	  end
      if HealBot_RetHealBot_Ressing(ebuName) then
        if ebuUnitDead then
          ebusr=0.2
          ebusg=1.0
          ebusb=0.2
          ebusa=1
        else
          HealBot_UnsetHealBot_Ressing(ebuName)
		end
      elseif ebuUnitDead and ebuName~=HealBot_PlayerName then
        if HealBot_rSpell then
		  HealBot_UnitRangeSpell[ebUnit]=HealBot_rSpell
          if ebuUnit_BuffRange then
            ebusr=0;
            ebusg=0;
            ebusb=0;
            ebusa=1;
		  else
            ebusr=0.4;
            ebusg=0.4;
            ebusb=0.4;
            ebusa=0.1;
          end
		end
      end
      HealBot_UnitRangeitr[ebUnit]=ebusr
      HealBot_UnitRangeitg[ebUnit]=ebusg
      HealBot_UnitRangeitb[ebUnit]=ebusb
      HealBot_UnitRangeotr[ebUnit]=ebusr
      HealBot_UnitRangeotg[ebUnit]=ebusg
      HealBot_UnitRangeotb[ebUnit]=ebusb
      ebua=HealBot_Config.bardisa[HealBot_Config.Current_Skin]
      ebubar:SetStatusBarColor(ebur,ebug,ebub,ebua);
      ebubar2:SetStatusBarColor(ebur,ebug,ebub,ebua);
	  if not HealBot_IsFighting and HealBot_Config.EnableHealthy==0 then
        if ebUnit==HealBot_Action_TooltipUnit then
          HealBot_Action_TooltipUnit = nil;
          HealBot_Action_DisableTooltipUnit = ebUnit;
        end
	    if ebUnit==HealBot_Action_DisableTooltipUnit then
	      HealBot_Tooltip_RefreshDisabledTooltip(ebUnit)
        end
	  end
    end
    HealBot_UnitRanger[ebUnit]=ebur
    HealBot_UnitRangeg[ebUnit]=ebug
    HealBot_UnitRangeb[ebUnit]=ebub
  elseif Delay_RecalcParty==0 then
    Delay_RecalcParty=1
  end
  
  ebubar.txt = getglobal(ebubar:GetName().."_text");
  ebtext=HealBot_Action_HBText(ebuhlth,ebumaxhlth,ebuName,ebUnit,ebuhealin)
  ebubar.txt:SetText(ebtext);
  ebubar.txt:SetTextColor(ebusr,ebusg,ebusb,ebusa);

  if not HealBot_UnitRange[ebUnit] then
    if ebuName==HealBot_PlayerName then
	  HealBot_UnitRange[ebUnit]=1
	else
      HealBot_UnitRange[ebUnit]=IsSpellInRange(HealBot_UnitRangeSpell[ebUnit], ebUnit) or 0
	end
  end
end

function HealBot_Action_HBText(hlth,maxhlth,Member_Name,unit,healin)
  btHBbarText=" "
  bttextlen = floor(5+(((HealBot_Config.bwidth[HealBot_Config.Current_Skin]*1.8)/HealBot_Config.btextheight[HealBot_Config.Current_Skin])-(HealBot_Config.btextheight[HealBot_Config.Current_Skin]/2)))-2
  if HealBot_Config.ShowClassOnBar==1 and UnitClass(unit) then
      if HealBot_Config.ShowClassOnBarWithName==1 then
        btname=UnitClass(unit)..":"..Member_Name;
      else
        btname=UnitClass(unit);
      end
  else
    btname=Member_Name;
  end

  if HealBot_Config.ShowHealthOnBar==1 and maxhlth then
    if HealBot_Config.BarHealthType==1 then
      if HealBot_Config.BarHealthIncHeals==1 then
	    healin = HealBot_RetHealsIn(Member_Name)
        bthlthdelta=(hlth+healin)-maxhlth;
      else
        bthlthdelta=hlth-maxhlth;
      end
      if bthlthdelta>0 then
        btHBbarText=btHBbarText.."+"..bthlthdelta;
      else
        btHBbarText=btHBbarText.."("..bthlthdelta..")";
      end
    else
      if HealBot_Config.BarHealthIncHeals==1 then
        healin = HealBot_RetHealsIn(Member_Name);
        btHBbarText=btHBbarText.."("..floor(((hlth+healin)/maxhlth)*100).."%)"
      else
        btHBbarText=btHBbarText.."("..floor((hlth/maxhlth)*100).."%)"
      end
    end
  end
  if HealBot_Aggro[Member_Name] and HealBot_Config.ShowAggroText==1 then
    btname=">> "..btname.." <<"
  end
  bttextlen=bttextlen-strlen(btHBbarText)
  if bttextlen<1 then bttextlen=1; end
  if strlen(Member_Name)>bttextlen then
    btHBbarText = string.sub(btname,1,bttextlen) .. '..'..btHBbarText;
  else
    btHBbarText = btname..btHBbarText;
  end
  return btHBbarText;
end

function HealBot_Action_GetUnitRange()
  return HealBot_UnitRange["player"] or 0
end

function HealBot_Action_RefreshButton(button, uName)
  if not button then return end
--  if type(button)~="table" then DEFAULT_CHAT_FRAME:AddMessage("***** "..type(button)) end
  HealBot_Action_EnableButton(button, uName)
end

function HealBot_Action_ResetSkin()
local bwidth = HealBot_Config.bwidth[HealBot_Config.Current_Skin];
local bheight=HealBot_Config.bheight[HealBot_Config.Current_Skin];
local br=HealBot_Config.headbarcolr[HealBot_Config.Current_Skin];  
local bg=HealBot_Config.headbarcolg[HealBot_Config.Current_Skin];
local bb=HealBot_Config.headbarcolb[HealBot_Config.Current_Skin];
local ba=HealBot_Config.headbarcola[HealBot_Config.Current_Skin];
local sr=HealBot_Config.headtxtcolr[HealBot_Config.Current_Skin];  
local sg=HealBot_Config.headtxtcolg[HealBot_Config.Current_Skin];
local sb=HealBot_Config.headtxtcolb[HealBot_Config.Current_Skin];
local sa=HealBot_Config.headtxtcola[HealBot_Config.Current_Skin];
local btexture=HealBot_Config.btexture[HealBot_Config.Current_Skin];
local btextheight=HealBot_Config.btextheight[HealBot_Config.Current_Skin];
local b,bar,bar2,bar3,bar4,icon1,icon2,icon3,barScale,h,hwidth,hheight

  for j=1,50 do
    b=getglobal("HealBot_Action_HealUnit"..j);
    bar = HealBot_Action_HealthBar(b);
    bar2 = HealBot_Action_HealthBar2(b);
    bar3 = HealBot_Action_HealthBar3(b);
	bar4 = HealBot_Action_HealthBar4(b)
    icon1 = getglobal(bar:GetName().."Icon1");
    icon2 = getglobal(bar:GetName().."Icon2");
    icon3 = getglobal(bar:GetName().."Icon3");
    HealBot_Panel_SetBarArrays(b)
    bar.txt = getglobal(bar:GetName().."_text");
    bar:SetHeight(bheight);
    bar:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture);
    bar.txt:SetTextHeight(btextheight);
    barScale = bar:GetScale();
    bar:SetScale(barScale + 0.01);
    bar:SetScale(barScale);
    bar2:SetHeight(bheight);
    bar2:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture);
    bar3:SetHeight(HealBot_Config.bar2size[HealBot_Config.Current_Skin]);
    bar3:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture);
	if HealBot_Config.bar2size[HealBot_Config.Current_Skin]==0 then
	  bar4:SetHeight(3)
	else
      bar4:SetHeight(HealBot_Config.bar2size[HealBot_Config.Current_Skin]+1)
	end
	bar4:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture)
	bar4:SetStatusBarColor(1,0,0,0.5)
	bar4:SetMinMaxValues(0,100)
	bar4:SetValue(0)
    b:SetHeight(bheight);  
    b:Enable();
	b.name="init"
    icon1:SetHeight(bheight-2);
    icon1:SetWidth(bheight-2);
    icon2:SetHeight(bheight-2);
    icon2:SetWidth(bheight-2);
    icon3:SetHeight(bheight-2);
    icon3:SetWidth(bheight-2);
	bar:SetMinMaxValues(0,100)
	bar2:SetMinMaxValues(0,100);
	bar2:SetValue(0);
    bar:SetStatusBarColor(0,1,0,HealBot_Config.bardisa[HealBot_Config.Current_Skin]);
    bar2:SetStatusBarColor(0,1,0,0);

    if HealBot_Config.HoTonBar==1 then
      HealBot_Panel_SetMultiColHoToffset(0)
      if HealBot_Config.HoTposBar==1 then
        icon1:ClearAllPoints();
        icon1:SetPoint("LEFT",b,"LEFT",1,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("LEFT",icon1,"RIGHT",1,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("LEFT",icon2,"RIGHT",1,0);
      else
        icon1:ClearAllPoints();
        icon1:SetPoint("RIGHT",b,"RIGHT",-1,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("RIGHT",icon1,"LEFT",-1,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("RIGHT",icon2,"LEFT",-1,0);
      end
    else
      HealBot_Panel_SetMultiColHoToffset(bheight*3)
      if HealBot_Config.HoTposBar==1 then
        icon1:ClearAllPoints();
        icon1:SetPoint("RIGHT",b,"LEFT",-1,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("RIGHT",icon1,"LEFT",-2,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("RIGHT",icon2,"LEFT",-2,0);
      else
        icon1:ClearAllPoints();
        icon1:SetPoint("LEFT",b,"RIGHT",2,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("LEFT",icon1,"RIGHT",2,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("LEFT",icon2,"RIGHT",2,0);
      end
    end

    if HealBot_Config.bar2size[HealBot_Config.Current_Skin]==0 then
      bar3:SetHeight(1);
      bar3:SetMinMaxValues(0,100);
      bar3:SetValue(0);
      bar3:SetStatusBarColor(0,0,0,0)
    end
	if HealBot_Config.ShowAggroBars==0 then
	  bar4:SetMinMaxValues(0,100)
	  bar4:SetValue(0)
	  bar4:SetStatusBarColor(0,0,0,0)
	end
  end
  for j=71,80 do
    b=getglobal("HealBot_Action_HealUnit"..j);
    bar = HealBot_Action_HealthBar(b);
    bar2 = HealBot_Action_HealthBar2(b);
    bar3 = HealBot_Action_HealthBar3(b);
	bar4 = HealBot_Action_HealthBar4(b)
    icon1 = getglobal(bar:GetName().."Icon1");
    icon2 = getglobal(bar:GetName().."Icon2");
    icon3 = getglobal(bar:GetName().."Icon3");
    HealBot_Panel_SetBarArrays(b)
    bar.txt = getglobal(bar:GetName().."_text");
    bar:SetHeight(bheight);
    bar:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture);
    bar.txt:SetTextHeight(btextheight);
    barScale = bar:GetScale();
    bar:SetScale(barScale + 0.01);
    bar:SetScale(barScale);
    bar2:SetHeight(bheight);
    bar2:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture);
    bar3:SetHeight(HealBot_Config.bar2size[HealBot_Config.Current_Skin]);
    bar3:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture);
	if HealBot_Config.bar2size[HealBot_Config.Current_Skin]==0 then
	  bar4:SetHeight(3)
	else
      bar4:SetHeight(HealBot_Config.bar2size[HealBot_Config.Current_Skin]+1)
	end
	bar4:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..btexture)
	bar4:SetStatusBarColor(1,0,0,0.5)
	bar4:SetMinMaxValues(0,100)
	bar4:SetValue(0)
    b:SetHeight(bheight);  
    b:Enable();
	b.name="init"
    icon1:SetHeight(bheight-3);
    icon1:SetWidth(bheight-3);
    icon2:SetHeight(bheight-3);
    icon2:SetWidth(bheight-3);
    icon2:ClearAllPoints();
    icon2:SetPoint("RIGHT",icon1,"LEFT",0,0);
    icon3:SetHeight(bheight-3);
    icon3:SetWidth(bheight-3);
    icon3:ClearAllPoints();
    icon3:SetPoint("RIGHT",icon2,"LEFT",0,0);
	bar:SetMinMaxValues(0,100)
	bar2:SetMinMaxValues(0,100);
	bar2:SetValue(0);
    bar:SetStatusBarColor(0,1,0,HealBot_Config.bardisa[HealBot_Config.Current_Skin]);
    bar2:SetStatusBarColor(0,1,0,0);
    if HealBot_Config.bar2size[HealBot_Config.Current_Skin]==0 then
      bar3:SetHeight(1);
      bar3:SetMinMaxValues(0,100);
      bar3:SetValue(0);
      bar3:SetStatusBarColor(0,0,0)
    end
	if HealBot_Config.ShowAggroBars==0 then
	  bar4:SetMinMaxValues(0,100)
	  bar4:SetValue(0)
	  bar4:SetStatusBarColor(0,0,0,0)
	end
  end
  for j=1,15 do
    h=getglobal("HealBot_Action_Header"..j);
    bar = HealBot_Action_HealthBar(h);
    hwidth = bwidth*HealBot_Config.headwidth[HealBot_Config.Current_Skin]
    hheight = ceil(bheight*0.8)
    HealBot_Panel_SetHeadArrays(h)
    h:SetHeight(hheight);
    h:SetWidth(hwidth);
    bar:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.headtexture[HealBot_Config.Current_Skin]);
    bar:SetMinMaxValues(0,100);
    bar:SetValue(100);
    bar:SetStatusBarColor(br,bg,bb,ba);
    bar:SetHeight(hheight);
    bar.txt = getglobal(bar:GetName().."_text");
    bar.txt:SetTextColor(sr,sg,sb,sa);
    h:Disable();
  end
  bar = HealBot_Action_HealthBar(HealBot_Action_OptionsButton);
  bar:SetStatusBarColor(0.1,0.1,0.4,0);
  bar.txt = getglobal(bar:GetName().."_text");
  bar.txt:SetTextColor(0.8,0.8,0.2,0.85);
  bar.txt:SetText(HEALBOT_ACTION_OPTIONS);
  HealBot_Action_ResetUnitStatus()
  HealBot_Action_SetAddHeight()
  Delay_RecalcParty=3;
  
  if not HealBot_Config.btextdisbledcolr[HealBot_Config.Current_Skin] then HealBot_Config.btextdisbledcolr[HealBot_Config.Current_Skin]=0.5 end
  if not HealBot_Config.btextdisbledcolg[HealBot_Config.Current_Skin] then HealBot_Config.btextdisbledcolg[HealBot_Config.Current_Skin]=0.5 end
  if not HealBot_Config.btextdisbledcolb[HealBot_Config.Current_Skin] then HealBot_Config.btextdisbledcolb[HealBot_Config.Current_Skin]=0.5 end
  if not HealBot_Config.btextdisbledcola[HealBot_Config.Current_Skin] then HealBot_Config.btextdisbledcola[HealBot_Config.Current_Skin]=0.5 end
  if not HealBot_Config.btextcursecolr[HealBot_Config.Current_Skin] then HealBot_Config.btextcursecolr[HealBot_Config.Current_Skin]=0.5 end
  if not HealBot_Config.btextcursecolg[HealBot_Config.Current_Skin] then HealBot_Config.btextcursecolg[HealBot_Config.Current_Skin]=0.5 end
  if not HealBot_Config.btextcursecolb[HealBot_Config.Current_Skin] then HealBot_Config.btextcursecolb[HealBot_Config.Current_Skin]=0.5 end
  if not HealBot_Config.btextcursecola[HealBot_Config.Current_Skin] then HealBot_Config.btextcursecola[HealBot_Config.Current_Skin]=0.5 end


  if HealBot_Options:IsVisible() then 
    HealBot_DiseaseColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_MagicColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_PoisonColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_CurseColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_EnTextColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_EnTextColorpickin:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_DisTextColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_DebTextColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_HeadBarColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.headtexture[HealBot_Config.Current_Skin]);
    HealBot_HeadTextColorpick:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.headtexture[HealBot_Config.Current_Skin]);
    HealBot_Buff1Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff2Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff3Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff4Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff5Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff6Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff7Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff8Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_Buff9Colour:SetStatusBarTexture("Interface\\AddOns\\HealBot\\images\\bar"..HealBot_Config.btexture[HealBot_Config.Current_Skin]);
    HealBot_SetSkinColours()
  end
end
local rbb=nil
local rName=nil
local rbsir=0

function HealBot_Action_RefreshButtons(unit)
  if unit then
    rbb=HealBot_Unit_Button[unit]
    HealBot_Action_RefreshButton(rbb, UnitName(unit))
  else
    if HealBot_ButtonArray==1 then
      for unit,button in pairs(HealBot_ButtonArray1) do
	    HealBot_Action_CheckRange(unit, button)
      end
	  HealBot_ButtonArray=2
    elseif HealBot_ButtonArray==2 then
      for unit,button in pairs(HealBot_ButtonArray2) do
	    HealBot_Action_CheckRange(unit, button)
      end
	  HealBot_ButtonArray=3
	else
      for unit,button in pairs(HealBot_ButtonArray3) do
	    HealBot_Action_CheckRange(unit, button)
      end
	  HealBot_ButtonArray=1
    end
  end
end

function HealBot_Action_CheckRange(unit, button)
  if not HealBot_UnitStatus[unit] then return end
  if HealBot_UnitStatus[unit]>0 then
	if not HealBot_UnitRangeSpell[unit] then
	  HealBot_Action_RefreshButton(button, UnitName(unit))
	else
	  if UnitName(unit)==HealBot_PlayerName then
        rbsir=1
      else
        rbsir=IsSpellInRange(HealBot_UnitRangeSpell[unit], unit) or 0
      end
      if HealBot_UnitRange[unit]==-1 then
	    HealBot_UnitRange[unit]=rbsir
	    HealBot_Action_RefreshButton(button, UnitName(unit))
	  elseif rbsir~=HealBot_UnitRange[unit] then
	    ebubar = HealBot_Action_HealthBar(button)
		ebubar.txt=getglobal(ebubar:GetName().."_text");
        HealBot_UnitRange[unit]=rbsir
	    if rbsir==1 then
          ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],HealBot_Config.Barcola[HealBot_Config.Current_Skin])
		  ebubar.txt:SetTextColor(HealBot_UnitRangeitr[unit],HealBot_UnitRangeitg[unit],HealBot_UnitRangeitb[unit],HealBot_Config.btextenabledcola[HealBot_Config.Current_Skin]);
	    else
	      ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],HealBot_Config.bardisa[HealBot_Config.Current_Skin])
		  ebubar.txt:SetTextColor(HealBot_UnitRangeotr[unit],HealBot_UnitRangeotg[unit],HealBot_UnitRangeotb[unit],HealBot_Config.btextdisbledcola[HealBot_Config.Current_Skin]);
        end
      end
    end
  end
end

local tmpn=0
function HealBot_Action_RetButtonArraySize(num)
  tmpn=0
  if num==1 then
    for Member_Name,button in pairs(HealBot_ButtonArray1) do
	  tmpn=tmpn+1
	end
    return tmpn
  elseif num==2 then
    for Member_Name,button in pairs(HealBot_ButtonArray2) do
	  tmpn=tmpn+1
	end
    return tmpn
  elseif num==3 then
    for Member_Name,button in pairs(HealBot_ButtonArray3) do
	  tmpn=tmpn+1
	end
    return tmpn
  end
end

function HealBot_Action_ResetUnit(unit)

  HealBot_Action_ResetUnitStatus(unit)
end

local HBid=nil
function HealBot_Action_ResetUnitStatus(unit)
  if unit then
    HealBot_UnitStatus[unit]=1;
    HealBot_UnitRange[unit]=-1
  else
    for unit,_ in pairs(HealBot_Unit_Button) do
      HealBot_UnitStatus[unit]=1;
      HealBot_UnitRange[unit]=-1
    end
  end
end

function HealBot_Action_ResetActiveUnitStatus()
  for unit,_ in pairs(HealBot_Unit_Button) do
    if HealBot_UnitStatus[unit]>0 and HealBot_Enabled[UnitName(unit)] then
      HealBot_UnitRange[unit]=-1
	elseif UnitHealth(unit)<2 then
	  HealBot_UnitRange[unit]=-1
	end
  end
end

local shbutton=nil
local shb=nil
local shbar=nil
function HealBot_Action_SetHealButtonChecked(index,unit,Member_Name,isTarget)

  shbutton="HealBot_Action_HealUnit"..index
  shb=getglobal(shbutton);

  if UnitClass(unit) then
    if not HealBot_ClassColR[Member_Name] or isTarget then
      HealBot_UnitID[Member_Name]="niL";
      HealBot_ClassColR[Member_Name],HealBot_ClassColG[Member_Name],HealBot_ClassColB[Member_Name] = HealBot_Action_ClassColour(UnitClass(unit));
      HealBot_Unit_Button[unit]="init";
      HealBot_UnitID[Member_Name] = "init"
	end
	if HealBot_UnitID[Member_Name]~=unit or shb.name~=Member_Name then
      HealBot_CheckAllBuffs(unit)
      HealBot_CheckAllDebuffs(unit)
	  HealBot_UnitID[Member_Name]=unit
	  HealBot_Panel_SetBarArrays(shb)
    end
    if HealBot_Unit_Button[unit]~=shb or Member_Name~=shb.name then
      shb.unit = unit;
	  shb.index=index
	  shb.name=Member_Name
      shb:SetAttribute("unit", unit);
      HealBot_Unit_Button[unit]=shb
	  shbar=HealBot_Action_HealthBar(shb)
	  HealBot_UnitBarUpdate[shbar]=100
	  HealBot_curUnitHealth[shbar]=100
      HealBot_UnitStatus[unit]=9
	  if not HealBot_AttribStatus[shbutton] then
	    HealBot_Action_SetButtonAttribs(shb,"Enabled")
      end
	elseif Member_Name~=shb.name then
	  shb.name=Member_Name
      HealBot_UnitStatus[unit]=9
    end
  else
    return nil
  end
  HealBot_TrackBars[shb]=nil
  return shb
end

local shbuName=nil
local uTargetButton=nil
local uTargetB=nil
local uOldTargetName=nil
function HealBot_Action_SetHealButton(index,unit,Member_Name,isTarget)

  if UnitClass(unit) then
    if index>69 then
	  if HealBot_Unit_Button[unit] then
	    shb=HealBot_Unit_Button[unit]
	    if shb.index<69 then
		  uTargetButton="HealBot_Action_HealUnit"..index
		  uTargetB=getglobal(uTargetButton)
--  	      Member_Name="target"
	      HealBot_Unit_Button[unit]=nil
	    end
	  end
	  if isTarget then uOldTargetName=Member_Name end
	  return HealBot_Action_SetHealButtonChecked(index,unit,Member_Name,isTarget)
	else
	  if not HealBot_Unit_Button[unit] then
	    if not HealBot_ButtonStore[1] then
          return nil
		else
          shb=HealBot_ButtonStore[1]
	      table.remove(HealBot_ButtonStore,1)
	      HealBot_Unit_Button[unit]="init"
		end
	  else
	    shb=HealBot_Unit_Button[unit]
	    if shb.index and shb.index>69 then
		  if not HealBot_ButtonStore[1] then 
		    return nil
          else
	        shb=HealBot_ButtonStore[1]
		    table.remove(HealBot_ButtonStore,1)
		    HealBot_Unit_Button[unit]="init"
		  end
	    end
	  end
      if not HealBot_ClassColR[Member_Name] then
        HealBot_ClassColR[Member_Name],HealBot_ClassColG[Member_Name],HealBot_ClassColB[Member_Name] = HealBot_Action_ClassColour(UnitClass(unit));
	    HealBot_UnitID[Member_Name]="init"
		HealBot_Unit_Button[unit]="init"
      end
	  if HealBot_UnitID[Member_Name]~=unit or shb.name~=Member_Name then
        HealBot_CheckAllBuffs(unit)
        HealBot_CheckAllDebuffs(unit)
	    HealBot_UnitID[Member_Name]=unit
	  end
      if HealBot_Unit_Button[unit]~=shb then
	    shb.unit=unit
	    shb.index=index
		shb.name=Member_Name
        shb:SetAttribute("unit", unit);
        HealBot_Unit_Button[unit]=shb
	    shbar=HealBot_Action_HealthBar(shb)
  	    HealBot_UnitBarUpdate[shbar]=100
		HealBot_curUnitHealth[shbar]=100
        HealBot_UnitStatus[unit]=9
        if not HealBot_AttribStatus[shbutton] then
	      HealBot_Action_SetButtonAttribs(shb,"Enabled")
        end
	  elseif Member_Name~=shb.name then
	    shb.name=Member_Name
        HealBot_UnitStatus[unit]=9
      end
	end
  else
    return nil
  end
  HealBot_TrackBars[shb]=nil
  return shb
end

function HealBot_Action_rmTargetButton(unit)
  HealBot_Action_InsButtonStore(uTargetB)
  uTargetB:Hide()
end

function HealBot_Action_SetAllAttribs()
    HealBot_AddDebug("In HealBot_Action_SetAllAttribs")
    for x,_ in pairs(HealBot_AttribStatus) do
      HealBot_AttribStatus[x]=nil;
    end
    for x,_ in pairs(HealBot_UnitID) do
      HealBot_UnitID[x]=nil;
    end

  HealBot_Action_ResetUnitStatus()
  HealBot_Action_PartyChanged()
end

function HealBot_Action_SetAllBars()
  HB_Action_Timer1 = 2000
  HealBot_Action_ResethbInitButtons()
  for j=1,50 do
    b=getglobal("HealBot_Action_HealUnit"..j)
    HealBot_Panel_SetBarArrays(b)
	b:Hide()
  end
  for j=71,80 do
    b=getglobal("HealBot_Action_HealUnit"..j)
    HealBot_Panel_SetBarArrays(b)
	b:Hide()
  end
  for unit,_ in pairs(HealBot_Unit_Button) do
    HealBot_Unit_Button[unit]=nil
  end 
end

local HB_button,HB_prefix=nil
local HB_combo_prefix=nil
local SpellTxt=nil
local lowerTarget=nil
local lowerAssist=nil
local lowerFocus=nil
local saSpellID=nil
local HealBot_Keys_List = {"","Shift","Ctrl","Alt"}
function HealBot_Action_SetButtonAttribs(button,status)
  
  for j=1,5 do
    if j==1 then HB_button="Left";
    elseif j==2 then HB_button="Right";
    elseif j==3 then HB_button="Middle";
    elseif j==4 then HB_button="Button4";
    elseif j==5 then HB_button="Button5";
    end
    
    for i=1, getn(HealBot_Keys_List), 1 do
	  HealBot_Action_SetButtonAttrib(button,HB_button,HealBot_Keys_List[i],status,j)
    end
  end
  HealBot_AttribStatus[button]=true;
end

function HealBot_Action_SetButtonAttrib(button,bbutton,bkey,status,j)
  
  lowerTarget="target"; --strlower(HEALBOT_DISABLED_TARGET);
  lowerAssist="assist"; --strlower(HEALBOT_ASSIST);
  lowerFocus="focus"; --strlower(HEALBOT_FOCUS);  
      if strlen(bkey)>1 then
        HB_prefix = strlower(bkey).."-"
      else
        HB_prefix = "";
      end
      HB_combo_prefix = bkey..bbutton;
      if status=="Enabled" then
        SpellTxt = HealBot_Action_AttribSpellPattern(HB_combo_prefix,HealBot_PlayerClass)
      elseif status=="Disabled" then
        SpellTxt = HealBot_Action_AttribDisSpellPattern(HB_combo_prefix,HealBot_PlayerClass)
      else
        SpellTxt=nil;
      end
      if SpellTxt then
        if strlower(SpellTxt)==lowerTarget then
          button:SetAttribute(HB_prefix.."helpbutton"..j, "target"..j);
		  button:SetAttribute(HB_prefix.."type"..j, "target")
          button:SetAttribute(HB_prefix.."type-target"..j, lowerTarget)
        elseif strlower(SpellTxt)==lowerFocus then
          button:SetAttribute(HB_prefix.."helpbutton"..j, "focus"..j);
		  button:SetAttribute(HB_prefix.."type"..j, "focus")
          button:SetAttribute(HB_prefix.."type-focus"..j, lowerFocus)
        elseif strlower(SpellTxt)==lowerAssist then
          button:SetAttribute(HB_prefix.."helpbutton"..j, "assist"..j);
--		  button:SetAttribute(HB_prefix.."type"..j, "assist")
          button:SetAttribute(HB_prefix.."type-assist"..j, lowerAssist)
        else
          saSpellID=HealBot_GetSpellId(SpellTxt)
          if saSpellID then
            button:SetAttribute(HB_prefix.."helpbutton"..j, "heal"..j);
            button:SetAttribute(HB_prefix.."type-heal"..j, "spell");
            button:SetAttribute(HB_prefix.."spell-heal"..j, SpellTxt);
          else
            button:SetAttribute(HB_prefix.."helpbutton"..j, "item"..j);
            button:SetAttribute(HB_prefix.."type-item"..j, "item");
            button:SetAttribute(HB_prefix.."item-item"..j, SpellTxt);
          end
        end
      else
        button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
      end
  HealBot_AttribStatus[button]=true;
end

local combos=nil
function HealBot_Action_AttribSpellPattern(HB_combo_prefix,class)
  combos = HealBot_Config.KeyCombo[class]
  if not combos then return nil end
  return combos[HB_combo_prefix]
end

function HealBot_Action_AttribDisSpellPattern(HB_combo_prefix,class)
  combos = HealBot_Config.DisKeyCombo[class]
  if not combos then return nil end
  return combos[HB_combo_prefix]
end

local hbInitButtons=false

function HealBot_Action_ResethbInitButtons()
  hbInitButtons=false
  for x,_ in pairs(HealBot_ButtonStore) do
    HealBot_ButtonStore[x]=nil;
  end
end

local uCnt=0
local tName=nil
local otName=nil
function HealBot_Action_PartyChanged(HealBot_PreCombat)
  if HealBot_Config.DisableHealBot==1 then return end
  if not HealBot_IsFighting then
    uTargetButton=nil

	for unit,_ in pairs(HealBot_ResetBars) do
	  if HealBot_Unit_Button[unit] then HealBot_Unit_Button[unit]=nil end
	  HealBot_ResetBars[unit]=nil
	end 
	
	for unit,button in pairs(HealBot_Unit_Button) do
	  HealBot_TrackBars[button]=unit
	end 
	
    if not hbInitButtons then
      for j=1,50 do
        b=getglobal("HealBot_Action_HealUnit"..j);
	    table.insert(HealBot_ButtonStore,b);
      end
      hbInitButtons=true
    end
    if HealBot_PreCombat then 
      HealBot_IsFighting=true;
    end
  
    if HealBot_Config.ShowHeader[HealBot_Config.Current_Skin]==1 then
	  HealBot_Panel_PartyChanged(true)
	else
	  HealBot_Panel_PartyChanged(false)
	end
	uCnt=0
	HealBot_ButtonArray=1
    for x,_ in pairs(HealBot_ButtonArray1) do
      HealBot_ButtonArray1[x]=nil;
    end
    for x,_ in pairs(HealBot_ButtonArray2) do
      HealBot_ButtonArray2[x]=nil;
    end
    for x,_ in pairs(HealBot_ButtonArray3) do
      HealBot_ButtonArray3[x]=nil;
    end
		
	for unit,button in pairs(HealBot_Unit_Button) do
	  uCnt=uCnt+1
	    b=HealBot_Unit_Button[unit]
        HealBot_Action_RefreshBar3(unit)
		if HealBot_ButtonArray==1 then
		  HealBot_ButtonArray1[unit]=HealBot_Unit_Button[unit]
		  HealBot_ButtonArray=2
		elseif HealBot_ButtonArray==2 then
		  HealBot_ButtonArray2[unit]=HealBot_Unit_Button[unit]
		  HealBot_ButtonArray=3
		else
 		  HealBot_ButtonArray3[unit]=HealBot_Unit_Button[unit]
		  HealBot_ButtonArray=1
        end
		if HealBot_UnitStatus[unit]==9 then
          HealBot_Action_ResetUnitStatus(unit)
		end
	end
	if uCnt==1 then
	  HealBot_ButtonArray2["player"]=HealBot_Unit_Button["player"]
	  HealBot_ButtonArray3["player"]=HealBot_Unit_Button["player"]
	elseif uCnt==2 then
	  HealBot_ButtonArray3["player"]=HealBot_Unit_Button["player"]
	end
	if uTargetButton then
	  HealBot_Action_RefreshButton(uTargetB, UnitName(uTargetB.unit))
	elseif uOldTargetName then
	  HealBot_Action_ResetUnitStatus(HealBot_UnitID[uOldTargetName])
	  uOldTargetName=nil
    end
	if otName then
	  uCnt=0
	  tName=UnitName("target")
	  if tName then
	    if otName~=tName then
		  uCnt=1
		end
	  else
	    uCnt=1
	  end
	  if uCnt~=0 then
        HealBot_Action_ResetUnitStatus(HealBot_UnitID[otName])
	    otName=nil
	  end
	end
	for button,unit in pairs(HealBot_TrackBars) do
	  if not HealBot_Unit_Button[unit] or HealBot_Unit_Button[unit]==button then
		if UnitName(unit) then
	      Delay_RecalcParty=2
		  HealBot_ResetBars[unit]=true
--		  HealBot_AddDebug("removed bar - set Delay_RecalcParty=2 : unit="..unit)
		else
		  HealBot_Unit_Button[unit]=nil
		end
	  else
	    HealBot_Action_ResetUnitStatus(unit)
	  end
	  HealBot_Action_InsButtonStore(button)
--	  HealBot_AddDebug("removed bar : unit="..unit)
	  HealBot_TrackBars[button]=nil
	end
	for unit,_ in pairs(HealBot_Action_rButtonOC) do
	  HealBot_Action_ResetUnitStatus(unit)
	  HealBot_Action_rButtonOC[unit]=nil
	end
  else
    Delay_RecalcParty=3;
  end
end

function HealBot_Action_SetOTname(uName)
  otName=uName
end

function HealBot_Action_RefreshBottonOnChange(unit)
  if not unit then return end
  HealBot_Action_rButtonOC[unit]=true
end

function HealBot_Action_InsButtonStore(b)
  if b.index<69 then
    table.insert(HealBot_ButtonStore,b)
    HealBot_Panel_SetBarArrays(b)
  end
end

function HealBot_Action_Reset()
  HealBot_Config.AutoClose=0
  HealBot_Config.PanelAnchorY=-1
  HealBot_Config.PanelAnchorX=-1
  HealBot_Action:ClearAllPoints();
  HealBot_Action:SetPoint("CENTER","UIParent","CENTER",0,0);
  HealBot_Action_PartyChanged()
  if HealBot_Config.DisableHealBot==0 then
    HealBot_Action:Show()
  else
    HealBot_Action:Hide()
  end
end

function HealBot_Action_ClassColour(class)
  if class==HEALBOT_DRUID then
    return 1.0,0.49,0.04
  elseif class==HEALBOT_HUNTER then
    return 0.67,0.83,0.45
  elseif class==HEALBOT_MAGE then
    return 0.41,0.8,0.94
  elseif class==HEALBOT_PALADIN then
    return 0.96,0.55,0.73
  elseif class==HEALBOT_PRIEST then
    return 1.0,1.0,1.0
  elseif class==HEALBOT_ROGUE then
    return 1.0,0.96,0.41
  elseif class==HEALBOT_SHAMAN then
    return 0.14,0.35,1.0
  elseif class==HEALBOT_WARLOCK then
    return 0.58,0.51,0.79
  elseif class==HEALBOT_WARRIOR then
    return 0.78,0.61,0.43
  end
  return 0,1,0
end

function HealBot_Action_ShowTooltip(this)
  if HealBot_Config.ShowTooltip==0 then return end
  HealBot_Action_TooltipUnit = this.unit;
  HealBot_Action_DisableTooltipUnit = nil;
  HealBot_Action_RefreshTooltip(this.unit);
end

function HealBot_Action_ShowDisabledTooltip(this)
  if HealBot_Config.ShowTooltip==0 then return end
  HealBot_Action_TooltipUnit = nil;
  HealBot_Action_DisableTooltipUnit = this.unit;
  HealBot_Tooltip_RefreshDisabledTooltip(this.unit);
end

function HealBot_Action_HideTooltip(this)
  HealBot_Action_TooltipUnit = nil;
  HealBot_Action_DisableTooltipUnit = nil;
  HealBot_Tooltip:Hide();
end

function HealBot_Action_Refresh(unit)
  if HealBot_Config.DisableHealBot==1 then return end
  if HealBot_PlayerDead then
    if HealBot_Config.AutoClose==1 and HealBot_Config.ActionVisible~=0 then 
      HideUIPanel(HealBot_Action); 
    else
      HealBot_Action_RefreshButtons(unit);
    end
    return;
  end
  HealBot_Action_RefreshButtons(unit);
  if not HealBot_IsFighting then
    if HealBot_Config.ActionVisible==0 then
      if HealBot_Action_ShouldHealSome(unit) then
        ShowUIPanel(HealBot_Action)
      end
    elseif HealBot_Config.AutoClose==1 then 
      if not HealBot_Action_ShouldHealSome(unit) then
        HideUIPanel(HealBot_Action);
      end
    end
  end
end

local press=nil
function HealBot_Action_SpellPattern(button)
  combos = HealBot_Config.KeyCombo[HealBot_PlayerClass]
  if not combos then return nil end
  press = button;
  if IsAltKeyDown() then press = "Alt"..press end
  if IsControlKeyDown() then press = "Ctrl"..press end
  if IsShiftKeyDown() then press = "Shift"..press end
  return combos[press]
end

--------------------------------------------------------------------------------------------------
-- Widget_OnFoo functions
--------------------------------------------------------------------------------------------------

local hueuName=nil
function HealBot_Action_HealUnit_OnEnter(this)
  if not this.unit then return; end
  hueuName=UnitName(this.unit);
  if HealBot_IsFighting then
    HealBot_Action_ShowTooltip(this);
  elseif HealBot_Config.UseTargetCombatStatus==1 and UnitAffectingCombat(this.unit)==1 then
    HealBot_Action_ShowTooltip(this);
  elseif HealBot_Enabled[hueuName] or HealBot_Config.EnableHealthy==1 then 
    HealBot_Action_ShowTooltip(this);
  else
    if not HealBot_PlayerDead then
      HealBot_Action_ShowDisabledTooltip(this);
    end
  end
end

function HealBot_Action_HealUnit_OnLeave(this)
  HealBot_Action_HideTooltip(this);
end


function HealBot_Action_OptionsButton_OnClick(this)
    HealBot_TogglePanel(HealBot_Options);
end

function HealBot_CT_RaidAssist()
--  if (type(CT_RA_MemberFrame_OnClick)=="function") then
--    HealBot_CT_RA_CustomOnClickFunction_Old = CT_RA_CustomOnClickFunction;
--    CT_RA_CustomOnClickFunction = HealBot_CT_RA_CustomOnClickFunction;
--  end
end



--------------------------------------------------------------------------------------------------
-- Frame_OnFoo functions
--------------------------------------------------------------------------------------------------

function HealBot_Action_OnLoad(this)
--  HealBot_CT_RaidAssist();
end

local HealBot_Action_Timer1 = 0
local haouCnt=false

function HealBot_Action_Set_Timers(override)
  if HealBot_Config.DisableHealBot==0 and HealBot_Config.UseFluidBars==1 and not override then
    HB_Action_Timer1 = 0.012
  else
    HB_Action_Timer1 = 2000
  end
end

function HealBot_Action_OnUpdate(this,arg1)
  HealBot_Action_Timer1 = HealBot_Action_Timer1+arg1
  if HealBot_Action_Timer1>HB_Action_Timer1 then
    HealBot_Action_Timer1 = 0
    for ebubar,value in pairs(HealBot_UnitBarUpdate) do 
	  if value>HealBot_curUnitHealth[ebubar] then
	    if value-HealBot_Config.BarFreq<=HealBot_curUnitHealth[ebubar] then
		  HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
		else
		  HealBot_UnitBarUpdate[ebubar]=value-HealBot_Config.BarFreq
		end
	  elseif value+HealBot_Config.BarFreq>=HealBot_curUnitHealth[ebubar] then
		HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
      else
		HealBot_UnitBarUpdate[ebubar]=value+HealBot_Config.BarFreq
	  end
	  ebubar:SetValue(HealBot_UnitBarUpdate[ebubar])
	  if HealBot_UnitBarUpdate[ebubar]==HealBot_curUnitHealth[ebubar] then
	    HealBot_UnitBarUpdate[ebubar]=nil
	  end
	end
	haouCnt=false
	for _,bar4 in pairs(HealBot_AggroBar4) do
	  bar4:SetStatusBarColor(1,HealBot_AggroBarA,HealBot_AggroBarA,1)
	  haouCnt=true
	end
	if haouCnt then
      if HealBot_AggroBarAup then
	    if HealBot_AggroBarA>=0.9 then
	      HealBot_AggroBarAup=false
	      HealBot_AggroBarA=HealBot_AggroBarA-0.05
        else
	      HealBot_AggroBarA=HealBot_AggroBarA+0.05
	    end
	  else
	    if HealBot_AggroBarA<=0.05 then
	      HealBot_AggroBarAup=true
	      HealBot_AggroBarA=HealBot_AggroBarA+0.05
	    else
	      HealBot_AggroBarA=HealBot_AggroBarA-0.05
	    end
	  end
	elseif not HealBot_AggroBarA~=0.1 then
      HealBot_AggroBarA=0.1
      HealBot_AggroBarAup=true
	end
  end
end

function HealBot_Action_OnShow(this)
  if HealBot_Config.PanelSounds==1 then
    PlaySound("igAbilityOpen");
  end
  HealBot_Config.ActionVisible = 1
  HealBot_Action:SetBackdropColor(
    HealBot_Config.backcolr[HealBot_Config.Current_Skin],
    HealBot_Config.backcolg[HealBot_Config.Current_Skin],
    HealBot_Config.backcolb[HealBot_Config.Current_Skin], 
    HealBot_Config.backcola[HealBot_Config.Current_Skin]);
  HealBot_Action:SetBackdropBorderColor(
    HealBot_Config.borcolr[HealBot_Config.Current_Skin],
    HealBot_Config.borcolg[HealBot_Config.Current_Skin],
    HealBot_Config.borcolb[HealBot_Config.Current_Skin],
    HealBot_Config.borcola[HealBot_Config.Current_Skin]);
end

function HealBot_Action_OnHide(this)
  HealBot_StopMoving(this);
  HealBot_Config.ActionVisible = 0
end

function HealBot_Action_OnMouseDown(this,button)
  if button~="RightButton" then
    if HealBot_Config.ActionLocked==0 then
      HealBot_StartMoving(this);
    end
  end
end

function HealBot_Action_OnMouseUp(this,button)
  if button~="RightButton" then
    HealBot_StopMoving(this);
  elseif not HealBot_IsFighting and HealBot_Config.RightButtonOptions~=0 then
    HealBot_Action_OptionsButton_OnClick();
  end
end

function HealBot_Action_OnDragStart(this,button)
  if HealBot_Config.ActionLocked==0 then
    HealBot_StartMoving(this);
  end
end

function HealBot_Action_OnDragStop(this)
  HealBot_StopMoving(this);
end

local usedSmartCast=nil
local buttonName=nil
local pcspell=nil
local pcuName=nil
local pcuSpellID=nil
local ModKey=nil
local abutton=nil
local aj=nil
function HealBot_Action_PreClick(self,button)
 usedSmartCast=false;
 ModKey=""
 pcuName=UnitName(self.unit)
 if IsControlKeyDown() then ModKey="Ctrl"
 elseif IsAltKeyDown() then ModKey="Alt"
 elseif IsShiftKeyDown() then ModKey="Shift" 
 end
 if button=="LeftButton" then 
   abutton="Left"
   aj=1
 elseif button=="RightButton" then 
   abutton="Right"
   aj=2
 elseif button=="MiddleButton" then 
   abutton="Middle"
   aj=3
 elseif button=="Button4" then 
   abutton="Button4"
   aj=4
 elseif button=="Button5" then 
   abutton="Button5"
   aj=5
 end
 if IsControlKeyDown() and IsAltKeyDown() then
   if HealBot_MyTargets[pcuName] then
     HealBot_MyTargets[pcuName]=nil
   else
     HealBot_MyTargets[pcuName]=true
   end
   HealBot_Action_ResetUnitStatus(HealBot_UnitID[pcuName])
   return
 elseif not HealBot_IsFighting then
  
  if HealBot_Config.UseTargetCombatStatus==1 then
    if UnitAffectingCombat(self.unit)==1 then return; end
  end
  buttonName=self:GetName();
  if HealBot_Config.ProtectPvP==1 then
    if UnitIsPVP(self.unit) and not UnitIsPVP("player") then 
	  HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"nil",aj)
--      HealBot_Action_SetButtonAttribs(self,"nil")
      usedSmartCast=true;
	  return;
    end
  end
  if button=="LeftButton" and not IsModifierKeyDown() then
    if HealBot_Config.SmartCast==1 then 
      pcspell=HealBot_Action_SmartCast(self.unit);
      if pcspell then
        pcuSpellID=HealBot_GetSpellId(pcspell)
        if pcuSpellID then
	      self:SetAttribute("helpbutton1", "heal1");
          self:SetAttribute("type-heal1", "spell");
          self:SetAttribute("spell-heal1", pcspell);
        else
	      self:SetAttribute("helpbutton1", "item1");
          self:SetAttribute("type-item1", "item");
          self:SetAttribute("item-item1", pcspell);
        end
        usedSmartCast=true;
		return;
      end
    end
  end
  if not HealBot_Enabled[pcuName] and HealBot_Config.EnableHealthy==0 then
    HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"Disabled",aj)
    usedSmartCast=true;
  end
 end
end

function HealBot_Action_PostClick(self,button)
  if usedSmartCast then
    HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"Enabled",aj)
  end
end

function HealBot_Action_RetMyTarget(uName)
  return HealBot_MyTargets[uName]
end

local scspell=nil
local scuName=nil
local schealin=nil
local schlth=nil
local scmaxhlth=nil
local schdelta=nil
local scrspell=nil
function HealBot_Action_SmartCast(unit)
  scuName=UnitName(unit);
  scspell=nil
  scrspell=HealBot_hSpell
  if HealBot_PlayerDead or not scuName then return nil; end
  
  if HealBot_Config.SmartCastRes==1 and UnitIsDeadOrGhost(unit) then
   scspell=SmartCast_Res;
   scrspell=HealBot_rSpell
  elseif HealBot_UnitDebuff[scuName] and HealBot_Config.SmartCastDebuff==1 then
    scspell=HealBot_DebuffSpell[HealBot_UnitDebuff[scuName]];
	scrspell=HealBot_dSpell
  elseif HealBot_UnitBuff[scuName] and HealBot_Config.SmartCastBuff==1 then
    scspell=HealBot_UnitBuff[scuName];
	scrspell=HealBot_bSpell
  elseif HealBot_Config.SmartCastHeal==1 then
	schealin = HealBot_RetHealsIn(scuName);
    schlth, scmaxhlth = HealBot_UnitHealth(unit);
    schdelta = scmaxhlth-(schlth+schealin);
    if schdelta>200 then
      scspell=HealBot_SmartCast(unit,schdelta)
    end
  end
  if scspell and scuName~=HealBot_PlayerName then
    if IsSpellInRange(scrspell, unit)~=1 then return nil; end
  end
  return scspell;
end

function HealBot_Action_RetHealBot_ClassCol(uName)
  return HealBot_ClassColR[uName],HealBot_ClassColG[uName],HealBot_ClassColB[uName]
end

function HealBot_Action_ClearHealsIn(Member_Name)
  if HealBot_Enabled[Member_Name] then
    HealBot_Enabled[Member_Name]=nil
    HealBot_ClassColR[Member_Name]=nil
    HealBot_ClassColG[Member_Name]=nil
    HealBot_ClassColB[Member_Name]=nil
    HealBot_UnitBuff[Member_Name]=nil
    HealBot_UnitDebuff[Member_Name]=nil
    if HealBot_AggroBar4[Member_Name] then
      HealBot_AggroBar4[Member_Name]=nil
    end
    if HealBot_Aggro[Member_Name] then
      HealBot_Aggro[Member_Name]=nil
    end
    if HealBot_UnitID[Member_Name] then
      if HealBot_Unit_Button[HealBot_UnitID[Member_Name]] then
	    if not UnitName(HealBot_UnitID[Member_Name]) then
	      HealBot_Unit_Button[HealBot_UnitID[Member_Name]]=nil
	    end
  	  end
      HealBot_UnitID[Member_Name]=nil
    end
  end
end
