local HealBot_ClassColR = {};
local HealBot_ClassColG = {};
local HealBot_ClassColB = {};
local class=nil
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_UnitRangea={}
local HealBot_UnitRangeitr={}
local HealBot_UnitRangeitg={}
local HealBot_UnitRangeitb={}
local HealBot_UnitRangeotr={}
local HealBot_UnitRangeotg={}
local HealBot_UnitRangeotb={}
local HealBot_UnitRangeota={}
local HealBot_curUnitHealth={}
local HealBot_UnitBarUpdate={}
local HealBot_TrackBars={}
local HealBot_ResetBars={}
local HealBot_UnitOffline={}
local HealBot_ResetAttribs=nil
local ceil=ceil;
local b=nil
local btHBbarText=nil
local bttextlen=0
local bthlthdelta=0
local floor=floor
local strlower=strlower
local strlen=strlen
local strsub=strsub
local HealBot_ButtonStore={}
local HealBot_hSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
local HealBot_bSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
local HealBot_dSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
local HealBot_rSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE

local HealBot_ButtonArray1={}
local HealBot_ButtonArray2={}
local HealBot_SmallArray=nil
local HealBot_ButtonArray=1
local HealBot_Aggro={}
local HealBot_AggroBar4={}
local HealBot_AggroBar3={}
local HealBot_AggroBarA=0.8
local HealBot_AggroBarAup=true
local HealBot_MyTargets={}
local HB_Action_Timer1 = 2000

local HealBot_Unit_Bar1={}
local HealBot_Unit_Bar2={}
local HealBot_Unit_Bar3={}
local HealBot_Unit_Bar4={}

local rangeSpell=nil
local sID=nil
local sName=nil
local uHealIn=nil
local uHlth=nil
local uMaxHlth=nil
local uName=nil
local xButton=nil
local xUnit=nil
local x=nil
local y=nil
local HealBot_FrameMoving=nil
local LSM = LibStub("LibSharedMedia-3.0")
local HealBot_UnitThreat={}

-- Register Default HealBot Media
for i = 1, #HealBot_Default_Textures do
	LSM:Register("statusbar", HealBot_Default_Textures[i].name, HealBot_Default_Textures[i].file)
end
for i = 1, #HealBot_Default_Sounds do
	LSM:Register("sound", HealBot_Default_Sounds[i].name, HealBot_Default_Sounds[i].file)
end

local barName=nil
local barTemp=nil
local UnitThreat=0
function HealBot_Action_UpdateAggro(unit,status,threatStatus)
  uName=HealBot_UnitName[unit]
  barName=HealBot_Unit_Bar4[unit]
  HealBot_UnitRange[unit]=-2
  HealBot_UnitStatus[unit]=1
  if not barName or not uName then 
    return 
  else
    HealBot_Aggro[uName]=status
  end
  HealBot_UnitThreat[uName]=threatStatus
  if not status then
    if HealBot_AggroBar4[uName] then
	  barName=HealBot_AggroBar4[uName]
	  barName:SetStatusBarColor(1,0,0,0)
--	  barName:SetValue(0)
	  HealBot_AggroBar4[uName]=nil
	end
  elseif Healbot_Config_Skins.ShowAggroBars[HealBot_Config.Current_Skin]==1 then
    if not HealBot_AggroBar4[uName] then
      HealBot_AggroBar4[uName]=barName
--      barName:SetValue(100)
--	  HealBot_Action_RefreshButtons(unit)
	elseif HealBot_AggroBar4[uName]~=barName then
	  barTemp=HealBot_AggroBar4[uName]
      barTemp:SetStatusBarColor(1,0,0,0)
      HealBot_AggroBar4[uName]=barName
--	  HealBot_Action_RefreshButtons(unit)
	end
  end
end

function HealBot_Action_RetUnitThreat(unitName)
  return HealBot_UnitThreat[unitName] or -1
end

function HealBot_Action_SetrSpell()
    HealBot_hSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
    HealBot_bSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
	HealBot_dSpell=HEALBOT_HEAVY_NETHERWEAVE_BANDAGE
  if strsub(HealBot_PlayerClassEN,1,4)=="DRUI" then
    HealBot_hSpell=HEALBOT_REJUVENATION
	HealBot_bSpell=HEALBOT_MARK_OF_THE_WILD
	HealBot_dSpell=HEALBOT_CURE_POISON
	HealBot_rSpell=HEALBOT_REVIVE
  elseif strsub(HealBot_PlayerClassEN,1,4)=="HUNT" then
    HealBot_hSpell=HEALBOT_MENDPET
  elseif strsub(HealBot_PlayerClassEN,1,4)=="MAGE" then
	HealBot_bSpell=HEALBOT_ARCANE_INTELLECT
	HealBot_dSpell=HEALBOT_REMOVE_LESSER_CURSE
  elseif strsub(HealBot_PlayerClassEN,1,4)=="PALA" then
    HealBot_hSpell=HEALBOT_HOLY_LIGHT
	HealBot_bSpell=HEALBOT_BLESSING_OF_MIGHT
	HealBot_dSpell=HEALBOT_PURIFY
	HealBot_rSpell=HEALBOT_REDEMPTION
  elseif strsub(HealBot_PlayerClassEN,1,4)=="PRIE" then
    HealBot_hSpell=HEALBOT_LESSER_HEAL
	HealBot_bSpell=HEALBOT_POWER_WORD_FORTITUDE
	HealBot_dSpell=HEALBOT_CURE_DISEASE
	HealBot_rSpell=HEALBOT_RESURRECTION
  elseif strsub(HealBot_PlayerClassEN,1,4)=="SHAM" then
    HealBot_hSpell=HEALBOT_HEALING_WAVE
	HealBot_bSpell=HEALBOT_EARTH_SHIELD
	HealBot_dSpell=HEALBOT_CURE_DISEASE
	HealBot_rSpell=HEALBOT_ANCESTRALSPIRIT
  elseif strsub(HealBot_PlayerClassEN,1,4)=="WARL" then
    HealBot_bSpell=HEALBOT_UNENDING_BREATH
  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 hcta=nil
