--[[

  HealBot Contined
	
]]

local HealBot_CalcEquipBonus=false;
local HealBot_HasDivineSpirit=2;
local HealBot_CheckForDivineSpirit=false;
local HealBot_NeedEquipUpdate=0;
local HealBot_CastingSpell  = nil;
local HealBot_HealValue = 0;
local HealBot_IamRessing = nil;
local HealBot_RequestVer=nil;
local HealBot_IamHealing = nil;
local HealBot_Loaded=nil;
local HealBot_InnerFocusTalent=nil;
local HealBot_Talents={}
local HealBot_SpellPowerHealAdj=1.88
local HealBot_CheckTalents=false
local HealBot_Player_HoT={};
local HealBot_Player_HoT_Icons={};
local HealBot_Watch_HoT={};
local HealBot_Watch_Timed_HoT={};
local HealBot_Watch_AM_HoT=0
local HealBot_HoT_Mending=0;
local HealBot_HoT_MendingWith="niL";
local HealBot_Lifebloom_Count={};
local HealBot_Earth_Shield_Count={};
local HealBot_Water_Shield_Count={};
local HealBot_CheckZoneFlag=false
local HealBot_InCombatUpdate=false
local HealBot_SmartCast_Spells={};
local HealBot_AddonMsgType=3;
local HealBot_InBG=nil;
local HealBot_HealingBonus=0;
local HealBot_HoT_Texture={};
local HealBot_SpamCnt=0;
local HealBot_ErrorCnt=0;
local HealBot_Healers = {};
local HealBot_TabHealers = {};
local HealBot_HealsIn = {};
local HealBot_CurrentSpells = {};
local HealBot_Ressing = {};
local HealBot_DelayBuffCheck = {};
local HealBot_DelayDebuffCheck = {};
local HealBot_DelayAuraCheck = {};
local HealBot_HealsIncCnt={}
local HealBot_Vers={}
local HealBot_MainTanks={};
local HealBot_CastingTarget = nil;
local i=nil
local k=nil
local s=nil
local w=nil
local x=nil
local y=nil
local z=nil
local target=8
local strfind=strfind
local strsub=strsub
local strlen=strlen
local gsub=gsub
local format=format
local tonumber=tonumber
local strlen=strlen
local HealBot_ThrottleCnt=5
local HealBot_Aggro={}
local HealBot_Reset_flag=nil
local HB_Timer1=0.02
local HB_Timer2=200
local HealBot_UpUnitInCombat={}
local HealBot_InCombatUpdCnt=0
local b=nil
local HealBot_Comms_Blacklist={}
local HealBot_Buff_Spells_List ={}
local HealBot_BuffNameSwap = {}			
local HealBot_IncomingHealEnd={};		   

local HealBot_Ignore_Class_Debuffs = {
    ["WARR"] = { [HEALBOT_DEBUFF_ANCIENT_HYSTERIA] = true,	
                          [HEALBOT_DEBUFF_IGNITE_MANA] = true, 
                          [HEALBOT_DEBUFF_TAINTED_MIND] = true, 
                          [HEALBOT_DEBUFF_VIPER_STING] = true,
                          [HEALBOT_DEBUFF_IMPOTENCE] = true,
                          [HEALBOT_DEBUFF_DECAYED_INT] = true,
                          [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["ROGU"] = {   [HEALBOT_DEBUFF_SILENCE] = true,	
                          [HEALBOT_DEBUFF_ANCIENT_HYSTERIA] = true, 
                          [HEALBOT_DEBUFF_IGNITE_MANA] = true, 
                          [HEALBOT_DEBUFF_TAINTED_MIND] = true, 
                          [HEALBOT_DEBUFF_VIPER_STING] = true,
                          [HEALBOT_DEBUFF_IMPOTENCE] = true,
                          [HEALBOT_DEBUFF_DECAYED_INT] = true,
                          [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["HUNT"] = {  [HEALBOT_DEBUFF_MAGMA_SHACKLES] = true, 
                          [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["MAGE"] = {    [HEALBOT_DEBUFF_MAGMA_SHACKLES] = true, 
                          [HEALBOT_DEBUFF_DECAYED_STR] = true,
                          [HEALBOT_DEBUFF_CRIPPLE] = true,
                          [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["DRUI"] = {   [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["PALA"] = { [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["PRIE"] = {  [HEALBOT_DEBUFF_DECAYED_STR] = true,
                          [HEALBOT_DEBUFF_CRIPPLE] = true,
                          [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["SHAM"] = {  [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["WARL"] = { [HEALBOT_DEBUFF_DECAYED_STR] = true,
                          [HEALBOT_DEBUFF_CRIPPLE] = true,
                          [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
    ["DEAT"] = { [HEALBOT_DEBUFF_UNSTABLE_AFFL] = true,},
};

local HealBot_Ignore_Movement_Debuffs = {
                                  [HEALBOT_DEBUFF_FROSTBOLT] = true,
                                  [HEALBOT_DEBUFF_MAGMA_SHACKLES] = true,
                                  [HEALBOT_DEBUFF_SLOW] = true,
                                  [HEALBOT_DEBUFF_CHILLED] = true,
                                  [HEALBOT_DEBUFF_CONEOFCOLD] = true,
                                  [HEALBOT_DEBUFF_CONCUSSIVESHOT] = true,
                                  [HEALBOT_DEBUFF_THUNDERCLAP] = true,
                                  [HEALBOT_DEBUFF_HOWLINGSCREECH] = true,
                                  [HEALBOT_DEBUFF_DAZED] = true,
								  [HEALBOT_DEBUFF_FROST_SHOCK] = true,
};

local HealBot_Ignore_NonHarmful_Debuffs = {
                                  [HEALBOT_DEBUFF_HUNTERS_MARK] = true,
                                  [HEALBOT_DEBUFF_ARCANE_BLAST] = true,
                                  [HEALBOT_DEBUFF_MAJOR_DREAMLESS] = true,
                                  [HEALBOT_DEBUFF_GREATER_DREAMLESS] = true,
                                  [HEALBOT_DEBUFF_DREAMLESS_SLEEP] = true,
};

local healingbonus_penalty=nil
local HealBot_Timer1,HealBot_Timer2,HealBot_Timer3 = -1.0,-2.0,0.48;
local HealBot_StopCastTimer=0
local Ti=nil
local inc_msg=nil
local datatype=nil
local datamsg=nil
local sender=nil
local arr=nil
local arrg = {}
local HoTActive=nil
local DebuffType=nil
local checkthis=nil
local WatchTarget=nil
local debuff_type=nil
local dName=nil
local HealBot_Ignore_Debuffs_Class=nil
local bName=nil
local PlayerBuffs = {}
local heal_val=nil
local addIcon=true
local xButton=nil
local bar=nil
local iconName=nil
local id=0
local sName=nil
local sRank=nil
local sNameRank=nil
local uName=nil
local xUnit=nil
local uClass=nil
local HealBot_xLocal=nil
local QuickHealth = nil
local HealBot_PlayerBuff={}
local HealBot_CheckBuffs = {}
local HealBot_ShortBuffs = {}
local HealBot_CheckBuffsTime=nil
local HealBot_CheckBuffsTimeuName=nil
local HealBot_QueueCheckBuffs={}
local PlayerBuffsUName=nil
local huUnit,huHoTtime,huHoTicon=nil,nil,nil
local HealBot_TrackWS={}
local HEALBOT_ADDON_ID="HealBot_Heals"
local HealBot_BuffWatch={}
local HealBot_DeBuff_Texture={}
local iconTexture=nil
local DeBuff_Count={}
local HealBot_unitHealth={}
local HealBot_unitHealthMax={}
local LSM = LibStub("LibSharedMedia-3.0")
local BuffClass=nil
local HasWeaponBuff=false
local CheckWeaponBuffs={[HEALBOT_ROCKBITER_WEAPON]=true, 
                        [HEALBOT_FLAMETONGUE_WEAPON]=true, 
					    [HEALBOT_EARTHLIVING_WEAPON]=true, 
						[HEALBOT_WINDFURY_WEAPON]=true,
                        [HEALBOT_FROSTBRAND_WEAPON]=true,}
local debuffCodes={ [HEALBOT_DISEASE_en]=5, [HEALBOT_MAGIC_en]=6, [HEALBOT_POISON_en]=7, [HEALBOT_CURSE_en]=8, [HEALBOT_CUSTOM_en]=9}
local HealBot_WeaponBuffCheck=nil

function HealBot_Check_WeaponBuffs(spellName)
  if CheckWeaponBuffs[spellName] then
    HealBot_WeaponBuffCheck=3
  end
end

function HealBot_Clear_BuffWatch()
  for x,_ in pairs(HealBot_BuffWatch) do
    HealBot_BuffWatch[x]=nil;
  end
end

function HealBot_Set_BuffWatch(buffName)
  table.insert(HealBot_BuffWatch,buffName);
end

function HealBot_Clear_CheckBuffs()
  for x,_ in pairs(HealBot_CheckBuffs) do
    HealBot_CheckBuffs[x]=nil;
  end
end

function HealBot_Set_CheckBuffs(buffName)
  if buffName==HEALBOT_FEAR_WARD then return end
  if not CheckWeaponBuffs[buffName] then
    HealBot_CheckBuffs[buffName]=buffName;
  end
end

function HealBot_Queue_MyBuffsCheck(unitName,unit)
  HealBot_QueueCheckBuffs[unitName]=unit
end

function HealBot_Queue_AllActiveMyBuffs()
  for z,y in pairs(HealBot_UnitID) do
    HealBot_Queue_MyBuffsCheck(z,y)
  end
end

function HealBot_RetMyBuffTime(unitName,buffName)
  if not HealBot_PlayerBuff[unitName] or not HealBot_PlayerBuff[unitName][buffName] then return end
  if HealBot_ShortBuffs[buffName] then
    return HealBot_PlayerBuff[unitName][buffName]+HealBot_Config.ShortBuffTimer
  else
    return HealBot_PlayerBuff[unitName][buffName]+HealBot_Config.LongBuffTimer
  end
end

function HealBot_retInnerFocus()
  return HealBot_Talents["InnerFocus"]
end

function HealBot_retAddonID()
  return HEALBOT_ADDON_ID
end

function HealBot_retHealBot_MainTanks(id)
  return HealBot_MainTanks[id]
end

function HealBot_SetResetFlag(mode)
  if mode=="HARD" and not HealBot_InCombat then
    ReloadUI()
  else
    HealBot_Reset_flag=true
  end
end

function HealBot_AltBuffNames(buffName)
  return HealBot_BuffNameSwap[buffName]
end

function HealBot_GetHealBot_AddonMsgType()
  return HealBot_AddonMsgType;
end

function HealBot_RetHealBot_Ressing(unitName)
  return HealBot_Ressing[unitName];
end

function HealBot_RetNumCurrentSpells()
  return table.getn(HealBot_CurrentSpells)
end

function HealBot_UnsetHealBot_Ressing(unitName)
  HealBot_Ressing[unitName]=nil;
end

function HealBot_TooltipInit()
  if ( HealBot_ScanTooltip:IsOwned(HealBot) ) then return; end;
  HealBot_ScanTooltip:SetOwner(HealBot, 'ANCHOR_NONE' );
  HealBot_ScanTooltip:ClearLines();
end

function HealBot_AddChat(HBmsg)
  x=HealBot_Comms_GetChan("HBmsg");
  if x and HealBot_SpamCnt < 17 then
    HealBot_SpamCnt=HealBot_SpamCnt+1;
    w,y = GetGameTime();
	if y==0 then
      HBmsg="["..w..":00] "..HBmsg;
    elseif y<10 then
      HBmsg="["..w..":0"..y.."] "..HBmsg; 
    else
      HBmsg="["..w..":"..y.."] "..HBmsg; 
    end
    SendChatMessage(HBmsg , "CHANNEL", nil, x); 
  elseif ( DEFAULT_CHAT_FRAME ) then
    DEFAULT_CHAT_FRAME:AddMessage(HBmsg, 0.5, 0.5, 1.0);
  end
end

function HealBot_AddDebug(HBmsg)
  x=HealBot_Comms_GetChan("HBmsg");
  if x and HealBot_SpamCnt < 17 then
    HealBot_SpamCnt=HealBot_SpamCnt+1;
    w,y = GetGameTime();
	if y==0 then
      HBmsg="["..w..":00] DEBUG: "..HBmsg;
    elseif y<10 then
      HBmsg="["..w..":0"..y.."] DEBUG: "..HBmsg; 
    else
      HBmsg="["..w..":"..y.."] DEBUG: "..HBmsg; 
    end
    SendChatMessage(HBmsg , "CHANNEL", nil, x);
  end
end

function HealBot_Report_Error(HBmsg)
  if HealBot_ErrorCnt<28 then
    HealBot_ErrorCnt=HealBot_ErrorCnt+1;
    HealBot_ErrorsIn(HBmsg,HealBot_ErrorCnt);
  end
end

function HealBot_ErrorCntReset()
  HealBot_ErrorCnt=0;
end

function HealBot_AddError(HBmsg)
  UIErrorsFrame:AddMessage(HBmsg, 1.0, 1.0, 1.0, 1.0, UIERRORS_HOLD_TIME);
  HealBot_AddDebug(HBmsg);
end

function HealBot_TogglePanel(HBpanel)
  if not HBpanel then return end
  if ( HBpanel:IsVisible() ) then
    HideUIPanel(HBpanel);
  else
    ShowUIPanel(HBpanel);
  end
end

function HealBot_StartMoving(HBframe)
  if ( not HBframe.isMoving ) and ( HBframe.isLocked ~= 1 ) then
    HBframe:StartMoving();
    HBframe.isMoving = true;
  end
end

function HealBot_StopMoving(HBframe)
  if ( HBframe.isMoving ) then
    HBframe:StopMovingOrSizing();
    HBframe.isMoving = false;
  end
  if HealBot_Config.Panel_Anchor==1 then
    HealBot_Config.PanelAnchorY = HealBot_Action:GetTop();
	HealBot_Config.PanelAnchorX = HealBot_Action:GetLeft()
  elseif HealBot_Config.Panel_Anchor==2 then
    HealBot_Config.PanelAnchorY = HealBot_Action:GetBottom();
	HealBot_Config.PanelAnchorX = HealBot_Action:GetLeft()
  elseif HealBot_Config.Panel_Anchor==3 then
    HealBot_Config.PanelAnchorY = HealBot_Action:GetTop();
	HealBot_Config.PanelAnchorX = HealBot_Action:GetRight()
  elseif HealBot_Config.Panel_Anchor==4 then
    HealBot_Config.PanelAnchorY = HealBot_Action:GetBottom();
	HealBot_Config.PanelAnchorX = HealBot_Action:GetRight()
  end
  HealBot_CheckFrame()
end

function HealBot_CheckFrame()
  if HealBot_Config.Panel_Anchor==1 then
    if HealBot_Config.PanelAnchorX<-9 then 
	  HealBot_Config.PanelAnchorX=-9
	elseif HealBot_Config.PanelAnchorX>(GetScreenWidth()-(Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7)) then
	  HealBot_Config.PanelAnchorX=GetScreenWidth()-(Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7)
	end
	if HealBot_Config.PanelAnchorY<11+Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin] then
      HealBot_Config.PanelAnchorY=11+Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin] 
	elseif HealBot_Config.PanelAnchorY>GetScreenHeight()+12 then
	  HealBot_Config.PanelAnchorY=GetScreenHeight()+12
	end
  elseif HealBot_Config.Panel_Anchor==2 then
    if HealBot_Config.PanelAnchorX<-9 then 
	  HealBot_Config.PanelAnchorX=-9
	elseif HealBot_Config.PanelAnchorX>(GetScreenWidth()-(Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7)) then
	  HealBot_Config.PanelAnchorX=GetScreenWidth()-(Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7)
	end
	if HealBot_Config.PanelAnchorY<-15 then
      HealBot_Config.PanelAnchorY=-15
	elseif HealBot_Config.PanelAnchorY>GetScreenHeight()-15-Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin] then
	  HealBot_Config.PanelAnchorY=GetScreenHeight()-15-Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin]
	end
  elseif HealBot_Config.Panel_Anchor==3 then
    if HealBot_Config.PanelAnchorX<Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7 then 
	  HealBot_Config.PanelAnchorX=Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7
	elseif HealBot_Config.PanelAnchorX>(GetScreenWidth()+8) then
	  HealBot_Config.PanelAnchorX=GetScreenWidth()+8
	end
	if HealBot_Config.PanelAnchorY<11+Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin] then
      HealBot_Config.PanelAnchorY=11+Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin] 
	elseif HealBot_Config.PanelAnchorY>GetScreenHeight()+12 then
	  HealBot_Config.PanelAnchorY=GetScreenHeight()+12
	end
  elseif HealBot_Config.Panel_Anchor==4 then
    if HealBot_Config.PanelAnchorX<Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7 then 
	  HealBot_Config.PanelAnchorX=Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Current_Skin]+7
	elseif HealBot_Config.PanelAnchorX>(GetScreenWidth()+8) then
	  HealBot_Config.PanelAnchorX=GetScreenWidth()+8
	end
	if HealBot_Config.PanelAnchorY<-15 then
      HealBot_Config.PanelAnchorY=-15
	elseif HealBot_Config.PanelAnchorY>GetScreenHeight()-15-Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin] then
	  HealBot_Config.PanelAnchorY=GetScreenHeight()-15-Healbot_Config_Skins.bheight[Healbot_Config_Skins.Current_Skin]
	end
  end
  Delay_RecalcParty=3
end

function HealBot_SlashCmd(HBcmd)
  if (HBcmd=="") then
    HealBot_TogglePanel(HealBot_Action);
  elseif (HBcmd=="o" or HBcmd=="options" or HBcmd=="opt" or HBcmd=="config" or HBcmd=="cfg") then
    HealBot_TogglePanel(HealBot_Options);
  elseif HBcmd=="defaults" then
    HealBot_Options_Defaults_OnClick(HealBot_Options_Defaults);
  elseif (HBcmd=="ui") then
    HealBot_SetResetFlag("HARD")
  elseif (HBcmd=="ri" or HBcmd=="reset" or HBcmd=="x") then
    HealBot_SetResetFlag("SOFT")
  elseif (HBcmd=="info" or HBcmd=="ver") then
    HealBot_Comms_Info()
  elseif (HBcmd=="status") then
    HealBot_Comms_DebugStatus();
  elseif (HBcmd=="show") then
    HealBot_Action_Reset()
  elseif (HBcmd=="fast") then
    if HealBot_Config.FastAttrib==0 then
	  HealBot_Config.FastAttrib=1
	  HealBot_AddChat("[HealBot]Fast attrib setting is ON")
	else
	  HealBot_Config.FastAttrib=0
	  HealBot_AddChat("[HealBot]Fast attrib setting is OFF")
	end
  elseif (HBcmd=="cb") then
    HealBot_Panel_ClearBlackList()
	Delay_RecalcParty=3
  elseif (HBcmd=="disable") then
    HealBot_Options_DisableHealBot:SetChecked(1)
    HealBot_Options_ToggleHealBot(1)
  elseif (HBcmd=="enable") then
    HealBot_Options_DisableHealBot:SetChecked(0)
    HealBot_Options_ToggleHealBot(0)
  elseif (HBcmd=="tinfo") then
    if UnitName("target") then
      HealBot_Comms_TargetInfo()
	else
	  HealBot_AddDebug( "No Target" );
	end
  elseif (HBcmd=="test") then
    HealBot_Action_SetUnitDebuffStatus(UnitName("player"),debuffCodes[HEALBOT_MAGIC_en])
	HealBot_Action_UpdateAggro("player","debuff",6)
  elseif (HBcmd=="testoff") then
    HealBot_Action_SetUnitDebuffStatus(UnitName("player"))
	if not HealBot_Aggro["player"] then HealBot_Aggro["player"]="player" end
  else
    HealBot_AddChat("[HealBot]Unknown slash command: /hb "..HBcmd)
  end
end

function HealBot_Reset()
	HB_Timer2=200
    HealBot_UnRegister_Events()
	HealBot_Panel_ClearBlackList()
	HealBot_Panel_ClearHealTarget()
	HealBot_Action_SetAllBars()
	HealBot_Action_SetAllAttribs()
    HealBot_Loaded=1
    HealBot_OnEvent_PlayerEnteringWorld(nil);
end

function HealBot_GetSpellName(id)
  if (not id) then return nil end
  sName, sRank = GetSpellName(id,BOOKTYPE_SPELL);
  if (not sName) then
    return nil;
  end
  if (not sRank or sRank=="") then
    return sName;
  end
  return sName, sRank;
end

function HealBot_GetSpellId(spellName)
  if (not spellName) then return nil end
  x,y = 1,0;
  if HealBot_Spells[spellName] then   
    if HealBot_Spells[spellName].x then return HealBot_Spells[spellName].x; end
  end
  while true do 
    sName, sRank = GetSpellName(x,BOOKTYPE_SPELL);
    if (sName) then
      if (spellName == sName .. "(" .. sRank .. ")") then
       return x;
      end
      if (spellName == sName) then
        y=x;
      end   
    else
      do break end
    end
    x = x + 1;
  end
  if y>0 then
    return y
  else
    return nil;
  end
end

function HealBot_ClearIncHeal()
    if HealBot_HealValue > 0 then
	  HealBot_OnEvent_AddonMsg(nil,HEALBOT_ADDON_ID, ">> "..0-HealBot_HealValue.." <<=>> "..HealBot_IamHealing, nil, HealBot_PlayerName, true)
      HealBot_Comms_SendAddonMsg(HEALBOT_ADDON_ID, ">> "..0-HealBot_HealValue.." <<=>> "..HealBot_IamHealing, HealBot_AddonMsgType, HealBot_PlayerName)
	  HealBot_IamHealing=nil
      HealBot_HealValue=0;
    end
end

function HealBot_StopCasting()
  HealBot_ClearIncHeal();
  HealBot_CastingSpell  = nil;
  HealBot_CastingTarget = nil;
end

function HealBot_UnitHealth(unit, unitName)
  if HealBot_unitHealth[unitName] then
    x=HealBot_unitHealth[unitName]
	y=HealBot_unitHealthMax[unitName]
  else
    x=UnitHealth(unit)
	y=UnitHealthMax(unit)
  end
  if unit=='target' and y==100 then
    if UnitLevel(unit)>0 then
      y = math.floor(10000/70*UnitLevel(unit)+0.5)
    else
      y = UnitHealthMax('player');
    end
    x = y/100*x;
  end
  return x,y;
end

local talenty,talentx,talentz=0,0,0
function HealBot_FindHealSpells()
  
  HealBot_HealingBonus=GetSpellBonusHealing()*HealBot_SpellPowerHealAdj;
  if HealBot_CalcEquipBonus then
    if strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PRIEST] then 
	  talentx=1+HealBot_Talents["SpiritalHealing"]+HealBot_Talents["FocusedPower"]
	elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_DRUID] then 
	  talentx=1+HealBot_Talents["GiftOfNature"]
	elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_SHAMAN] then 
	  talentx=1+HealBot_Talents["Purification"]
	else
	  talentx=1
	end
  end
  
  table.foreach(HealBot_Spells, function (spell,_)
      if HealBot_CalcEquipBonus then
        if HealBot_Spells[spell].NoBonus then
          HealBot_Spells[spell].RealHealing = 0
        else
		  talenty=talentx
		  talentz=HealBot_HealingBonus
		  if strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_DRUID] then
		    y=HealBot_Talents["NurturingInstinct"]
		  elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_SHAMAN] then
		    y=HealBot_Talents["NatureBlessing"]
		  else
		    y=0
		  end
          healingbonus_penalty=1;
          if HealBot_Spells[spell].Level < 20 then
            healingbonus_penalty=(1-((20-HealBot_Spells[spell].Level)*0.0375));
          end
		  if strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_DRUID] and HealBot_Spells[spell].HealsExt>0 then
		    y=y+(GetSpellBonusHealing()*HealBot_Talents["EmpoweredRejuvenation"])
		  end
          if HealBot_Spells[spell].Cast == 0 then
		    if strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PRIEST] then 
			  talenty=talenty+HealBot_Talents["TwinDisciplines"]
              if strfind(spell,HEALBOT_RENEW,1) then
			    talenty=talenty+HealBot_Talents["ImprovedRenew"]
			  end
			elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_DRUID] then
			  talenty=talenty+HealBot_Talents["Genesis"]
              if strfind(spell,HEALBOT_REJUVENATION,1) then
			    talenty=talenty+HealBot_Talents["ImprovedRejuvenation"]
			  end
			end
            w=3.5;
          else
		    if strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PRIEST] then 
			  if strfind(spell,HEALBOT_GREATER_HEAL,1) then
		        y=y+(GetSpellBonusHealing()*(HealBot_Talents["EmpoweredPower"]*2))
			  elseif strfind(spell,HEALBOT_FLASH_HEAL,1) then
			    y=y+(GetSpellBonusHealing()*HealBot_Talents["EmpoweredPower"])
			  elseif strfind(spell,HEALBOT_BINDING_HEAL,1) then
			    talenty=talenty+HealBot_Talents["DivineProvidence"]
                y=y+(GetSpellBonusHealing()*HealBot_Talents["EmpoweredPower"])
			  elseif strfind(spell,HEALBOT_PRAYER_OF_HEALING,1) then
			    talentz=GetSpellBonusHealing()
			    talenty=talenty+HealBot_Talents["DivineProvidence"]
			  end
			elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_DRUID] then
			  if strfind(spell,HEALBOT_HEALING_TOUCH,1) then
			    y=y+(GetSpellBonusHealing()*HealBot_Talents["EmpoweredTouch"])
			  end
			elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PALADIN] then 
			  talenty=talenty+HealBot_Talents["HealingLight"]
			elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_SHAMAN] then
  		      if strfind(spell,HEALBOT_CHAIN_HEAL,1) then			
			    talenty=talenty+HealBot_Talents["ImprovedChainHeal"]
			  end
			end
		    if HealBot_Spells[spell].Cast >= 1.5 and HealBot_Spells[spell].Cast < 3.5 then
              w=HealBot_Spells[spell].Cast;
		    else
		      w=3.5
            end
		  end
          x = ((talentz * healingbonus_penalty) * (w/3.5));
          z = ((HealBot_Spells[spell].Level)+6)/(UnitLevel("player"))
          if z<1 then x=x*z; end
		  x=x+y
		  if talenty>1 then
            x=x*talenty
		    x=x+(floor((HealBot_Spells[spell].HealsCast+HealBot_Spells[spell].HealsExt)*(talenty-1)))
		  end
          HealBot_Spells[spell].RealHealing = floor(x);
        end
      end
      HealBot_Spells[spell].HealsDur = floor((HealBot_Spells[spell].HealsCast+HealBot_Spells[spell].HealsExt) + HealBot_Spells[spell].RealHealing);
  end);

  HealBot_CalcEquipBonus=false;
