GW_VERSION_STRING = 'GW2_UI v4.0.9release'



GW2UI_SETTINGS = {}
GW_DEFAULT ={}
    
GW_DEFAULT['TARGET_ENABLED'] = true
GW_DEFAULT['FOCUS_ENABLED'] = true
GW_DEFAULT['PET_ENABLED'] = true
GW_DEFAULT['POWERBAR_ENABLED'] = true
GW_DEFAULT['CHATBUBBLES_ENABLED'] = true
GW_DEFAULT['NAMEPLATES_ENABLED'] = true
GW_DEFAULT['MINIMAP_ENABLED'] = true
GW_DEFAULT['QUESTTRACKER_ENABLED'] = true
GW_DEFAULT['TOOLTIPS_ENABLED'] = true
GW_DEFAULT['CHATFRAME_ENABLED'] = true
GW_DEFAULT['QUESTVIEW_ENABLED'] = true
GW_DEFAULT['HEALTHGLOBE_ENABLED'] = true
GW_DEFAULT['PLAYER_BUFFS_ENABLED'] = true
GW_DEFAULT['ACTIONBARS_ENABLED'] = true
GW_DEFAULT['BAGS_ENABLED'] = true
GW_DEFAULT['NPC_CAM_ENABLED'] = false
GW_DEFAULT['FONTS_ENABLED'] = true
GW_DEFAULT['CASTINGBAR_ENABLED'] = true
GW_DEFAULT['HIDEACTIONBAR_BACKGROUND_ENABLED'] = false
GW_DEFAULT['SHOW_QUESTTRACKER_COMPASS'] = true
GW_DEFAULT['MINIMAP_HOVER'] = 'NONE'
GW_DEFAULT['CLASS_POWER'] = true
GW_DEFAULT['GROUP_FRAMES'] = true
GW_DEFAULT['PETBAR_ENABLED'] = true

GW_DEFAULT['HUD_SPELL_SWAP'] = true


GW_DEFAULT['BAG_ITEM_SIZE'] = 45
GW_DEFAULT['BANK_ITEM_SIZE'] = 45


GW_DEFAULT['BAG_WIDTH'] = 480

GW_DEFAULT['BAG_POSITION'] ={}
GW_DEFAULT['BAG_POSITION']['point'] = 'RIGHT'
GW_DEFAULT['BAG_POSITION']['relativePoint'] = 'RIGHT'
GW_DEFAULT['BAG_POSITION']['xOfs'] = -256
GW_DEFAULT['BAG_POSITION']['yOfs']  = 256


GW_DEFAULT['RAID_CLASS_COLOR'] = false
GW_DEFAULT['RAID_STYLE_PARTY'] = false
GW_DEFAULT['RAID_UNIT_FLAGS'] = 'NONE'
GW_DEFAULT['RAID_UNIT_MARKERS'] = false

    
GW_DEFAULT['target_HEALTH_VALUE_ENABLED'] = false
GW_DEFAULT['target_HEALTH_VALUE_TYPE'] = false
GW_DEFAULT['target_CLASS_COLOR'] = true
        
GW_DEFAULT['FADE_BOTTOM_ACTIONBAR'] = true
GW_DEFAULT['HIDE_CHATSHADOW'] = false
GW_DEFAULT['HIDE_QUESTVIEW'] = false;
GW_DEFAULT['USE_CHAT_BUBBLES'] = false;
GW_DEFAULT['DISABLE_NAMEPLATES'] = false
GW_DEFAULT['DISABLE_TOOLTIPS'] = false
GW_DEFAULT['DISABLE_CHATFRAME'] = false
GW_DEFAULT['CHATFRAME_FADE'] = true
    
GW_DEFAULT['target_TARGET_ENABLED'] = true
GW_DEFAULT['target_DEBUFFS'] = true
GW_DEFAULT['target_DEBUFFS_FILTER'] = true
GW_DEFAULT['target_BUFFS'] = true
GW_DEFAULT['target_BUFFS_FILTER'] = true
GW_DEFAULT['target_BUFFS_FILTER_ALL'] = false