local hcpct=nil
local hrpct=100
function HealBot_HealthColor(unit,hlth,maxhlth,tooltipcol,Member_Name,UnitDead,Member_Buff,Member_Debuff,healin)
  if UnitDead then
    hcpct=0
    hrpct=0
  else
    hcpct = hlth+healin
    if hcpct<maxhlth then
      hcpct=hcpct/maxhlth
    else
      hcpct=1;
    end
    if maxhlth == 0 then
    	hrpct = 100;
    else
    	hrpct=floor((hlth/maxhlth)*100)
    end
  end
  
  if not tooltipcol then
    HealBot_UnitStatus[unit]=1
    if UnitDead then
	  if HealBot_HoT_Active_Button[Member_Name] then HealBot_HoT_RemoveIcon(Member_Name) end
    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_Skins.bareora[HealBot_Config.Current_Skin],
			   hrpct,
			   Healbot_Config_Skins.btextenabledcola[HealBot_Config.Current_Skin]
    elseif Member_Buff and UnitIsConnected(unit) then
		hcr,hcg,hcb=HealBot_Options_RetBuffRGB(Member_Buff)
        return hcr,hcg,hcb,Healbot_Config_Skins.bareora[HealBot_Config.Current_Skin],hrpct,Healbot_Config_Skins.btextenabledcola[HealBot_Config.Current_Skin]
    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 or HealBot_Aggro[Member_Name]) then
    hca=Healbot_Config_Skins.bareora[HealBot_Config.Current_Skin]
	hcta=Healbot_Config_Skins.btextenabledcola[HealBot_Config.Current_Skin]
  else
    hca=Healbot_Config_Skins.bardisa[HealBot_Config.Current_Skin]
	hcta=Healbot_Config_Skins.btextdisbledcola[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
  
  if (Healbot_Config_Skins.SetBarClassColour[HealBot_Config.Current_Skin] == 1) then
  	hcr,hcg,hcb = HealBot_Action_ClassColour(unit);
  end
  
  return hcr,hcg,hcb,hca,hrpct,hcta
end

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 xUnit,_ in pairs(HealBot_Unit_Button) do
  	  if HealBot_Enabled[UnitName(xUnit)] then
  	    return true
 	  end
    end
  end
end

function HealBot_MayHeal(unit, unitName)
  if unitName then
    if unit ~= 'target' then return true end
    if UnitCanAttack("player",unit) then return false end
    return true;
  else
    return false;
  end
end

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

local b3r=nil
local b3g=nil
local b3b=nil
function HealBot_Action_SetBar3Value(button)
  if not button then return end
  barName = HealBot_Unit_Bar3[button.unit]
  if UnitManaMax(button.unit)==0 then
    x=100
  else
    x=UnitManaMax(button.unit)
  end
  y=floor((UnitMana(button.unit)/x)*100)
  b3r,b3g,b3b=HealBot_Action_GetManaBarCol(button.unit)
  barName:SetValue(y);
  barName:SetStatusBarColor(b3r,b3g,b3b,1)
end

function HealBot_Action_GetManaBarCol(unit)
  z=UnitPowerType(unit);
  if z==0 then
    return 0,0,0.8
  elseif z==1 then
    return 0.8,0,0
  elseif z==4 then
    return 0.8,0,0.8
  elseif z==6 then
    return 0.2,0.8,0.8
  end
  return 0.8,0.8,0
end

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

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 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, unitName)

  ebUnit=button.unit
  if not ebUnit then return end

  if not unitName then 
    unitName=HEALBOT_WORDS_UNKNOWN
  end
  ebuUnitDead = UnitIsDeadOrGhost(ebUnit)
  ebubar = HealBot_Unit_Bar1[ebUnit]
  ebubar2 = HealBot_Unit_Bar2[ebUnit]
  uHlth,uMaxHlth=HealBot_UnitHealth(ebUnit, unitName)
  HealBot_UnitRangeSpell[ebUnit]=HealBot_hSpell

  if unitName~=HEALBOT_WORDS_UNKNOWN then
  
    ebuHealBot_UnitDebuff=HealBot_UnitDebuff[unitName]
    ebuHealBot_UnitBuff=HealBot_UnitBuff[unitName]
    uHealIn = HealBot_RetHealsIn(unitName)
	
    if ebuUnitDead then
      if UnitIsFeignDeath(ebUnit) then
        ebuUnitDead = nil
      else
	    if uHealIn>0 then
	      uHealIn=0
        end
        if HealBot_Aggro[unitName] then
	      HealBot_Action_UpdateAggro(ebUnit,false)
	      HealBot_Aggro[unitName]=nil
	    end
	    if HealBot_UnitDebuff[unitName] then
		  HealBot_UnitDebuff[unitName]=nil
        end
	  end
  	  if unitName==HealBot_PlayerName and not HealBot_PlayerDead then
	    HealBot_Action_ResetActiveUnitStatus()
	    HealBot_PlayerDead=true
	  end
    elseif unitName==HealBot_PlayerName and HealBot_PlayerDead then
	  HealBot_Action_ResetActiveUnitStatus()
	  HealBot_PlayerDead=false
    end

    if uHealIn>0 then
      ebipct = uHlth+uHealIn
      if ebipct<uMaxHlth then
        ebipct=ebipct/uMaxHlth
      else
        ebipct=1;
	  end
      ebipct=floor(ebipct*100)
      ebubar2:SetValue(ebipct);
--	  HealBot_UnitRange[ebUnit]=-2
    elseif ebubar2:GetValue()>0 then
      ebubar2:SetValue(0)
    end	
    
	ebur,ebug,ebub,ebua,ebpct,ebusa = HealBot_HealthColor(ebUnit,uHlth,uMaxHlth,false,unitName,ebuUnitDead,ebuHealBot_UnitBuff,ebuHealBot_UnitDebuff,uHealIn)

    if Healbot_Config_Skins.SetClassColourText[HealBot_Config.Current_Skin]==1 and HealBot_ClassColB[unitName] then
      ebusr,ebusg,ebusb = HealBot_ClassColR[unitName],HealBot_ClassColG[unitName],HealBot_ClassColB[unitName];
    elseif ebuHealBot_UnitDebuff then
      ebusr=Healbot_Config_Skins.btextcursecolr[HealBot_Config.Current_Skin];
      ebusg=Healbot_Config_Skins.btextcursecolg[HealBot_Config.Current_Skin];
      ebusb=Healbot_Config_Skins.btextcursecolb[HealBot_Config.Current_Skin];
    else
      ebusr=Healbot_Config_Skins.btextenabledcolr[HealBot_Config.Current_Skin] or 0;
      ebusg=Healbot_Config_Skins.btextenabledcolg[HealBot_Config.Current_Skin] or 0;
      ebusb=Healbot_Config_Skins.btextenabledcolb[HealBot_Config.Current_Skin] or 0;
    end
	
    if HealBot_UnitInRange(HealBot_bSpell, ebUnit)==1 then
    	ebuUnit_BuffRange=true
		ebuUnit_HealRange=true
    elseif HealBot_UnitInRange(HealBot_hSpell, ebUnit)==1 then
    	ebuUnit_BuffRange=false
		ebuUnit_HealRange=true
	else
    	ebuUnit_BuffRange=false
		ebuUnit_HealRange=false
    end

    if uMaxHlth<1 then uHlth=1 end
    if uHlth>uMaxHlth then
       uMaxHlth=HealBot_CorrectPetHealth(ebUnit,uHlth,uMaxHlth,unitName)
    end
  
    if Healbot_Config_Skins.UseFluidBars[HealBot_Config.Current_Skin]==0 then
	  if ebuUnitDead then
	    ebubar:SetValue(0)
	  else
        ebubar:SetValue(ebpct)
	  end
    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 (uHlth<=(uMaxHlth*HealBot_Config.AlertLevel) or HealBot_Aggro[unitName] or HealBot_MyTargets[unitName]) and ebuUnit_HealRange then
	    ebufastenable=true
	  end
	end
    if ebufastenable then
	  HealBot_UnitRange[ebUnit]=1
      ebusa = Healbot_Config_Skins.btextenabledcola[HealBot_Config.Current_Skin];
	  HealBot_Enabled[unitName]=true
      ebubar:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.Barcola[HealBot_Config.Current_Skin]);
      ebubar2:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.BarcolaInHeal[HealBot_Config.Current_Skin]);
      HealBot_UnitRangeitr[ebUnit]=ebusr
      HealBot_UnitRangeitg[ebUnit]=ebusg
      HealBot_UnitRangeitb[ebUnit]=ebusb
      HealBot_UnitRangeotr[ebUnit]=Healbot_Config_Skins.btextdisbledcolr[HealBot_Config.Current_Skin]
      HealBot_UnitRangeotg[ebUnit]=Healbot_Config_Skins.btextdisbledcolg[HealBot_Config.Current_Skin]
      HealBot_UnitRangeotb[ebUnit]=Healbot_Config_Skins.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[unitName]=false
	  if Healbot_Config_Skins.SetClassColourText[HealBot_Config.Current_Skin]==0 then
        ebusr=Healbot_Config_Skins.btextdisbledcolr[HealBot_Config.Current_Skin]
        ebusg=Healbot_Config_Skins.btextdisbledcolg[HealBot_Config.Current_Skin]
        ebusb=Healbot_Config_Skins.btextdisbledcolb[HealBot_Config.Current_Skin]
	  end
      if HealBot_RetHealBot_Ressing(unitName) then
        if ebuUnitDead then
          ebusr=0.2
          ebusg=1.0
          ebusb=0.2
          ebusa=1
        else
          HealBot_UnsetHealBot_Ressing(unitName)
		end
        HealBot_UnitRangeitr[ebUnit]=0.2
        HealBot_UnitRangeitg[ebUnit]=1
        HealBot_UnitRangeitb[ebUnit]=0.2
        HealBot_UnitRangeotr[ebUnit]=0.2
        HealBot_UnitRangeotg[ebUnit]=1
        HealBot_UnitRangeotb[ebUnit]=0.2
      elseif ebuUnitDead and unitName~=HealBot_PlayerName then
        if HealBot_rSpell then
		  HealBot_UnitRangeSpell[ebUnit]=HealBot_rSpell
          if ebuUnit_BuffRange and not UnitIsGhost(ebUnit) then
            ebusr=0;
            ebusg=0;
            ebusb=0;
            ebusa=1;
          else
            ebusr=0.7;
            ebusg=0.7;
            ebusb=0.7;
            ebusa=1;
          end
          if not UnitIsGhost(ebUnit) then
          	HealBot_UnitRangeitr[ebUnit]=0
          	HealBot_UnitRangeitg[ebUnit]=0
          	HealBot_UnitRangeitb[ebUnit]=0
          else
          	HealBot_UnitRangeitr[ebUnit]=0.7
          	HealBot_UnitRangeitg[ebUnit]=0.7
          	HealBot_UnitRangeitb[ebUnit]=0.7
          end
          HealBot_UnitRangeotr[ebUnit]=0.7
          HealBot_UnitRangeotg[ebUnit]=0.7
          HealBot_UnitRangeotb[ebUnit]=0.7
		end
      else
        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_Skins.bardisa[HealBot_Config.Current_Skin]
	  end
	  if UnitIsVisible(ebUnit) or HealBot_Config.NotVisibleDisable==0 then
        ebubar:SetStatusBarColor(ebur,ebug,ebub,ebua);
        ebubar2:SetStatusBarColor(ebur,ebug,ebub,ebua);
	  else
        ebubar:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.bardisa[HealBot_Config.Current_Skin]);
        ebubar2:SetStatusBarColor(ebur,ebug,ebub,Healbot_Config_Skins.bardisa[HealBot_Config.Current_Skin]);
	  end
	  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
	  HealBot_UnitRange[ebUnit]=HealBot_UnitInRange(HealBot_UnitRangeSpell[ebUnit], ebUnit)
    end
    HealBot_UnitRanger[ebUnit]=ebur
    HealBot_UnitRangeg[ebUnit]=ebug
    HealBot_UnitRangeb[ebUnit]=ebub
    HealBot_UnitRangea[ebUnit]=ebua
	HealBot_UnitRangeota[ebUnit]=ebusa
  elseif Delay_RecalcParty==0 then
    Delay_RecalcParty=3
  end
  
  ebubar.txt = getglobal(ebubar:GetName().."_text");
  ebtext=HealBot_Action_HBText(uHlth,uMaxHlth,unitName,ebUnit,uHealIn)
  ebubar.txt:SetText(ebtext);
  if UnitIsVisible(ebUnit) or HealBot_Config.NotVisibleDisable==0 then
    ebubar.txt:SetTextColor(ebusr,ebusg,ebusb,ebusa);
  else
    ebubar.txt:SetTextColor(ebusr,ebusg,ebusb,Healbot_Config_Skins.btextdisbledcola[HealBot_Config.Current_Skin]);
  end