end

function HealBot_RecalcHeals(unit)
  HealBot_Action_Refresh(unit);
end

function HealBot_RecalcParty(HealBot_PreCombat)
  HealBot_Action_PartyChanged(HealBot_PreCombat);
end

function HealBot_RecalcSpells()
  HealBot_FindHealSpells();
end

function HealBot_OnLoad(self)
    HealBot_PlayerClass, HealBot_PlayerClassEN=UnitClass("player")
    HealBot_PlayerRace, HealBot_PlayerRaceEN=UnitRace("player")
    HealBot_PlayerName=UnitName("player")
    self:RegisterEvent("VARIABLES_LOADED");
    SLASH_HEALBOT1 = "/healbot";
    SLASH_HEALBOT2 = "/hb";
    SlashCmdList["HEALBOT"] = function(msg)
      HealBot_SlashCmd(msg);
    end
end

local ouName=nil
local aSwitch=0

function HealBot_Set_Timers()
  if HealBot_Config.DisableHealBot==0 then
    HB_Timer1=1
	HB_Timer2=HealBot_Config.RangeCheckFreq
  else
    HB_Timer1=5000
	HB_Timer2=5000
  end
end

local HealBot_ErrorNum=0
function HealBot_OnUpdate(self,arg1)
  if HealBot_Config.DisableHealBot==1 then return end
  HealBot_Timer1 = HealBot_Timer1+arg1;
  HealBot_Timer2 = HealBot_Timer2+arg1;
  HealBot_Timer3 = HealBot_Timer3+arg1;
  if HealBot_Timer1>=HB_Timer1 then
    HealBot_Timer1 = 0;
	if HealBot_Watch_AM_HoT>0 then
	  HealBot_Watch_AM_HoT=HealBot_Watch_AM_HoT-1
	end
    for uName,_ in pairs(HealBot_Watch_Timed_HoT) do
      HealBot_Watch_Timed_HoT[uName] = HealBot_Watch_Timed_HoT[uName]-1;
	  if HealBot_Watch_Timed_HoT[uName]<0 then HealBot_Watch_Timed_HoT[uName]=nil end
	end
    if not HealBot_IsFighting then
      if HealBot_Reset_flag then
		HealBot_Reset_flag=nil
        HealBot_Reset()
      elseif Delay_RecalcParty>0 and not HealBot_InCombatUpdate then
        Delay_RecalcParty=Delay_RecalcParty+1
        if Delay_RecalcParty>3 then
          Delay_RecalcParty=0;
		  HealBot_Timer2=0-HB_Timer2
          HealBot_RecalcParty();
		  HealBot_InCombatUpdCnt=0
        end
      elseif HealBot_CheckZoneFlag then
          HealBot_CheckZoneFlag=false; 
          HealBot_CheckZone();
      elseif HealBot_NeedEquipUpdate then
          HealBot_CalcEquipBonus=true;
          HealBot_NeedEquipUpdate=false
          HealBot_RecalcSpells();
		  if strsub(HealBot_PlayerClassEN,1,4)=="SHAM" then
		    HealBot_CheckAllBuffs(HealBot_PlayerName)
		  end
      elseif HealBot_CheckTalents then
          HealBot_CheckTalents=false; 
          HealBot_NeedEquipUpdate=true
          if strsub(HealBot_PlayerClassEN,1,4)=="PRIE" then 
            i=HealBot_HasTalent(1,8,HEALBOT_INNER_FOCUS); 
            if i>0 then 
	          HealBot_InnerFocusTalent=true
	        else
	          HealBot_InnerFocusTalent=nil
	        end
            HealBot_Talents["ImprovedRenew"]=(5*HealBot_HasTalent(2,2,HEALBOT_IMPROVED_RENEW))/100
	        HealBot_Talents["SpiritalHealing"]=(2*HealBot_HasTalent(2,16,HEALBOT_SPIRITAL_HEALING))/100
	        HealBot_Talents["TwinDisciplines"]=HealBot_HasTalent(1,2,HEALBOT_TWIN_DISCIPLINES)/100
			HealBot_Talents["FocusedPower"]=(2*HealBot_HasTalent(1,16,HEALBOT_FOCUSED_POWER))/100
			HealBot_Talents["EmpoweredPower"]=(4*HealBot_HasTalent(2,20,HEALBOT_EMPOWERED_HEALING))/100
			HealBot_Talents["DivineProvidence"]=(2*HealBot_HasTalent(2,25,HEALBOT_DIVINE_PROVIDENCE))/100
	      elseif strsub(HealBot_PlayerClassEN,1,4)=="DRUI" then 
            HealBot_Talents["Genesis"]=HealBot_HasTalent(1,2,HEALBOT_GENESIS)/100
			_,i=UnitStat("player", 2)
			HealBot_Talents["NurturingInstinct"]=i*(0+(35*HealBot_HasTalent(2,15,HEALBOT_NURTURING_INSTINCT)/100))
			HealBot_Talents["ImprovedRejuvenation"]=(5*HealBot_HasTalent(3,11,HEALBOT_IMPROVED_REJUVENATION))/100
			HealBot_Talents["GiftOfNature"]=(2*HealBot_HasTalent(3,13,HEALBOT_GIFT_OF_NATURE))/100
			HealBot_Talents["EmpoweredTouch"]=(20*HealBot_HasTalent(3,15,HEALBOT_EMPOWERED_TOUCH))/100
			HealBot_Talents["EmpoweredRejuvenation"]=(4*HealBot_HasTalent(3,20,HEALBOT_EMPOWERED_REJUVENATION))/100
		  elseif strsub(HealBot_PlayerClassEN,1,4)=="SHAM" then 
            HealBot_Talents["Purification"]=(2*HealBot_HasTalent(3,15,HEALBOT_PURIFICATION))/100
			HealBot_Talents["ImprovedChainHeal"]=(10*HealBot_HasTalent(3,20,HEALBOT_IMPROVED_CHAIN_HEAL))/100
			_,i=UnitStat("player", 4)
			HealBot_Talents["NatureBlessing"]=i*(0+(5*HealBot_HasTalent(3,21,HEALBOT_NATURES_BLESSING)/100))
          elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PALADIN] then 
		    HealBot_Talents["HealingLight"]=(4*HealBot_HasTalent(1,3,HEALBOT_HEALING_LIGHT))/100
          end	    
      elseif HealBot_CheckForDivineSpirit then
        HealBot_CheckForDivineSpirit=false
        x=0;
        Ti=1;
        while true do
          bName,_,_,_,_,_ = UnitBuff("player",Ti) 
          if bName then
            if bName==HEALBOT_DIVINE_SPIRIT or bName==HEALBOT_PRAYER_OF_SPIRIT then
              x=1;
              do break end;
            end
            Ti = Ti + 1;
          else
            do break end
          end
        end
        if x~=HealBot_HasDivineSpirit then
          HealBot_HasDivineSpirit=x;
		  HealBot_RecalcSpells()
		end
	  elseif HealBot_CheckBuffsTimeuName and HealBot_CheckBuffsTime<GetTime() and not HealBot_UnitBuff[HealBot_CheckBuffsTimeuName] then
	    if HealBot_UnitSpec[HealBot_CheckBuffsTimeuName] then
          PlayerBuffsUName=HealBot_PlayerBuff[HealBot_CheckBuffsTimeuName]
		  Ti=0
		  if PlayerBuffsUName then
	        for bName,_ in pairs (PlayerBuffsUName) do
	          if PlayerBuffsUName[bName] < GetTime() then
                xUnit=HealBot_UnitID[HealBot_CheckBuffsTimeuName]
                HealBot_CheckUnitBuffs(xUnit)
		        Ti=1
     		    do break end
              end
		    end
		  end
		  if Ti==0 then
		    HealBot_CheckMyBuffs(HealBot_CheckBuffsTimeuName)
            HealBot_ResetCheckBuffsTime()  
		  end
		else
		  HealBot_PlayerBuff[HealBot_CheckBuffsTimeuName]=nil
		  HealBot_ResetCheckBuffsTime()
		end
      elseif HealBot_WeaponBuffCheck then
	    HealBot_WeaponBuffCheck=HealBot_WeaponBuffCheck-1
		if HealBot_WeaponBuffCheck<=0 then HealBot_WeaponBuffCheck=nil end
		if HealBot_UnitBuff[HealBot_PlayerName] then HealBot_CheckUnitBuffs("player") end
	  end
      if HealBot_Config.TooltipUpdate==1 and HealBot_Action_TooltipUnit then
        HealBot_Action_RefreshTooltip(HealBot_Action_TooltipUnit,"Enabled");
      elseif HealBot_Config.TooltipUpdate==1 and HealBot_Action_DisableTooltipUnit then
        HealBot_Action_RefreshTooltip(HealBot_Action_DisableTooltipUnit,"Disabled");
      end
    else
	  HealsIn_Timer=0;
    end
    HealBot_SpamCnt = 0;
    if HealBot_RequestVer then
      HealBot_Comms_SendAddonMsg("HealBot", ">> SendVersion <<=>> "..HealBot_RequestVer.." <<=>> Version="..HEALBOT_VERSION, HealBot_AddonMsgType, HealBot_PlayerName)
      HealBot_RequestVer=nil;
    end
    
    for uName in pairs(HealBot_Ressing) do
      if HealBot_Ressing[uName]=="_NIL_" then 
        HealBot_Ressing[uName]=nil;
		if HealBot_UnitID[uName] then HealBot_Action_ResetUnitStatus(HealBot_UnitID[uName]) end
       elseif HealBot_Ressing[uName]=="_RESSED2_" then 
        HealBot_Ressing[uName]="_NIL_"
      elseif HealBot_Ressing[uName]=="_RESSED1_" then 
        HealBot_Ressing[uName]="_RESSED2_";
      elseif HealBot_Ressing[uName]=="_RESSED_" then 
        HealBot_Ressing[uName]="_RESSED1_";
      end
    end
	
    for uName in pairs(HealBot_HealsIn) do
      if not HealBot_HealsIncCnt[uName] then HealBot_HealsIncCnt[uName]=2 end
	  HealBot_HealsIncCnt[uName]=HealBot_HealsIncCnt[uName]+1; 
      if HealBot_HealsIncCnt[uName]>4 then
	     HealBot_HealsIn[uName]=nil
		 HealBot_HealsIncCnt[uName]=nil
         HealBot_Action_ResetUnitStatus(HealBot_UnitID[uName])
      end
    end

  elseif HealBot_Timer3>=0.5 and HealBot_Timer2<0.5 then
    HealBot_Timer3=0
    if HealBot_StopCastTimer>0 then
	  HealBot_StopCastTimer=HealBot_StopCastTimer-1
	end

    for uName in pairs(HealBot_Player_HoT) do
	  huHoTtime=HealBot_Player_HoT[uName]
	  for sName in pairs(huHoTtime) do
	    if huHoTtime[sName]<40 then
	      huHoTtime[sName]=huHoTtime[sName]-1
		  if huHoTtime[sName]<2 then
		    if HealBot_Player_HoT_Icons[uName][sName]>0 then
			  HealBot_AddDebug("In HoT timer: time<2 icon>0")
			  HealBot_AddDebug("uName="..uName.."  sName="..sName)
			  HealBot_HoT_Update(uName, sName)
			else
              huHoTtime[sName]=nil
            end
 		  end
		else
          HealBot_HoT_Update(uName, sName)
		end
      end
    end
	
	for uName in pairs(HealBot_IncomingHealEnd) do
      HealBot_Action_ResetUnitStatus(HealBot_UnitID[uName])
	  HealBot_IncomingHealEnd[uName]=nil
	end
	
    HealBot_ThrottleCnt=0
	if not HealBot_IsFighting then
      Ti=0;
      for uName,_ in pairs(HealBot_DelayDebuffCheck) do
		xUnit=HealBot_UnitID[uName]
		if not HealBot_UnitDebuff[uName] then
           HealBot_CheckUnitDebuffs(xUnit)
		   Ti=Ti+1
		end
        HealBot_DelayDebuffCheck[uName] = nil;
        if Ti>7/HealBot_Config.RangeCheckFreq then 
	      do break end;
	    end
      end
      for uName,_ in pairs(HealBot_DelayBuffCheck) do
        if Ti>5/HealBot_Config.RangeCheckFreq then 
		  do break end;
		end
		xUnit=HealBot_UnitID[uName]
		if not HealBot_UnitBuff[uName] then
		  HealBot_CheckUnitBuffs(xUnit)
          Ti=Ti+1
        end		
		HealBot_DelayBuffCheck[uName] = nil;
      end
      for uName,_ in pairs(HealBot_QueueCheckBuffs) do
	    HealBot_CheckMyBuffs(uName)
	    HealBot_QueueCheckBuffs[uName]=nil
		do break end
      end
	  if HealBot_Loaded==1 then
	    HealBot_InitSpells()
        HealBot_Action_ResetSkin()
        HealBot_Buff_Spells_List=HealBot_Options_InitBuffList()
        HealBot_Options_EmergencyFilter_Reset()
        HealBot_Options_SetSkins();
        HealBot_Options_CheckCombos()
	    HealBot_CheckMyBuffs(HealBot_PlayerName)
		HealBot_Init_SetSpec()
        if HealBot_Config.AutoClose==0 then HealBot_Config.ActionVisible=1; end
        if HealBot_Config.DisableHealBot==0 and HealBot_Config.ActionVisible==1 then HealBot_Action:Show() end
        HealBot_RecalcParty();
	    HealBot_Options_Debuff_Reset()
	    HealBot_Options_Buff_Reset()
        HealBot_Comms_SendAddonMsg("HealBot", ">> SendVersion <<=>> "..HealBot_PlayerName.." <<=>> Version="..HEALBOT_VERSION, HealBot_AddonMsgType, HealBot_PlayerName)	   
        HealBot_Comms_SendAddonMsg("HealBot", ">> RequestVersion <<=>> "..HealBot_PlayerName.." <<=>> nil <<", HealBot_AddonMsgType, HealBot_PlayerName)
        if HealBot_UnitSpec[HealBot_PlayerName] then
          if HealBot_UnitSpec[HealBot_PlayerName] == " " then
            HealBot_GetTalentInfo(HealBot_PlayerName)
	      end
        end
	    HealBot_Loaded=true
		HealBot_CheckFrame()
		HealBot_CheckTalents=true
        HealBot_Comms_SendAddonMsg("CTRA", "SR", 2, HealBot_PlayerName)
        HealBot_AddDebug(HEALBOT_ADDON .. HEALBOT_LOADED);
		HealBot_Action_SetDebuffAggroCols()
	  end
	end
  elseif HealBot_Timer2>HB_Timer2 then
    HealBot_Timer2=0
	if not HealBot_Loaded then return end
	aSwitch=aSwitch+1
	if aSwitch<3 then
      HealBot_Action_RefreshButtons()
    else
	  if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 then HealBot_CheckAggro() end
      for xUnit,uName in pairs(HealBot_DelayAuraCheck) do
	    if UnitIsVisible(xUnit) then 
          HealBot_HasMyBuffs(xUnit,uName) 
		end
        HealBot_DelayAuraCheck[xUnit] = nil;
	  end
	  aSwitch=0      
	end
	if HealBot_InCombatUpdate then
		HealBot_IC_PartyMembersChanged()
    end
  end