GW_DEFAULT['focus_TARGET_ENABLED'] = true
GW_DEFAULT['focus_DEBUFFS'] = true
GW_DEFAULT['focus_DEBUFFS_FILTER'] = true
GW_DEFAULT['focus_BUFFS'] = true
GW_DEFAULT['focus_BUFFS_FILTER'] = true
GW_DEFAULT['focus_BUFFS_FILTER_ALL'] = false

GW_DEFAULT['focus_HEALTH_VALUE_ENABLED'] = false
GW_DEFAULT['focus_HEALTH_VALUE_TYPE'] = false
GW_DEFAULT['focus_CLASS_COLOR'] = true
                    
GW_DEFAULT['target_x_position'] = -100
GW_DEFAULT['target_y_position'] = -100

GW_DEFAULT['focus_x_position'] = -350
GW_DEFAULT['focus_y_position'] = -100

GW_DEFAULT['multibarleft_x_position'] = -300
GW_DEFAULT['multibarleft_y_position'] = -0
    
GW_DEFAULT['multibarright_x_position'] = -260
GW_DEFAULT['multibarright_y_position'] = -0
    
GW_DEFAULT['multibarleft_pos'] ={}
GW_DEFAULT['multibarleft_pos']['point'] = 'RIGHT'
GW_DEFAULT['multibarleft_pos']['relativePoint'] = 'RIGHT'
GW_DEFAULT['multibarleft_pos']['xOfs'] = -300
GW_DEFAULT['multibarleft_pos']['yOfs']= 0

GW_DEFAULT['multibarright_pos'] ={}
GW_DEFAULT['multibarright_pos']['point'] = 'RIGHT'
GW_DEFAULT['multibarright_pos']['relativePoint'] = 'RIGHT'
GW_DEFAULT['multibarright_pos']['xOfs'] = -260
GW_DEFAULT['multibarright_pos']['yOfs']  = 0

GW_DEFAULT['target_pos'] ={}
GW_DEFAULT['target_pos']['point'] = 'TOP'
GW_DEFAULT['target_pos']['relativePoint'] = 'TOP'
GW_DEFAULT['target_pos']['xOfs'] =  -56
GW_DEFAULT['target_pos']['yOfs']  = -100

GW_DEFAULT['pet_pos'] ={}
GW_DEFAULT['pet_pos']['point'] = 'BOTTOMLEFT'
GW_DEFAULT['pet_pos']['relativePoint'] = 'BOTTOM'
GW_DEFAULT['pet_pos']['xOfs'] =  -372
GW_DEFAULT['pet_pos']['yOfs']  = 220  

GW_DEFAULT['castingbar_pos'] ={}
GW_DEFAULT['castingbar_pos']['point'] = 'BOTTOM'
GW_DEFAULT['castingbar_pos']['relativePoint'] = 'BOTTOM'
GW_DEFAULT['castingbar_pos']['xOfs'] =  0
GW_DEFAULT['castingbar_pos']['yOfs']  = 300
    
    
    
GW_DEFAULT['targettarget_pos'] ={}
GW_DEFAULT['targettarget_pos']['point'] = 'TOP'
GW_DEFAULT['targettarget_pos']['relativePoint'] = 'TOP'
GW_DEFAULT['targettarget_pos']['xOfs'] =  250
GW_DEFAULT['targettarget_pos']['yOfs']  = -100


GW_DEFAULT['focus_pos'] ={}
GW_DEFAULT['focus_pos']['point'] = 'CENTER'
GW_DEFAULT['focus_pos']['relativePoint'] = 'CENTER'
GW_DEFAULT['focus_pos']['xOfs'] =  -350
GW_DEFAULT['focus_pos']['yOfs']  = 0

    
GW_DEFAULT['focustarget_pos'] ={}
GW_DEFAULT['focustarget_pos']['point'] = 'CENTER'
GW_DEFAULT['focustarget_pos']['relativePoint'] = 'CENTER'
GW_DEFAULT['focustarget_pos']['xOfs'] =  -80
GW_DEFAULT['focustarget_pos']['yOfs']  = 0