--  HealBot_UnitRange[ebUnit]=HealBot_UnitInRange(HealBot_UnitRangeSpell[ebUnit], ebUnit) 
end

function HealBot_Action_HBText(hlth,maxhlth,unitName,unit,healin)
  btHBbarText=" "
  if Healbot_Config_Skins.ShowClassOnBar[HealBot_Config.Current_Skin]==1 and UnitClass(unit) then
      if Healbot_Config_Skins.ShowNameOnBar[HealBot_Config.Current_Skin]==1 then
        uName=UnitClass(unit)..":"..unitName;
      else
        uName=UnitClass(unit);
      end
  elseif Healbot_Config_Skins.ShowNameOnBar[HealBot_Config.Current_Skin]==1 then
    uName=unitName;
  else
    uName=nil
  end
  
  bttextlen = floor(5+(((Healbot_Config_Skins.bwidth[HealBot_Config.Current_Skin]*1.8)/Healbot_Config_Skins.btextheight[HealBot_Config.Current_Skin])-(Healbot_Config_Skins.btextheight[HealBot_Config.Current_Skin]/2)))-2
  if Healbot_Config_Skins.ShowHealthOnBar[HealBot_Config.Current_Skin]==1 and maxhlth then
    if Healbot_Config_Skins.BarHealthType[HealBot_Config.Current_Skin]==1 then
      if Healbot_Config_Skins.BarHealthIncHeals[HealBot_Config.Current_Skin]==1 then
	    healin = HealBot_RetHealsIn(unitName)
        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_Skins.BarHealthIncHeals[HealBot_Config.Current_Skin]==1 then
        healin = HealBot_RetHealsIn(unitName);
        btHBbarText=btHBbarText.."("..floor(((hlth+healin)/maxhlth)*100).."%)"
      else
        btHBbarText=btHBbarText.."("..floor((hlth/maxhlth)*100).."%)"
      end
    end
  end
	if HealBot_Action_UnitIsOffline(unit, unitName) and uName then
  	uName=HEALBOT_DISCONNECTED_TEXT.." "..uName;
  end	-- added by Diacono of Ursin
  if HealBot_Aggro[unitName] and Healbot_Config_Skins.ShowAggroText[HealBot_Config.Current_Skin]==1 and uName then
    uName=">> "..uName.." <<"
  end
  bttextlen=bttextlen-strlen(btHBbarText)
  if bttextlen<1 then bttextlen=1; end
  if uName and strlen(uName)>bttextlen then
    btHBbarText = string.sub(uName,1,bttextlen) .. '..'..btHBbarText;
  elseif uName then
    btHBbarText = uName..btHBbarText;
  end
  return btHBbarText;
end

function HealBot_Action_UnitIsOffline(unit,unitName,preset) -- added by Diacono of Ursin
  x = nil;
  if preset then
    HealBot_UnitOffline[unitName]=preset
  else
	if HealBot_UnitOffline[unitName] then
		if time() - HealBot_UnitOffline[unitName] <= 2 then
			x = true;
		end
	end
	if not UnitIsConnected(unit) or x then
		if not HealBot_UnitOffline[unitName] then
			HealBot_UnitOffline[unitName] = time();
		elseif HealBot_UnitOffline[unitName] == -1 then
			HealBot_UnitOffline[unitName] = nil;
		end
	else
		HealBot_UnitOffline[unitName] = nil;
	end
	return HealBot_UnitOffline[unitName]
  end
end

function HealBot_Action_retUnitOffline(unitName)
  return HealBot_UnitOffline[unitName]
end

function HealBot_Action_RefreshButton(button, unitName)
  if not button then return end
--  if type(button)~="table" then DEFAULT_CHAT_FRAME:AddMessage("***** "..type(button)) end
  HealBot_Action_EnableButton(button, unitName)
  if HealBot_UnitName["target"] and HealBot_UnitName["target"]==unitName and button.unit~="target" and UnitName("target") and UnitName("target")==unitName and HealBot_Unit_Button["target"] then
    HealBot_Action_EnableButton(HealBot_Unit_Button["target"], unitName)
  end
end