end

function HealBot_RetVersion()
  return HEALBOT_VERSION
end

function HealBot_OnEvent(self, event, arg1,arg2,arg3,arg4)
  if (event=="CHAT_MSG_ADDON") then
    HealBot_OnEvent_AddonMsg(self,arg1,arg2,arg3,arg4);
  elseif (event=="UNIT_AURA") then
    HealBot_OnEvent_UnitAura(self,arg1);
  elseif (event=="UNIT_MANA") or (event=="UNIT_RAGE") or (event=="UNIT_ENERGY") or (event=="UNIT_RUNIC_POWER") then
    HealBot_OnEvent_UnitMana(self,arg1);
  elseif (event=="UNIT_HEALTH") then
    HealBot_OnEvent_UnitHealth(self,arg1,UnitHealth(arg1),UnitHealthMax(arg1));
  elseif (event=="UnitHealthUpdated") then
    HealBot_OnEvent_UnitHealth(self,arg1,arg2,arg3);
  elseif (event=="UNIT_COMBAT") or (event=="UNIT_THREAT_LIST_UPDATE") or (event=="UNIT_THREAT_SITUATION_UPDATE") then
	if arg1 then HealBot_OnEvent_UnitCombat(self,arg1); end
  elseif (event=="UNIT_SPELLCAST_START") then
	HealBot_OnEvent_UnitSpellcastStart(self,arg1)
  elseif (event=="UNIT_SPELLCAST_STOP") or (event=="UNIT_SPELLCAST_SUCCEEDED") then
    HealBot_OnEvent_UnitSpellcastStop(self,arg1);
  elseif (event=="UNIT_SPELLCAST_FAILED") or (event=="UNIT_SPELLCAST_INTERRUPTED") then
    HealBot_OnEvent_UnitSpellcastFail(self,arg1);
  elseif (event=="UNIT_SPELLCAST_SENT") then
    HealBot_OnEvent_UnitSpellcastSent(self,arg1,arg2,arg3,arg4);  
  elseif (event=="PLAYER_REGEN_DISABLED") then
    HealBot_OnEvent_PlayerRegenDisabled(self);
  elseif (event=="PLAYER_REGEN_ENABLED") then
    HealBot_OnEvent_PlayerRegenEnabled(self);
  elseif (event=="UNIT_NAME_UPDATE") then
    HealBot_OnEvent_UnitNameUpdate(self,arg1)
  elseif (event=="CHAT_MSG_SYSTEM") then
    HealBot_OnEvent_SystemMsg(self,arg1);
  elseif (event=="PARTY_MEMBERS_CHANGED") then
    HealBot_OnEvent_PartyMembersChanged(self);
  elseif (event=="PLAYER_TARGET_CHANGED") then
    HealBot_OnEvent_PlayerTargetChanged(self);
  elseif (event=="MODIFIER_STATE_CHANGED") then
    HealBot_OnEvent_ModifierStateChange(self,arg1,arg2);
  elseif (event=="UNIT_INVENTORY_CHANGED") then
    HealBot_OnEvent_PlayerEquipmentChanged(self,arg1);
  elseif (event=="UNIT_PET") then
    HealBot_OnEvent_PartyPetChanged(self);
  elseif (event=="RAID_ROSTER_UPDATE") then
    HealBot_OnEvent_RaidRosterUpdate(self);
  elseif (event=="LEARNED_SPELL_IN_TAB") then
    HealBot_OnEvent_SpellsChanged(self,arg1);
  elseif (event=="INSPECT_TALENT_READY") then
    HealBot_GetTalentInfo(UnitName("target"))
  elseif (event=="PLAYER_ENTERING_WORLD") then
    HealBot_OnEvent_PlayerEnteringWorld(self);
  elseif (event=="PLAYER_LEAVING_WORLD") then
    HealBot_OnEvent_PlayerLeavingWorld(self);
  elseif (event=="CHARACTER_POINTS_CHANGED") then
    HealBot_OnEvent_TalentsChanged(self, arg1);
  elseif (event=="ZONE_CHANGED_NEW_AREA") then
    HealBot_OnEvent_ZoneChanged(self);
  elseif (event=="VARIABLES_LOADED") then
    HealBot_OnEvent_VariablesLoaded(self);
  else
    HealBot_AddDebug("OnEvent (" .. event .. ")");
  end
end

function HealBot_OnEvent_VariablesLoaded(self)
    table.foreach(HealBot_ConfigDefaults, function (key,val)
      if not HealBot_Config[key] then
        HealBot_Config[key] = val;
      end
    end);
    table.foreach(HealBot_Config_SkinsDefaults, function (key,val)
      if not Healbot_Config_Skins[key] then
	    Healbot_Config_Skins[key] = val;
      end
    end);

    HealBot_Update_Skins()
    HealBot_Options_Init(9)
    HealBot_PlayerClass, HealBot_PlayerClassEN=UnitClass("player")
    HealBot_PlayerRace, HealBot_PlayerRaceEN=UnitRace("player")
    HealBot_PlayerName=UnitName("player")
	HealBot_PlayerBuff[HealBot_PlayerName]={}
    if not HealBot_Config.DisabledKeyCombo then HealBot_InitNewChar(HealBot_PlayerClassEN) end
    HealBot_Class_En[HEALBOT_DRUID]="DRUI"
    HealBot_Class_En[HEALBOT_HUNTER]="HUNT"
    HealBot_Class_En[HEALBOT_MAGE]="MAGE"
    HealBot_Class_En[HEALBOT_PALADIN]="PALA"
    HealBot_Class_En[HEALBOT_PRIEST]="PRIE"
    HealBot_Class_En[HEALBOT_ROGUE]="ROGU"
    HealBot_Class_En[HEALBOT_SHAMAN]="SHAM"
    HealBot_Class_En[HEALBOT_WARLOCK]="WARL"
    HealBot_Class_En[HEALBOT_WARRIOR]="WARR"
	HealBot_Class_En[HEALBOT_DEATHKNIGHT]="DEAT"
	if strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PRIEST] then
      HealBot_ShortBuffs[HEALBOT_SHADOW_PROTECTION]=true
	  HealBot_ShortBuffs[HEALBOT_TOUCH_OF_WEAKNESS]=true
	  HealBot_ShortBuffs[HEALBOT_INNER_FIRE]=true
	  	HealBot_HoT_Texture[HEALBOT_GUARDIAN_SPIRIT]   = "Interface\\Icons\\Spell_Holy_GuardianSpirit";
      HealBot_HoT_Texture[HEALBOT_RENEW]             = "Interface\\Icons\\Spell_Holy_Renew";
      HealBot_HoT_Texture[HEALBOT_POWER_WORD_SHIELD] = "Interface\\Icons\\Spell_Holy_PowerWordShield";
      HealBot_HoT_Texture[HEALBOT_PRAYER_OF_MENDING] = "Interface\\Icons\\Spell_Holy_PrayerOfMendingtga";
	  HealBot_HoT_Texture[HEALBOT_ABOLISH_DISEASE] = "Interface\\ICONS\\Spell_Nature_NullifyDisease";
	  HealBot_Watch_HoT[HEALBOT_GUARDIAN_SPIRIT]="A"
	  HealBot_Watch_HoT[HEALBOT_RENEW]="C"
	  HealBot_Watch_HoT[HEALBOT_ABOLISH_DISEASE]="C"
	  HealBot_Watch_HoT[HEALBOT_POWER_WORD_SHIELD]="A"
	  HealBot_Watch_HoT[HEALBOT_PRAYER_OF_MENDING]="A"
	  HealBot_BuffNameSwap = {[HEALBOT_POWER_WORD_FORTITUDE] = HEALBOT_PRAYER_OF_FORTITUDE,
                              [HEALBOT_PRAYER_OF_FORTITUDE] = HEALBOT_POWER_WORD_FORTITUDE,
                              [HEALBOT_SHADOW_PROTECTION] = HEALBOT_PRAYER_OF_SHADOW_PROTECTION,
                              [HEALBOT_PRAYER_OF_SHADOW_PROTECTION] = HEALBOT_SHADOW_PROTECTION,
                              [HEALBOT_DIVINE_SPIRIT] = HEALBOT_PRAYER_OF_SPIRIT,
                              [HEALBOT_PRAYER_OF_SPIRIT] = HEALBOT_DIVINE_SPIRIT}
      HealBot_Talents["InnerFocus"]=false
      HealBot_Talents["ImprovedRenew"]=0
	  HealBot_Talents["TwinDisciplines"]=0
	  HealBot_Talents["SpiritalHealing"]=0
	  HealBot_Talents["EmpoweredPower"]=0
	  HealBot_Talents["DivineProvidence"]=0
	  HealBot_Talents["FocusedPower"]=0
	elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_DRUID] then
	  HealBot_ShortBuffs[HEALBOT_THORNS]=true
      HealBot_HoT_Texture[HEALBOT_REJUVENATION]      = "Interface\\Icons\\Spell_Nature_Rejuvenation";
      HealBot_HoT_Texture[HEALBOT_REGROWTH]          = "Interface\\Icons\\Spell_Nature_ResistNature";
      HealBot_HoT_Texture[HEALBOT_LIFEBLOOM]         = "Interface\\Icons\\INV_Misc_Herb_Felblossom";
	  HealBot_HoT_Texture[HEALBOT_TRANQUILITY]       = "Interface\\Icons\\Spell_Nature_Tranquility";
--	  HealBot_HoT_Texture[HEALBOT_WILD_GROWTH]       = "Interface\\Icons\\Spell_Nature_WildGrowth";
	  HealBot_HoT_Texture[HEALBOT_WILD_GROWTH]       = "Interface\\Icons\\Ability_Druid_Flourish";
	  HealBot_HoT_Texture[HEALBOT_ABOLISH_POISON]       = "Interface\\ICONS\\Spell_Nature_NullifyPoison"
	  HealBot_Watch_HoT[HEALBOT_REJUVENATION]="C"
	  HealBot_Watch_HoT[HEALBOT_REGROWTH]="C"
	  HealBot_Watch_HoT[HEALBOT_LIFEBLOOM]="C"
	  HealBot_Watch_HoT[HEALBOT_ABOLISH_POISON]="C"
	  HealBot_Watch_HoT[HEALBOT_WILD_GROWTH]="AM"
	  HealBot_Watch_HoT[HEALBOT_TRANQUILITY]="AM"
	  HealBot_BuffNameSwap = {[HEALBOT_MARK_OF_THE_WILD] = HEALBOT_GIFT_OF_THE_WILD,
                              [HEALBOT_GIFT_OF_THE_WILD] = HEALBOT_MARK_OF_THE_WILD}
	  HealBot_Talents["Genesis"]=0
	  HealBot_Talents["NurturingInstinct"]=0
	  HealBot_Talents["ImprovedRejuvenation"]=0
	  HealBot_Talents["GiftOfNature"]=0
	  HealBot_Talents["EmpoweredTouch"]=0
	  HealBot_Talents["EmpoweredRejuvenation"]=0
	elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PALADIN] then
      HealBot_ShortBuffs[HEALBOT_BLESSING_OF_MIGHT]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_WISDOM]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_SALVATION]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_SANCTUARY]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_LIGHT]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_PROTECTION]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_FREEDOM]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_SACRIFICE]=true
	  HealBot_ShortBuffs[HEALBOT_BLESSING_OF_KINGS]=true
	  HealBot_ShortBuffs[HEALBOT_BEACON_OF_LIGHT]=true
	  HealBot_ShortBuffs[HEALBOT_SACRED_SHIELD]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_RIGHTEOUSNESS]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_BLOOD]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_CORRUPTION]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_JUSTICE]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_LIGHT]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_VENGEANCE]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_WISDOM]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_COMMAND]=true
	  HealBot_ShortBuffs[HEALBOT_SEAL_OF_THE_MARTYR]=true
	  HealBot_Watch_HoT[HEALBOT_GLYPH_OF_FLASH_OF_LIGHT]="C"
	  HealBot_HoT_Texture[HEALBOT_GLYPH_OF_FLASH_OF_LIGHT] = "Interface\\Icons\\Spell_Holy_Renew";
	  HealBot_Watch_HoT[HEALBOT_BEACON_OF_LIGHT]="CT"
	  HealBot_HoT_Texture[HEALBOT_BEACON_OF_LIGHT] = "Interface\\Icons\\Ability_Paladin_BeaconofLight";
	  HealBot_Watch_HoT[HEALBOT_SACRED_SHIELD]="CT"
	  HealBot_HoT_Texture[HEALBOT_SACRED_SHIELD] = "Interface\\Icons\\Ability_Paladin_SacredCleansing";
	  HealBot_BuffNameSwap = {[HEALBOT_BLESSING_OF_MIGHT] = HEALBOT_GREATER_BLESSING_OF_MIGHT,
                              [HEALBOT_GREATER_BLESSING_OF_MIGHT] = HEALBOT_BLESSING_OF_MIGHT,
                              [HEALBOT_BLESSING_OF_WISDOM] = HEALBOT_GREATER_BLESSING_OF_WISDOM,
                              [HEALBOT_GREATER_BLESSING_OF_WISDOM] = HEALBOT_BLESSING_OF_WISDOM,
                              [HEALBOT_BLESSING_OF_KINGS] = HEALBOT_GREATER_BLESSING_OF_KINGS,
                              [HEALBOT_GREATER_BLESSING_OF_KINGS] = HEALBOT_BLESSING_OF_KINGS,
                              [HEALBOT_BLESSING_OF_LIGHT] = HEALBOT_GREATER_BLESSING_OF_LIGHT,
                              [HEALBOT_GREATER_BLESSING_OF_LIGHT] = HEALBOT_BLESSING_OF_LIGHT,
                              [HEALBOT_BLESSING_OF_SALVATION] = HEALBOT_GREATER_BLESSING_OF_SALVATION,
                              [HEALBOT_GREATER_BLESSING_OF_SALVATION] = HEALBOT_BLESSING_OF_SALVATION,
                              [HEALBOT_BLESSING_OF_SANCTUARY] = HEALBOT_GREATER_BLESSING_OF_SANCTUARY,
                              [HEALBOT_GREATER_BLESSING_OF_SANCTUARY] = HEALBOT_BLESSING_OF_SANCTUARY}
	  HealBot_Talents["HealingLight"]=0
	elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_HUNTER] then
	  HealBot_HoT_Texture[HEALBOT_MENDPET] = "Interface\\Icons\\Ability_Hunter_MendPet";
	  HealBot_Watch_HoT[HEALBOT_MENDPET]="C"
	elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_MAGE] then
	  HealBot_BuffNameSwap = {[HEALBOT_ARCANE_INTELLECT] = HEALBOT_ARCANE_BRILLIANCE,
                              [HEALBOT_ARCANE_BRILLIANCE] = HEALBOT_ARCANE_INTELLECT,
							  [HEALBOT_DALARAN_BRILLIANCE]=HEALBOT_DALARAN_INTELLECT,
							  [HEALBOT_DALARAN_INTELLECT]=HEALBOT_DALARAN_BRILLIANCE}
	elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_SHAMAN] then
	  HealBot_Watch_HoT[HEALBOT_RIPTIDE]="C"
	  HealBot_HoT_Texture[HEALBOT_RIPTIDE] = "Interface\\Icons\\Ability_Druid_Typhoon";
      HealBot_Watch_HoT[HEALBOT_EARTHLIVING_WEAPON]="C"
	  HealBot_Watch_HoT[HEALBOT_EARTH_SHIELD]="C"
      HealBot_HoT_Texture[HEALBOT_EARTHLIVING_WEAPON] = "Interface\\Icons\\Spell_Shaman_GiftEarthmother";
      HealBot_HoT_Texture[HEALBOT_EARTH_SHIELD] = "Interface\\Icons\\Spell_Nature_SkinofEarth";  
      HealBot_Watch_HoT[HEALBOT_WATER_SHIELD]="C"
      HealBot_HoT_Texture[HEALBOT_WATER_SHIELD] = "Interface\\Icons\\Ability_Shaman_WaterShield"; 
	  HealBot_Talents["Purification"]=0
	  HealBot_Talents["ImprovedChainHeal"]=0
	  HealBot_Talents["NatureBlessing"]=0
	end
    HealBot_CheckForDivineSpirit=true;
    if GetLocale() == "deDE" then
	  HealBot_xLocal=true
	end
	if HealBot_Config.EnLibQuickHealth==1 then
	    QuickHealth = LibStub("LibQuickHealth-2.0")
    	QuickHealth.RegisterCallback(self, "UnitHealthUpdated", function(event, unitID, health, healthMax)
	        HealBot_OnEvent(self, "UnitHealthUpdated", unitID, health, healthMax)
	    end);
    end
	HealBot_OptionsTab5:SetText(HEALBOT_OPTIONS_TAB_SKIN.." 1");
	HealBot_OptionsTab6:SetText(HEALBOT_OPTIONS_TAB_SKIN.." 2");
    HealBot:RegisterEvent("PLAYER_ENTERING_WORLD");
    HealBot:RegisterEvent("PLAYER_LEAVING_WORLD");
	HealBot_OnEvent_PlayerEnteringWorld(self);
	HealBot_Loaded=1;
end

function HealBot_Register_Events()
 if HealBot_Config.DisableHealBot==0 then
    HealBot:RegisterEvent("PLAYER_REGEN_DISABLED");
    HealBot:RegisterEvent("PLAYER_REGEN_ENABLED");
    HealBot:RegisterEvent("PLAYER_TARGET_CHANGED");
    HealBot:RegisterEvent("PARTY_MEMBERS_CHANGED");
    HealBot:RegisterEvent("UNIT_HEALTH");
    if Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Current_Skin]>0 then HealBot_Register_Mana() end
    HealBot:RegisterEvent("LEARNED_SPELL_IN_TAB");
    HealBot:RegisterEvent("UNIT_SPELLCAST_START");
    HealBot:RegisterEvent("UNIT_AURA");
    HealBot:RegisterEvent("CHARACTER_POINTS_CHANGED");
    HealBot:RegisterEvent("UNIT_INVENTORY_CHANGED");
    HealBot:RegisterEvent("CHAT_MSG_ADDON");
    HealBot:RegisterEvent("CHAT_MSG_SYSTEM");
    HealBot:RegisterEvent("MODIFIER_STATE_CHANGED");
    HealBot:RegisterEvent("UNIT_PET");
    HealBot:RegisterEvent("UNIT_NAME_UPDATE");
    if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 then HealBot_Register_Aggro() end
	HealBot:RegisterEvent("ZONE_CHANGED_NEW_AREA");
	HealBot:RegisterEvent("RAID_ROSTER_UPDATE");
	HealBot:RegisterEvent("INSPECT_TALENT_READY");
--    HealBot:RegisterEvent("PARTY_MEMBER_DISABLE");
--    HealBot:RegisterEvent("PARTY_MEMBER_ENABLE");
    if HealBot_Loaded then HealBot_Comms_SendAddonMsg("CTRA", "SR", 2, HealBot_PlayerName) end
  end
  HealBot:RegisterEvent("UNIT_SPELLCAST_SENT");
  HealBot:RegisterEvent("CHAT_MSG_ADDON");
  HealBot:RegisterEvent("UNIT_SPELLCAST_STOP");
  HealBot:RegisterEvent("UNIT_SPELLCAST_FAILED");
  HealBot:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED");
  HealBot:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED");
  
  HealBot_Set_Timers()
  HealBot_Action_Set_Timers()
  HealBot_Options_Set_Timers()
end

function HealBot_Register_Aggro()
  HealBot:RegisterEvent("UNIT_COMBAT")
  HealBot:RegisterEvent("UNIT_THREAT_LIST_UPDATE")
  HealBot:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE")
