function LeoAltholicToolbarUI:OnWindowMoveStop()
    LeoAltholic.globalData.positionToolbar = {
        left = LeoAltholicToolbar:GetLeft(),
        top = LeoAltholicToolbar:GetTop()
    }
end

function LeoAltholicToolbarUI.RestorePosition()
    local position = LeoAltholic.globalData.positionToolbar or { left = 100; top = 100; }
    local left = position.left
    local top = position.top

    LeoAltholicToolbar:ClearAnchors()
    LeoAltholicToolbar:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
    if LeoAltholic.globalData.settings.toolbar.enabled == false then
        LeoAltholicToolbar:SetHidden(true)
    else
        LeoAltholicToolbar:SetHidden(false)
    end
end

function LeoAltholicToolbarUI.bumpCompass()

    if LeoAltholic.globalData.settings.toolbar.enabled and LeoAltholic.globalData.settings.toolbar.bumpCompass then
        if LeoAltholicToolbar:GetTop() <= 60 then
            if math.floor(ZO_CompassFrame:GetTop()) ~= math.floor(LeoAltholicToolbar:GetTop()) + 60 then
                ZO_CompassFrame:ClearAnchors()
                ZO_CompassFrame:SetAnchor( TOP, GuiRoot, TOP, 0, LeoAltholicToolbar:GetTop() + 60)
                ZO_TargetUnitFramereticleover:ClearAnchors()
                ZO_TargetUnitFramereticleover:SetAnchor( TOP, GuiRoot, TOP, 0, LeoAltholicToolbar:GetTop() + 110)
            end
        elseif ZO_CompassFrame:GetTop() ~= 40 then
            ZO_CompassFrame:ClearAnchors()
            ZO_CompassFrame:SetAnchor( TOP, GuiRoot, TOP, 0, 40 )
            ZO_TargetUnitFramereticleover:ClearAnchors()
            ZO_TargetUnitFramereticleover:SetAnchor( TOP, GuiRoot, TOP, 0, 88 )
        end
    elseif ZO_CompassFrame:GetTop() ~= 40 then
        ZO_CompassFrame:ClearAnchors()
        ZO_CompassFrame:SetAnchor( TOP, GuiRoot, TOP, 0, 40 )
        ZO_TargetUnitFramereticleover:ClearAnchors()
        ZO_TargetUnitFramereticleover:SetAnchor( TOP, GuiRoot, TOP, 0, 88 )
    end
end

function LeoAltholicToolbarUI.update()

    LeoAltholicToolbarUI.bumpCompass()

    if not LeoAltholic.globalData.settings.toolbar.enabled then return end

    local char = LeoAltholic.GetMyself()
    local toolbar = WINDOW_MANAGER:GetControlByName("LeoAltholicToolbar")

    local offsetX = 0
    local width = 120
    local numSections = 0

    local label = GetControl(toolbar, "Riding")
    local texture = GetControl(toolbar, "RidingTexture")
    label:SetHidden(true)
    texture:SetHidden(true)
    if LeoAltholic.globalData.settings.toolbar.riding and
            (char.attributes.riding.speed < char.attributes.riding.speedMax or
            char.attributes.riding.stamina < char.attributes.riding.staminaMax or
            char.attributes.riding.capacity < char.attributes.riding.capacityMax) then
        label:SetText(LeoAltholic.FormatTime(char.attributes.riding.time - GetTimeStamp(), true, true))
        label:SetHidden(false)
        texture:SetHidden(false)
        offsetX = offsetX + 110
        numSections = numSections + 1
    end

    for _,craft in pairs(LeoAltholic.craftResearch) do
        label = GetControl(toolbar, "Craft"..craft)
        texture = GetControl(toolbar, "Craft"..craft.."Texture")
        label:SetHidden(true)
        texture:SetHidden(true)
    end
    if LeoAltholic.globalData.settings.toolbar.researching then
        for _,craft in pairs(LeoAltholic.craftResearch) do
            if LeoAltholic.HasStillResearchFor(craft) then
                label = GetControl(toolbar, "Craft"..craft)
                texture = GetControl(toolbar, "Craft"..craft.."Texture")
                local researching, total, lowest = LeoAltholic.GetResearchCounters(craft)
                local color = '|c21A121'
                if researching < total then
                    color = '|cCB110E'
                end
                label:SetText(color .. researching .. '/' .. total .. '|r ' .. LeoAltholic.FormatTime(lowest, true, true))
                label:SetHidden(false)
                texture:SetHidden(false)
                texture:SetAnchor(TOPLEFT, toolbar, TOPLEFT, offsetX, 0)
                label:SetAnchor(TOPLEFT, texture, TOPRIGHT, 2, 0)
                offsetX = offsetX + width
                numSections = numSections + 1
            end
        end
    end

    --[[
    if LeoAltholic.globalData.settings.toolbar.checklist then
        if char.attributes.riding.time - GetTimeStamp() < 0 then
            label = GetControl(toolbar, "Checklist")
            label:SetText("|t28:28:esoui/art/tutorial/store_indexicon_mounts_up.dds|t")
            label:SetAnchor(TOPLEFT, toolbar, TOPLEFT, offsetX, 0)
            label:SetHidden(false)
        else
            label = GetControl(toolbar, "Checklist")
            label:SetText("|cFF0000|t28:28:esoui/art/tutorial/store_indexicon_mounts_up.dds|t|r")
            label:SetAnchor(TOPLEFT, toolbar, TOPLEFT, offsetX, 0)
            label:SetHidden(false)
        end
    end
    ]]

    if numSections > 0 then
        toolbar:SetWidth(numSections * 130)
        GetControl(toolbar, "BG"):SetWidth(numSections * 130)
    else
        toolbar:SetHidden(true)
    end
end