function HealBot_Action_ResetSkin()
local bwidth = Healbot_Config_Skins.bwidth[HealBot_Config.Current_Skin];
local bheight=Healbot_Config_Skins.bheight[HealBot_Config.Current_Skin];
local br=Healbot_Config_Skins.headbarcolr[HealBot_Config.Current_Skin];  
local bg=Healbot_Config_Skins.headbarcolg[HealBot_Config.Current_Skin];
local bb=Healbot_Config_Skins.headbarcolb[HealBot_Config.Current_Skin];
local ba=Healbot_Config_Skins.headbarcola[HealBot_Config.Current_Skin];
local sr=Healbot_Config_Skins.headtxtcolr[HealBot_Config.Current_Skin];  
local sg=Healbot_Config_Skins.headtxtcolg[HealBot_Config.Current_Skin];
local sb=Healbot_Config_Skins.headtxtcolb[HealBot_Config.Current_Skin];
local sa=Healbot_Config_Skins.headtxtcola[HealBot_Config.Current_Skin];
local btexture=Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin];
local btextheight=Healbot_Config_Skins.btextheight[HealBot_Config.Current_Skin]; 
local b,bar,bar2,bar3,bar4,icon1,icon2,icon3,icon4,icon5,icon1t,icon2t,icon3t,icon4t,icon1ta,icon2ta,icon3ta,icon4ta,barScale,h,hwidth,hheight
local iScale = (bheight*Healbot_Config_Skins.IconScale[HealBot_Config.Current_Skin])-2
local itScale=ceil(iScale*Healbot_Config_Skins.IconTextScale[HealBot_Config.Current_Skin])
local abSize = Healbot_Config_Skins.AggroBarSize[HealBot_Config.Current_Skin] or 2
local abtSize = {[0]=1,[1]=1,[2]=1,[3]=2,[4]=2,[5]=2,[6]=3,[7]=3,[8]=3,[9]=3,[10]=4,[11]=4,[12]=4,[13]=4,[14]=4,[15]=5}
  HealBot_Tooltip:SetBackdropColor(0,0,0,HealBot_Config.ttalpha)
  
  if Healbot_Config_Skins.ShowAggro[HealBot_Config.Current_Skin]==0 or Healbot_Config_Skins.ShowAggroBars[HealBot_Config.Current_Skin]==0 then abSize=0 end
  
  for x=1,51 do
    b=getglobal("HealBot_Action_HealUnit"..x);
    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");
    icon4 = getglobal(bar:GetName().."Icon4");
	icon5 = getglobal(bar:GetName().."Icon5");
    icon1t = getglobal(bar:GetName().."Count1");
    icon2t = getglobal(bar:GetName().."Count2");
    icon3t = getglobal(bar:GetName().."Count3"); 
    icon4t = getglobal(bar:GetName().."Count4"); 
	icon5t = getglobal(bar:GetName().."Count5"); 
    icon1ta = getglobal(bar:GetName().."Count1a");
    icon2ta = getglobal(bar:GetName().."Count2a");
    icon3ta = getglobal(bar:GetName().."Count3a"); 
    icon4ta = getglobal(bar:GetName().."Count4a"); 
	icon5ta = getglobal(bar:GetName().."Count5a"); 
    HealBot_Panel_SetBarArrays(b)
    bar.txt = getglobal(bar:GetName().."_text");
    bar:SetHeight(bheight);
    bar:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
  	bar.txt:SetFont(LSM:Fetch('font',Healbot_Config_Skins.btextfont[HealBot_Config.Current_Skin]),btextheight,"");
    --bar.txt:SetTextHeight(btextheight);
    bar2:SetHeight(bheight);
    bar2:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    bar3:SetHeight(Healbot_Config_Skins.bar2size[HealBot_Config.Current_Skin]);
    bar3:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
	bar4:ClearAllPoints();
	bar4:SetPoint("TOPLEFT",bar,"TOPLEFT",0,abSize);
	bar4:SetPoint("TOPRIGHT",bar,"TOPRIGHT",0,abSize);
	if Healbot_Config_Skins.bar2size[HealBot_Config.Current_Skin]==0 then
	  bar4:SetHeight(bheight+(abSize*2))
	else
      bar4:SetHeight(Healbot_Config_Skins.bar2size[HealBot_Config.Current_Skin]+bheight+(abSize*2))
	end
	bar4:SetStatusBarTexture('Interface\\Addons\\HealBot\\Images\\aggro'..abtSize[abSize]..'.tga')
	bar4:SetStatusBarColor(1,0,0,0)
	bar4:SetMinMaxValues(0,1)
	bar4:SetValue(1)
    b:SetHeight(bheight);  
    b:Enable();
	b.name="init"
    icon1:SetHeight(iScale);
    icon1:SetWidth(iScale);
    icon2:SetHeight(iScale);
    icon2:SetWidth(iScale);
    icon3:SetHeight(iScale);
    icon3:SetWidth(iScale);
    icon4:SetHeight(iScale);
    icon4:SetWidth(iScale);
	icon5:SetHeight(iScale);
	icon5:SetWidth(iScale);
	icon1t:SetTextHeight(itScale)
	icon1ta:SetTextHeight(itScale)
	icon2t:SetTextHeight(itScale)
	icon2ta:SetTextHeight(itScale)
	icon3t:SetTextHeight(itScale)
	icon3ta:SetTextHeight(itScale)
	icon4t:SetTextHeight(itScale)
	icon4ta:SetTextHeight(itScale)
	icon5t:SetTextHeight(itScale)
	icon5ta:SetTextHeight(itScale)
	bar:SetMinMaxValues(0,100)
	bar2:SetMinMaxValues(0,100);
	bar3:SetMinMaxValues(0,100)
	bar2:SetValue(0);
    bar:SetStatusBarColor(0,1,0,Healbot_Config_Skins.bardisa[HealBot_Config.Current_Skin]);
    bar2:SetStatusBarColor(0,1,0,0);

    if Healbot_Config_Skins.HoTonBar[HealBot_Config.Current_Skin]==1 then
      HealBot_Panel_SetMultiColHoToffset(0)
      if Healbot_Config_Skins.HoTposBar[HealBot_Config.Current_Skin]==1 then
        icon1:ClearAllPoints();
        icon1:SetPoint("BOTTOMLEFT",b,"BOTTOMLEFT",1,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("BOTTOMLEFT",icon1,"BOTTOMRIGHT",1,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("BOTTOMLEFT",icon2,"BOTTOMRIGHT",1,0);
        icon4:ClearAllPoints();
        icon4:SetPoint("BOTTOMLEFT",icon3,"BOTTOMRIGHT",1,0);
		icon5:ClearAllPoints();
		icon5:SetPoint("BOTTOMLEFT",icon4,"BOTTOMRIGHT",1,0);
        icon1t:ClearAllPoints();
        icon1t:SetPoint("BOTTOMLEFT",icon1,"BOTTOMLEFT",-2,0);
        icon1ta:ClearAllPoints();
        icon1ta:SetPoint("TOPRIGHT",icon1,"TOPRIGHT",4,0);
        icon2t:ClearAllPoints();
        icon2t:SetPoint("BOTTOMLEFT",icon2,"BOTTOMLEFT",-2,0);
        icon2ta:ClearAllPoints();
        icon2ta:SetPoint("TOPRIGHT",icon2,"TOPRIGHT",4,0);
        icon3t:ClearAllPoints();
        icon3t:SetPoint("BOTTOMLEFT",icon3,"BOTTOMLEFT",-2,0); 
        icon3ta:ClearAllPoints();
        icon3ta:SetPoint("TOPRIGHT",icon3,"TOPRIGHT",4,0);
        icon4t:ClearAllPoints();
        icon4t:SetPoint("BOTTOMLEFT",icon4,"BOTTOMLEFT",-2,0); 
        icon4ta:ClearAllPoints();
        icon4ta:SetPoint("TOPRIGHT",icon4,"TOPRIGHT",4,0);
        icon5t:ClearAllPoints();
        icon5t:SetPoint("BOTTOMLEFT",icon5,"BOTTOMLEFT",-2,0); 
        icon5ta:ClearAllPoints();
        icon5ta:SetPoint("TOPRIGHT",icon5,"TOPRIGHT",4,0);
      else
        icon1:ClearAllPoints();
        icon1:SetPoint("BOTTOMRIGHT",b,"BOTTOMRIGHT",-1,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMLEFT",-1,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMLEFT",-1,0);
        icon4:ClearAllPoints();
        icon4:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMLEFT",-1,0);
		icon5:ClearAllPoints();
        icon5:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMLEFT",-1,0);
        icon1t:ClearAllPoints();
        icon1t:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMRIGHT",4,0);
        icon1ta:ClearAllPoints();
        icon1ta:SetPoint("TOPLEFT",icon1,"TOPLEFT",-2,0);
        icon2t:ClearAllPoints();
        icon2t:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMRIGHT",4,0);
        icon2ta:ClearAllPoints();
        icon2ta:SetPoint("TOPLEFT",icon2,"TOPLEFT",-2,0);
        icon3t:ClearAllPoints();
        icon3t:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMRIGHT",4,0); 
        icon3ta:ClearAllPoints();
        icon3ta:SetPoint("TOPLEFT",icon3,"TOPLEFT",-2,0);
        icon4t:ClearAllPoints();
        icon4t:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMRIGHT",4,0); 
        icon4ta:ClearAllPoints();
        icon4ta:SetPoint("TOPLEFT",icon4,"TOPLEFT",-2,0);
		icon5t:ClearAllPoints();
        icon5t:SetPoint("BOTTOMRIGHT",icon5,"BOTTOMRIGHT",4,0); 
        icon5ta:ClearAllPoints();
        icon5ta:SetPoint("TOPLEFT",icon5,"TOPLEFT",-2,0);
      end
    else
      HealBot_Panel_SetMultiColHoToffset((iScale+1)*5)
      if Healbot_Config_Skins.HoTposBar[HealBot_Config.Current_Skin]==1 then
        icon1:ClearAllPoints();
        icon1:SetPoint("BOTTOMRIGHT",b,"BOTTOMLEFT",-1,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMLEFT",-2,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMLEFT",-2,0);
        icon4:ClearAllPoints();
        icon4:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMLEFT",-2,0);
		icon5:ClearAllPoints();
        icon5:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMLEFT",-2,0);
        icon1t:ClearAllPoints();
        icon1t:SetPoint("BOTTOMRIGHT",icon1,"BOTTOMRIGHT",4,0);
        icon1ta:ClearAllPoints();
        icon1ta:SetPoint("TOPLEFT",icon1,"TOPLEFT",-2,0);
        icon2t:ClearAllPoints();
        icon2t:SetPoint("BOTTOMRIGHT",icon2,"BOTTOMRIGHT",4,0);
        icon2ta:ClearAllPoints();
        icon2ta:SetPoint("TOPLEFT",icon2,"TOPLEFT",-2,0);
        icon3t:ClearAllPoints();
        icon3t:SetPoint("BOTTOMRIGHT",icon3,"BOTTOMRIGHT",4,0); 
        icon3ta:ClearAllPoints();
        icon3ta:SetPoint("TOPLEFT",icon3,"TOPLEFT",-2,0);
        icon4t:ClearAllPoints();
        icon4t:SetPoint("BOTTOMRIGHT",icon4,"BOTTOMRIGHT",4,0); 
        icon4ta:ClearAllPoints();
        icon4ta:SetPoint("TOPLEFT",icon4,"TOPLEFT",-2,0);
		icon5t:ClearAllPoints();
        icon5t:SetPoint("BOTTOMRIGHT",icon5,"BOTTOMRIGHT",4,0); 
        icon5ta:ClearAllPoints();
        icon5ta:SetPoint("TOPLEFT",icon5,"TOPLEFT",-2,0);
      else
        icon1:ClearAllPoints();
        icon1:SetPoint("BOTTOMLEFT",b,"BOTTOMRIGHT",2,0);
        icon2:ClearAllPoints();
        icon2:SetPoint("BOTTOMLEFT",icon1,"BOTTOMRIGHT",2,0);
        icon3:ClearAllPoints();
        icon3:SetPoint("BOTTOMLEFT",icon2,"BOTTOMRIGHT",2,0);
        icon4:ClearAllPoints();
        icon4:SetPoint("BOTTOMLEFT",icon3,"BOTTOMRIGHT",2,0);
		icon5:ClearAllPoints();
        icon5:SetPoint("BOTTOMLEFT",icon4,"BOTTOMRIGHT",2,0);
        icon1t:ClearAllPoints();
        icon1t:SetPoint("BOTTOMLEFT",icon1,"BOTTOMLEFT",-2,0);
        icon1ta:ClearAllPoints();
        icon1ta:SetPoint("TOPRIGHT",icon1,"TOPRIGHT",4,0);
        icon2t:ClearAllPoints();
        icon2t:SetPoint("BOTTOMLEFT",icon2,"BOTTOMLEFT",-2,0);
        icon2ta:ClearAllPoints();
        icon2ta:SetPoint("TOPRIGHT",icon2,"TOPRIGHT",4,0);
        icon3t:ClearAllPoints();
        icon3t:SetPoint("BOTTOMLEFT",icon3,"BOTTOMLEFT",-2,0); 
        icon3ta:ClearAllPoints();
        icon3ta:SetPoint("TOPRIGHT",icon3,"TOPRIGHT",4,0);
        icon4t:ClearAllPoints();
        icon4t:SetPoint("BOTTOMLEFT",icon4,"BOTTOMLEFT",-2,0); 
        icon4ta:ClearAllPoints();
        icon4ta:SetPoint("TOPRIGHT",icon4,"TOPRIGHT",4,0);
		icon5t:ClearAllPoints();
        icon5t:SetPoint("BOTTOMLEFT",icon5,"BOTTOMLEFT",-2,0); 
        icon5ta:ClearAllPoints();
        icon5ta:SetPoint("TOPRIGHT",icon5,"TOPRIGHT",4,0);
      end
    end

    if Healbot_Config_Skins.bar2size[HealBot_Config.Current_Skin]==0 then
      bar3:SetHeight(1);
      bar3:SetValue(0);
      bar3:SetStatusBarColor(0,0,0,0)
    end
	if Healbot_Config_Skins.ShowAggroBars[HealBot_Config.Current_Skin]==0 then
	  bar4:SetMinMaxValues(0,100)
	  bar4:SetValue(0)
	  bar4:SetStatusBarColor(0,0,0,0)
	end
    barScale = bar:GetScale();
    bar:SetScale(barScale + 0.01);
    bar:SetScale(barScale);
    barScale = bar2:GetScale();
    bar2:SetScale(barScale + 0.01);
    bar2:SetScale(barScale);
    barScale = bar3:GetScale();
    bar3:SetScale(barScale + 0.01);
    bar3:SetScale(barScale);
    barScale = bar4:GetScale();
    bar4:SetScale(barScale + 0.01);
    bar4:SetScale(barScale);
  end
  for x=1,15 do
    h=getglobal("HealBot_Action_Header"..x);
    bar = HealBot_Action_HealthBar(h);
    hwidth = bwidth*Healbot_Config_Skins.headwidth[HealBot_Config.Current_Skin]
    hheight = ceil(bheight*0.8)
    HealBot_Panel_SetHeadArrays(h)
    h:SetHeight(hheight);
    h:SetWidth(hwidth);
    bar:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.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:SetFont(LSM:Fetch('font',Healbot_Config_Skins.headtextfont[HealBot_Config.Current_Skin]),Healbot_Config_Skins.headtextheight[HealBot_Config.Current_Skin],"");
    bar.txt:SetTextColor(sr,sg,sb,sa);
    h:Disable();
    bar:SetScale(barScale + 0.01);
    bar:SetScale(barScale);
    barScale = bar2:GetScale();
  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_SetAddHeight()
  Delay_RecalcParty=1;

  if HealBot_Options:IsVisible() then 
    HealBot_DiseaseColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_MagicColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_PoisonColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_CurseColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_CustomColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_EnTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_EnTextColorpickin:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_DisTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_DebTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_HeadBarColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.headtexture[HealBot_Config.Current_Skin]));
    HealBot_HeadTextColorpick:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.headtexture[HealBot_Config.Current_Skin]));
    HealBot_Buff1Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff2Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff3Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff4Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff5Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff6Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff7Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff8Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_Buff9Colour:SetStatusBarTexture(LSM:Fetch('statusbar',Healbot_Config_Skins.btexture[HealBot_Config.Current_Skin]));
    HealBot_SetSkinColours()
	HealBot_CheckFrame()
  end