end

function HealBot_UnRegister_Aggro()
  HealBot:UnregisterEvent("UNIT_COMBAT")
  HealBot:UnregisterEvent("UNIT_THREAT_LIST_UPDATE")
  HealBot:UnregisterEvent("UNIT_THREAT_SITUATION_UPDATE")
end

function HealBot_Register_Mana()
  HealBot:RegisterEvent("UNIT_MANA")
  HealBot:RegisterEvent("UNIT_RAGE")
  HealBot:RegisterEvent("UNIT_ENERGY")
  HealBot:RegisterEvent("UNIT_RUNIC_POWER")
end

function HealBot_UnRegister_Mana()
  HealBot:UnregisterEvent("UNIT_MANA")
  HealBot:UnregisterEvent("UNIT_RAGE")
  HealBot:UnregisterEvent("UNIT_ENERGY")
  HealBot:UnregisterEvent("UNIT_RUNIC_POWER")
end

function HealBot_UnRegister_Events()

    HealBot:UnregisterEvent("ZONE_CHANGED_NEW_AREA");
    HealBot:UnregisterEvent("PLAYER_REGEN_DISABLED");
    HealBot:UnregisterEvent("PLAYER_REGEN_ENABLED");
    HealBot:UnregisterEvent("PLAYER_TARGET_CHANGED");
    HealBot:UnregisterEvent("PARTY_MEMBERS_CHANGED");
--    HealBot:UnregisterEvent("UNIT_HEALTH");
--    HealBot_UnRegister_Mana()
    HealBot:UnregisterEvent("LEARNED_SPELL_IN_TAB");
    HealBot:UnregisterEvent("UNIT_SPELLCAST_SENT");
    HealBot:UnregisterEvent("UNIT_SPELLCAST_START");
    HealBot:UnregisterEvent("UNIT_SPELLCAST_STOP");
    HealBot:UnregisterEvent("UNIT_SPELLCAST_FAILED");
    HealBot:UnregisterEvent("UNIT_SPELLCAST_INTERRUPTED");
    HealBot:UnregisterEvent("UNIT_SPELLCAST_SUCCEEDED");
    HealBot:UnregisterEvent("UNIT_AURA");
    HealBot:UnregisterEvent("CHARACTER_POINTS_CHANGED");
    HealBot:UnregisterEvent("UNIT_INVENTORY_CHANGED");
    HealBot:UnregisterEvent("CHAT_MSG_ADDON");
    HealBot:UnregisterEvent("CHAT_MSG_SYSTEM");
    HealBot:UnregisterEvent("MODIFIER_STATE_CHANGED");
    HealBot:UnregisterEvent("UNIT_PET");
    HealBot:UnregisterEvent("UNIT_NAME_UPDATE");
	HealBot:UnregisterEvent("RAID_ROSTER_UPDATE");
	HealBot:UnregisterEvent("INSPECT_TALENT_READY");
    HealBot_UnRegister_Aggro()
	
	HB_Timer2=5
	HealBot_Action_Set_Timers(true)
	HealBot_Options_Set_Timers(true)
	
--    HealBot:UnregisterEvent("PARTY_MEMBER_DISABLE");
--    HealBot:UnregisterEvent("PARTY_MEMBER_ENABLE");
end

local aUnit,oUnit=nil,nil
function HealBot_CheckAggro()
  if hbtestaggro then return end
  for aUnit,oUnit in pairs(HealBot_Aggro) do 
    _,z,_,_,_ = UnitDetailedThreatSituation(aUnit, aUnit.."target")
    _,y,_,_,_ = UnitThreatSituation(aUnit)
    if not z then z=0 end
    if not y then y=0 end
    if y>z then z=y end
	if z==0 then
      if UnitIsEnemy(aUnit, aUnit.."target") then
	    uName=UnitName(aUnit.."targettarget")
	    if uName~=UnitName(aUnit) then
	      if UnitIsEnemy(oUnit, oUnit.."target") then
		    uName=UnitName(oUnit.."targettarget")
            if uName~=UnitName(aUnit) then
			  _,z,_,_,_ = UnitDetailedThreatSituation(oUnit, oUnit.."target")
              _,y,_,_,_ = UnitThreatSituation(oUnit)
              if not z then z=0 end
              if not y then y=0 end
              if y>z then z=y end
		      HealBot_SetAggro(oUnit, z)
              HealBot_Aggro[aUnit]=nil
              HealBot_Action_UpdateAggro(aUnit,false)
		    end
		  else
            HealBot_Aggro[aUnit]=nil
            HealBot_Action_UpdateAggro(aUnit,false)
		  end
        end
	  elseif UnitIsEnemy(oUnit, oUnit.."target") then
	    uName=UnitName(oUnit.."targettarget")
	    if uName~=UnitName(aUnit) then
          _,z,_,_,_ = UnitDetailedThreatSituation(oUnit, oUnit.."target")
          _,y,_,_,_ = UnitThreatSituation(oUnit)
          if not z then z=0 end
          if not y then y=0 end
          if y>z then z=y end
	      HealBot_SetAggro(oUnit, z)
          HealBot_Aggro[aUnit]=nil
          HealBot_Action_UpdateAggro(aUnit,false)
	    end
	  else
        HealBot_Aggro[aUnit]=nil
        HealBot_Action_UpdateAggro(aUnit,false)
	  end
	elseif z~=HealBot_Action_RetUnitThreat(UnitName(aUnit)) then
	  HealBot_Action_UpdateAggro(aUnit,true,z)
	end
  end
end

function HealBot_SetAggro(unit, aggroStatus)
    uName=UnitName(unit.."targettarget")
	xUnit=HealBot_UnitID[uName]
	if xUnit=="target" then return end
	if xUnit and (aggroStatus~=HealBot_Action_RetUnitThreat(UnitName(xUnit)) or not HealBot_Aggro[xUnit]) then
      HealBot_Aggro[xUnit]=unit
	  HealBot_Action_UpdateAggro(xUnit,true,aggroStatus)
	end
end

function HealBot_RetSetAggroSize()
  z=0
  for _,_ in pairs(HealBot_Aggro) do 
	  z=z+1
  end
  return z
end

function HealBot_ClearAggro()
  for x,_ in pairs(HealBot_Aggro) do 
	HealBot_Aggro[x]=nil
	HealBot_Action_UpdateAggro(x,false)
  end
end

local HealBotAddonSummary={}
local HealBotAddonIncHeals={}
local utName=nil
function HealBot_OnEvent_AddonMsg(self,addon_id,msg,distribution,sender_id,internal)
--  inc_msg = gsub(msg, "%$", "s");
--  inc_msg = gsub(inc_msg, "§", "S");
  inc_msg=msg
  if HealBot_InBG then sender_id = string.match(sender_id, "^[^-]*") end
  heal_val=nil
  utName=nil
  if not HealBotAddonSummary[addon_id] then
    HealBotAddonSummary[addon_id]=1
  else
    HealBotAddonSummary[addon_id]=HealBotAddonSummary[addon_id]+1
  end
  if addon_id==HEALBOT_ADDON_ID or addon_id=="VisualHeal" or addon_id=="HealComm" or addon_id=="CastCommLib" or addon_id=="X-PerlHeal" then
    if HealBot_xLocal then
	  if addon_id==HEALBOT_ADDON_ID then return end
	else
      if sender_id==HealBot_PlayerName and not internal then return end
	  if HealBot_Vers[sender_id] and addon_id~=HEALBOT_ADDON_ID then return end
	end
    if HealBot_Healers[sender_id] then
	  if HealBot_Comms_Blacklist[sender_id]==addon_id then
        _,_,heal_val,utName = string.find(HealBot_Healers[sender_id], ">> (.%d+) <<=>> (.+)$" )
        if heal_val then heal_val=0-tonumber(heal_val) end
	  end
	  if utName and heal_val then
		HealBot_Healers[sender_id]=nil
        if HealBot_TabHealers[sender_id] then
          utName = HealBot_TabHealers[sender_id]
		  HealBot_TabHealers[sender_id] = nil
		end
	  end
	elseif HealBot_UnitID[sender_id] then
	  if addon_id==HEALBOT_ADDON_ID then
	    _,_,heal_val,utName = strfind(inc_msg, ">> (.%d+) <<=>> (.+)$" )
		if strfind(utName,":") then utName = HealBot_Split(utName, ":"); end
	  elseif addon_id=="CastCommLib" then
	    arr = strsub(inc_msg, 1, 2)
	    if arr=="C " then
	      spell, rank, _, _, _, _, _ = UnitCastingInfo(HealBot_UnitID[sender_id])
	      if spell and rank and HealBot_Spells[spell.."("..rank..")"] then 
		    utName = strsub(inc_msg, 3, -1)
	        heal_val=HealBot_Spells[spell.."("..rank..")"].HealsDur 
          end
		end
	  elseif addon_id=="HealComm" then
	    arr = tonumber(strsub(inc_msg, 1, 3))
	    if arr==0 then
          heal_val = tonumber(strsub(inc_msg, 4, 8))
          utName = strsub(inc_msg, 9, -1)
        elseif arr==2 then
          heal_val = tonumber(strsub(inc_msg, 4, 8))
		  utName = HealBot_Split(strsub(inc_msg, 9, -1), ":");
        end	
	  elseif addon_id=="X-PerlHeal" then
	    datatype, utName = strmatch(msg, "^(%a+) (%a+)$")
        if datatype=="HEAL" then
          spell, rank, _, _, _, _, _ = UnitCastingInfo(HealBot_UnitID[sender_id])
	      if spell and rank and HealBot_Spells[spell.."("..rank..")"] then 
	        heal_val=HealBot_Spells[spell.."("..rank..")"].HealsDur 
		  end
	    end
	  elseif addon_id=="VisualHeal" then
	    _, _, utName, heal_val = strfind(inc_msg, "^%d+:(.+):(%d+):")
	  end
	  if heal_val and tonumber(heal_val)<1 then heal_val=nil end
	  if utName and heal_val then
		if (type(utName) == "table") then
		  HealBot_Healers[sender_id] = ">> "..heal_val.." <<=>> table"
		  HealBot_TabHealers[sender_id] = utName
		  table.foreach(utName, function (index,unitn) HealBot_HealsIncCnt[unitn]=0 end)
		else
          HealBot_Healers[sender_id] = ">> "..heal_val.." <<=>> "..utName
		  HealBot_HealsIncCnt[utName]=0
		end
		HealBot_Comms_Blacklist[sender_id]=addon_id
        arr=addon_id.." : "..sender_id
        if not HealBotAddonIncHeals[arr] then
          HealBotAddonIncHeals[arr]=1
        else
          HealBotAddonIncHeals[arr]=HealBotAddonIncHeals[arr]+1
        end
	  end
	end
    if utName and heal_val then
      if (type(utName) == "table") then
        table.foreach(utName, function (index,unitn)
		  if HealBot_InBG then unitn = string.match(unitn, "^[^-]*") end
          HealBot_HealsInUpdate(unitn,heal_val)
        end)
      else
	    if HealBot_InBG then utName = string.match(utName, "^[^-]*") end
        HealBot_HealsInUpdate(utName,heal_val)
--		if addon_id~=HEALBOT_ADDON_ID then HealBot_AddDebug(addon_id..":IncHeal from "..sender_id.."  heal:"..utName..":"..heal_val) end
      end 
    end
  elseif addon_id=="HealBot" then
    _,_, datatype, sender, datamsg = strfind(inc_msg, ">> (%a+) <<=>> (%a+) <<=>> (.+)");
    if datatype=="RequestVersion" then
      HealBot_RequestVer=sender;
    elseif datatype=="SendVersion" then
	  HealBot_Vers[sender_id]=datamsg
      HealBot_AddDebug(sender_id..":  "..datamsg);
    end
  elseif addon_id=="CTRA" then
    if ( strfind(inc_msg, "#") ) then
      arr = HealBot_Split(inc_msg, "#");
      for k in pairs(arr) do
        HealBot_ParseCTRAMsg(sender_id, arr[k]);
      end
    else
        HealBot_ParseCTRAMsg(sender_id, inc_msg);
    end
  end
end

function HealBot_RetHealBotAddonSummary()
  return HealBotAddonSummary
end

function HealBot_RetHealBotAddonIncHeals()
  return HealBotAddonIncHeals
end

function HealBot_GetInfo()
  return HealBot_Vers
end

function HealBot_Split(msg, char)
  for x,_ in pairs(arrg) do
    arrg[x]=nil;
  end
	while (strfind(msg, char) ) do
		x, y = strfind(msg, char);
		tinsert(arrg, strsub(msg, 1, x-1));
		msg = strsub(msg, y+1, strlen(msg));
	end
	if ( strlen(msg) > 0 ) then
		tinsert(arrg, msg);
	end
	return arrg;
end

local HealBot_MT=nil
function HealBot_ParseCTRAMsg(sender_id, inc_msg)
    if ( strsub(inc_msg, 1, 3) == "RES" ) then
      if ( inc_msg == "RESNO" ) then
        for j in pairs(HealBot_Ressing) do
          if HealBot_Ressing[j]==sender_id then
            HealBot_Ressing[j] = "_RESSED_";
            break
          end
        end
      else
        _,_, uName = strfind(inc_msg, "^RES (.+)$");
        if ( uName ) then
          HealBot_Ressing[uName] = sender_id;
          HealBot_RecalcHeals(HealBot_UnitID[uName]);
         end
      end
    elseif ( strsub(inc_msg, 1, 4) == "SET " ) then
      _,_, x, uName = strfind(inc_msg, "^SET (%d+) (.+)$");
      if ( x and uName ) then
        for k in pairs(HealBot_MainTanks) do
          if ( HealBot_MainTanks[k] == uName ) then
            HealBot_MainTanks[k] = nil;
          end
        end
        HealBot_MainTanks[tonumber(x)] = uName;
		HealBot_MT=true
        Delay_RecalcParty=2;
      end
    elseif ( strsub(inc_msg, 1, 2) == "R " ) then
      _,_, uName = strfind(inc_msg, "^R (.+)$");
      if ( uName ) then
        for k in pairs(HealBot_MainTanks) do
          if ( HealBot_MainTanks[k] == uName ) then
            HealBot_MainTanks[k] = nil;
            Delay_RecalcParty=2;
          end
        end
      end
    end
end

function HealBot_OnEvent_RaidRosterUpdate(self)
   HealBot_OnEvent_PartyMembersChanged(self)
   if HealBot_MT then return end
   for x,_ in pairs(HealBot_MainTanks) do
     HealBot_MainTanks[x]=nil;
   end
   y = 0
   for index=1,40 do
      xUnit = "raid"..index
      if UnitExists(xUnit) then
         uName,_,_,_,_,_,_,_,_,z = GetRaidRosterInfo(index)
         if z == "MAINTANK" then
            y = y + 1
            HealBot_MainTanks[y] = uName
         end
      end
   end
   Delay_RecalcParty=2;
end

function HealBot_RetHealsIn(unitName)
  return HealBot_HealsIn[unitName] or 0;
end

function HealBot_ClearHealsIn(unitName)
  if HealBot_HealsIn[unitName] then HealBot_HealsIn[unitName]=nil end
end

function HealBot_OnEvent_UnitHealth(self,unit,health,healthMax)
    xUnit=HealBot_UnitID[UnitName(unit)]
    if not HealBot_UnitName[xUnit] or HealBot_Config.DisableHealBot==1 then return end
	uName=HealBot_UnitName[xUnit]
	if not HealBot_unitHealth[uName] then 
	  HealBot_unitHealth[uName]=-1
	  HealBot_unitHealthMax[uName]=-1
	end
	if HealBot_unitHealth[uName]~=health or HealBot_unitHealthMax[uName]~=healthMax then
	  HealBot_unitHealth[uName]=health
      HealBot_unitHealthMax[uName]=healthMax
	  HealBot_RecalcHeals(xUnit)
	end
    _,z,_,_,_ = UnitDetailedThreatSituation(xUnit, xUnit.."target")
    _,y,_,_,_ = UnitThreatSituation(xUnit)
    if not z then z=0 end
    if not y then y=0 end
    if y>z then z=y end
	if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 and (UnitIsEnemy(xUnit, xUnit.."target") or z>0) then
	  HealBot_SetAggro(xUnit, z)
	end
end

function HealBot_OnEvent_UnitMana(self,unit)
  xUnit=HealBot_UnitID[UnitName(unit)]
  if not HealBot_UnitName[xUnit] or HealBot_Config.DisableHealBot==1 then return end
  HealBot_Action_RefreshBar3(xUnit)
end

function HealBot_OnEvent_UnitCombat(self,unit)
  xUnit=HealBot_UnitID[UnitName(unit)]
  if not HealBot_UnitName[xUnit] then return end
  _,z,_,_,_ = UnitDetailedThreatSituation(xUnit, xUnit.."target")
  _,y,_,_,_ = UnitThreatSituation(xUnit)
  if not z then z=0 end
  if not y then y=0 end
  if y>z then z=y end
  if UnitIsEnemy(xUnit, xUnit.."target") or z>0 then
    HealBot_SetAggro(xUnit, z)
  end
end

function HealBot_OnEvent_ModifierStateChange(self,arg1,arg2)
  if HealBot_Action_TooltipUnit then
    HealBot_Action_RefreshTooltip(HealBot_Action_TooltipUnit,"Enabled");
  elseif HealBot_Action_DisableTooltipUnit then
    HealBot_Action_RefreshTooltip(HealBot_Action_DisableTooltipUnit,"Disabled");
  end
end

function HealBot_OnEvent_ZoneChanged(self)
  HealBot_CheckZoneFlag=true
end

function HealBot_CheckZone()
--  z = GetRealZoneText()
  _,z = IsInInstance()
--  if z==HEALBOT_ZONE_AB or z==HEALBOT_ZONE_WG  or z==HEALBOT_ZONE_AV or z==HEALBOT_ZONE_ES then
  if z=="pvp" then
    HealBot_InBG=true;
  else
    HealBot_InBG=nil;
  end
  HealBot_Action_ResetUnitStatus(HealBot_UnitID[HealBot_PlayerName])
  HealBot_SetAddonComms()
  Delay_RecalcParty=1
  HealBot_Queue_AllActiveMyBuffs()
end

local uaUnit=nil
function HealBot_OnEvent_UnitAura(self,unit)
  uName=UnitName(unit)
  if not HealBot_UnitID[uName] then return end
  uaUnit=HealBot_UnitID[uName]
  if not HealBot_Unit_Button[uaUnit] then
    if UnitName("target")==UnitName(uaUnit) then
	  uaUnit="target"
	else
      return;
	end
  end

  if HealBot_Config.DebuffWatch==1 then
    if HealBot_Config.DebuffWatchInCombat==0 and HealBot_IsFighting then
      HealBot_DelayDebuffCheck[uName]=true;
    else
      if uaUnit then HealBot_CheckUnitDebuffs(uaUnit); end
    end
  end  
  
  if uName==HealBot_PlayerName and strsub(HealBot_PlayerClassEN,1,4)=="PRIE" then
    HealBot_CheckForDivineSpirit=true;
    if HealBot_InnerFocusTalent then HealBot_CheckForInnerFocus(); end
  end
  
  if HealBot_Config.BuffWatch==1 then 
    if HealBot_Config.BuffWatchInCombat==0 and HealBot_IsFighting then
      HealBot_DelayBuffCheck[uName]=true;
    else
      HealBot_CheckUnitBuffs(uaUnit);
    end
  end

  if uaUnit==HealBot_Action_TooltipUnit then
    HealBot_Action_RefreshTooltip(HealBot_Action_TooltipUnit,"Enabled");
  elseif uaUnit==HealBot_Action_DisableTooltipUnit then
    HealBot_Action_RefreshTooltip(HealBot_Action_DisableTooltipUnit,"Disabled");
  end

  if Healbot_Config_Skins.ShowHoTicons[Healbot_Config_Skins.Current_Skin]==1 then 
    HealBot_DelayAuraCheck[uaUnit]=uName
  end

end

function HealBot_SetUnitBuffTimer(unitName,buffName,endtime)

  if not HealBot_PlayerBuff[unitName] then
    HealBot_PlayerBuff[unitName]={}
  end

  if HealBot_ShortBuffs[buffName] then 
    HealBot_PlayerBuff[unitName][buffName] = endtime-HealBot_Config.ShortBuffTimer
  else
    HealBot_PlayerBuff[unitName][buffName] = endtime-HealBot_Config.LongBuffTimer
  end