GW_DEFAULT['MultiBarBottomLeft'] ={}
GW_DEFAULT['MultiBarBottomLeft']['point'] = 'BOTTOMLEFT'
GW_DEFAULT['MultiBarBottomLeft']['relativePoint'] = 'BOTTOM'
GW_DEFAULT['MultiBarBottomLeft']['xOfs'] = -372
GW_DEFAULT['MultiBarBottomLeft']['yOfs'] = 120

GW_DEFAULT['MultiBarBottomLeft']['size'] = 38
GW_DEFAULT['MultiBarBottomLeft']['margin'] = 2
GW_DEFAULT['MultiBarBottomLeft']['ButtonsPerRow'] = 6
GW_DEFAULT['MultiBarBottomLeft']['hideDefaultBackground'] = true
    
GW_DEFAULT['MultiBarBottomRight'] ={}
GW_DEFAULT['MultiBarBottomRight']['point'] = 'BOTTOMRIGHT'
GW_DEFAULT['MultiBarBottomRight']['relativePoint'] = 'BOTTOM'
GW_DEFAULT['MultiBarBottomRight']['xOfs'] = 372
GW_DEFAULT['MultiBarBottomRight']['yOfs'] = 120

    
GW_DEFAULT['MultiBarBottomRight']['size'] = 38
GW_DEFAULT['MultiBarBottomRight']['margin'] = 2
GW_DEFAULT['MultiBarBottomRight']['ButtonsPerRow'] = 6
GW_DEFAULT['MultiBarBottomRight']['hideDefaultBackground'] = true
    

GW_DEFAULT['MultiBarRight'] ={}
GW_DEFAULT['MultiBarRight']['point'] = 'RIGHT'
GW_DEFAULT['MultiBarRight']['relativePoint'] = 'RIGHT'
GW_DEFAULT['MultiBarRight']['xOfs'] = -320
GW_DEFAULT['MultiBarRight']['yOfs'] = 0

    
GW_DEFAULT['MultiBarRight']['size'] = 38
GW_DEFAULT['MultiBarRight']['margin'] = 2
GW_DEFAULT['MultiBarRight']['ButtonsPerRow'] = 1
GW_DEFAULT['MultiBarRight']['hideDefaultBackground'] = true

GW_DEFAULT['MultiBarLeft'] ={}
GW_DEFAULT['MultiBarLeft']['point'] = 'RIGHT'
GW_DEFAULT['MultiBarLeft']['relativePoint'] = 'RIGHT'
GW_DEFAULT['MultiBarLeft']['xOfs'] = -368
GW_DEFAULT['MultiBarLeft']['yOfs'] = 0

    
GW_DEFAULT['MultiBarLeft']['size'] = 38
GW_DEFAULT['MultiBarLeft']['margin'] = 2
GW_DEFAULT['MultiBarLeft']['ButtonsPerRow'] = 1
GW_DEFAULT['MultiBarLeft']['hideDefaultBackground'] = true


GW_DEFAULT['raid_pos'] ={}
GW_DEFAULT['raid_pos']['point'] = 'TOPLEFT'
GW_DEFAULT['raid_pos']['relativePoint'] = 'TOPLEFT'
GW_DEFAULT['raid_pos']['xOfs'] =  65
GW_DEFAULT['raid_pos']['yOfs']  = -60

GW_DEFAULT['RAID_WIDTH'] = 55
GW_DEFAULT['RAID_HEIGHT'] = 47
GW_DEFAULT['RAID_POWER_BARS'] = false
GW_DEFAULT['RAID_UNITS_PER_COLUMN'] = 5
GW_DEFAULT['RAID_ONLY_DISPELL_DEBUFFS'] = false

GW_DEFAULT['HUD_SCALE'] = 1
GW_DEFAULT['MINIMAP_SCALE'] = 140
GW_DEFAULT['CASTINGBAR_DATA'] = false
GW_DEFAULT['USE_CHARACTER_WINDOW'] = true


GW_DEFAULT['ACTIVE_PROFILE'] = nil