end

function HealBot_Action_RefreshButtons(unit)
  if unit then
    HealBot_Action_RefreshButton(HealBot_Unit_Button[unit], UnitName(unit))
  else
    if HealBot_ButtonArray==1 then
      for xUnit,xButton in pairs(HealBot_ButtonArray1) do
	    HealBot_Action_CheckRange(xUnit, xButton)
      end
	  HealBot_ButtonArray=2
    elseif HealBot_ButtonArray==2 then
      for xUnit,xButton in pairs(HealBot_ButtonArray2) do
	    HealBot_Action_CheckRange(xUnit, xButton)
      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
    y=HealBot_UnitInRange(HealBot_UnitRangeSpell[unit] or HealBot_hSpell, unit)
    if HealBot_UnitRange[unit]==-2 then
	  HealBot_Action_RefreshButton(button, UnitName(unit))
	elseif y~=HealBot_UnitRange[unit] then
	  ebubar = HealBot_Unit_Bar1[unit]
	  ebubar.txt=getglobal(ebubar:GetName().."_text");
      HealBot_UnitRange[unit]=y
	  if y==1 and not HealBot_PlayerDead then
        ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],Healbot_Config_Skins.Barcola[HealBot_Config.Current_Skin])
        ebubar.txt:SetTextColor(HealBot_UnitRangeitr[unit],HealBot_UnitRangeitg[unit],HealBot_UnitRangeitb[unit],Healbot_Config_Skins.btextenabledcola[HealBot_Config.Current_Skin]);
	  elseif y==0 or HealBot_Config.NotVisibleDisable==0 then
	    --ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],Healbot_Config_Skins.bardisa[HealBot_Config.Current_Skin])
        ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],HealBot_UnitRangea[unit])
        ebubar.txt:SetTextColor(HealBot_UnitRangeotr[unit],HealBot_UnitRangeotg[unit],HealBot_UnitRangeotb[unit],HealBot_UnitRangeota[unit]);
	  else
		ebubar:SetStatusBarColor(HealBot_UnitRanger[unit],HealBot_UnitRangeg[unit],HealBot_UnitRangeb[unit],Healbot_Config_Skins.bardisa[HealBot_Config.Current_Skin])
        ebubar.txt:SetTextColor(HealBot_UnitRangeotr[unit],HealBot_UnitRangeotg[unit],HealBot_UnitRangeotb[unit],Healbot_Config_Skins.btextdisbledcola[HealBot_Config.Current_Skin]);
      end
    end
  end