--  HealBot_AddDebug("Set bufftimer.buffName="..buffName.."  uName="..unitName.."  endtime="..endtime)
  if not HealBot_CheckBuffsTime or HealBot_PlayerBuff[unitName][buffName] < HealBot_CheckBuffsTime then
	HealBot_CheckBuffsTime=HealBot_PlayerBuff[unitName][buffName]
	HealBot_CheckBuffsTimeuName=unitName
  end
end

function HealBot_CheckForInnerFocus()
  HealBot_Talents["InnerFocus"]=HealBot_HasBuff(HEALBOT_INNER_FOCUS,"player")
end

local bCount, expirationTime, isMine=nil,nil,nil
function HealBot_HasBuff(buffName, unit)
  x,_,_,bCount,_,_,expirationTime, isMine,_ = UnitBuff(unit,buffName)
  if x then
    return true, expirationTime, isMine, bCount;
  end
  return false;
end

function HealBot_HasMyBuffs(unit,unitName)
  for bName,y in pairs(HealBot_Watch_HoT) do
    if y=="A" then
      HoTActive, expirationTime, isMine, bCount=HealBot_HasBuff(bName, unit)
	  if HoTActive then
		if not HealBot_Player_HoT[unitName] then HealBot_Player_HoT[unitName]={} end
        if not HealBot_Player_HoT_Icons[unitName] then HealBot_Player_HoT_Icons[unitName]={} end
        if not HealBot_Player_HoT_Icons[unitName][bName] then HealBot_Player_HoT_Icons[unitName][bName]=0 end
	    if not HealBot_Player_HoT[unitName][bName] then HealBot_Player_HoT[unitName][bName]=GetTime()+50 end
		if bName==HEALBOT_PRAYER_OF_MENDING then
	      if isMine then
		    HealBot_HoT_MendingWith=unitName
		    HealBot_HoT_Mending=bCount
		  else
		    if HealBot_HoT_MendingWith==unitName then HealBot_HoT_MendingWith="BouncedToFast" end
		  end
		end
		if HealBot_Player_HoT[unitName][bName]~=expirationTime then
		  HealBot_Player_HoT[unitName][bName]=expirationTime
		  HealBot_HoT_Update(unitName, bName)
		end
      elseif HealBot_Player_HoT_Icons[unitName] and HealBot_Player_HoT_Icons[unitName][bName] and HealBot_Player_HoT_Icons[unitName][bName]>0 then
		if bName==HEALBOT_POWER_WORD_SHIELD then
		  if not HealBot_TrackWS[unitName] then 
            HealBot_Player_HoT[unitName][bName]=10
            HealBot_HoT_Update(unitName, bName)
		  end
		else
          HealBot_Player_HoT[unitName][bName]=10
          HealBot_HoT_Update(unitName, bName)
		end
	  end
	elseif y=="AM" and HealBot_Watch_AM_HoT>0 then
	  HoTActive, expirationTime, bCount=HealBot_HasMyBuff(bName, unit)
	  if HoTActive then
		if not HealBot_Player_HoT[unitName] then HealBot_Player_HoT[unitName]={} end
        if not HealBot_Player_HoT_Icons[unitName] then HealBot_Player_HoT_Icons[unitName]={} end
        if not HealBot_Player_HoT_Icons[unitName][bName] then HealBot_Player_HoT_Icons[unitName][bName]=0 end
        if not HealBot_Player_HoT[unitName][bName] then HealBot_Player_HoT[unitName][bName]=GetTime()+50 end
	    if HealBot_Player_HoT[unitName][bName]~=expirationTime then
		  HealBot_Player_HoT[unitName][bName]=expirationTime
		  HealBot_HoT_Update(unitName, bName)
		end
      elseif HealBot_Player_HoT_Icons[unitName] and HealBot_Player_HoT_Icons[unitName][bName] and HealBot_Player_HoT_Icons[unitName][bName]>0 then
        HealBot_Player_HoT[unitName][bName]=10
        HealBot_HoT_Update(unitName, bName)
	  end
	elseif y=="CT" and HealBot_Watch_Timed_HoT[unitName] then
      HoTActive, expirationTime, bCount=HealBot_HasMyBuff(bName, unit)
      if HoTActive then
		if not HealBot_Player_HoT[unitName] then HealBot_Player_HoT[unitName]={} end
        if not HealBot_Player_HoT_Icons[unitName] then HealBot_Player_HoT_Icons[unitName]={} end
        if not HealBot_Player_HoT_Icons[unitName][bName] then HealBot_Player_HoT_Icons[unitName][bName]=0 end
        if not HealBot_Player_HoT[unitName][bName] then HealBot_Player_HoT[unitName][bName]=GetTime()+50 end
		if HealBot_Player_HoT[unitName][bName]~=expirationTime then
		  HealBot_Player_HoT[unitName][bName]=expirationTime
		  HealBot_HoT_Update(unitName, bName)
		end
	  elseif HealBot_Player_HoT_Icons[unitName] and HealBot_Player_HoT_Icons[unitName][bName] and HealBot_Player_HoT_Icons[unitName][bName]>0 then
        HealBot_Player_HoT[unitName][bName]=10
        HealBot_HoT_Update(unitName, bName)
	  end
	elseif HealBot_Player_HoT[unitName] and HealBot_Player_HoT[unitName][bName] then
      HoTActive, expirationTime, bCount=HealBot_HasMyBuff(bName, unit)
      if HoTActive then
        if bName==HEALBOT_LIFEBLOOM then
		  HealBot_Lifebloom_Count[unitName]=bCount
		elseif bName==HEALBOT_EARTH_SHIELD then
		  HealBot_Earth_Shield_Count[unitName]=bCount
		elseif bName==HEALBOT_WATER_SHIELD then
		  HealBot_Water_Shield_Count[unitName]=bCount
		end	
		if HealBot_Player_HoT[unitName][bName]~=expirationTime then
		  HealBot_Player_HoT[unitName][bName]=expirationTime
		  HealBot_HoT_Update(unitName, bName)
		end
	  elseif HealBot_Player_HoT_Icons[unitName][bName]>0 then
        HealBot_Player_HoT[unitName][bName]=10
        HealBot_HoT_Update(unitName, bName)
	  end
	end
  end
end

function HealBot_HasMyBuff(buffName, unit)
  x,_,_,bCount,_,_,expirationTime, isMine,_ = UnitBuff(unit,buffName)
  if x and isMine then
    return true, expirationTime, bCount
  end
  return false;
end

function HealBot_CheckMyBuffs(unitName)
  xUnit=HealBot_UnitID[unitName]
  if not xUnit then return end
  for bName,_ in pairs(HealBot_CheckBuffs) do
    _,_,_,_,_,_,z,isMine,_ = UnitBuff(xUnit,bName)
	if z and isMine then
	  HealBot_SetUnitBuffTimer(unitName,bName,z)
	elseif HealBot_PlayerBuff[unitName] and HealBot_PlayerBuff[unitName][bName] then
      if HealBot_PlayerBuff[unitName][bName]==HealBot_CheckBuffsTime then
        HealBot_PlayerBuff[unitName][bName]=nil
		HealBot_ResetCheckBuffsTime()
	  else
	    HealBot_PlayerBuff[unitName][bName]=nil
	  end
	end
  end
end

function HealBot_CheckAllBuffs(unitName)
  if HealBot_Config.BuffWatch==1 then 
    if unitName then
	  HealBot_DelayBuffCheck[unitName]=true
	else
      for x,_ in pairs(HealBot_DelayBuffCheck) do
        HealBot_DelayBuffCheck[x]=nil;
      end
      for uName,_  in pairs(HealBot_UnitID) do
           HealBot_DelayBuffCheck[uName]=true;
      end
	end
  end
end

function HealBot_ClearAllBuffs(unitName)
  if unitName then
    if HealBot_DelayBuffCheck[unitName] then HealBot_DelayBuffCheck[unitName]=nil end
  else
    for x,_ in pairs(HealBot_DelayBuffCheck) do
      HealBot_DelayBuffCheck[x]=nil;
    end
  end
end

function HealBot_CheckAllDebuffs(unitName)
  if HealBot_Config.DebuffWatch==1 then 
    if unitName then
	  HealBot_DelayDebuffCheck[unitName]=true
	else
      for x,_ in pairs(HealBot_DelayDebuffCheck) do
        HealBot_DelayDebuffCheck[x]=nil;
      end
      for uName,_ in pairs(HealBot_UnitID) do
           HealBot_DelayDebuffCheck[uName]=true;
      end
	end
  end
end

function HealBot_ClearAllDebuffs(unitName)
  if unitName then
    if HealBot_DelayDebuffCheck[unitName] then HealBot_DelayDebuffCheck[unitName]=nil end
  else
    for x,_ in pairs(HealBot_DelayDebuffCheck) do
      HealBot_DelayDebuffCheck[x]=nil;
    end
  end
end

local DebuffIn=nil
local DebuffClass=nil
local myhTargets=nil
local checkWS=nil
local inSpellRange = 0 -- added by Diacono
local iTexture=nil
local udName=nil
local debuffDuration=nil
function HealBot_CheckUnitDebuffs(unit)
  if not unit then return end
  xUnit=HealBot_UnitID[UnitName(unit)]
  if not xUnit or not HealBot_Unit_Button[xUnit] then return end
  udName=UnitName(xUnit);
  _,DebuffClass=UnitClass(xUnit)
  if not DebuffClass then DebuffClass=HealBot_Class_En[HEALBOT_WARRIOR] end
  DebuffType=nil;
  y = 1;
  DebuffIn=HealBot_UnitDebuff[udName] or "x"
  checkWS=nil
  while true do
    dName,_,_,_,debuff_type,debuffDuration,_,_,_ = UnitDebuff(xUnit,y)
    if dName then
      y = y + 1;
	  if dName~=DebuffIn then
        checkthis=false;
        WatchTarget=HealBot_Options_retDebuffWatchTarget(debuff_type);
        if HealBot_Config.HealBot_Custom_Debuffs[dName] then
    		debuff_type = HEALBOT_CUSTOM_en;
    		WatchTarget={["Raid"]=true,};
        end
	  
	    if dName==HEALBOT_DEBUFF_WEAKENED_SOUL then
	      HealBot_TrackWS[udName]=true
		  checkWS=true
	    end
      
        if WatchTarget then 
          if WatchTarget["Raid"] then
            checkthis=true;
          elseif WatchTarget["Party"] and (UnitInParty(xUnit) or udName==HealBot_PlayerName) then
            checkthis=true;
          elseif WatchTarget["Self"] and udName==HealBot_PlayerName then
	        checkthis=true
          elseif WatchTarget[strsub(DebuffClass,1,4)] then
            checkthis=true;
          elseif WatchTarget["PvP"] and UnitIsPVP("player") then
            checkthis=true;
          elseif WatchTarget["MainTanks"] then
            for i=1, #HealBot_MainTanks do
		 		if udName==HealBot_MainTanks[i] then
		 			checkthis=true;
		 			break;
		 		end
		 	end
          elseif WatchTarget["MyTargets"] then
		 	myhTargets=HealBot_GetMyHealTargets();
		 	for i=1, #myhTargets do
		 		if udName==myhTargets[i] then
		 			checkthis=true;
		 			break;
		 		end
		 	end
          end
       
          if checkthis then
            if HealBot_Config.IgnoreMovementDebuffs==1 and HealBot_Ignore_Movement_Debuffs[dName] then
              checkthis=false;
            elseif HealBot_Config.IgnoreFastDurDebuffs==1 and debuffDuration and debuffDuration<=HealBot_Config.IgnoreFastDurDebuffsSecs then
              checkthis=false;
            elseif HealBot_Config.IgnoreNonHarmfulDebuffs==1 and HealBot_Ignore_NonHarmful_Debuffs[dName] then
              checkthis=false;
            elseif HealBot_Config.IgnoreClassDebuffs==1 then
              HealBot_Ignore_Debuffs_Class=HealBot_Ignore_Class_Debuffs[strsub(DebuffClass,1,4)];
              if HealBot_Ignore_Debuffs_Class[dName] then
                checkthis=false;
              end
            end
		  end
		end
	  else
	    checkthis=true
	  end
      if checkthis then
        HealBot_UnitDebuff[udName]=debuff_type
        HealBot_UnitDebuff[udName.."_debuff_name"]=dName
        DebuffType=debuff_type;
        if HealBot_Options_retDebuffPriority()==debuff_type then
          do break end
        end
      end
    else
      if not DebuffType then 
       if HealBot_UnitDebuff[udName] then
	      HealBot_UnitDebuff[udName] = nil;
	      if HealBot_ThrottleCnt>5 then
	        HealBot_Action_ResetUnitStatus(xUnit)
	      else
	        HealBot_RecalcHeals(xUnit)
	    	HealBot_ThrottleCnt=HealBot_ThrottleCnt+1
	      end
		  if HealBot_Player_HoT[udName] and HealBot_Player_HoT[udName][HealBot_UnitDebuff[udName.."_debuff_name"]] then
		    HealBot_Player_HoT[udName][HealBot_UnitDebuff[udName.."_debuff_name"]]=10
			HealBot_HoT_Update(udName, HealBot_UnitDebuff[udName.."_debuff_name"])
		  end
		  HealBot_UnitDebuff[udName.."_debuff_name"] = nil;
		  if HealBot_Config.CDCshownAB==1 then
            HealBot_Action_SetUnitDebuffStatus(udName)
		    if not HealBot_Aggro[xUnit] then HealBot_Aggro[xUnit]=xUnit end
          end
	    end
      end
      do break end
    end 
  end

  if HealBot_UnitDebuff[udName] then
    if HealBot_UnitDebuff[udName]~=DebuffIn then
      inSpellRange = 0
	  if HealBot_Config.CDCshownAB==1 then
        HealBot_Action_SetUnitDebuffStatus(udName,debuffCodes[DebuffType])
	    HealBot_Action_UpdateAggro(unit,"debuff",debuffCodes[DebuffType])
      end
      if (HealBot_UnitDebuff[udName] ~= HEALBOT_CUSTOM_en) and (HealBot_DebuffSpell[HealBot_UnitDebuff[udName]] ~= HEALBOT_STONEFORM) then
      	inSpellRange = HealBot_UnitInRange(HealBot_DebuffSpell[HealBot_UnitDebuff[udName]], xUnit)
      end
      if inSpellRange == 1 then
        if HealBot_Config.ShowDebuffWarning==1 then
          UIErrorsFrame:AddMessage(udName.." suffers from "..HealBot_UnitDebuff[udName.."_debuff_name"], 
                                   HealBot_Config.CDCBarColour[DebuffType].R,
                                   HealBot_Config.CDCBarColour[DebuffType].G,
                                   HealBot_Config.CDCBarColour[DebuffType].B,
                                   1, UIERRORS_HOLD_TIME);
        end
        if HealBot_Config.SoundDebuffWarning==1 then HealBot_PlaySound(HealBot_Config.SoundDebuffPlay); end
	    if HealBot_ThrottleCnt>5 then
	      HealBot_Action_ResetUnitStatus(xUnit)
	    else
	      HealBot_RecalcHeals(xUnit)
		  HealBot_ThrottleCnt=HealBot_ThrottleCnt+1
	    end
	  else
	    HealBot_Action_ResetUnitStatus(xUnit)
      end
	end
	if Healbot_Config_Skins.ShowDebuffIcon[Healbot_Config_Skins.Current_Skin]==1 and UnitIsVisible(xUnit) then
	  _,_,iTexture,bCount,_,_,expirationTime,_,_ = UnitDebuff(xUnit,HealBot_UnitDebuff[udName.."_debuff_name"])
	  if bCount then
        if not HealBot_DeBuff_Texture[HealBot_UnitDebuff[udName.."_debuff_name"]] then
	      HealBot_DeBuff_Texture[HealBot_UnitDebuff[udName.."_debuff_name"]]=iTexture
	    end
        if not HealBot_Player_HoT[udName] then HealBot_Player_HoT[udName]={} end
	    if not HealBot_Player_HoT[udName][HealBot_UnitDebuff[udName.."_debuff_name"]] then HealBot_Player_HoT[udName][HealBot_UnitDebuff[udName.."_debuff_name"]]=GetTime()+31 end
        if not HealBot_Player_HoT_Icons[udName] then HealBot_Player_HoT_Icons[udName]={} end
        if not HealBot_Player_HoT_Icons[udName][HealBot_UnitDebuff[udName.."_debuff_name"]] then HealBot_Player_HoT_Icons[udName][HealBot_UnitDebuff[udName.."_debuff_name"]]=0 end
	    if HealBot_Player_HoT[udName][HealBot_UnitDebuff[udName.."_debuff_name"]]~=expirationTime or DeBuff_Count[udName]~=bCount then
	      DeBuff_Count[udName]=bCount
	      HealBot_Player_HoT[udName][HealBot_UnitDebuff[udName.."_debuff_name"]]=expirationTime
	      HealBot_HoT_Update(udName, HealBot_UnitDebuff[udName.."_debuff_name"])
	    end
	  end
    end
  end
  if HealBot_TrackWS[udName] and not checkWS then
    HealBot_TrackWS[udName]=nil
	HealBot_HasMyBuffs(xUnit,udName)
  end
end

function HealBot_CheckUnitBuffs(unit)
  if not unit or not UnitName(unit) then return end
  xUnit=HealBot_UnitID[UnitName(unit)]
  if not xUnit or not HealBot_Unit_Button[xUnit] then return end
  uName=UnitName(xUnit);
  if not uName then return end
  _,BuffClass=UnitClass(xUnit)
  if not BuffClass then BuffClass=HealBot_Class_En[HEALBOT_WARRIOR] end
  y = 1;

  for x,_ in pairs(PlayerBuffs) do
    PlayerBuffs[x]=nil;
  end

    while true do
	  bName,_,_,_,_,_,w,_ = UnitAura(xUnit,y,"HELPFUL") 
      if bName then
        PlayerBuffs[bName]=true
        y = y + 1;
		if HealBot_CheckBuffs[bName] then
		  if w-GetTime()>0 then
		    HealBot_SetUnitBuffTimer(uName,bName,w)
		  elseif HealBot_PlayerBuff[uName] and HealBot_PlayerBuff[uName][bName] then
		    if HealBot_PlayerBuff[uName][bName]==HealBot_CheckBuffsTime then
			  HealBot_PlayerBuff[uName][bName]=nil
			  HealBot_ResetCheckBuffsTime()
			else
			  HealBot_PlayerBuff[uName][bName]=nil
			end
		    
--			HealBot_ResetCheckBuffsTime()
		  end
		end
      else
        do break end
      end
    end 

  if HealBot_PlayerBuff[uName] then
    PlayerBuffsUName=HealBot_PlayerBuff[uName]
    for z,_ in pairs (PlayerBuffsUName) do
      if not PlayerBuffs[z] then
--	  HealBot_AddDebug("Removed buff="..z)
	    if PlayerBuffsUName[z]==HealBot_CheckBuffsTime then
		  PlayerBuffsUName[z]=nil
		  HealBot_ResetCheckBuffsTime()
		else
		  PlayerBuffsUName[z]=nil
		end
	  end
    end
    for x,_ in pairs(PlayerBuffs) do
      if HealBot_PlayerBuff[uName][x] and HealBot_PlayerBuff[uName][x] < GetTime() then
	    PlayerBuffs[x]=nil
	  end
    end
  end

    HasWeaponBuff=GetWeaponEnchantInfo()
	bName=nil;
    for k in pairs(HealBot_BuffWatch) do
      checkthis=false;
      WatchTarget=HealBot_Options_retBuffWatchTarget(HealBot_BuffWatch[k]);
      if WatchTarget["Raid"] then
        checkthis=true;
      elseif WatchTarget["Party"] and (UnitInParty(xUnit) or uName==HealBot_PlayerName) then
        checkthis=true;
      elseif WatchTarget["Self"] and uName==HealBot_PlayerName then 
	    checkthis=true
	  elseif WatchTarget[strsub(BuffClass,1,4)] then
        checkthis=true
	  elseif WatchTarget["PvP"] and UnitIsPVP("player") then
		checkthis=true;
	  elseif WatchTarget["MainTanks"] then
	 	for i=1, #HealBot_MainTanks do
	 		if uName==HealBot_MainTanks[i] then
	 			checkthis=true;
	 			break;
	 		end
	 	end
       elseif WatchTarget["MyTargets"] then
			myhTargets=HealBot_GetMyHealTargets();
			for i=1, #myhTargets do
				if uName==myhTargets[i] then
					checkthis=true;
					break;
				end
			end
     end
	   if CheckWeaponBuffs[HealBot_BuffWatch[k]] and HasWeaponBuff then checkthis=false end
       if checkthis then
         if not PlayerBuffs[HealBot_BuffWatch[k]] then
           checkthis=false;
           if HealBot_BuffNameSwap[HealBot_BuffWatch[k]] then
             checkthis=HealBot_BuffNameSwap[HealBot_BuffWatch[k]];
             if not PlayerBuffs[checkthis] then
               bName=HealBot_BuffWatch[k];
               do break end
             end
           else
             bName=HealBot_BuffWatch[k];
             do break end
           end
         end
       end
     end
     if bName then
	   if HealBot_UnitBuff[uName] and HealBot_UnitBuff[uName]==bName then
	     return
	   else
         HealBot_UnitBuff[uName]=bName;
	     HealBot_Action_ResetUnitStatus(xUnit)
		 if bName==HEALBOT_INNER_FIRE then HealBot_NeedEquipUpdate=true end
	   end
     elseif HealBot_UnitBuff[uName] then 
	    HealBot_UnitBuff[uName]=nil
	    if HealBot_ThrottleCnt>3 then
	      HealBot_Action_ResetUnitStatus(xUnit)
	    else
		  HealBot_RecalcHeals(xUnit)
		  HealBot_ThrottleCnt=HealBot_ThrottleCnt+1
	    end
     end
     
