--[[ **********************************************
-- 		Shared Functions for Broker_Everything
--***********************************************]]
local addon, ns = ...
local shared = {}


-- function to hide the tooltip, if the mouse isn't over it.
shared.hideTooltip = function(tooltip)
	if MouseIsOver(tooltip) then return end
	tooltip:SetScript('OnLeave', nil)
	LibQTip:Release(tooltip)
	tooltip = nil
end

-- Function to anchor & show the tooltip
shared.createTooltip = function(frame, tooltip)
	if Broker_EverythingDB.tooltipScale == false then 
		tooltip:SetScale(1)
	else 
		tooltip:SetScale(tonumber(GetCVar("uiScale")))
	end
	tooltip:SmartAnchorTo(frame)
	tooltip:UpdateScrolling()
	tooltip:Show()
end


--
-- text coloring
--
shared.color = {
	className  = function(x, class)  -- When providing the class name.
	local classcol = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class] or {["r"] = 1, ["g"] = 1, ["b"] = 0}
	return format("|cff%02x%02x%02x%s|r", classcol["r"]*255, classcol["g"]*255, classcol["b"]*255, x)

		--local classcolor = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
		--return format("|c%s%s|r", classcolor.colorStr, x)
	end,
	
	red = function(x)
		return format("|cffc41f3b%s|r", x)
	end,

	brtred = function(x)
		return format("|cffff0000%s|r", x)
	end,

	orange = function(x)
		return format("|cffff7d0a%s|r", x)
	end,

	ltyellow = function(x)
		return format("|cfffff569%s|r", x)
	end,

	dkyellow = function(x)
		return format("|cFFFFCC00%s|r", x)
	end,

	ltblue = function(x)
		return format("|cff69ccf0%s|r", x)
	end,

	ltgreen = function(x)
		return format("|cffabd473%s|r", x)
	end,

	dkgreen = function(x)
		return format("|cff00ff00%s|r", x)
	end,

	grey = function(x)
		return format("|cff808080%s|r", x)
	end,

	class = function(x,c)
		local co = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
		co = co[c].colorStr
--		local r, g, b = floor(co.r * 255), floor(co.g * 255), floor(co.b * 255)
--		return string.format("|cFF%02x%02x%02x%s|r", r, g, b, x)
		return format("|c%s%s", co, x)

	end,

	white = function(x)
		return format("|cffffffff%s|r", x)
	end,
}


--[[*****************************************************
--		Function to Sort a table by the keys
-- Sort function fom http://www.lua.org/pil/19.3.html
********************************************************]]
shared.pairsByKeys = function(t, f)
      local a = {}
      for n in pairs(t) do table.insert(a, n) end
      table.sort(a, f)
      local i = 0      -- iterator variable
      local iter = function ()   -- iterator function
        i = i + 1
        if a[i] == nil then return nil
        else return a[i], t[a[i]]
        end
      end
      return iter
    end


--[[ ****************************************************
--		Function to split a string
--	http://stackoverflow.com/questions/1426954/split-string-in-lua 
--	Because mucking around with strings makes my head hurt.
*********************************************************]]
shared.splitText = function(inputstr, sep)
       if sep == nil then
                sep = "%s"
        end
        local t={} 
        local i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                t[i] = strtrim(str) .. sep
                i = i + 1
        end
     
      return table.concat(t, "|n")
end

	
ns.shared = shared