GW_DEFAULT['WARNIG_MESSAGE'] ={}
GW_DEFAULT['WARNIG_MESSAGE']['point'] = 'TOP'
GW_DEFAULT['WARNIG_MESSAGE']['relativePoint'] = 'TOP'
GW_DEFAULT['WARNIG_MESSAGE']['xOfs'] =  0
GW_DEFAULT['WARNIG_MESSAGE']['yOfs']  = 0




local ADDOON_LOADED = false;
local PLAYER_ENTERING_WORLD = false;
local SETTINGS_LOADED = false;
local GW_UI_LOADED = false;


GW_MOVABLE_FRAMES ={}
GW_MOVABLE_FRAMES_REF ={}
GW_MOVABLE_FRAMES_SETTINGS_KEY ={}

local swimAnimation = 0
local lastSwimState = true

function gwGetActiveProfile()
    if GW2UI_SETTINGS_DB_03==nil then
        GW2UI_SETTINGS_DB_03 = {}
    end
    return GW2UI_SETTINGS_DB_03['ACTIVE_PROFILE']
end

function gwSetProfileSettings()
    
    local profileIndex = gwGetActiveProfile()
    
    if profileIndex==nil then return end
    if GW2UI_SETTINGS_PROFILES[profileIndex]==nil then return end
    
    
    
    for k,v in pairs(GW2UI_SETTINGS_DB_03) do
        GW2UI_SETTINGS_PROFILES[profileIndex][k] = v
    end
    
end

function gwGetSetting(name)
    
    local profileIndex = gwGetActiveProfile()
    
    if GW2UI_SETTINGS_PROFILES==nil then 
        GW2UI_SETTINGS_PROFILES = {}
    end
    
    if profileIndex~=nil and GW2UI_SETTINGS_PROFILES[profileIndex]~=nil then
        if GW2UI_SETTINGS_PROFILES[profileIndex][name]==nil then
            GW2UI_SETTINGS_PROFILES[profileIndex][name] = gwGetDefault(name)
        end
        return GW2UI_SETTINGS_PROFILES[profileIndex][name]
    end
    
    
    if GW2UI_SETTINGS_DB_03==nil then
        GW2UI_SETTINGS_DB_03 = GW_DEFAULT
    end
    if GW2UI_SETTINGS_DB_03[name]==nil then
        GW2UI_SETTINGS_DB_03[name] = gwGetDefault(name)
    end
    
    return GW2UI_SETTINGS_DB_03[name]
end

function gwSetSetting(name,state)
    
    local profileIndex = gwGetActiveProfile()
    
    if profileIndex~=nil and GW2UI_SETTINGS_PROFILES[profileIndex]~=nil then
        
        GW2UI_SETTINGS_PROFILES[profileIndex][name] = state
        GW2UI_SETTINGS_PROFILES[profileIndex]['profileLastUpdated'] = date("%m/%d/%y %H:%M:%S")
        return
        
    end
    
    GW2UI_SETTINGS_DB_03[name] = state
end

function gwGetDefault(name)    
    return GW_DEFAULT[name]
end
function gwResetToDefault()    
    
    local profileIndex = gwGetActiveProfile()
    
    if profileIndex~=nil and GW2UI_SETTINGS_PROFILES[profileIndex]~=nil then
        for k,v in pairs(GW_DEFAULT) do
            GW2UI_SETTINGS_PROFILES[profileIndex][k] = v 
        end
        GW2UI_SETTINGS_PROFILES[profileIndex]['profileLastUpdated'] = date("%m/%d/%y %H:%M:%S")
        return
        
    end
    GW2UI_SETTINGS_DB_03 = GW_DEFAULT
end

function gwGetSettingsProfiles()
    
    if GW2UI_SETTINGS_PROFILES==nil then
        GW2UI_SETTINGS_PROFILES = {}
    end
    return GW2UI_SETTINGS_PROFILES;

end