end

function HealBot_OnEvent_PlayerRegenDisabled(self)
  if not HealBot_Loaded then return end
  if not HealBot_UnitID[HealBot_PlayerName] or HealBot_UnitName["target"] then
    HealBot_RecalcParty(true);
  else
    HealBot_IsFighting = true;
  end
  if HealBot_Config.BuffWatch==1 and HealBot_Config.BuffWatchInCombat==0 then
    for uName,_ in pairs(HealBot_UnitBuff) do
	  HealBot_UnitBuff[uName]=nil
	  if HealBot_UnitID[uName] and HealBot_Unit_Button[HealBot_UnitID[uName]] then
        HealBot_RecalcHeals(HealBot_UnitID[uName])
        HealBot_DelayBuffCheck[uName]=true
      end
    end
	if HealBot_UnitID[HealBot_PlayerName] then HealBot_DelayBuffCheck[HealBot_PlayerName]=true; end
  end
    
  if HealBot_Config.DebuffWatch==1 and HealBot_Config.DebuffWatchInCombat==0 then
    for uName,_ in pairs(HealBot_UnitDebuff) do
	  HealBot_UnitDebuff[uName]=nil
	  if HealBot_UnitID[uName] and HealBot_Unit_Button[HealBot_UnitID[uName]] then
	    HealBot_RecalcHeals(HealBot_UnitID[uName])
        HealBot_DelayDebuffCheck[uName]=true
      end
    end
	if HealBot_UnitID[HealBot_PlayerName] then HealBot_DelayDebuffCheck[HealBot_PlayerName]=true; end
  end
  if HealBot_Config.AutoClose==1 and HealBot_Config.ActionVisible==0 and HealBot_Config.DisableHealBot==0 then HealBot_TogglePanel(HealBot_Action) end
  if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 then HealBot_OnEvent_UnitCombat(self,"player") end
end

function HealBot_OnEvent_PlayerRegenEnabled(self)
  HealBot_IsFighting = nil;
end

unUnit=nil
function HealBot_OnEvent_UnitNameUpdate(self,unit)
  unUnit=HealBot_RaidUnit(unit,UnitName(unit))
  if not HealBot_Unit_Button[unUnit] then return end
  if HealBot_UnitName[unUnit] then
    if HealBot_UnitID[UnitName(unUnit)]~=unit then		
	  HealBot_Action_ResetUnitStatus(HealBot_UnitID[UnitName(unUnit)])
    end
    HealBot_UnitName[unUnit]=UnitName(unUnit)
    HealBot_UnitID[UnitName(unUnit)]=unUnit
    HealBot_CheckAllDebuffs(unUnit)
    HealBot_CheckAllBuffs(unUnit)
	b=HealBot_Unit_Button[unUnit]
	b.name=UnitName(unUnit)
	if HealBot_HoT_Active_Button[UnitName(unUnit)] then
	  if HealBot_HoT_Active_Button[UnitName(unUnit)]~=b then
		HealBot_HoT_MoveIcon(HealBot_HoT_Active_Button[UnitName(unUnit)], b, UnitName(unUnit))
		HealBot_HoT_Active_Button[UnitName(unUnit)]=b
      end
    end	
    HealBot_Action_ResetUnitStatus(unUnit)	
  end
end

function HealBot_RaidUnit(unit,unitName)
  if unitName==HealBot_PlayerName then
    unit="player"
  elseif GetNumRaidMembers()>0 then
	if unit=="party1" or unit=="party2" or unit=="party3" or unit=="party4" or unit=="unknown" then
      if HealBot_UnitID[unitName] and UnitName(HealBot_UnitID[unitName])==unitName then
        unit=HealBot_UnitID[unitName]
	  else
        for j=1,GetNumRaidMembers() do
          xUnit = "raid"..j
	      if UnitName(xUnit)==unitName then
	        unit=xUnit
		    do break end
          end
        end
	  end
    end
  elseif unit=="unknown" and GetNumPartyMembers()>0 then
    for j=1,GetNumPartyMembers() do
      xUnit = "party"..j
	  if UnitName(xUnit)==unitName then
	    unit=xUnit
	    do break end
      end
    end
  end
  return unit
end

function HealBot_IC_PartyMembersChanged()
  z=false
  HealBot_InCombatUpdCnt=HealBot_InCombatUpdCnt+1
  if HealBot_InCombatUpdCnt<15 then
    for xUnit,_ in pairs(HealBot_UpUnitInCombat) do
	  HealBot_UpUnitInCombat[xUnit]=nil
    end
    for xUnit,uName in pairs(HealBot_UnitName) do
	  if not UnitName(xUnit) then 
	    HealBot_Action_ResetUnitStatus(xUnit)
      elseif uName~=UnitName(xUnit) then
	    table.insert(HealBot_UpUnitInCombat,xUnit)
      end
    end
    table.foreach(HealBot_UpUnitInCombat, function (index,xUnit)
      HealBot_OnEvent_UnitNameUpdate(nil,xUnit)
	  z=true
    end)
  end
  if z then
--    HealBot_AddDebug("ReVisit HealBot_IC_PartyMembersChanged")
	HealBot_InCombatUpdCnt=HealBot_InCombatUpdCnt+1
  else
	HealBot_InCombatUpdate=false
	HealBot_InCombatUpdCnt=0
  end
  Delay_RecalcParty=3
end

function HealBot_OnEvent_PartyMembersChanged(self)
  if HealBot_IsFighting then HealBot_InCombatUpdate=true end
  Delay_RecalcParty=3
end

function HealBot_OnEvent_PlayerTargetChanged(self)
  if HealBot_Config.TargetHeals==1 then
    HealBot_RecalcParty();
	if UnitName("target") and not UnitIsEnemy("target", "player") then
	  HealBot_OnEvent_UnitAura(nil,"target")
	end
  end
  if HealBot_UnitSpec[UnitName("target")] then
    if HealBot_UnitSpec[UnitName("target")] == " " then
	  NotifyInspect("target")
	end
  end
end

function HealBot_GetTalentInfo(unitName)
  x,y,z=0,0,0
  if unitName==HealBot_PlayerName then
    _,_,x,_=GetTalentTabInfo(1)
    _,_,y,_=GetTalentTabInfo(2)
    _,_,z,_=GetTalentTabInfo(3)
    uClass=HealBot_PlayerClassEN
  else
    _,_,x = GetTalentTabInfo(1,true)
    _,_,y = GetTalentTabInfo(2,true)
    _,_,z = GetTalentTabInfo(3,true)
    _,uClass=UnitClass("target")
  end
  
  if x+y+z>0 and uClass then
	uClass=strsub(uClass,1,4)
	if ((x > y) and (x > z)) then
		w = 1
	elseif (y > z) then
		w = 2
	else
		w = 3
	end
	s=HealBot_Init_retSpec(uClass,w)
	if s then HealBot_UnitSpec[unitName] = " "..s.." " end
  end
end

local HealBot_PrevComm=0
function HealBot_SetAddonComms()
  if not HealBot_InBG then
    if GetNumRaidMembers()>0 then
      HealBot_AddonMsgType=2;
    elseif GetNumPartyMembers()>0 then
      HealBot_AddonMsgType=3;
    else
      HealBot_AddonMsgType=4;
    end
  else
    HealBot_AddonMsgType=1;
  end
  if HealBot_PrevComm~=HealBot_AddonMsgType then
    HealBot_PrevComm=HealBot_AddonMsgType
    HealBot_Comms_SendAddonMsg("HealBot", ">> RequestVersion <<=>> "..HealBot_PlayerName.." <<=>> nil <<", HealBot_AddonMsgType, HealBot_PlayerName)
  end
end

function HealBot_OnEvent_PartyPetChanged(self)
  if HealBot_Config.PetHeals==1 and Delay_RecalcParty==0 then
    Delay_RecalcParty=3
  end
end

function HealBot_OnEvent_SystemMsg(self,msg)
  if type(msg)=="string" then

    if msg==HB_YOULEAVETHEGROUP or msg==HB_YOULEAVETHERAID or msg==HB_YOUJOINTHERAID or msg==HB_YOUJOINTHEGROUP then
        Delay_RecalcParty=3;
		HealBot_CheckZoneFlag=true
    elseif (string.find(msg, HB_ONLINE)) or (string.find(msg, HB_OFFLINE)) then
		msg = gsub(msg, "|Hplayer:([^%c^%d^%s^%p]+)|h(.+)|h", "%1")
		uName = msg:match("([^%c^%d^%s^%p]+)")
		if HealBot_UnitID[uName] then
			unit=HealBot_UnitID[uName]
			if (string.find(msg, HB_ONLINE)) then
			  HealBot_Action_UnitIsOffline(unit,uName,-1)
			else
			  HealBot_Action_UnitIsOffline(unit,uName,time())
			end
			HealBot_Action_RefreshButtons(unit);
		end
	end
  end
end

function HealBot_OnEvent_PlayerEquipmentChanged(self,unit)
  if UnitName(unit)==HealBot_PlayerName then
    HealBot_NeedEquipUpdate=true
  end
end

function HealBot_OnEvent_SpellsChanged(self, arg1)
  if arg1==0 then return; end
  HealBot_InitSpells()
  HealBot_Buff_Spells_List=HealBot_Options_InitBuffList() -- added by Diacono of Ursin
  HealBot_Options_BuffTxt1_Refresh()
  HealBot_Options_BuffTxt2_Refresh()
  HealBot_Options_BuffTxt3_Refresh()
  HealBot_Options_BuffTxt4_Refresh()
  HealBot_Options_BuffTxt5_Refresh()
  HealBot_Options_BuffTxt6_Refresh()
  HealBot_Options_BuffTxt7_Refresh()
  HealBot_Options_BuffTxt8_Refresh()
  HealBot_Options_BuffTxt9_Refresh()
end

function HealBot_OnEvent_TalentsChanged(self, arg1)
  HealBot_CheckTalents=true
end

function HealBot_OnEvent_PlayerEnteringWorld(self)
  if not HealBot_Loaded then return end
  if HealBot_Config.HidePartyFrames==1 then
    HealBot_Options_DisablePartyFrame()
  end
  if HealBot_Config.HidePlayerTarget==1 then
    HealBot_Options_DisablePlayerFrame()
    HealBot_Options_DisablePetFrame()
    HealBot_Options_DisableTargetFrame()
  end
  HealBot_Register_Events()
  HealBot_CheckZoneFlag=true
end

function HealBot_OnEvent_PlayerLeavingWorld(self)
  if HealBot_Config.HidePartyFrames==1 then
    HealBot_Options_EnablePartyFrame()
  end
  if HealBot_Config.HidePlayerTarget==1 then
    HealBot_Options_EnablePlayerFrame()
    HealBot_Options_EnablePetFrame()
    HealBot_Options_EnableTargetFrame()
  end
  HealBot_UnRegister_Events();
  HB_Timer1=5
end

local uscSpell,uscName=nil,nil
function HealBot_OnEvent_UnitSpellcastSent(self,caster,spellName,spellRank,unitName)
  if HealBot_InBG then 
    uscName = string.match(unitName, "^[^-]*"); 
  else
    uscName = unitName
  end
  if uscName=="" then
    if spellName==HEALBOT_PRAYER_OF_HEALING or HEALBOT_INNER_FIRE or spellName==HEALBOT_WILD_GROWTH or spellName==HEALBOT_TRANQUILITY then 
	  uscName=HealBot_PlayerName 
	  if spellName==HEALBOT_INNER_FIRE then HealBot_NeedEquipUpdate=true end
	elseif spellName==HEALBOT_MENDPET then
	  uscName=UnitName("pet")
	end
  end
  if caster=="player" and uscName == HEALBOT_WORDS_UNKNOWN then
    uscName = HealBot_GetCorpseName(uscName)
  end
  xUnit = HealBot_UnitID[uscName];
  if not xUnit then
    if UnitName("target") and UnitName("target")==uscName then
      xUnit="target"
    end
  end
  if caster=="player" and xUnit then
    uscSpell=spellName.."("..spellRank..")"
    HealBot_ClearIncHeal();
    if spellName==HEALBOT_RESURRECTION or spellName==HEALBOT_ANCESTRALSPIRIT or spellName==HEALBOT_REBIRTH or spellName==HEALBOT_REDEMPTION or spellName==HEALBOT_REVIVE then
      HealBot_IamRessing = uscName;
      HealBot_IsCasting = true;
      if HealBot_IamRessing then
        HealBot_Comms_SendAddonMsg("CTRA", "RES "..HealBot_IamRessing, HealBot_AddonMsgType, HealBot_PlayerName)
      end
      HealBot_CastingSpell  = uscSpell;
      HealBot_CastingTarget = xUnit;
	  if HealBot_Config.CastNotify>1 and HealBot_Config.CastNotifyResOnly==1 then
	    HealBot_CastNotify(uscSpell,uscName,spellName)
	  end
    elseif HealBot_Spells[uscSpell] then
      HealBot_IsCasting = true;
      if HealBot_Spells[uscSpell].CastTime>1 then
        HealBot_HealValue=HealBot_Spells[uscSpell].HealsDur;
        if spellName==HEALBOT_BINDING_HEAL then
          HealBot_AddIamHealingList(HealBot_PlayerName)
          HealBot_AddIamHealingList(uscName);
        elseif spellName==HEALBOT_PRAYER_OF_HEALING then
          HealBot_AddIamHealingList(HealBot_PlayerName)
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "party1")==1 then
            HealBot_AddIamHealingList(UnitName("party1"));
          end
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "party2")==1 then
            HealBot_AddIamHealingList(UnitName("party2"));
          end
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "party3")==1 then
            HealBot_AddIamHealingList(UnitName("party3"));
          end
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "party4")==1 then 
            HealBot_AddIamHealingList(UnitName("party4"));
          end
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "partypet1")==1 then
            HealBot_AddIamHealingList(UnitName("partypet1"));
          end
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "partypet2")==1 then
            HealBot_AddIamHealingList(UnitName("partypet2"));
          end
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "partypet3")==1 then
            HealBot_AddIamHealingList(UnitName("partypet3"));
          end
          if HealBot_UnitInRange(HEALBOT_POWER_WORD_FORTITUDE, "partypet4")==1 then
            HealBot_AddIamHealingList(UnitName("partypet4"));
          end
        elseif uscName then
          HealBot_AddIamHealingList(uscName)
        end
		HealBot_OnEvent_AddonMsg(nil,HEALBOT_ADDON_ID, ">> "..HealBot_HealValue.." <<=>> "..HealBot_IamHealing, nil, HealBot_PlayerName, true)
		HealBot_Comms_SendAddonMsg(HEALBOT_ADDON_ID, ">> "..HealBot_HealValue.." <<=>> "..HealBot_IamHealing, HealBot_AddonMsgType, HealBot_PlayerName)
		HealBot_Timer3=0
		HealBot_StopCastTimer=2
      end
	  if not HealBot_Spells[uscSpell] or not HealBot_Spells[uscSpell].Duration then HealBot_AddDebug("Not HealBot_Spells[uscSpell].?  uscSpell="..uscSpell) end
      HealBot_CastingSpell  = uscSpell;
      HealBot_CastingTarget = xUnit;
	  
      if HealBot_Spells[uscSpell].Duration and HealBot_Spells[uscSpell].Duration>0 and Healbot_Config_Skins.ShowHoTicons[Healbot_Config_Skins.Current_Skin]==1 and HealBot_Unit_Button[HealBot_UnitID[uscName]] then
	    if spellName==HEALBOT_FLASH_OF_LIGHT then spellName=HEALBOT_GLYPH_OF_FLASH_OF_LIGHT end

        if not HealBot_Player_HoT[uscName] then HealBot_Player_HoT[uscName]={} end
		if not HealBot_Player_HoT_Icons[uscName] then HealBot_Player_HoT_Icons[uscName]={} end
		HealBot_Player_HoT[uscName][spellName]=9
		if not HealBot_Player_HoT_Icons[uscName][spellName] then HealBot_Player_HoT_Icons[uscName][spellName]=0 end
        
        if spellName==HEALBOT_PRAYER_OF_MENDING then
		  HealBot_HoT_MendingWith="niL"
        elseif spellName==HEALBOT_LIFEBLOOM then
		  if not HealBot_Lifebloom_Count[uscName] then HealBot_Lifebloom_Count[uscName]=0 end
		elseif spellName==HEALBOT_EARTH_SHIELD then
		  if not HealBot_Earth_Shield_Count[uscName] then HealBot_Earth_Shield_Count[uscName]=0 end
		elseif spellName==HEALBOT_WATER_SHIELD then
		  if not HealBot_Water_Shield_Count[uscName] then HealBot_Water_Shield_Count[uscName]=0 end
		elseif spellName==HEALBOT_REGROWTH then
		  if HealBot_Player_HoT[uscName][spellName]<17 then HealBot_Player_HoT[uscName][spellName]=17 end
		elseif spellName==HEALBOT_GLYPH_OF_FLASH_OF_LIGHT then
		  if HealBot_Player_HoT[uscName][spellName]<15 then HealBot_Player_HoT[uscName][spellName]=15 end
        elseif spellName==HEALBOT_WILD_GROWTH or spellName==HEALBOT_TRANQUILITY then
		  HealBot_Watch_AM_HoT=3
		end
      end
	elseif spellName==HEALBOT_SACRED_SHIELD or spellName==HEALBOT_BEACON_OF_LIGHT then
	  HealBot_Watch_Timed_HoT[uscName]=31
	elseif spellName==HEALBOT_ABOLISH_DISEASE or spellName==HEALBOT_ABOLISH_POISON or spellName==HEALBOT_EARTHLIVING_WEAPON or spellName==HEALBOT_EARTH_SHIELD then
	  if not HealBot_Player_HoT[uscName] then HealBot_Player_HoT[uscName]={} end
	  if not HealBot_Player_HoT_Icons[uscName] then HealBot_Player_HoT_Icons[uscName]={} end
	  HealBot_Player_HoT[uscName][spellName]=9
	  if not HealBot_Player_HoT_Icons[uscName][spellName] then HealBot_Player_HoT_Icons[uscName][spellName]=0 end
    end
	if HealBot_Spells[uscSpell] or HealBot_OtherSpells[uscSpell] then
      if HealBot_Config.CastNotify>1 and HealBot_Config.CastNotifyResOnly==0 then
   		if spellName==HEALBOT_GLYPH_OF_FLASH_OF_LIGHT then spellName=HEALBOT_FLASH_OF_LIGHT end
        HealBot_CastNotify(uscSpell,uscName,spellName)
      end
	end
  end
end

function HealBot_GetCorpseName(cName)
  x = getglobal("GameTooltipTextLeft1"):GetText();
  if (x) then
    cName = string.gsub(x, HEALBOT_TOOLTIP_CORPSE, "")
  end
  return cName
end


function HealBot_ResetCheckBuffsTime()
  HealBot_CheckBuffsTime=GetTime()+10000200
  for z,_ in pairs(HealBot_PlayerBuff) do
    if HealBot_PlayerBuff[z] then
      PlayerBuffsUName=HealBot_PlayerBuff[z]
	  for y,_ in pairs (PlayerBuffsUName) do
	    if PlayerBuffsUName[y]<0 then
		  PlayerBuffsUName[y]=nil
	    elseif PlayerBuffsUName[y] < HealBot_CheckBuffsTime then
	      HealBot_CheckBuffsTime=PlayerBuffsUName[y]
		  HealBot_CheckBuffsTimeuName=z
        end
	  end
	end
  end
  if HealBot_CheckBuffsTime>GetTime()+10000000 then
    HealBot_CheckBuffsTime=nil
	HealBot_CheckBuffsTimeuName=nil
  end