end

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

function HealBot_Action_ResetActiveUnitStatus()
  for x,_ in pairs(HealBot_Unit_Button) do
    if HealBot_UnitStatus[x]>0 or UnitHealth(x)<2 then
      HealBot_UnitRange[x]=-2
	end
  end
end

local shb=nil
local shbar=nil
function HealBot_Action_SetHealButton(index,unit,unitName)

  if UnitClass(unit) then
	  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]
	  end
      if not HealBot_ClassColR[unitName] then
        HealBot_ClassColR[unitName],HealBot_ClassColG[unitName],HealBot_ClassColB[unitName] = HealBot_Action_ClassColour(unit);
	    HealBot_UnitID[unitName]="init"
		HealBot_Unit_Button[unit]="init"
		if not HealBot_UnitSpec[unitName] then
		  HealBot_UnitSpec[unitName] = " "
		end
      end
	  if HealBot_UnitID[unitName]~=unit then
        HealBot_CheckAllBuffs(unitName)
        HealBot_CheckAllDebuffs(unitName)
	    HealBot_UnitID[unitName]=unit
	  end
      if HealBot_Unit_Button[unit]~=shb then
	    shb.unit=unit
		shb:SetAttribute("unit", unit);
		if not shb.index or HealBot_Config.FastAttrib==0 then 
		  HealBot_Action_SetAllButtonAttribs(shb,"Enabled")
		else
          HealBot_Action_SetMacroButtonAttribs(shb)
		end
	    shb.index=index
		shb.name=unitName
        HealBot_Unit_Button[unit]=shb
        HealBot_UnitStatus[unit]=9
		HealBot_Unit_Bar1[unit]=HealBot_Action_HealthBar(shb)
        HealBot_Unit_Bar2[unit]=HealBot_Action_HealthBar2(shb)
        HealBot_Unit_Bar3[unit]=HealBot_Action_HealthBar3(shb)
        HealBot_Unit_Bar4[unit]=HealBot_Action_HealthBar4(shb)
	    shbar=HealBot_Unit_Bar1[unit]
  	    HealBot_UnitBarUpdate[shbar]=100
		HealBot_curUnitHealth[shbar]=100
		HealBot_UnitRangeSpell[unit]=HealBot_hSpell
	  elseif unitName~=shb.name then
	    shb.name=unitName
        HealBot_UnitStatus[unit]=9
      end
  else
    return nil
  end
  HealBot_TrackBars[shb]=nil
  return shb
end

function HealBot_Action_SetTargetHealButton(unit,unitName)
  if UnitClass(unit) then
    shb=getglobal("HealBot_Action_HealUnit51");
    if not HealBot_ClassColR[unitName] then
      HealBot_ClassColR[unitName],HealBot_ClassColG[unitName],HealBot_ClassColB[unitName] = HealBot_Action_ClassColour(unit);
	  HealBot_Unit_Button[unit]="init"
	  if not HealBot_UnitSpec[unitName] then
		HealBot_UnitSpec[unitName] = " "
	  end
    end
    if HealBot_Unit_Button[unit]~=shb then
	  shb.unit=unit
  	  shb.index=51
	  shb.name=unitName
      shb:SetAttribute("unit", unit);
      HealBot_Unit_Button[unit]=shb
      HealBot_UnitStatus[unit]=9
	  HealBot_Unit_Bar1[unit]=HealBot_Action_HealthBar(shb)
      HealBot_Unit_Bar2[unit]=HealBot_Action_HealthBar2(shb)
      HealBot_Unit_Bar3[unit]=HealBot_Action_HealthBar3(shb)
      HealBot_Unit_Bar4[unit]=HealBot_Action_HealthBar4(shb)
	  shbar=HealBot_Unit_Bar1[unit]
  	  HealBot_UnitBarUpdate[shbar]=100
	  HealBot_curUnitHealth[shbar]=100
	  HealBot_UnitRangeSpell[unit]=HealBot_hSpell
	elseif unitName~=shb.name then
      HealBot_CheckAllBuffs(unitName)
      HealBot_CheckAllDebuffs(unitName)
	  shb.name=unitName
      HealBot_UnitStatus[unit]=9
    end
  else
    return nil
  end
  HealBot_TrackBars[shb]=nil
  return shb
end

local HealBotButtonMacroAttribs={}
function HealBot_Action_SetAllAttribs()
 --   HealBot_AddDebug("In HealBot_Action_SetAllAttribs")
    HealBot_ResetAttribs=true
    Delay_RecalcParty=1
end

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

local HB_button,HB_prefix=nil
local HB_combo_prefix=nil
local HealBot_Keys_List = {"","Shift","Ctrl","Alt"}
function HealBot_Action_SetAllButtonAttribs(button,status)
  for x=1,5 do
    if x==1 then HB_button="Left";
    elseif x==2 then HB_button="Right";
    elseif x==3 then HB_button="Middle";
    elseif x==4 then HB_button="Button4";
    elseif x==5 then HB_button="Button5";
    end
    
    for y=1, getn(HealBot_Keys_List), 1 do
	  HealBot_Action_SetButtonAttrib(button,HB_button,HealBot_Keys_List[y],status,x)
    end
  end
end

local mUnit=nil
local mText=nil
local mId=nil
local mName=nil
local mPrefix=nil
function HealBot_Action_SetMacroButtonAttribs(button)
  for mPrefix,mName in pairs(HealBotButtonMacroAttribs) do
    HealBot_Action_SetMacroButtonAttrib(button,mPrefix,mName)
  end
end

function HealBot_Action_SetMacroButtonAttrib(button,mPrefix,mName)
  if strlen(mPrefix)>1 then
    HB_prefix=strsub(mPrefix,1,strlen(mPrefix)-1)
    j=strsub(mPrefix,strlen(mPrefix),1)
  else
    HB_prefix=""
	j=mPrefix
  end
  mId=GetMacroIndexByName(mName)
  if mId ~= 0 then
    _,_,mText=GetMacroInfo(mId)
    mUnit = button.unit
--    HealBot_AddDebug("Set Macro "..mName.." on unit "..mUnit.." mPrefix="..mPrefix)
    mText=string.gsub(mText,"hbtarget",mUnit)
    button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
    button:SetAttribute(HB_prefix.."type"..j,"macro")
    button:SetAttribute(HB_prefix.."macrotext"..j, mText)
  else
    HealBot_Action_SetAllButtonAttribs(button,"Enabled")
  end
end