function gw_register_movable_frame(name,frame,settingsName,dummyFrame)
    
    local moveframe = CreateFrame('Frame', name..'MoveAble',UIParent,dummyFrame);

    moveframe:SetSize(frame:GetSize())
    moveframe.frameName:SetText(name)    
 
    
    local dummyPoint = gwGetSetting(settingsName)
    moveframe:ClearAllPoints()
    moveframe:SetPoint(dummyPoint['point'],UIParent,dummyPoint['relativePoint'],dummyPoint['xOfs'],dummyPoint['yOfs'])
    GW_MOVABLE_FRAMES[name]=moveframe
    GW_MOVABLE_FRAMES_REF[name]=frame
    GW_MOVABLE_FRAMES_SETTINGS_KEY[name]=settingsName
    moveframe:Hide()
    moveframe:RegisterForDrag("LeftButton")
    
    moveframe:SetScript("OnDragStart", frame.StartMoving)
    moveframe:SetScript("OnDragStop", function(self)
        moveframe:StopMovingOrSizing()
        point, relativeTo, relativePoint, xOfs, yOfs = moveframe:GetPoint()
            
        new_point = gwGetSetting(settingsName)
        new_point['point']=point
        new_point['relativePoint'] =relativePoint
        new_point['xOfs'] =math.floor(xOfs)
        new_point['yOfs'] = math.floor(yOfs)
        gwSetSetting(settingsName,new_point)

    end)
    
end

function gw_update_moveableframe_positions()
    
    for k,v in pairs(GW_MOVABLE_FRAMES_REF) do
        local newp = gwGetSetting(GW_MOVABLE_FRAMES_SETTINGS_KEY[k])
        v:ClearAllPoints()
        GW_MOVABLE_FRAMES_REF[k]:SetPoint(newp['point'],UIParent,newp['relativePoint'],newp['xOfs'],newp['yOfs'])
       
    end
    
end

function gwUpdateHudScale(scale)
  
    for k,v in pairs(GW_MAIN_HUD_FRAMES) do
      if _G[v] then
        _G[v]:SetScale(gwGetSetting('HUD_SCALE')) 
      end
    end
    
end

function gwToggleMainHud(b)
  
    for k,v in pairs(GW_MAIN_HUD_FRAMES) do
        if v~=nil then
            if b then
                if GW_MAIN_HUD_FRAMES_OLD_STATE[k] then
                    _G[v]:Show()
                end
            else
                GW_MAIN_HUD_FRAMES_OLD_STATE[k] = _G[v]:IsShown()
                _G[v]:Hide()
            end
        end
    end
    
end


if AchievementMicroButton_Update==nil then
   function AchievementMicroButton_Update()
        return
    end
end

if UnitIsTapDenied==nil then
   function UnitIsTapDenied()
        if (UnitIsTapped("target")) and (not UnitIsTappedByPlayer("target")) then
            return true
        end
        return false
    end
end

function countTable(T)
  local c = 0
    if T~=nil and type(T) == 'table' then
        for _ in pairs(T) do c = c + 1 end
    end
  return c
end

function timeCount(numSec,com)
	local nSeconds = tonumber(numSec)
        if nSeconds==nil then
            nSeconds = 0
        end
		if nSeconds == 0 then

				coolTime = "0";
		else
			local	nHours = math.floor(nSeconds/3600);
			local	nMins =  math.floor(nSeconds/60 - (nHours*60));
			local	nSecs = math.floor(nSeconds - nHours*3600 - nMins *60);
			local	nMilsecs = math.floor( ((nSeconds - nHours*3600 - nMins) * 10^1) + 0.5) / (10^1) 
			
            if nHours>0 then
                coolTime =nHours..'h'
            else
                if nMins>0 then
                     coolTime =nMins..'m'
                else
                    if com~=nil then
                        coolTime = nMilsecs..'s'
                    else
                        coolTime =   nSecs..'s'
                    end
                end
            end

	end
    return coolTime
end


function comma_value(n)
    n = round(n)
	local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
	return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

animations = {}

function round(number, decimals)
    return (("%%.%df"):format(decimals)):format(number)