end

function HealBot_AddIamHealingList(uxName)
  if (type(uxName) == "table") then
    HealBot_AddDebug("HealBot_AddIamHealingList has table for uxName")
  end  
  if HealBot_IamHealing then
    HealBot_IamHealing=HealBot_IamHealing..":"..uxName
  else
    HealBot_IamHealing=uxName
  end
end

function HealBot_HealsInUpdate(unitName,healVal)
  if not HealBot_HealsIn[unitName] then HealBot_HealsIn[unitName]=0 end
  HealBot_HealsIn[unitName] = HealBot_HealsIn[unitName] + tonumber(healVal)
  if HealBot_HealsIn[unitName]<0 then HealBot_HealsIn[unitName]=0 end
  if HealBot_Unit_Button[HealBot_UnitID[unitName]] then
    if HealBot_HealsIn[unitName]>0 then
	  HealBot_RecalcHeals(HealBot_UnitID[unitName])
	else
	  HealBot_HealsIn[unitName]=nil
	  if HealBot_Config.EnLibQuickHealth==1 then
	    HealBot_Action_ResetUnitStatus(HealBot_UnitID[unitName])
	  else
        HealBot_IncomingHealEnd[unitName]=true
	  end
	end
  end
end

function HealBot_CastNotify(spellrank,unitName,spell)
  z = HealBot_Config.CastNotify;
  if HealBot_Spells[spellrank] then
    if HealBot_Spells[spellrank].HealsDur>0 then
	  s = gsub(HealBot_Config.NotifyHealMsg,"#s",spell)
  	  s = gsub(s,"#n",unitName)
	  s = gsub(s,"#h",HealBot_Spells[spellrank].HealsDur)
	else
	  s = gsub(HealBot_Config.NotifyOtherMsg,"#s",spell)
  	  s = gsub(s,"#n",unitName)
	end
  elseif HealBot_OtherSpells[spellrank] then
    if HealBot_OtherSpells[spellrank].HealsDur and HealBot_OtherSpells[spellrank].HealsDur>0 then
	  s = gsub(HealBot_Config.NotifyHealMsg,"#s",spell)
  	  s = gsub(s,"#n",unitName)
	  s = gsub(s,"#h",HealBot_Spells[spellrank].HealsDur)
	else
	  s = gsub(HealBot_Config.NotifyOtherMsg,"#s",spell)
  	  s = gsub(s,"#n",unitName)
	end
  else
	s = gsub(HealBot_Config.NotifyOtherMsg,"#s",spell)
  	s = gsub(s,"#n",unitName)
  end
  w=nil;
    if z==6 then
      w=HealBot_Comms_GetChan(HealBot_Config.NotifyChan) 
      if not w then z=2 end
    end
    if z==5 and GetNumRaidMembers()==0 then z = 4 end
    if z==4 and GetNumPartyMembers()==0 then z = 2 end
    if z==3 and not (UnitPlayerControlled(HealBot_CastingTarget) and HealBot_CastingTarget~='player' and HealBot_CastingTarget~='pet') then z = 2 end
    if z==3 then
	  s = gsub(s,unitName,HEALBOT_WORDS_YOU)
      SendChatMessage(s,"WHISPER",nil,unitName);
    elseif z==4 then
      SendChatMessage(s,"PARTY",nil,nil);
    elseif z==5 then
      SendChatMessage(s,"RAID",nil,nil);
    elseif z==6 then
      SendChatMessage(s,"CHANNEL",nil,w);
    else
      HealBot_AddChat(s);
    end
end

function HealBot_Titan_Check()
  if HealBot_HealValue and HealBot_CastingTarget then
    return UnitName(HealBot_CastingTarget), HealBot_HealValue
  else
    return nil
  end
end

function HealBot_OnEvent_UnitSpellcastStart(self,unit)
  if not HealBot_UnitName[unit] then return end
  if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 then HealBot_OnEvent_UnitCombat(self,unit) end
end

function HealBot_OnEvent_UnitSpellcastStop(self,unit)
  if UnitName(unit)==HealBot_PlayerName then
    if HealBot_IsCasting and HealBot_StopCastTimer==0 then
      HealBot_IsCasting = false;
      HealBot_StopCasting();
--      HealBot_RecalcHeals();
      if HealBot_IamRessing then
        HealBot_Comms_SendAddonMsg("CTRA", "RESNO", HealBot_AddonMsgType, HealBot_PlayerName)
        HealBot_IamRessing=nil;
      end
	end
  end
  if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 then HealBot_OnEvent_UnitCombat(self,unit) end
end

function HealBot_OnEvent_UnitSpellcastFail(self,unit)
  uscsuName=UnitName(unit)
  if uscsuName==HealBot_PlayerName then
    if HealBot_IsCasting then
      HealBot_IsCasting = false;
      HealBot_StopCasting();
      if HealBot_IamRessing then
        HealBot_Comms_SendAddonMsg("CTRA", "RESNO", HealBot_AddonMsgType, HealBot_PlayerName)
        HealBot_IamRessing=nil;
      end
	end
  end
  if Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Current_Skin]==1 then HealBot_OnEvent_UnitCombat(self,unit) end
end

local HoTtxt2=nil
function HealBot_HoT_Update(unitName, hotName)
  huUnit=HealBot_UnitID[unitName]
  huHoTtime=HealBot_Player_HoT[unitName]
  huHoTicon=HealBot_Player_HoT_Icons[unitName]
  
  if not huHoTtime[hotName] then
    HealBot_AddDebug("huHoTtime[hotName]==Nil")
	HealBot_AddDebug("unitName="..unitName.."  hotName="..hotName)
	return
  end

  if hotName==HEALBOT_PRAYER_OF_MENDING then
    if HealBot_HoT_MendingWith~=unitName then
	  HoTtxt2=nil
	else
	  HoTtxt2=HealBot_HoT_Mending
	end
  elseif hotName==HEALBOT_LIFEBLOOM then
    HoTtxt2=HealBot_Lifebloom_Count[unitName]
  elseif hotName==HEALBOT_EARTH_SHIELD then
    HoTtxt2=HealBot_Earth_Shield_Count[unitName]
  elseif hotName==HEALBOT_WATER_SHIELD then
    HoTtxt2=HealBot_Water_Shield_Count[unitName]
  elseif hotName==HEALBOT_POWER_WORD_SHIELD and HealBot_TrackWS[unitName] then
    HoTtxt2="+"
  elseif HealBot_DeBuff_Texture[hotName] then
    if DeBuff_Count[unitName]>0 then
      HoTtxt2=DeBuff_Count[unitName]
	else
	  HoTtxt2=nil
	end
  else
    HoTtxt2=nil
  end
  iconTexture=HealBot_Icon_Texture(hotName)
  secLeft=floor(huHoTtime[hotName]-GetTime())

  if huHoTicon[hotName]>0 then
    if secLeft<0 then
	  HealBot_HoT_UpdateIcon(HealBot_Unit_Button[huUnit], huHoTicon[hotName], 0)
	  if HealBot_DeBuff_Texture[hotName] then
	    huHoTtime[hotName]=nil
	  else
        huHoTtime[hotName]=9
	  end
  	  huHoTicon[hotName]=0
	  HealBot_HoT_RefreshIcons(unitName,HealBot_Unit_Button[huUnit])
	  if hotName==HEALBOT_LIFEBLOOM then
	    HealBot_Lifebloom_Count[unitName]=nil
 	  elseif hotName==HEALBOT_EARTH_SHIELD then
  	    HealBot_Earth_Shield_Count[unitName]=nil
 	  elseif hotName==HEALBOT_WATER_SHIELD then
  	    HealBot_Water_Shield_Count[unitName]=nil
  	  end
	else
	  HealBot_HoT_UpdateIcon(HealBot_Unit_Button[huUnit], huHoTicon[hotName], HealBot_HoT_AlphaValue(secLeft+1),iconTexture,secLeft, HoTtxt2)
	end
  else
    i=0
	for k,z in pairs(huHoTicon) do 
	  if z>0 then 
		if HealBot_DeBuff_Texture[k] then
		  if not HealBot_DeBuff_Texture[hotName] then
		    huHoTicon[k]=i+2
		    HealBot_HoT_UpdateIcon(HealBot_Unit_Button[huUnit], i+2, HealBot_HoT_AlphaValue(secLeft),HealBot_DeBuff_Texture[k], secLeft, HoTtxt2)
		  end
		else
		  i=i+1
		end
	  end 
	end	
	i=i+1
	if i>5 then i=5 end -- Crazy I know - tbr
	huHoTicon[hotName]=i
	HealBot_HoT_UpdateIcon(HealBot_Unit_Button[huUnit], i, HealBot_HoT_AlphaValue(secLeft),iconTexture, secLeft, HoTtxt2)
	HealBot_HoT_Active_Button[unitName]=HealBot_Unit_Button[huUnit]
  end
end

local temp_icons={}
function HealBot_HoT_RefreshIcons(unitName,button)
  huUnit=HealBot_UnitID[unitName]
  huHoTtime=HealBot_Player_HoT[unitName]
  huHoTicon=HealBot_Player_HoT_Icons[unitName]
  z=0
  for k,i in pairs(huHoTicon) do
    if i>0 then 
	  temp_icons[i]=k
	  z=z+1
	end
  end
  if z>0 then
    for i=1,4 do
      if not temp_icons[i] then
	    if temp_icons[i+1] then
		  HealBot_HoT_UpdateIcon(button, i+1, 0)
	      huHoTicon[temp_icons[i+1]]=i
		  secLeft=floor(huHoTtime[temp_icons[i+1]]-GetTime())
		  iconTexture=HealBot_Icon_Texture(temp_icons[i+1])
		  HealBot_HoT_UpdateIcon(button, i, HealBot_HoT_AlphaValue(secLeft), iconTexture, secLeft)
	--	HealBot_HoT_Update(unitName, temp_icons[i+1])
	      huHoTicon[temp_icons[i+1]]=i
		  temp_icons[i+1]=nil
	    end
	  end
      temp_icons[i]=nil
    end
  else
    HealBot_HoT_Active_Button[unitName]=nil
  end
end

function HealBot_HoT_MoveIcon(oldButton, newButton, unitName)
  huUnit=HealBot_UnitID[unitName]
  huHoTtime=HealBot_Player_HoT[unitName]
  huHoTicon=HealBot_Player_HoT_Icons[unitName]
  if not huHoTicon then 
    HealBot_AddDebug("huHoTicon is NIL in HealBot_HoT_MoveIcon")
	return 
  end
  for k,i in pairs(huHoTicon) do
    if i>0 then
      HealBot_HoT_UpdateIcon(oldButton, huHoTicon[k], 0)
	  secLeft=floor(huHoTtime[k]-GetTime())
	  iconTexture=HealBot_Icon_Texture(k)
	  HealBot_HoT_UpdateIcon(newButton, i, HealBot_HoT_AlphaValue(secLeft), iconTexture, secLeft)
	end
  end
end

function HealBot_HoT_RemoveIcon(unitName)
  xButton=HealBot_HoT_Active_Button[unitName];
  huHoTicon=HealBot_Player_HoT_Icons[unitName]
  if not huHoTicon then 
    HealBot_AddDebug("huHoTicon is NIL in HealBot_HoT_RemoveIcon")
	return 
  end
  for _,i in pairs(huHoTicon) do
    if i>0 then
      HealBot_HoT_UpdateIcon(xButton, i, 0)
	end
  end
  HealBot_HoT_Active_Button[unitName]=nil;
end

function HealBot_HoT_RemoveIconButton(button)
  HealBot_HoT_UpdateIcon(button, 1, 0)
  HealBot_HoT_UpdateIcon(button, 2, 0)
  HealBot_HoT_UpdateIcon(button, 3, 0)
  HealBot_HoT_UpdateIcon(button, 4, 0)
end

local Texture=nil
local hbiconcount=nil
local hbiconcount2=nil

function HealBot_HoT_UpdateIcon(button, index, a, Texture, txt1, txt2)
  if not button then return; end;
  if not txt1 or Healbot_Config_Skins.ShowIconTextDuration[Healbot_Config_Skins.Current_Skin]==0 then txt1=10 end;
  bar = HealBot_Action_HealthBar(button);
  iconName = getglobal(bar:GetName().."Icon"..index);
  if not iconName then
    HealBot_AddDebug("iconName is nil index="..index)
	return
  end
  iconName:SetAlpha(a);
  if Texture then iconName:SetTexture(Texture); end
  hbiconcount = getglobal(bar:GetName().."Count"..index);
  hbiconcount2 = getglobal(bar:GetName().."Count"..index.."a");
  hbiconcount:SetText(txt1);
  if txt2 and Healbot_Config_Skins.ShowIconTextCount[Healbot_Config_Skins.Current_Skin]==1 then
	hbiconcount2:SetText(txt2);
	hbiconcount2:SetTextColor(1,1,1,1);
  else
    hbiconcount2:SetText(" ");
	hbiconcount2:SetTextColor(1,1,1,0);
  end
  if txt1<0 or txt1>9 then
    hbiconcount:SetTextColor(1,1,1,0);
  elseif txt1<4 then
    if (Texture==HealBot_HoT_Texture[HEALBOT_REJUVENATION] or Texture==HealBot_HoT_Texture[HEALBOT_REGROWTH]) then
      y, x, _ = GetSpellCooldown(HEALBOT_SWIFTMEND);
      if x and y and (x+y)==0 then
         hbiconcount:SetTextColor(0,1,0,1);
      else
         hbiconcount:SetTextColor(1,0,0,1);        
      end
    else
      hbiconcount:SetTextColor(1,0,0,1);           
    end
  else
    hbiconcount:SetTextColor(1,1,1,1);
  end    
end 

function HealBot_HoT_AlphaValue(secLeft)
  if secLeft>0 and secLeft<6 then
    return (secLeft/8)+.25
  else
    return 1
  end
end

function HealBot_Icon_Texture(spellName)
  if HealBot_HoT_Texture[spellName] then
    return HealBot_HoT_Texture[spellName]
  elseif HealBot_DeBuff_Texture[spellName] then
    return HealBot_DeBuff_Texture[spellName]
  else
    return nil
  end
end

function HealBot_PlaySound(id)
  if HealBot_Loaded then
    PlaySoundFile(LSM:Fetch('sound',id));
  end
end

function HealBot_InitSpells()
  
  for x in pairs(HealBot_CurrentSpells) do
    HealBot_CurrentSpells[x]=nil;
  end

  for x,_ in pairs(HealBot_SmartCast_Spells) do
    HealBot_SmartCast_Spells[x]=nil;
  end
  
  HealBot_Init_Spells_Defaults(HealBot_PlayerClassEN);
  
  for x in pairs(HealBot_Spells) do
    if not HealBot_Spells[x].HealsExt then
      HealBot_Spells[x].HealsExt=0;
    end
    if not HealBot_Spells[x].RealHealing then
      HealBot_Spells[x].RealHealing=0;
    end
    if not HealBot_Spells[x].HealsDur then
      if HealBot_Spells[x].HealsMax then
        HealBot_Spells[x].HealsDur=HealBot_Spells[x].HealsMax;
      else
        HealBot_Spells[x].HealsDur=0;
      end
    end
	HealBot_InitClearSpellNils(x)
  end

  id = 1
  z = 0;  
  local lsName,lsRank,iSpell = nil,nil,nil
  while true do
    lsName, lsRank = HealBot_GetSpellName(id);
    iSpell=nil;
    if not lsName then
      do break end
    end
    if lsRank then
      iSpell=lsName .. "(" .. lsRank .. ")";
    end
    if (HealBot_Spells[iSpell]) then
      HealBot_Spells[iSpell].id = id;
      HealBot_InitGetSpellData(iSpell, id, HealBot_PlayerClassEN, lsName);
      table.insert(HealBot_CurrentSpells,iSpell);
      z = z + 1;
    end
    id = id + 1;
  end
  if strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PRIEST] then
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_LESSER_HEAL))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_LESSER_HEAL]=sName .. "(" .. sRank .. ")"
	elseif sName then
      HealBot_SmartCast_Spells[HEALBOT_LESSER_HEAL]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_HEAL))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_HEAL]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_HEAL]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_GREATER_HEAL))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_GREATER_HEAL]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_GREATER_HEAL]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_RENEW))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_RENEW]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_RENEW]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_FLASH_HEAL))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_FLASH_HEAL]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_FLASH_HEAL]=sName
	end
  elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_DRUID] then
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_REGROWTH))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_REGROWTH]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_REGROWTH]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_HEALING_TOUCH))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_HEALING_TOUCH]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_HEALING_TOUCH]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_REJUVENATION))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_REJUVENATION]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_REJUVENATION]=sName
	end
  elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_PALADIN] then
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_HOLY_LIGHT))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_HOLY_LIGHT]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_HOLY_LIGHT]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_FLASH_OF_LIGHT))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_FLASH_OF_LIGHT]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_FLASH_OF_LIGHT]=sName
	end
  elseif strsub(HealBot_PlayerClassEN,1,4)==HealBot_Class_En[HEALBOT_SHAMAN] then
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_HEALING_WAVE))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_HEALING_WAVE]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_HEALING_WAVE]=sName
	end
	sName,sRank= HealBot_GetSpellName(HealBot_GetSpellId(HEALBOT_LESSER_HEALING_WAVE))
	if sName and sRank then
	  HealBot_SmartCast_Spells[HEALBOT_LESSER_HEALING_WAVE]=sName .. "(" .. sRank .. ")"
	elseif sName then
	  HealBot_SmartCast_Spells[HEALBOT_LESSER_HEALING_WAVE]=sName
	end
  end
  HealBot_Action_SetrSpell()
  HealBot_AddDebug("Initiated HealBot_CurrentSpells with ".. z .." Spells");
  HealBot_Options_CheckCombos();
  HealBot_Init_SmartCast();
end