function HealBot_Action_SetButtonAttrib(button,bbutton,bkey,status,j)
 
      if strlen(bkey)>1 then
        HB_prefix = strlower(bkey).."-"
      else
        HB_prefix = "";
      end
      HB_combo_prefix = bkey..bbutton;
      if status=="Enabled" then
        sName = HealBot_Action_AttribSpellPattern(HB_combo_prefix)
      elseif status=="Disabled" then
        sName = HealBot_Action_AttribDisSpellPattern(HB_combo_prefix)
      else
        sName=nil;
      end
      if sName then
        if strlower(sName)==strlower(HEALBOT_DISABLED_TARGET) then
          button:SetAttribute(HB_prefix.."helpbutton"..j, "target"..j);
		  button:SetAttribute(HB_prefix.."type"..j, "target")
          button:SetAttribute(HB_prefix.."type-target"..j, "target")
        elseif strlower(sName)==strlower(HEALBOT_FOCUS) then
          button:SetAttribute(HB_prefix.."helpbutton"..j, "focus"..j);
		  button:SetAttribute(HB_prefix.."type"..j, "focus")
          button:SetAttribute(HB_prefix.."type-focus"..j, "focus")
        elseif strlower(sName)==strlower(HEALBOT_ASSIST) then
          button:SetAttribute(HB_prefix.."helpbutton"..j, "assist"..j);
		  button:SetAttribute(HB_prefix.."type"..j, "assist")
          button:SetAttribute(HB_prefix.."type-assist"..j, "assist")
        else
          sID=HealBot_GetSpellId(sName)
          if sID then
            button:SetAttribute(HB_prefix.."helpbutton"..j, "heal"..j);
            button:SetAttribute(HB_prefix.."type-heal"..j, "spell");
            button:SetAttribute(HB_prefix.."spell-heal"..j, sName);
	      else
	        mId=GetMacroIndexByName(sName)
			if mId ~= 0 then
               _,_,mText=GetMacroInfo(mId)
              mUnit = button.unit
              mText=string.gsub(mText,"hbtarget",mUnit)
              button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
              button:SetAttribute(HB_prefix.."type"..j,"macro")
              button:SetAttribute(HB_prefix.."macrotext"..j, mText)
			  if status=="Enabled" then
			    HealBotButtonMacroAttribs[HB_prefix..j]=sName
			  end
            else
              button:SetAttribute(HB_prefix.."helpbutton"..j, "item"..j);
              button:SetAttribute(HB_prefix.."type-item"..j, "item");
              button:SetAttribute(HB_prefix.."item-item"..j, sName);
            end
		  end
        end
      else
        button:SetAttribute(HB_prefix.."helpbutton"..j, nil);
      end
end

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

function HealBot_Action_AttribDisSpellPattern(HB_combo_prefix)
  combos = HealBot_Config.DisabledKeyCombo
  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
function HealBot_Action_PartyChanged(HealBot_PreCombat)
  if HealBot_Config.DisableHealBot==1 then return end
  if not HealBot_IsFighting then
    if HealBot_FrameMoving then
	  HealBot_Action_OnDragStop(self)
	end
  
	if HealBot_ResetAttribs then
      for j=1,50 do
	    b=getglobal("HealBot_Action_HealUnit"..j)
	    b.index=nil
      end
      for x,_ in pairs(HealBot_UnitID) do
        HealBot_UnitID[x]=nil;
      end
      for x,_ in pairs(HealBotButtonMacroAttribs) do
        HealBotButtonMacroAttribs[x]=nil;
      end
	  HealBot_ResetAttribs=nil
	end

	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 x=1,50 do
        b=getglobal("HealBot_Action_HealUnit"..x);
	    table.insert(HealBot_ButtonStore,b);
      end
      hbInitButtons=true
    end
    if HealBot_PreCombat then 
      HealBot_IsFighting=true;
    end
  
    if Healbot_Config_Skins.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 unit,button in pairs(HealBot_Unit_Button) do
	  uCnt=uCnt+1
	    b=HealBot_Unit_Button[unit]
        if Healbot_Config_Skins.bar2size[HealBot_Config.Current_Skin]>0 then HealBot_Action_RefreshBar3(unit) end
		if HealBot_ButtonArray==1 then
		  HealBot_ButtonArray1[unit]=HealBot_Unit_Button[unit]
		  HealBot_ButtonArray=2
		else
 		  HealBot_ButtonArray2[unit]=HealBot_Unit_Button[unit]
		  HealBot_ButtonArray=1
		end
		if HealBot_UnitStatus[unit]==9 then
          HealBot_Action_ResetUnitStatus(unit)
		end
		if uCnt==1 then
		  HealBot_SmallArray=unit
		end 
	end
	if uCnt==1 then
	  HealBot_ButtonArray2[HealBot_SmallArray]=HealBot_Unit_Button[HealBot_SmallArray]
	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=3
		  HealBot_ResetBars[unit]=true
--		  HealBot_AddDebug("removed bar - set Delay_RecalcParty=2 : unit="..unit)
		else
		  HealBot_Unit_Button[unit]=nil
		end
	  end
	  HealBot_Action_InsButtonStore(button)
--	  HealBot_AddDebug("removed bar : unit="..unit)
	  HealBot_TrackBars[button]=nil
	end
  else
    Delay_RecalcParty=3
  end
end

function HealBot_Action_InsButtonStore(b)
  table.insert(HealBot_ButtonStore,b)
  HealBot_HoT_RemoveIconButton(b)
  if b.index then b.index=nil end
  HealBot_Panel_SetBarArrays(b)
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(unit)
  _,class=UnitClass(unit)
  if class ~= nil then
  	if strsub(class,1,4)=="DRUI" then
    	return 1.0,0.49,0.04
  	elseif strsub(class,1,4)=="HUNT" then
   		return 0.67,0.83,0.45
  	elseif strsub(class,1,4)=="MAGE" then
    	return 0.41,0.8,0.94
  	elseif strsub(class,1,4)=="PALA" then
    	return 0.96,0.55,0.73
  	elseif strsub(class,1,4)=="PRIE" then
    	return 1.0,1.0,1.0
  	elseif strsub(class,1,4)=="ROGU" then
    	return 1.0,0.96,0.41
  	elseif strsub(class,1,4)=="SHAM" then
    	return 0.14,0.35,1.0
  	elseif strsub(class,1,4)=="WARL" then
    	return 0.58,0.51,0.79
  	elseif strsub(class,1,4)=="DEAT" then
    	return 0.78,0.04,0.04
  	end
  end
  return 0.78,0.61,0.43  -- WARR
end

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

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

function HealBot_Action_HideTooltip(self)
  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 and not HealBot_IsFighting 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

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

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

function HealBot_Action_HealUnit_OnLeave(self)
  HealBot_Action_HideTooltip(self);
end


function HealBot_Action_OptionsButton_OnClick(self)
    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

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

local HealBot_Action_Timer1,HealBot_Action_Timer2 = -4,-4

function HealBot_Action_Set_Timers(override)
  if HealBot_Config.DisableHealBot==0 and not override and Healbot_Config_Skins.UseFluidBars[HealBot_Config.Current_Skin]==1 then
    HB_Action_Timer1 = 0.05
  else
    HB_Action_Timer1 = 2000
  end
  if HealBot_Config.DisableHealBot==0 and not override and Healbot_Config_Skins.ShowAggroBars[HealBot_Config.Current_Skin]==1 and Healbot_Config_Skins.ShowAggro[HealBot_Config.Current_Skin]==1 then
    HB_Action_Timer2 = 0.05
  else
    HB_Action_Timer2 = 2000
  end
end

local HealBot_AggroBarCol = {[0]=0.0, [1]=0.2, [2]=0.5, [3]=0.5}
local HealBot_AggroBarColr = {[0]=0.2, [1]=0.5, [2]=1, [3]=1}
local HealBot_AggroUnitThreat=1
local HealBot_HasAggro=false
function HealBot_Action_OnUpdate(self,arg1)
  HealBot_Action_Timer1 = HealBot_Action_Timer1+arg1
  HealBot_Action_Timer2 = HealBot_Action_Timer2+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_Skins.BarFreq[HealBot_Config.Current_Skin]<=HealBot_curUnitHealth[ebubar] then
		  HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
		else
		  HealBot_UnitBarUpdate[ebubar]=value-Healbot_Config_Skins.BarFreq[HealBot_Config.Current_Skin]
		end
	  elseif value+Healbot_Config_Skins.BarFreq[HealBot_Config.Current_Skin]>=HealBot_curUnitHealth[ebubar] then
		HealBot_UnitBarUpdate[ebubar]=HealBot_curUnitHealth[ebubar]
      else
		HealBot_UnitBarUpdate[ebubar]=value+Healbot_Config_Skins.BarFreq[HealBot_Config.Current_Skin]
	  end
	  ebubar:SetValue(HealBot_UnitBarUpdate[ebubar])
	  if HealBot_UnitBarUpdate[ebubar]==HealBot_curUnitHealth[ebubar] then
	    HealBot_UnitBarUpdate[ebubar]=nil
	  end
	end
  end
  if HealBot_Action_Timer2>HB_Action_Timer2 then
    HealBot_Action_Timer2 = 0
	for uName,bar4 in pairs(HealBot_AggroBar4) do
	  HealBot_AggroUnitThreat=HealBot_UnitThreat[uName] or 2
	  bar4:SetStatusBarColor(HealBot_AggroBarColr[HealBot_AggroUnitThreat],HealBot_AggroBarA-HealBot_AggroBarCol[HealBot_AggroUnitThreat],HealBot_AggroBarA-HealBot_AggroBarCol[HealBot_AggroUnitThreat],HealBot_AggroBarCol[HealBot_AggroUnitThreat]+0.5)
	end
    if HealBot_AggroBarAup then
	  if HealBot_AggroBarA>=1 then
	    HealBot_AggroBarAup=false
	    HealBot_AggroBarA=HealBot_AggroBarA-0.04
      else
	    HealBot_AggroBarA=HealBot_AggroBarA+0.04
	  end
	else
	  if HealBot_AggroBarA<=0.5 then
	    HealBot_AggroBarAup=true
	    HealBot_AggroBarA=HealBot_AggroBarA+0.04
	  else
	    HealBot_AggroBarA=HealBot_AggroBarA-0.04
	  end
	end
  end