end
function intRound(v)
    if v==nil then return 0 end
    vf = math.floor(v)
    if (v-vf)>0.5 then return vf+1 end
    return vf
end
function dif(a,b)
    
    if a==nil then a = 0 end
    if b==nil then b = 0 end
    
    if a > b then
        return a-b
    else
        return b-a 
    end
end
function  lerp( v0,  v1,  t) 
    if v0==nil then 
        v0=0
    end
    local p = (v1-v0)
  return v0 + t*p;
end
function length(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
function splitString(inputstr, sep,sep2,sep3)
        if sep == nil then
                sep = "%s"
        end
        inputstr = inputstr:gsub ('\n','')
        local t={} ; i=1
        for str in string.gmatch(inputstr, "([^"..sep.."|"..sep2.."|"..sep3.."]+)") do
            st, en, cap1, cap2, cap3 = string.find (inputstr, str)
            if en ~= nil then
                s = string.sub (inputstr, en+1, en+1)

                if s ~= nil or s ~= '' then
                    str =  str..s
                end
            end
            t[i] = str
            i = i + 1
        end
        return t
end

function gw_button_enter(self)
    local name = self:GetName()
    local startTime = GetTime()
   local w = self:GetWidth()
    _G[name..'OnHover']:SetAlpha(1)
    
    self.animationValue = 0
    
    addToAnimation(name,self.animationValue,1,GetTime(),0.2,function()
        
       local prog = animations[name]['progress']
        local l = lerp(0,w,prog)
            
        _G[name..'OnHover']:SetPoint('RIGHT',self,'LEFT',l,0)
            
        _G[name..'OnHover']:SetVertexColor(1,1,1,lerp(0,1,((prog) - 0.5)/0.5))

           
    end)
end

function gw_button_leave(self)
    local name = self:GetName()
    local startTime = GetTime()
     local w = self:GetWidth()
       _G[name..'OnHover']:SetAlpha(1)
    
    self.animationValue = 1
    
    addToAnimation(name,self.animationValue,0,GetTime(),0.2,function()
        
       local prog = animations[name]['progress']
        local l = lerp(0,w,prog)
            
        _G[name..'OnHover']:SetPoint('RIGHT',self,'LEFT',l,0)
            
        _G[name..'OnHover']:SetVertexColor(1,1,1,lerp(0,1,((prog) - 0.5)/0.5))

           
    end)
end

function gwBar(self,value)

    if self==nil then return end
    local barWidth = self:GetWidth()
    local sparkWidth = self.spark:GetWidth()
    
    addToAnimation(self.animationName,self.animationValue,value,GetTime(),0.2,function()
            
        local snap = (animations[self.animationName]['progress']*100)/5
            
        local round_closest = 0.05 * snap
  
        local spark_min =  math.floor(snap)
        local spark_max =  math.ceil(snap) 
        local spark_current = snap

        local spark_prec = spark_current - spark_min
                
                            
        local spark = math.min(barWidth - sparkWidth,math.floor(barWidth*round_closest) - math.floor(sparkWidth*spark_prec))
        local bI = 17 - math.max(1,intRound(16 * spark_prec))

        self.spark:SetTexCoord(
        bloodSpark[bI].left,
        bloodSpark[bI].right,
        bloodSpark[bI].top,
        bloodSpark[bI].bottom)

        self:SetValue(round_closest)
        self.spark:ClearAllPoints()
        self.spark:SetPoint('LEFT',spark,0)
            
            
    end)            
        self.animationValue = value;
end
function gw_setClassIcon(self,class)
    
    if class==nil or class>12 then
        class = 0
    end
    
  
  self:SetTexCoord(
        GW_CLASS_ICONS[class].l,
        GW_CLASS_ICONS[class].r,
        GW_CLASS_ICONS[class].t,
        GW_CLASS_ICONS[class].b
    )
end
function gw_setDeadIcon(self)
    self:SetTexCoord(GW_CLASS_ICONS['dead'].l,
        GW_CLASS_ICONS['dead'].r,
        GW_CLASS_ICONS['dead'].t,
        GW_CLASS_ICONS['dead'].b
    )
end

function isnan(n) return tostring(n) == '-1.#IND' end
function addToAnimation(name,from,to,start,duration,method,easeing,onCompleteCallback)

    newAnimation = true
    if animations[name]~=nil then
        if (animations[name]['start'] + animations[name]['duration'])>GetTime() then
            newAnimation = false
        end
    end
    
    if newAnimation==false then
  --      animations[name]['start'] = start
        animations[name]['duration'] = duration 
        animations[name]['to'] = to 
        animations[name]['progress'] = 0
        animations[name]['method'] = method
        animations[name]['completed'] = false
        animations[name]['easeing'] = easeing
        animations[name]['onCompleteCallback'] = onCompleteCallback
    else
        animations[name] = {}
        animations[name]['start'] = start
        animations[name]['duration'] = duration 
        animations[name]['from'] = from 
        animations[name]['to'] = to 
        animations[name]['progress'] = 0
        animations[name]['method'] = method
        animations[name]['completed'] = false
        animations[name]['easeing'] = easeing
        animations[name]['onCompleteCallback'] = onCompleteCallback
    end
   
end

local l = CreateFrame("Frame",nil,UIParent)

l:SetScript('OnUpdate',function() 
        
        if ADDOON_LOADED~=true or ADDOON_LOADED~=true then
            return
        end
        local foundAnimation = false
        for k,v in pairs(animations) do 
            
            if v['completed']==false and GetTime()>=(v['start']+ v['duration']) then
                if  v['easeing']==nil then
                    v['progress'] = lerp(v['from'],v['to'],math.sin(1 * math.pi * 0.5))
                else
                    v['progress'] = lerp(v['from'],v['to'],1)
                end
                if  v['method']~=nil then
                    v['method'](v['progress'])
                end
                
                if v['onCompleteCallback']~=nil then
                    v['onCompleteCallback']()
                end
                
                v['completed'] = true
                foundAnimation = true
            end            
            if v['completed']==false then
                
                if  v['easeing']==nil then
                    v['progress'] = lerp(v['from'],v['to'],math.sin((GetTime() - v['start'])/v['duration'] * math.pi * 0.5))
                else
                    v['progress'] = lerp(v['from'],v['to'],(GetTime() - v['start'])/v['duration'])
                end
            v['method'](v['progress'])
                foundAnimation = true
            end
        end
        
        if foundAnimation==false then
            animations = {}
        end
        

    
        if gwGetSetting('ACTIONBARS_ENABLED') then
            fadet_action_bar_check(MultiBarBottomLeft)
            fadet_action_bar_check(MultiBarBottomRight)
        end
        
        --Swim hud
        
        if  lastSwimState~=IsSwimming() then 
            if IsSwimming() then
                addToAnimation('swimAnimation',swimAnimation,1,GetTime(),0.1,function()
                        local r,g,b = _G['GwActionBarHudRIGHTSWIM']:GetVertexColor()
                        _G['GwActionBarHudRIGHTSWIM']:SetVertexColor(r,g,b,animations['swimAnimation']['progress']);
                        _G['GwActionBarHudLEFTSWIM']:SetVertexColor(r,g,b,animations['swimAnimation']['progress']);
                end)   
                swimAnimation = 1
            else
                addToAnimation('swimAnimation',swimAnimation,0,GetTime(),3.0,function()
                        local r,g,b = _G['GwActionBarHudRIGHTSWIM']:GetVertexColor()
                        _G['GwActionBarHudRIGHTSWIM']:SetVertexColor(r,g,b,animations['swimAnimation']['progress']);
                        _G['GwActionBarHudLEFTSWIM']:SetVertexColor(r,g,b,animations['swimAnimation']['progress']);
                end) 
                swimAnimation = 0
            end
            lastSwimState = IsSwimming()
        end
        
        
        
        

end)

l:SetScript('OnEvent',function(self,event,name) 
        
        if GW_UI_LOADED then
            return
        end
        
        if event=='ADDON_LOADED' and name=='GW2_UI' then
            ADDOON_LOADED = true;
            SETTINGS_LOADED = true;
            return    
        end
        if event=='ADDON_LOADED' and name~='GW2_UI' then
            return
        end
        if event=='PLAYER_ENTERING_WORLD' then
            PLAYER_ENTERING_WORLD = true;
        end

        GW_UI_LOADED = true
            
            --Create Settings window
            create_settings_window()
            display_options()
            
            --Create hud art
            loadHudArt()
            
            --Create experiencebar
            loadExperienceBar() 
        
        
        
       if gwGetSetting('FONTS_ENABLED') then
            gw_register_fonts()
        end  
        if gwGetSetting('CASTINGBAR_ENABLED') then
            gw_register_castingbar()
        end  
        
       if gwGetSetting('MINIMAP_ENABLED') then
            gw_set_minimap()
        end
        if gwGetSetting('QUESTTRACKER_ENABLED') then
               --QUESTTRACKER
            gw_load_questTracker()
        end
        if gwGetSetting('TOOLTIPS_ENABLED') then
            gw_set_tooltips()
        end
        if gwGetSetting('QUESTVIEW_ENABLED') then
            gw_create_questview()
        end
        if gwGetSetting('CHATFRAME_ENABLED') then
            
            gw_set_chatframe_bg()
       
        end
            --Create player hud
        if gwGetSetting('HEALTHGLOBE_ENABLED') then    
            gw_create_player_hud()
        end
        
        if gwGetSetting('POWERBAR_ENABLED') then
            gw_create_power_bar()
        end
        
        if gwGetSetting('CLASS_POWER') then
            create_classpowers()
        end
        
        
        if gwGetSetting('BAGS_ENABLED') then
            gw_create_bgframe()
            gw_create_bankframe()
        end 
        
        
        Gw_LoadWindows();
        
        gw_breath_meter()
        
            --Create unitframes
        if gwGetSetting('FOCUS_ENABLED') then
            gw_unitframes_register_Focus()
            if gwGetSetting('focus_TARGET_ENABLED') then
         
                gw_unitframes_register_Focusstarget()
            end
        end
        if gwGetSetting('TARGET_ENABLED') then
             gw_unitframes_register_Target()
            if gwGetSetting('target_TARGET_ENABLED') then
               gw_unitframes_register_Targetstarget()
                
              
            end
        end
        
        
        --create buff frame        
        if gwGetSetting('PLAYER_BUFFS_ENABLED') then
            gw_set_buffframe()
        end
        
		
		if gwGetSetting('PETBAR_ENABLED') then
            gw_create_pet_frame()
        end
		
        
        if gwGetSetting('ACTIONBARS_ENABLED') then
            gw_setupActionbars()
          --  gw_set_actionbars()
        end  
        
                
        if gwGetSetting('CHATBUBBLES_ENABLED') then
            gw_register_chatbubbles()
        end
        
        -- create new microbuttons
        create_micro_menu()
        
        if gwGetSetting('GROUP_FRAMES') then
            gw_register_partyframes()
            gw_register_raidframes()
        end
        

        
        -- move error frame
        UIErrorsFrame:ClearAllPoints()
		gw_register_movable_frame('errorframe',UIErrorsFrame,'WARNIG_MESSAGE','GwErrorFrameDummy')
        UIErrorsFrame:SetPoint(gwGetSetting('WARNIG_MESSAGE')['point'],UIParent,gwGetSetting('WARNIG_MESSAGE')['relativePoint'],gwGetSetting('WARNIG_MESSAGE')['xOfs'],gwGetSetting('WARNIG_MESSAGE')['yOfs'])
        
        gwUpdateHudScale()
  
            
        
end)
l:RegisterEvent('ADDON_LOADED')
l:RegisterEvent('PLAYER_ENTERING_WORLD')


function GwaddTOClique(frame)
    if type(frame) == "string" then
        local frameName = frame
        frame = _G[frameName]
        if not frame then
          
        end
    end

    if frame and frame.RegisterForClicks and ClickCastFrames~=nil then
        ClickCastFrames[frame] = true
    end
end


