XPerlLocked		= 1
XPerl_OutOfCombatQueue	= {}

local conf
local ConfigRequesters = {}

-- GiveConfig
local function GiveConfig()
	conf = XPerlConfig
	for k,v in pairs(ConfigRequesters) do
		v(conf)
	end
end

-- XPerl_RequestConfig
-- Setup a callback to give config around to local variables
function XPerl_RequestConfig(getConfig)
	tinsert(ConfigRequesters, getConfig)
	if (XPerlConfig) then
		getConfig(XPerlConfig)
	end
end

-- XPerl_ShowMessage
-- debug function
function XPerl_ShowMessage(cMsg)
	local str = "|c00FF7F00"..event.."|r"
	str = str..", "..tostring(arg1)
	if (arg1) then
		if (strfind(arg1, "^raid") or strfind(arg1, "^party") or strfind(arg1, "^player") or strfind(arg1, "^pet") or strfind(arg1, "^target")) then
			str = str.."("..UnitName(arg1)..")"
		end
	end

	str = str..", "..tostring(arg2)
	str = str..", "..tostring(arg3)
	str = str..", "..tostring(arg4)
	str = str..", "..tostring(arg5)
	str = str..", "..tostring(arg6)
	str = str..", "..tostring(arg7)
	str = str..", "..tostring(arg8)
	str = str..", "..tostring(arg9)

	if (cMsg) then
		str = cMsg.." - "..str
	end

	local cf = ChatFrame7
	if (not cf:IsVisible()) then
		cf = DEFAULT_CHAT_FRAME
	end
	cf:AddMessage("|c00007F7F"..this:GetName().."|r - "..str)
end

function XPerl_Notice(str)
	if (DEFAULT_CHAT_FRAME) then
		DEFAULT_CHAT_FRAME:AddMessage(XPerl_ProductName.." - |c00B03030"..str)
	end
end

-- XPerl_SetMyGlobal
function XPerl_SetMyGlobal()
	if (XPerlConfigSavePerCharacter) then
		if (not XPerlConfig_Global) then
			XPerlConfig_Global = {}
		end

		if (not XPerlConfig_Global[GetRealmName()]) then
			XPerlConfig_Global[GetRealmName()] = {}
		end

		XPerlConfig_Global[GetRealmName()][UnitName("player")] = XPerlConfig
	--else
	--	if (XPerlConfig_Global[GetRealmName()][UnitName("player")]) then
	--		XPerlConfig = XPerlConfig_Global[GetRealmName()][UnitName("player")]
	--	end
	end

	GiveConfig()
end

local function DefaultVar(name, value)
	if (conf[name] == nil or (type(value) ~= type(conf[name]))) then
		conf[name] = value
	end
end

function XPerl_DefaultBarColours(reset)
	if (reset) then
		conf.ClassicHealthBar = nil
		conf.ColourHealthEmpty = nil
		conf.ColourHealthFull = nil
		conf.ColourMana = nil
		conf.ColourEnergy = nil
		conf.ColourRage = nil
		conf.ColourFocus = nil
	end

	DefaultVar("ClassicHealthBar",		1)
	DefaultVar("ColourHealthEmpty",		{r = 1, g = 0, b = 0})
	DefaultVar("ColourHealthFull",		{r = 0, g = 1, b = 0})
	DefaultVar("ColourMana",		{r = 0, g = 0, b = 1})
	DefaultVar("ColourEnergy",		{r = 1, g = 1, b = 0})
	DefaultVar("ColourRage",		{r = 1, g = 0, b = 0})
	DefaultVar("ColourFocus",		{r = 1, g = 0.5, b = 0.25})
end

function XPerl_DefaultReactionColours(reset)
	if (reset) then
		conf.ColourReactionEnemy = nil
		conf.ColourReactionNeutral = nil
		conf.ColourReactionUnfriendly = nil
		conf.ColourReactionFriend = nil
		conf.ColourReactionNone = nil
	        conf.ColourTapped = nil
	end

	DefaultVar("ColourReactionEnemy",	{r = 1, g = 0, b = 0})
	DefaultVar("ColourReactionNeutral",	{r = 1, g = 1, b = 0})
	DefaultVar("ColourReactionUnfriendly",	{r = 1, g = 0.5, b = 0})
	DefaultVar("ColourReactionFriend",	{r = 0, g = 1, b = 0})
	DefaultVar("ColourReactionNone",	{r = 0.5, g = 0.5, b = 1})
        DefaultVar("ColourTapped",		{r = 0.5, g = 0.5, b = 0.5})