end

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

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

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

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

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

function HealBot_Action_OnDragStop(self)
  HealBot_StopMoving(self);
  HealBot_FrameMoving=nil
end

local usedSmartCast=nil
local ModKey=nil
local abutton=nil
local aj=nil
function HealBot_Action_PreClick(self,button)
 usedSmartCast=false;
 ModKey=""
 uName=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 self.unit=="target" then
   if button=="RightButton" then
     if HealBot_UnitID[uName] then
       HealBot_Panel_ToggelHealTarget(uName)
	   Delay_RecalcParty=3
	   if HealBot_Config.ShowTooltip==1 then HealBot_Action_RefreshTargetTooltip(uName, self.unit) end
	 end
   elseif button=="LeftButton" and HealBot_Config.SmartCast==1 and not IsModifierKeyDown() then
    HealBot_Action_UseSmartCast(self,uName)
   end
 elseif IsControlKeyDown() and IsAltKeyDown() and (button=="LeftButton" or button=="MiddleButton" or button=="RightButton") then
   if button=="LeftButton" then
     if HealBot_MyTargets[uName] then
       HealBot_MyTargets[uName]=nil
     else
       HealBot_MyTargets[uName]=true
     end
     HealBot_Action_ResetUnitStatus(HealBot_UnitID[uName])
   elseif button=="RightButton" then
     HealBot_Panel_ToggelHealTarget(uName)
	 Delay_RecalcParty=3
   elseif uName~=HealBot_PlayerName then
     HealBot_Panel_AddBlackList(uName)
	 Delay_RecalcParty=3
   end
 elseif not HealBot_IsFighting then
  if HealBot_Config.UseTargetCombatStatus==1 then
    if UnitAffectingCombat(self.unit)==1 then return; end
  end
  if HealBot_Config.ProtectPvP==1 then
    if UnitIsPVP(self.unit) and not UnitIsPVP("player") then 
	  HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"nil",aj)
      usedSmartCast=true;
    end
  end
  if button=="LeftButton" and HealBot_Config.SmartCast==1 and not IsModifierKeyDown() then
    HealBot_Action_UseSmartCast(self,uName)
  end
  if not HealBot_Enabled[uName] and HealBot_Config.EnableHealthy==0 and not usedSmartCast then
    HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"Disabled",aj)
    usedSmartCast=true;
  end
 end
end

function HealBot_Action_UseSmartCast(bp,unitName)
    sName=HealBot_Action_SmartCast(bp.unit);
    if sName then
      sID=HealBot_GetSpellId(sName)
      if sID then
	    if HealBot_UnitInRange(sName, bp.unit)==1 or unitName==HealBot_PlayerName then
          bp:SetAttribute("helpbutton1", "heal1");
          bp:SetAttribute("type-heal1", "spell");
          bp:SetAttribute("spell-heal1", sName);
	    end
      else
        mId=GetMacroIndexByName(sName)
	    if mId ~= 0 then
          _,_,mText=GetMacroInfo(mId)
          mUnit = bp.unit
          mText=string.gsub(mText,"hbtarget",mUnit)
          bp:SetAttribute("type1","macro")
          bp:SetAttribute("macrotext1", mText)
        else
	      bp:SetAttribute("helpbutton1", "item1");
          bp:SetAttribute("type-item1", "item");
          bp:SetAttribute("item-item1", sName);
        end
      end
	  usedSmartCast=true;
	end
end

function HealBot_Action_PostClick(self,button)
  if usedSmartCast then
    if self.unit=="target" then
      if aj==1 then
        self:SetAttribute(HB_prefix.."helpbutton"..aj, "target"..aj);
		self:SetAttribute(HB_prefix.."type"..aj, "target")
        self:SetAttribute(HB_prefix.."type-target"..aj, "target")
	  end
	else
	  HealBot_Action_SetButtonAttrib(self,abutton,ModKey,"Enabled",aj)
    end
  end
end

function HealBot_Action_RetMyTarget(unitName)
  return HealBot_MyTargets[unitName]
end

function HealBot_Action_SmartCast(unit)
  uName=UnitName(unit);
  sName=nil
  rangeSpell=HealBot_hSpell
  if HealBot_PlayerDead or not uName then return nil; end
  
  if HealBot_Config.SmartCastRes==1 and UnitIsDead(unit) and not UnitIsGhost(unit) then
   sName=HealBot_Init_retSmartCast_Res();
   rangeSpell=HealBot_rSpell
  elseif HealBot_UnitDebuff[uName] and HealBot_Config.SmartCastDebuff==1 then
    sName=HealBot_DebuffSpell[HealBot_UnitDebuff[uName]];
	rangeSpell=HealBot_dSpell
  elseif HealBot_UnitBuff[uName] and HealBot_Config.SmartCastBuff==1 then
    sName=HealBot_UnitBuff[uName];
	rangeSpell=HealBot_bSpell
  elseif HealBot_Config.SmartCastHeal==1 then
	uHealIn = HealBot_RetHealsIn(uName);
    uHlth, uMaxHlth = HealBot_UnitHealth(unit, uName);
    x = uMaxHlth-(uHlth+uHealIn);
    if x>200 then
      sName=HealBot_SmartCast(unit,x)
    end
  end
  if sName and uName~=HealBot_PlayerName then
    if HealBot_UnitInRange(rangeSpell, unit)~=1 then return nil; end
  end
  return sName;
end

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

function HealBot_Action_ClearHealsIn(unitName)
  if unitName=="target" then
    HealBot_Unit_Button["target"]=nil
	if not HealBot_UnitID[HealBot_UnitName["target"]] then
	  unitName=HealBot_UnitName["target"]
	end
	HealBot_UnitName["target"]=nil
  end
  if HealBot_ClassColR[unitName] then
    HealBot_Enabled[unitName]=nil
    HealBot_ClassColR[unitName]=nil
    HealBot_ClassColG[unitName]=nil
    HealBot_ClassColB[unitName]=nil
    HealBot_UnitBuff[unitName]=nil
    HealBot_UnitDebuff[unitName]=nil
	HealBot_UnitSpec[unitName]=nil
	if HealBot_UnitOffline[unitName] then HealBot_UnitOffline[unitName]=nil end
    if HealBot_AggroBar4[unitName] then
      HealBot_AggroBar4[unitName]=nil
    end
    if HealBot_Aggro[unitName] then
      HealBot_Aggro[unitName]=nil
    end
    if HealBot_UnitID[unitName] then
      if HealBot_Unit_Button[HealBot_UnitID[unitName]] then
	    if not UnitName(HealBot_UnitID[unitName]) then
	      HealBot_Unit_Button[HealBot_UnitID[unitName]]=nil
	    end
  	  end
      HealBot_UnitID[unitName]=nil
    end
	HealBot_ClearLocalArr(unitName)
	HealBot_ClearAllBuffs(unitName)
	HealBot_ClearAllDebuffs(unitName)
  end
end