function HealBot_InitNewChar(PlayerClassEN)
  if strsub(PlayerClassEN,1,4)=="DRUI" then
    HealBot_Config.EnabledKeyCombo = {
      ["Left"] = HEALBOT_REGROWTH,
      ["ShiftLeft"] = HEALBOT_DISABLED_TARGET,
      ["AltLeft"] =  HEALBOT_ABOLISH_POISON,
      ["Right"] = HEALBOT_HEALING_TOUCH,
      ["ShiftRight"] = HEALBOT_ASSIST,
      ["AltRight"] =  HEALBOT_REMOVE_CURSE,
      ["Middle"] = HEALBOT_REJUVENATION,
      ["ShiftMiddle"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
      ["AltMiddle"] = HEALBOT_REBIRTH,
      ["Button4"] = HEALBOT_MARK_OF_THE_WILD,
      ["Button5"] = HEALBOT_THORNS,
	                                 }
	HealBot_Config.DisabledKeyCombo = {
      ["Left"] = HEALBOT_DISABLED_TARGET,
      ["ShiftLeft"] = HEALBOT_MARK_OF_THE_WILD,
      ["CtrlLeft"] = HEALBOT_THORNS,
      ["Right"] = HEALBOT_ASSIST,
      ["Middle"] = HEALBOT_REJUVENATION,
	                                 }
  elseif strsub(PlayerClassEN,1,4)=="PALA" then
    HealBot_Config.EnabledKeyCombo = {
      ["Left"] = HEALBOT_FLASH_OF_LIGHT,
      ["ShiftLeft"] = HEALBOT_DISABLED_TARGET,
      ["AltLeft"] =  HEALBOT_CLEANSE,
      ["Right"] = HEALBOT_HOLY_LIGHT,
      ["ShiftRight"] = HEALBOT_ASSIST,
      ["Middle"] =  HEALBOT_HAND_OF_SALVATION,
      ["ShiftMiddle"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
	                                 }
	HealBot_Config.DisabledKeyCombo = {
      ["Left"] = HEALBOT_DISABLED_TARGET,
      ["Right"] = HEALBOT_ASSIST,
      ["Middle"] =  HEALBOT_HAND_OF_SALVATION,
	                                 }
  elseif strsub(PlayerClassEN,1,4)=="PRIE" then
    HealBot_Config.EnabledKeyCombo = {
      ["Left"] = HEALBOT_FLASH_HEAL,
      ["ShiftLeft"] = HEALBOT_BINDING_HEAL,
      ["CtrlLeft"] = HEALBOT_DISABLED_TARGET,
      ["AltLeft"] =  HEALBOT_ABOLISH_DISEASE,
      ["Right"] = HEALBOT_GREATER_HEAL,
      ["ShiftRight"] = HEALBOT_POWER_WORD_SHIELD,
      ["CtrlRight"] = HEALBOT_ASSIST,
      ["AltRight"] =  HEALBOT_DISPEL_MAGIC,
      ["Middle"] = HEALBOT_RENEW,
      ["ShiftMiddle"] = HEALBOT_PRAYER_OF_MENDING,
      ["CtrlMiddle"] = HEALBOT_POWER_WORD_SHIELD,
      ["AltMiddle"] = HEALBOT_PRAYER_OF_HEALING,
      ["Button4"] = HEALBOT_POWER_WORD_FORTITUDE,
      ["ShiftButton4"] = HEALBOT_PRAYER_OF_FORTITUDE,
      ["Button5"] = HEALBOT_DIVINE_SPIRIT,
      ["ShiftButton5"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
	                                 }
	HealBot_Config.DisabledKeyCombo = {
      ["Left"] = HEALBOT_DISABLED_TARGET,
      ["ShiftLeft"] = HEALBOT_ASSIST,
      ["AltLeft"] = HEALBOT_RESURRECTION,
      ["Right"] = HEALBOT_POWER_WORD_SHIELD,
      ["Middle"] = HEALBOT_RENEW,
      ["Button4"] = HEALBOT_POWER_WORD_FORTITUDE,
      ["Button5"] = HEALBOT_DIVINE_SPIRIT,
	                                 }
  elseif strsub(PlayerClassEN,1,4)=="SHAM" then
    HealBot_Config.EnabledKeyCombo = {
      ["Left"] = HEALBOT_LESSER_HEALING_WAVE,
      ["ShiftLeft"] = HEALBOT_DISABLED_TARGET,
      ["AltLeft"] =  HEALBOT_CURE_POISON,
      ["Right"] = HEALBOT_HEALING_WAVE,
      ["ShiftRight"] = HEALBOT_ASSIST,
      ["AltRight"] =  HEALBOT_CURE_DISEASE,
      ["Middle"] = HEALBOT_CHAIN_HEAL,
      ["ShiftMiddle"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
	  ["AltMiddle"] = HEALBOT_CLEANSE_SPIRIT,
	  ["Button4"] = HEALBOT_EARTH_SHIELD,
	                                 }
	HealBot_Config.DisabledKeyCombo = {
      ["Left"] = HEALBOT_DISABLED_TARGET,
      ["ShiftLeft"] = HEALBOT_ASSIST,
      ["Right"] = HEALBOT_ANCESTRALSPIRIT,
	                                 }
  elseif strsub(PlayerClassEN,1,4)=="MAGE" then
    HealBot_Config.EnabledKeyCombo = {
      ["Left"] = HEALBOT_REMOVE_LESSER_CURSE,
      ["CtrlLeft"] = HEALBOT_DISABLED_TARGET,
      ["ShiftLeft"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
      ["Right"] = HEALBOT_ASSIST,
	                                 }
	HealBot_Config.DisabledKeyCombo = {
      ["Left"] = HEALBOT_REMOVE_LESSER_CURSE,
      ["CtrlLeft"] = HEALBOT_DISABLED_TARGET,
      ["ShiftLeft"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
      ["Right"] = HEALBOT_ASSIST,
	                                 }
  else
    HealBot_Config.EnabledKeyCombo = {
      ["Left"] = HEALBOT_DISABLED_TARGET,
      ["ShiftLeft"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
      ["Right"] = HEALBOT_ASSIST,
	                                 }
	HealBot_Config.DisabledKeyCombo = {
      ["Left"] = HEALBOT_DISABLED_TARGET,
      ["ShiftLeft"] = HEALBOT_HEAVY_RUNECLOTH_BANDAGE,
      ["Right"] = HEALBOT_ASSIST,
	                                 }
  end
  HealBot_Config.HealBotDebuffText = {[1]="",[2]="",[3]=""}
  HealBot_Config.HealBotBuffText = {[1]="",[2]="",[3]="",[4]="",[5]="",[6]="",[7]="",[8]="",[9]=""}
  HealBot_Config.HealBotBuffDropDown = {[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,[7]=1,[8]=1,[9]=1}
  HealBot_Config.HealBotDebuffDropDown = {[1]=1,[2]=1,[3]=1}
  HealBot_Config.HealBotBuffColR = {[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,[7]=1,[8]=1,[9]=1}
  HealBot_Config.HealBotBuffColG = {[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,[7]=1,[8]=1,[9]=1}
  HealBot_Config.HealBotBuffColB = {[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,[7]=1,[8]=1,[9]=1}
end

function HealBot_ToggleOptions()
  HealBot_TogglePanel(HealBot_Options)
end

function HealBot_HasTalent(tab,icon,talentName)
  s,_,_,_,z,_ = GetTalentInfo(tab,icon);
  if s then
    if s~=talentName then
--      HealBot_Report_Error( "================================" );
--      HealBot_Report_Error( "ERROR: in function HealBot_HasTalent" );
--      HealBot_Report_Error( "found talent "..s.." when expecting "..talentName );
	  HealBot_AddDebug("ERROR: in function HealBot_HasTalent");
	  HealBot_AddDebug("found talent "..s.." when expecting "..talentName);
	  z=0
	end
  else
    z=0
  end
  return z
end

function HealBot_MMButton_UpdatePosition()
	HealBot_ButtonFrame:SetPoint(
		"TOPLEFT",
		"Minimap",
		"TOPLEFT",
		54 - (HealBot_Config.HealBot_ButtonRadius * cos(HealBot_Config.HealBot_ButtonPosition)),
		(HealBot_Config.HealBot_ButtonRadius * sin(HealBot_Config.HealBot_ButtonPosition)) - 55
	);
end

function HealBot_MMButton_Init()
	if(HealBot_Config.ButtonShown==1) then
		HealBot_ButtonFrame:Show();
	else
		HealBot_ButtonFrame:Hide();
	end
end

function HealBot_MMButton_OnEnter(self)
    GameTooltip:SetOwner(self, "ANCHOR_LEFT");
    GameTooltip:SetText(HEALBOT_BUTTON_TOOLTIP);
	GameTooltipTextLeft1:SetTextColor(1, 1, 1);
    GameTooltip:Show();
end

function HealBot_MMButton_OnClick(self,button)
  if button~="RightButton" then
    HealBot_ToggleOptions()
  else
    HealBot_AddDebug("right click")
  end
end

function HealBot_MMButton_BeingDragged()
    w,x = GetCursorPosition() 
    y,z = Minimap:GetLeft(), Minimap:GetBottom() 
    w = y-w/UIParent:GetScale()+70 
    x = x/UIParent:GetScale()-z-70 
    HealBot_MMButton_SetPosition(math.deg(math.atan2(x,w)));
end

function HealBot_MMButton_SetPosition(c)
    if(c < 0) then
        c = c + 360;
    end

    HealBot_Config.HealBot_ButtonPosition = c;
    HealBot_MMButton_UpdatePosition();
end

function HealBot_SmartCast(unit,hlthDelta)
  w=nil;
  x=0;
  y=0;
  z=false;
    for sName,sNameRank in pairs(HealBot_SmartCast_Spells) do
      if (HealBot_Spells[sNameRank]) then
        if HealBot_Spells[sNameRank].HealsDur<(hlthDelta*2) then
          if HealBot_Spells[sNameRank].Buff then
            if HealBot_HasBuff(sName,unit) then 
              z=false;  
            else
              z=true;
            end;  
          else
            z=true
          end
          if z then
            x=(HealBot_Spells[sNameRank].HealsDur/hlthDelta)*800;
            x=x-(HealBot_Spells[sNameRank].Mana/25);
            x=x/(HealBot_Spells[sNameRank].CastTime+0.5);
            if x>y then
              y=x;
              w=sName;
            end
          end
        end
      end
    end
  return w;
end

local uRange=0
function HealBot_UnitInRange(spellName, unit) -- added by Diacono of Ursin
   if UnitName(unit)==HealBot_PlayerName then
     uRange = 1
   elseif not spellName then 
     uRange = 0
   elseif IsSpellInRange(spellName, unit) ~= nil then
      uRange = IsSpellInRange(spellName, unit)
   elseif IsItemInRange(spellName, unit) ~= nil then
      uRange = IsItemInRange(spellName, unit)
   elseif UnitInRange(unit) == 1 then
      uRange = 1
   else
     uRange=0
   end
   if uRange==0 then
     if not UnitIsVisible(unit) then uRange=-1 end
   end
   return uRange
end


function HealBot_ClearLocalArr(unitName)
  if HealBot_PlayerBuff[unitName] then HealBot_PlayerBuff[unitName]=nil end
  if HealBot_Player_HoT[unitName] then HealBot_Player_HoT[unitName]=nil end
  if HealBot_Player_HoT_Icons[unitName] then HealBot_Player_HoT_Icons[unitName]=nil end
  if DeBuff_Count[unitName] then DeBuff_Count[unitName]=nil end
  if HealBot_unitHealth[unitName] then
    HealBot_unitHealth[unitName]=nil
	HealBot_unitHealthMax[unitName]=nil
  end
  for k in pairs(HealBot_MainTanks) do
    if ( HealBot_MainTanks[k] == unitName ) then
      HealBot_MainTanks[k] = nil;
    end
  end
end

function HealBot_Update_Skins()

  if HealBot_Config.Skin_Version then
    if HealBot_Config.Skin_Version>Healbot_Config_Skins.Skin_Version then
	  Healbot_Config_Skins.Skin_Version=HealBot_Config.Skin_Version
	end
	HealBot_Config.Skin_Version=nil
  end
  
  if HealBot_Config.Current_Skin then
    if Healbot_Config_Skins.Current_Skin == HEALBOT_SKINS_STD then
      Healbot_Config_Skins.Current_Skin = HealBot_Config.Current_Skin
	end
	HealBot_Config.Current_Skin = nil
	for x in pairs (Healbot_Config_Skins.Skins) do
	  if Healbot_Config_Skins.btexture[Healbot_Config_Skins.Skins[x]] and Healbot_Config_Skins.btexture[Healbot_Config_Skins.Skins[x]] == "Empty" then
	    Healbot_Config_Skins.btexture[Healbot_Config_Skins.Skins[x]]="Smooth"
	  end
	end
  end

  if Healbot_Config_Skins.Skin_Version<2 then
    HealBot_Config={}                  --
	HealBot_Options_SetDefaults()      --  So now we are forcing a wipe of WTF for those users upgrading from a version before 3.0.2.17
	HealBot_AddDebug("SkinVer Updated to 2")
  end
  
  if Healbot_Config_Skins.Skin_Version<3 then
    if HealBot_Config.RangeCheckFreq<0.2 then HealBot_Config.RangeCheckFreq=0.2 end
  	for x,y in pairs(Healbot_Config_Skins.headtexture) do
  		y = tonumber(y) or 0
  		if y <= 15 and y > 0 then
      	Healbot_Config_Skins.headtexture[x]=HealBot_Default_Textures[y].name
   		end
  	end
  	for x,y in pairs(Healbot_Config_Skins.btexture) do
  		y = tonumber(y) or 0
  		if y <= 15  and y > 0 then
      	Healbot_Config_Skins.btexture[x]=HealBot_Default_Textures[y].name
   		end
  	end
  	if tonumber(HealBot_Config.SoundDebuffPlay) then
  		HealBot_Config.SoundDebuffPlay=HealBot_Default_Sounds[HealBot_Config.SoundDebuffPlay].name;
  	end
  	HealBot_AddDebug("SkinVer Updated to 3")
  end
  
  if Healbot_Config_Skins.Skin_Version<4 then
    for x in pairs (Healbot_Config_Skins.Skins) do
      if not Healbot_Config_Skins.AggroBarSize[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.AggroBarSize[Healbot_Config_Skins.Skins[x]] = 3 end
	  if not Healbot_Config_Skins.btextfont[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextfont[Healbot_Config_Skins.Skins[x]] = HealBot_Default_Font end
	  if not Healbot_Config_Skins.headtextfont[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headtextfont[Healbot_Config_Skins.Skins[x]] = HealBot_Default_Font end
	  if not Healbot_Config_Skins.headtextheight[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headtextheight[Healbot_Config_Skins.Skins[x]] = 10 end
	end
	HealBot_AddDebug("SkinVer Updated to 4")
  end
  
  if Healbot_Config_Skins.Skin_Version<5 then
	HealBot_AddDebug("SkinVer Updated to 5")
  end
  
  if Healbot_Config_Skins.Skin_Version<6 then
    for x in pairs (Healbot_Config_Skins.Skins) do
      if not Healbot_Config_Skins.numcols[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.numcols[Healbot_Config_Skins.Skins[x]] = 2 end
      if not Healbot_Config_Skins.btexture[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btexture[Healbot_Config_Skins.Skins[x]] = HealBot_Default_Textures[8].name end
      if not Healbot_Config_Skins.bcspace[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.bcspace[Healbot_Config_Skins.Skins[x]] = 4 end
      if not Healbot_Config_Skins.brspace[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.brspace[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.bwidth[Healbot_Config_Skins.Skins[x]] = 144 end
      if not Healbot_Config_Skins.bheight[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.bheight[Healbot_Config_Skins.Skins[x]] = 22 end
      if not Healbot_Config_Skins.btextenabledcolr[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextenabledcolr[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.btextenabledcolg[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextenabledcolg[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.btextenabledcolb[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextenabledcolb[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.btextenabledcola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextenabledcola[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.btextdisbledcolr[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextdisbledcolr[Healbot_Config_Skins.Skins[x]] = 0.5 end
      if not Healbot_Config_Skins.btextdisbledcolg[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextdisbledcolg[Healbot_Config_Skins.Skins[x]] = 0.5 end
      if not Healbot_Config_Skins.btextdisbledcolb[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextdisbledcolb[Healbot_Config_Skins.Skins[x]] = 0.5 end
      if not Healbot_Config_Skins.btextdisbledcola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextdisbledcola[Healbot_Config_Skins.Skins[x]] = 0.5 end
      if not Healbot_Config_Skins.btextcursecolr[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextcursecolr[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.btextcursecolg[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextcursecolg[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.btextcursecolb[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextcursecolb[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.btextcursecola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextcursecola[Healbot_Config_Skins.Skins[x]] = 1 end
      if not Healbot_Config_Skins.backcola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.backcola[Healbot_Config_Skins.Skins[x]] = 0.02 end
      if not Healbot_Config_Skins.Barcola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.Barcola[Healbot_Config_Skins.Skins[x]] = 0.9 end
      if not Healbot_Config_Skins.BarcolaInHeal[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.BarcolaInHeal[Healbot_Config_Skins.Skins[x]] = 0.45 end
      if not Healbot_Config_Skins.backcolr[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.backcolr[Healbot_Config_Skins.Skins[x]] = 0 end
      if not Healbot_Config_Skins.backcolg[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.backcolg[Healbot_Config_Skins.Skins[x]] = 0 end
      if not Healbot_Config_Skins.backcolb[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.backcolb[Healbot_Config_Skins.Skins[x]] = 0.8 end
      if not Healbot_Config_Skins.borcolr[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.borcolr[Healbot_Config_Skins.Skins[x]] = 0.4	end  
      if not Healbot_Config_Skins.borcolg[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.borcolg[Healbot_Config_Skins.Skins[x]] = 0.4	end 
      if not Healbot_Config_Skins.borcolb[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.borcolb[Healbot_Config_Skins.Skins[x]] = 0.4	end
      if not Healbot_Config_Skins.borcola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.borcola[Healbot_Config_Skins.Skins[x]] = 0.2 end
      if not Healbot_Config_Skins.btextheight[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.btextheight[Healbot_Config_Skins.Skins[x]] = 10 end
      if not Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.bardisa[Healbot_Config_Skins.Skins[x]] = 0.28 end
      if not Healbot_Config_Skins.bareora[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.bareora[Healbot_Config_Skins.Skins[x]] = 0.45 end
      if not Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.bar2size[Healbot_Config_Skins.Skins[x]] = 0 end
      if not Healbot_Config_Skins.ShowHeader[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowHeader[Healbot_Config_Skins.Skins[x]] = 0 end
      if not Healbot_Config_Skins.headbarcolr[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headbarcolr[Healbot_Config_Skins.Skins[x]] = 0.1 end
      if not Healbot_Config_Skins.headbarcolg[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headbarcolg[Healbot_Config_Skins.Skins[x]] = 0.1 end
      if not Healbot_Config_Skins.headbarcolb[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headbarcolb[Healbot_Config_Skins.Skins[x]] = 0.4 end
      if not Healbot_Config_Skins.headbarcola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headbarcola[Healbot_Config_Skins.Skins[x]] = 0.25 end
      if not Healbot_Config_Skins.headtxtcolr[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headtxtcolr[Healbot_Config_Skins.Skins[x]] = 0.8 end
      if not Healbot_Config_Skins.headtxtcolg[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headtxtcolg[Healbot_Config_Skins.Skins[x]] = 0.8 end
      if not Healbot_Config_Skins.headtxtcolb[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headtxtcolb[Healbot_Config_Skins.Skins[x]] = 0.4 end
      if not Healbot_Config_Skins.headtxtcola[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headtxtcola[Healbot_Config_Skins.Skins[x]] = 0.8 end
      if not Healbot_Config_Skins.headtexture[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headtexture[Healbot_Config_Skins.Skins[x]] = HealBot_Default_Textures[6].name end
      if not Healbot_Config_Skins.headwidth[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.headwidth[Healbot_Config_Skins.Skins[x]] = 0.72 end
	  if not Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowAggro[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.ShowAggroBars[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowAggroBars[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.ShowAggroText[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowAggroText[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.UseFluidBars[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.UseFluidBars[Healbot_Config_Skins.Skins[x]] = 0 end
	  if not Healbot_Config_Skins.BarFreq[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.BarFreq[Healbot_Config_Skins.Skins[x]] = 5 end
	  if not Healbot_Config_Skins.ShowHoTicons[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowHoTicons[Healbot_Config_Skins.Skins[x]] = 1  end
	  if not Healbot_Config_Skins.HoTonBar[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.HoTonBar[Healbot_Config_Skins.Skins[x]] = 1  end
	  if not Healbot_Config_Skins.HoTposBar[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.HoTposBar[Healbot_Config_Skins.Skins[x]] = 1  end
	  if not Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowClassOnBar[Healbot_Config_Skins.Skins[x]] = 0 end
	  if not Healbot_Config_Skins.ShowNameOnBar[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowNameOnBar[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.ShowHealthOnBar[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowHealthOnBar[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.BarHealthIncHeals[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.BarHealthIncHeals[Healbot_Config_Skins.Skins[x]] = 1  end
	  if not Healbot_Config_Skins.BarHealthType[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.BarHealthType[Healbot_Config_Skins.Skins[x]] = 1  end
	  if not Healbot_Config_Skins.SetClassColourText[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.SetClassColourText[Healbot_Config_Skins.Skins[x]] = 1  end
	  if not Healbot_Config_Skins.SetBarClassColour[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.SetBarClassColour[Healbot_Config_Skins.Skins[x]] = 0 end
	  if not Healbot_Config_Skins.ShowDebuffIcon[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowDebuffIcon[Healbot_Config_Skins.Skins[x]] = 1  end
	  if not Healbot_Config_Skins.ShowIconTextCount[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowIconTextCount[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.ShowIconTextDuration[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.ShowIconTextDuration[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.IconScale[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.IconScale[Healbot_Config_Skins.Skins[x]] = 1 end
	  if not Healbot_Config_Skins.IconTextScale[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.IconTextScale[Healbot_Config_Skins.Skins[x]] = 0.55 end
	end
	HealBot_AddDebug("SkinVer Updated to 6")
  end
  
  if Healbot_Config_Skins.Skin_Version<7 then
    for x in pairs (Healbot_Config_Skins.Skins) do
	  if not Healbot_Config_Skins.DoubleText[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.DoubleText[Healbot_Config_Skins.Skins[x]] = 0 end
	  if not Healbot_Config_Skins.TextAlignment[Healbot_Config_Skins.Skins[x]] then Healbot_Config_Skins.TextAlignment[Healbot_Config_Skins.Skins[x]] = 2 end
	end
	HealBot_AddDebug("SkinVer Updated to 7")
  end
  
  if Healbot_Config_Skins.Skin_Version<8 then
    HealBot_AddDebug("SkinVer Updated to 8")
  end  
  Healbot_Config_Skins.Skin_Version=HealBotSkinVersion
end