end

-- XPerl_DefaultRaidClasses
function XPerl_DefaultRaidClasses(reset)
	if (reset) then
		for i = 1,9 do
			conf["RaidClass"..i] = nil
		end
	end

	DefaultVar("RaidClass1",		"WARRIOR")
	DefaultVar("RaidClass2",		"ROGUE")
	DefaultVar("RaidClass3",		"HUNTER")
	DefaultVar("RaidClass4",		"MAGE")
	DefaultVar("RaidClass5",		"WARLOCK")
	DefaultVar("RaidClass6",		"PRIEST")
	DefaultVar("RaidClass7",		"DRUID")
	DefaultVar("RaidClass8",		"SHAMAN")
	DefaultVar("RaidClass9",		"PALADIN")
end

-- XPerl_Defaults
function XPerl_Defaults()

	local _, class = UnitClass("player")

	GiveConfig()
	if (type(XPerl_Tutorial) == "table") then XPerl_Tutorial:Reset() end

	DefaultVar("BarTextures",		1)
	DefaultVar("BackgroundTextures",	0)		-- 1.8.6
	DefaultVar("Transparency",		0.8)
	DefaultVar("TextTransparency",		1)
	DefaultVar("BackColour",		{r = 0, g = 0, b = 0, a = 1})
	DefaultVar("BorderColour",		{r = 0.5, g = 0.5, b = 0.5, a = 1})
	if (not conf.BackColour.a) then
		conf.BackColour.a = 1
	end
	if (not conf.BorderColour.a) then
		conf.BorderColour.a = 1
	end

	DefaultVar("OldCastBar",		1)
	DefaultVar("CastTime",			1)
	DefaultVar("ClassColouredNames",	1)
	DefaultVar("ApplyToGuildList",		1)		-- 1.8.9
	DefaultVar("HighlightSelection",	1)
	DefaultVar("MinimapButtonPosition",	186)
	DefaultVar("MinimapButtonShown",	1)
	DefaultVar("PerlCombatFlash",		1)
	DefaultVar("CombatHitIndicator",	1)
	DefaultVar("HighlightDebuffs",		1)
	DefaultVar("HighlightDebuffsBorder",	1)
	DefaultVar("HighlightDebuffsClass",	0)		-- 1.9.4
	DefaultVar("BuffTooltipHelper",		1)
	DefaultVar("FadingTooltip",		1)		-- 1.8.3
	DefaultVar("FatHealthBars",		1)
	DefaultVar("FadingBars",		1)		-- 1.8.9
	DefaultVar("FadingBarsTime",		0.5)		-- 1.9.1
	DefaultVar("MaximumScale",		1.5)
	DefaultVar("OptionsColour",		{r = 0.7, g = 0.2, b = 0.2})	-- 1.8.3
	DefaultVar("InverseBars",		0)		-- 1.8.6
	DefaultVar("XPerlTooltipInfo",		0)		-- 1.8.6
	DefaultVar("UnitTooltips",		1)		-- 2.0.5
	DefaultVar("UnitTooltipsModifiers",	"all")		-- 2.0.6

	DefaultVar("RangeFinder", {enabled = conf.SpellRangeCheck == 1,	-- 2.1.7
				Main		= {enabled = true, FadeAmount = XPerlConfig.RangeFadeAmount or 0.3, PlusHealth = XPerlConfig.SpellRangeCheckPlusHealth == 1, HealthLowPoint = XPerlConfig.RangeHealthLowPoint or 0.7},
				NameFrame	= {		   FadeAmount = XPerlConfig.RangeFadeAmount or 0.3,			HealthLowPoint = 0.7},
				StatsFrame	= {		   FadeAmount = XPerlConfig.RangeFadeAmount or 0.3, PlusHealth = true,	HealthLowPoint = 0.7},
			})

	XPerl_DefaultBarColours()
	XPerl_DefaultReactionColours()

	local healerModeDefault = XPerlConfig.HealerMode
	if (not healerModeDefault) then
		healerModeDefault = 0
	end
	local healerModeDefaultType = XPerlConfig.HealerModeType
	if (not healerModeDefaultType) then
		healerModeDefaultType = 1
	end

	-- Player options
	DefaultVar("ArcaneBarPlayer",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("ShowPlayerPortrait",	1)
	DefaultVar("ShowPlayerPortrait3D",	1)
	DefaultVar("ShowPlayerLevel",		1)
	DefaultVar("ShowPlayerClassIcon",	1)
	DefaultVar("ShowPlayerXPBar",		0)
	DefaultVar("ShowPlayerRepBar",		0)		-- 2.0.6
	DefaultVar("ShowPlayerPVPRank",		1)
	DefaultVar("ShowPlayerPVP",		1)		-- 1.8.3
	DefaultVar("ShowPlayerValues",		1)
	DefaultVar("ShowPlayerPercent",		1)
	DefaultVar("Scale_PlayerFrame",		0.8)
	DefaultVar("ShowPartyNumber",		1)
	DefaultVar("ShowPartyNumberWithName",	0)		-- 2.1.3
	DefaultVar("EnergyTicker",		1)
	DefaultVar("FullScreenStatus",		0)		-- 1.8.4
	DefaultVar("FullScreenStatusWarn",	20)
	DefaultVar("FullScreenStatusOK",	40)
	DefaultVar("HealerModePlayer",		healerModeDefault)	-- 1.9.1
	DefaultVar("HealerModePlayerType",	healerModeDefaultType)	-- 1.9.1

	-- Player Pet
	DefaultVar("ShowPlayerPetPortrait",	1)
	DefaultVar("ShowPlayerPetPortrait3D",	1)		-- 1.8.3
	DefaultVar("PetHappiness",		1)
	DefaultVar("PetHappinessSad",		1)		-- Only show happiness when not happy
	DefaultVar("PetFlashWhenSad",		1)
	DefaultVar("ShowPetLevel",		1)
	DefaultVar("Scale_PetFrame",		0.8)
	DefaultVar("ShowPetXP",			1)
	DefaultVar("ShowPlayerPetName",		1)
	DefaultVar("PlayerPetBuffSize",		20)
	DefaultVar("ShowPlayerPetValues",	1)
	DefaultVar("PetCombatHitIndicator",	XPerlConfig.CombatHitIndicator)	-- 2.1.7

	-- Target
	DefaultVar("ArcaneBarTarget",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("ShowTargetPortrait",	1)
	DefaultVar("ShowTargetPortrait3D",	1)		-- 1.8.3
	DefaultVar("ShowTargetClassIcon",	1)
	DefaultVar("ShowTargetClassText",	0)		-- 2.0.6
	DefaultVar("ShowTargetMobType",		1)
	DefaultVar("ShowTargetLevel",		1)
	DefaultVar("ShowTargetElite",		2)		-- Default changed to 2 for 2.1.7
	DefaultVar("ShowTargetMana",		1)
	DefaultVar("ShowTargetPercent",		1)
	DefaultVar("ShowTargetValues",		1)
	DefaultVar("UseCPMeter",		1)
	DefaultVar("BlizzardCPMeter",		0)
	DefaultVar("BlizzardCPPosition",	"top")
	DefaultVar("ShowTargetPVPRank",		1)
	DefaultVar("ShowTargetPVP",		1)		-- 1.8.3
	DefaultVar("Show30YardSymbol",		1)
	DefaultVar("Scale_TargetFrame",		0.8)
	DefaultVar("AlternateRaidIcon",		1)
	DefaultVar("TargetBuffSize",		22)
	DefaultVar("TargetBuffRows",		3)
	DefaultVar("TargetBuffsAbove",		0)
	DefaultVar("TargetReactionHighlight",	0)		-- 1.8.6
	DefaultVar("HealerModeTarget",		healerModeDefault)	-- 1.9.1
	DefaultVar("HealerModeTargetType",	healerModeDefaultType)	-- 1.9.1
	DefaultVar("TargetChangeDefer",		1)		-- 1.9.5
	DefaultVar("TargetCombatHitIndicator",	XPerlConfig.CombatHitIndicator)	-- 2.1.7
	DefaultVar("TargetDebuffHighlight",	0)
	DefaultVar("TargetDebuffHighlightWho",	1)		-- 2.2.0

	-- Focus
	DefaultVar("ArcaneBarFocus",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("ShowFocusPortrait",		1)		-- 2.0.0
	DefaultVar("ShowFocusPortrait3D",	1)		-- 2.0.0
	DefaultVar("ShowFocusClassIcon",	1)		-- 2.0.0
	DefaultVar("ShowFocusClassText",	0)		-- 2.0.6
	DefaultVar("ShowFocusMobType",		1)		-- 2.0.0
	DefaultVar("ShowFocusLevel",		1)		-- 2.0.0
	DefaultVar("ShowFocusElite",		2)		-- 2.0.0 - Default changed to 2 for 2.1.7
	DefaultVar("ShowFocusMana",		1)		-- 2.0.0
	DefaultVar("ShowFocusPercent",		1)		-- 2.0.0
	DefaultVar("ShowFocusValues",		1)		-- 2.0.0
	DefaultVar("ShowFocusPVPRank",		1)		-- 2.0.0
	DefaultVar("ShowFocusPVP",		1)		-- 2.0.0
	DefaultVar("Scale_FocusFrame",		0.8)		-- 2.0.0
	DefaultVar("FocusBuffSize",		22)		-- 2.0.0
	DefaultVar("FocusBuffRows",		3)		-- 2.0.0
	DefaultVar("FocusBuffsAbove",		0)		-- 2.0.0
	DefaultVar("FocusReactionHighlight",	0)		-- 2.0.0
	DefaultVar("HealerModeFocus",		healerModeDefault)		-- 2.0.0
	DefaultVar("HealerModeFocysType",	healerModeDefaultType)		-- 2.0.0
	DefaultVar("FocusCombatHitIndicator",	XPerlConfig.CombatHitIndicator)	-- 2.1.7

	-- Target's Target
	DefaultVar("ShowTargetTarget",		1)
	DefaultVar("TargetTargetHistory",	0)
	DefaultVar("ShowTargetTargetTarget",	1)
	DefaultVar("TargetTargetBuffs",		1)
	DefaultVar("TargetCastableBuffs",	0)
	DefaultVar("TargetCurableDebuffs",	0)
	DefaultVar("TargetTargetTargetBuffs",	1)
	DefaultVar("Scale_TargetTargetFrame",	0.8)
	DefaultVar("ShowTargetTargetPercent",	1)
	DefaultVar("ShowTargetTargetValues",	1)
	DefaultVar("ShowTargetTargetLevel",	1)
	DefaultVar("ShowTargetTargetMana",	1)
	DefaultVar("TargetTargetBuffsAbove",	0)

	-- Cast Party
	DefaultVar("CastParty",			1)
	DefaultVar("CastPartyRaidOnly",		0)		-- Cast Party works on raid frame only?

	-- Party
	DefaultVar("ArcaneBarParty",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("PartySpacing",		23)
	DefaultVar("PartyAnchor",		"TOP")
	DefaultVar("ShowPartyPortrait",		1)
	DefaultVar("ShowPartyPortrait3D",	1)		-- 1.8.3
	DefaultVar("ShowPartyTarget",		1)
	DefaultVar("PartyTargetLarge",		1)
	DefaultVar("PartyTargetSize",		120)		-- 2.0.9
	DefaultVar("ShowPartyLevel",		1)
	DefaultVar("ShowPartyNames",		1)
	DefaultVar("ShowPartyValues",		1)
	DefaultVar("ShowPartyPercent",		1)
	DefaultVar("ShowPartyClassIcon",	1)
	DefaultVar("ShowPartyPVP",		1)		-- 1.8.3
	DefaultVar("ShowPartyRaid",		1)
	DefaultVar("PartyBuffs",		1)
	DefaultVar("PartyDebuffs",		1)
	DefaultVar("PartyCastableBuffs",	0)
	DefaultVar("PartyCurableDebuffs",	0)
	DefaultVar("Scale_PartyFrame",		0.8)
	DefaultVar("PartyDebuffsBelow",		0)
	DefaultVar("ShowPartyPets",		1)
	DefaultVar("Scale_PartyPets",		conf.Scale_PartyFrame)
	DefaultVar("ShowPartyPetName",		1)
	DefaultVar("ShowPartyPetBuffs",		1)		-- 1.8.3
	DefaultVar("ShowPartyPetMana",		1)		-- 1.9.1
	DefaultVar("ShowPartyPetLevel",		1)		-- 1.9.1
	DefaultVar("ShowParty30YardSymbol",	1)
	DefaultVar("PartyBuffSize",		20)
	DefaultVar("HealerModeParty",		healerModeDefault)	-- 1.9.1
	DefaultVar("HealerModePartyType",	healerModeDefaultType)	-- 1.9.1
	DefaultVar("PartyCombatHitIndicator",	XPerlConfig.CombatHitIndicator)	-- 2.1.7

	-- Raid
	DefaultVar("SortRaidByClass",		0)
	DefaultVar("ShowRaid",			1)
	DefaultVar("ShowRaidPets",		0)		-- 2.1.3
	DefaultVar("ShowRaidPetsHunter",	1)		-- 2.1.3
	DefaultVar("ShowRaidPetsWarlock",	1)		-- 2.1.3
	DefaultVar("ShowGroup1",		1)
	DefaultVar("ShowGroup2",		1)
	DefaultVar("ShowGroup3",		1)
	DefaultVar("ShowGroup4",		1)
	DefaultVar("ShowGroup5",		1)
	DefaultVar("ShowGroup6",		1)
	DefaultVar("ShowGroup7",		1)
	DefaultVar("ShowGroup8",		1)
	DefaultVar("ShowGroup9",		1)
	XPerl_DefaultRaidClasses()
	DefaultVar("RaidClass1Enable",		1)
	DefaultVar("RaidClass2Enable",		1)
	DefaultVar("RaidClass3Enable",		1)
	DefaultVar("RaidClass4Enable",		1)
	DefaultVar("RaidClass5Enable",		1)
	DefaultVar("RaidClass6Enable",		1)
	DefaultVar("RaidClass7Enable",		1)
	DefaultVar("RaidClass8Enable",		1)
	DefaultVar("RaidClass9Enable",		1)

	DefaultVar("ShowRaidTitles",		1)
	DefaultVar("ShowRaidPercents",		1)
	DefaultVar("Scale_Raid",		0.8)
	DefaultVar("RaidVerticalSpacing",	0)
	DefaultVar("RaidBuffs",			1)
	DefaultVar("RaidDebuffs",		0)
	DefaultVar("BuffsCastableCurable",	0)
	DefaultVar("RaidBuffsRight",		1)
	DefaultVar("RaidBuffsInside",		1)
	DefaultVar("RaidBuffsUntilDebuffed",	0)		-- 2.1.3
	DefaultVar("RaidMana",			1)		-- 1.8.3
	DefaultVar("HealerModeRaid",		healerModeDefault)	-- 1.9.1
	DefaultVar("RaidAnchor",		"TOP")
	DefaultVar("SortRaidAlpha",		0)		-- 2.0.4

	if (class == "PRIEST" or class == "DRUID") then
		DefaultVar("RaidHighlights",		1)	-- 2.1.7
		DefaultVar("RaidHighlightHoTs",		1)      -- 2.1.7
		DefaultVar("RaidHighlightShields",	1)      -- 2.1.9
		DefaultVar("RaidHighlightAggro",	1)      -- 2.1.7
		DefaultVar("RaidHighlightMissing",	0)	-- 2.1.9
	elseif (class == "MAGE") then
		DefaultVar("RaidHighlights",		0)	-- 2.1.7
		DefaultVar("RaidHighlightHoTs",		0)      -- 2.1.7
		DefaultVar("RaidHighlightShields",	0)      -- 2.1.9
		DefaultVar("RaidHighlightAggro",	0)      -- 2.1.7
		DefaultVar("RaidHighlightMissing",	0)	-- 2.1.9
	elseif (class == "PALADIN") then
		DefaultVar("RaidHighlights",		1)	-- 2.1.7
		DefaultVar("RaidHighlightHoTs",		0)      -- 2.1.7
		DefaultVar("RaidHighlightShields",	1)      -- 2.1.9
		DefaultVar("RaidHighlightAggro",	1)      -- 2.1.7
		DefaultVar("RaidHighlightMissing",	0)	-- 2.1.9
	else
		DefaultVar("RaidHighlights",		0)	-- 2.1.7
		DefaultVar("RaidHighlightHoTs",		0)      -- 2.1.7
		DefaultVar("RaidHighlightShields",	0)      -- 2.1.9
		DefaultVar("RaidHighlightAggro",	0)      -- 2.1.7
		DefaultVar("RaidHighlightMissing",	0)	-- 2.1.9
	end
	DefaultVar("RaidHighlightMissingAll",	0)		-- 2.1.9

	XPerlConfig.ArcaneBar		= nil		-- 2.0.0 replaced by per frame options
	XPerlConfig.RaidUpward		= nil		-- 2.0.0 (beta10) replaced by anchor option
	XPerlConfig.RaidMotion		= nil		-- 2.0.0 removed, not possible
	XPerlConfig.RaidPositions	= nil		-- 2.0.0 unused

	XPerlConfig.SpellRangeCheck	= nil		-- 2.1.7 Replaced by improved range finder options
	XPerlConfig.RangeFadeAmount	= nil		-- 2.1.7    "      "     "      "     "       "
	XPerlConfig.SpellRangeCheckPlusHealth = nil     -- 2.1.7    "      "     "      "     "       "
	XPerlConfig.RangeHealthLowPoint = nil           -- 2.1.7    "      "     "      "     "       "

end

-- XPerl_ResetDefaults
function XPerl_ResetDefaults()

	XPerlConfig = {}
	GiveConfig()
	XPerl_Defaults()

	if (XPerlConfigSavePerCharacter) then
		XPerlConfig_Global[GetRealmName()][UnitName("player")] = XPerlConfig
	end

	XPerl_OptionActions()

	if (XPerl_Options and XPerl_Options:IsShown()) then
		XPerl_Options:Hide()
		XPerl_Options:Show()
	end
end

-- XPerl_Globals_OnEvent
function XPerl_Globals_OnEvent(self, event, unit, spell, rank, targetName)
	if (arg1 and XPerl_UnitEvent(arg1, event)) then
		return
	end

	if (event == "VARIABLES_LOADED") then
		-- Between sessions variable saving.
		self:UnregisterEvent(event)
		local newUser

		newUser = not XPerlConfig

		if (XPerlConfigSavePerCharacter) then
			local realm = GetRealmName()
			local name = UnitName("player")

			if (not XPerlConfig_Global) then
				XPerlConfig_Global = {}
			end

			if (not XPerlConfig_Global[realm]) then
				XPerlConfig_Global[realm] = {}
			end

			if (not XPerlConfig_Global[realm][name]) then
				XPerlConfig_Global[realm][name] = {}

				if (XPerlConfig and XPerlConfig.BarTextures) then
					XPerlConfig_Global[realm][name] = XPerlConfig
				end
			else
				XPerlConfig = XPerlConfig_Global[realm][name]
			end

			if (not XPerlConfig) then
				XPerlConfig = XPerlConfig_Global[realm][name]
			end
		else
			if (not XPerlConfig) then
				XPerlConfig = {}
			end
		end

		GiveConfig()

		-- Tell DHUD to hide Blizzard default Player and Target frames
		if (DHUD_Config) then
			local bChanged = false
			if (XPerl_Player) then
				DHUD_Config["bplayer"] = 0
				bChanged = true
			end
			if (XPerl_Target) then
				DHUD_Config["btarget"] = 0
				bChanged = true
			end
		end

		-- Variable checking only occurs for new install and version number change
		if ((not conf.ConfigVersion) or (conf.ConfigVersion ~= XPerl_VersionNumber)) then
			conf.ConfigVersion = XPerl_VersionNumber;	-- Set ConfigVersion to current

			XPerl_Defaults()
		end

		if (newUser) then
			conf.ShowTutorials = true
		end

		XPerl_RegisterOptionChanger(XPerl_StartupSpellRange)

		XPerl_Init()

	elseif (event == "PLAYER_REGEN_ENABLED") then
		if (XPerl_OutOfCombatOptionSet) then
			XPerl_OptionActions()
		end

		local queue = XPerl_OutOfCombatQueue
		XPerl_OutOfCombatQueue = {}
		for k,v in pairs(queue) do
			if (type(v) == "function") then
				v()
			elseif (type(v) == "table") then
				v[1](v[2])
			elseif (type(v) == "string") then
				RunScript(v)
			end
		end
	end
end

-- XPerl_ProtectedCall
function XPerl_ProtectedCall(func, self)
	if (InCombatLockdown()) then
		if (self) then
			tinsert(XPerl_OutOfCombatQueue, {func, self})
		else
			tinsert(XPerl_OutOfCombatQueue, func)
		end
	else
		func(self)
	end
end
