--creates all the clool things this addon does some code used from wmarker as it had alot of code pertaining to some of these functions
local L = MaggzMarkerLocals
local options = MaggzMarker.options
local defaultBackdrop = {
	bgFile = "Interface\\TabardFrame\\TabardFrameBackground",
	edgeFile = "Interface\\LFGFRAME\\LFGBorder",
	tile = true,
	tileSize = 16,
	edgeSize = 16,
	insets = {left = 4, right = 4, top = 4, bottom = 4,}
}
local mM = "MaggzMarker"
local mWM ="MaggzWM"
local combatWait = false
local checkUpdate
-- Default database values
local mM_defaults = {
    crf= false,
	locked= false,
	clamped = false,
	shown = true,
	flipped = false,
	vertical = false,
	partyShow = false,
	targetShow = false,
	assistShow = false,
	bgHide = false,
	tooltips = true,
	detach = false,
	scale = 1,
	alpha = 1,
	iconSpace = 0,
	x = 0,
	y = 0,
	relPt = "CENTER",
}
local mWM_defaults = {
	locked = false,
	clamped = false,
	shown = true,
	flipped = false,
	vertical = false,
	icons = false,
	partyShow = false,
	assistShow = false,
	bgHide = false,
	tooltips = true,
	worldTex = 1,
	scale = 1,
	alpha = 1,
	x= 0,
	y = 50,
	relPt = "CENTER",
}
local mAI_defaults = {
	locked = false,
	clamped = false,
	shown = true,
	bgHide = false,
	sound = false,
	x= -20,
	y = -50,
	relPt = "CENTER",
}

---main window setup
local function visibility()
	if (combatWait==false) then
		if (MaggzMarkerDB.crf==true) then
			CompactRaidFrameManager:UnregisterAllEvents()
			CompactRaidFrameManager:Hide()
			CompactRaidFrameContainer:UnregisterAllEvents()
			CompactRaidFrameContainer:Hide()
		end
		MaggzMarker.main:Show()
		if (MaggzMarkerDB.shown==false) then MaggzMarker.main:Hide() end
		if (MaggzMarkerDB.partyShow==true) then if not (GetNumGroupMembers() > 0) and not (IsInRaid()) then MaggzMarker.main:Hide() end end
		if (MaggzMarkerDB.targetShow==true) then if not (UnitExists("target")) then MaggzMarker.main:Hide() end end
		if (MaggzMarkerDB.assistShow==true) then if (IsInRaid() and not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) ) then MaggzMarker.main:Hide() end end
		MaggzAI.main:Show()
		if (MaggzAIDB.shown==false) then MaggzAI.main:Hide() end
		if not(UnitAffectingCombat("player")) then
			MaggzWM.main:Show()
			if (MaggzWMDB.shown==false) then MaggzWM.main:Hide() end
			if (MaggzWMDB.partyShow==true) then if not (GetNumGroupMembers() > 0) and not (IsInRaid()) then MaggzWM.main:Hide() end end
			if (MaggzWMDB.assistShow==true) then if (IsInRaid() and not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) ) then MaggzWM.main:Hide() end end
		end
	else
		return
		--print("MaggzMarker has SecureActionButtons which cannot be changed during combat!!!")
	end
end

local function lock()
	if (MaggzMarkerDB.locked==true) then
		MaggzMarker.other.moverLeft:SetAlpha(0)
		MaggzMarker.other.moverLeft:EnableMouse(false)
		MaggzMarker.other.moverRight:SetAlpha(0)
		MaggzMarker.other.moverRight:EnableMouse(false)
	elseif (MaggzMarkerDB.locked==false) then
		MaggzMarker.other.moverLeft:SetAlpha(1)
		MaggzMarker.other.moverLeft:EnableMouse(true)
		MaggzMarker.other.moverRight:SetAlpha(1)
		MaggzMarker.other.moverRight:EnableMouse(true)
	end
	if (MaggzWMDB.locked==true) then
		MaggzWM.main.moverLeft:SetAlpha(0)
		MaggzWM.main.moverLeft:EnableMouse(false)
		MaggzWM.main.moverRight:SetAlpha(0)
		MaggzWM.main.moverRight:EnableMouse(false)
	elseif (MaggzWMDB.locked==false) then
		MaggzWM.main.moverLeft:SetAlpha(1)
		MaggzWM.main.moverLeft:EnableMouse(true)
		MaggzWM.main.moverRight:SetAlpha(1)
		MaggzWM.main.moverRight:EnableMouse(true)
	end
		if (MaggzAIDB.locked==true) then
		MaggzAI.main.moverLeft:SetAlpha(0)
		MaggzAI.main.moverLeft:EnableMouse(false)
		MaggzAI.main.moverRight:SetAlpha(0)
		MaggzAI.main.moverRight:EnableMouse(false)
	elseif (MaggzAIDB.locked==false) then
		MaggzAI.main.moverLeft:SetAlpha(1)
		MaggzAI.main.moverLeft:EnableMouse(true)
		MaggzAI.main.moverRight:SetAlpha(1)
		MaggzAI.main.moverRight:EnableMouse(true)
	end
end

local function backgroundVisibility()
	if (MaggzMarkerDB.bgHide==true) then
		MaggzMarker.iconFrame:SetBackdropColor(0,0,0,0)
		MaggzMarker.iconFrame:SetBackdropBorderColor(0,0,0,0)
		MaggzMarker.other.controlFrame:SetBackdropColor(0,0,0,0)
		MaggzMarker.other.controlFrame:SetBackdropBorderColor(0,0,0,0)
	elseif (MaggzMarkerDB.bgHide==false) then
		MaggzMarker.iconFrame:SetBackdropColor(0.1,0.1,0.1,0.7)
		MaggzMarker.iconFrame:SetBackdropBorderColor(1,1,1,1)
		MaggzMarker.other.controlFrame:SetBackdropColor(0.1,0.1,0.1,0.7)
		MaggzMarker.other.controlFrame:SetBackdropBorderColor(1,1,1,1)
	end
	if (MaggzWMDB.bgHide==true) then
		MaggzWM.main:SetBackdropColor(0,0,0,0)
		MaggzWM.main:SetBackdropBorderColor(0,0,0,0)
	elseif (MaggzWMDB.bgHide==false) then
		MaggzWM.main:SetBackdropColor(0.1,0.1,0.1,1)
		MaggzWM.main:SetBackdropBorderColor(1,1,1,1)
	end
	if (MaggzAIDB.bgHide==true) then
		MaggzAI.main:SetBackdropColor(0,0,0,0)
		MaggzAI.main:SetBackdropBorderColor(0,0,0,0)
	elseif (MaggzAIDB.bgHide==false) then
		MaggzAI.main:SetBackdropColor(0.1,0.1,0.1,1)
		MaggzAI.main:SetBackdropBorderColor(1,1,1,1)
	end
end
local function clamp()
	if (MaggzMarkerDB.clamped==true) then MaggzMarker.main:SetClampedToScreen(true) else MaggzMarker.main:SetClampedToScreen(false) end
	if (MaggzWMDB.clamped==true) then MaggzWM.main:SetClampedToScreen(true) else MaggzWM.main:SetClampedToScreen(false) end
	if (MaggzAIDB.clamped==true) then MaggzAI.main:SetClampedToScreen(true) else MaggzAI.main:SetClampedToScreen(false) end
end
function MaggzMarker:lockToggle()
	MaggzMarkerDB.locked = not MaggzMarkerDB.locked
	lock()
	checkUpdate()
end
function MaggzWM:lockToggle()
	MaggzWMDB.locked = not MaggzWMDB.locked
	lock()
	checkUpdate()
end
function MaggzAI:lockToggle()
	MaggzAIDB.locked = not MaggzAIDB.locked
	lock()
	checkUpdate()
end
function MaggzMarker:clampToggle()
	MaggzMarkerDB.clamped = not MaggzMarkerDB.clamped
	clamp()
	checkUpdate()
end
function MaggzWM:clampToggle()
	MaggzWMDB.clamped = not MaggzWMDB.clamped
	clamp()
	checkUpdate()
end
function MaggzAI:clampToggle()
	MaggzAIDB.clamped = not MaggzAIDB.clamped
	clamp()
	checkUpdate()
end
function MaggzMarker:frameFormat(orien)
	if (orien=="horiz") then
		MaggzMarker.main:SetSize(225+(MaggzMarkerDB.iconSpace*7),35)
		MaggzMarker.iconFrame:SetSize(170+(MaggzMarkerDB.iconSpace*7),35)
		MaggzMarker.other.controlFrame:SetSize(95,35)
		MaggzMarker.iconFrame:SetPoint("LEFT", MaggzMarker.main, "LEFT")
		MaggzMarker.other.controlFrame:SetPoint("LEFT", MaggzMarker.iconFrame, "RIGHT")
		MaggzMarker.other.moverLeft:SetSize(20,35)
		MaggzMarker.other.moverRight:SetSize(20,35)
		MaggzMarker.other.moverLeft:SetPoint("RIGHT",MaggzMarker.main,"LEFT")
		MaggzMarker.other.moverRight:SetPoint("LEFT",MaggzMarker.main,"RIGHT",40,0)
	elseif (orien=="vert") then
		MaggzMarker.main:SetSize(35,225+(MaggzMarkerDB.iconSpace*7))
		MaggzMarker.iconFrame:SetSize(35,170+(MaggzMarkerDB.iconSpace*7))
		MaggzMarker.other.controlFrame:SetSize(35,95)
		MaggzMarker.iconFrame:SetPoint("TOP", MaggzMarker.main, "TOP")
		MaggzMarker.other.controlFrame:SetPoint("TOP", MaggzMarker.iconFrame, "BOTTOM")
		MaggzMarker.other.moverLeft:SetSize(35,20)
		MaggzMarker.other.moverRight:SetSize(35,20)
		MaggzMarker.other.moverLeft:SetPoint("BOTTOM",MaggzMarker.main,"TOP")
		MaggzMarker.other.moverRight:SetPoint("TOP",MaggzMarker.main,"BOTTOM",0,-40)
	end
end

function MaggzMarker:orienFormat(dir)
	for k,v in pairs(MaggzMarker.icon) do v:ClearAllPoints() end
	for k,v in pairs(MaggzMarker.other) do v:ClearAllPoints() end
	if (dir==1) then -- Normal
		MaggzMarker.icon["Skull"]:SetPoint("LEFT", MaggzMarker.iconFrame, "LEFT",5,0)
		for i = 2,8 do MaggzMarker.icon[i]:SetPoint("LEFT", MaggzMarker.icon[i-1], "RIGHT",MaggzMarkerDB.iconSpace,0) end
		MaggzMarker.other.clearIcon:SetPoint("LEFT", MaggzMarker.other.controlFrame, "LEFT",10,0)
		MaggzMarker.other.readyCheck:SetPoint("LEFT", MaggzMarker.other.clearIcon, "RIGHT")
		MaggzMarker.other.roleCheck:SetPoint("LEFT", MaggzMarker.other.clearIcon, "RIGHT",18,0)
		MaggzMarker.other.convertRaid:SetPoint("LEFT", MaggzMarker.other.clearIcon, "RIGHT",38,0)
		MaggzMarker:frameFormat("horiz")
	elseif (dir==2) then -- Backwards
		MaggzMarker.icon["Star"]:SetPoint("LEFT",MaggzMarker.iconFrame,"LEFT",5,0)
		for i = 7,1,-1 do MaggzMarker.icon[i]:SetPoint("LEFT",MaggzMarker.icon[i+1],"RIGHT",MaggzMarkerDB.iconSpace,0) end
		MaggzMarker.other.clearIcon:SetPoint("LEFT", MaggzMarker.other.controlFrame, "LEFT",10,0)
		MaggzMarker.other.readyCheck:SetPoint("LEFT", MaggzMarker.other.clearIcon, "RIGHT")
		MaggzMarker.other.roleCheck:SetPoint("LEFT", MaggzMarker.other.clearIcon, "RIGHT",18,0)
		MaggzMarker.other.convertRaid:SetPoint("LEFT", MaggzMarker.other.clearIcon, "RIGHT",38,0)
		MaggzMarker:frameFormat("horiz")
	elseif (dir==3) then -- Normal vertical
		MaggzMarker.icon["Skull"]:SetPoint("TOP", MaggzMarker.iconFrame, "TOP",0,-5)
		for i = 2,8 do MaggzMarker.icon[i]:SetPoint("TOP",MaggzMarker.icon[i-1], "BOTTOM",0,-MaggzMarkerDB.iconSpace) end
		MaggzMarker.other.clearIcon:SetPoint("TOP", MaggzMarker.other.controlFrame, "TOP",0,-10)
		MaggzMarker.other.readyCheck:SetPoint("TOP", MaggzMarker.other.clearIcon, "BOTTOM")
		MaggzMarker.other.roleCheck:SetPoint("TOP", MaggzMarker.other.clearIcon, "BOTTOM",0,-18)
		MaggzMarker.other.convertRaid:SetPoint("TOP", MaggzMarker.other.clearIcon, "BOTTOM",0,-38)
		MaggzMarker:frameFormat("vert")
	elseif (dir==4) then -- Backwards vertical
		MaggzMarker.icon["Star"]:SetPoint("TOP", MaggzMarker.iconFrame, "TOP",0,-5)
		for i = 7,1,-1 do MaggzMarker.icon[i]:SetPoint("TOP", MaggzMarker.icon[i+1], "BOTTOM",0,-MaggzMarkerDB.iconSpace) end
		MaggzMarker.other.clearIcon:SetPoint("TOP", MaggzMarker.other.controlFrame, "TOP",0,-10)
		MaggzMarker.other.readyCheck:SetPoint("TOP", MaggzMarker.other.clearIcon, "BOTTOM")
		MaggzMarker.other.roleCheck:SetPoint("TOP", MaggzMarker.other.clearIcon, "BOTTOM",0,-18)
		MaggzMarker.other.convertRaid:SetPoint("TOP", MaggzMarker.other.clearIcon, "BOTTOM",0,-38)
		MaggzMarker:frameFormat("vert")
	end
end
function MaggzMarker:orien()
	if (MaggzMarkerDB.flipped==false) and (MaggzMarkerDB.vertical==false) then
		MaggzMarker:orienFormat(1)
	elseif (MaggzMarkerDB.flipped==true) and (MaggzMarkerDB.vertical==false) then 
		MaggzMarker:orienFormat(2)	
	elseif (MaggzMarkerDB.flipped==false) and (MaggzMarkerDB.vertical==true) then
		MaggzMarker:orienFormat(3)
	elseif (MaggzMarkerDB.flipped==true) and (MaggzMarkerDB.vertical==true) then
		MaggzMarker:orienFormat(4)
	end
end
function MaggzWM:frameFormat(orien)
	if (orien=="horiz") then
		MaggzWM.main:SetSize(135,30)
		MaggzWM.main.moverLeft:SetSize(20,30)
		MaggzWM.main.moverRight:SetSize(20,30)
		MaggzWM.main.moverLeft:SetPoint("RIGHT",MaggzWM.main,"LEFT")
		MaggzWM.main.moverRight:SetPoint("LEFT",MaggzWM.main,"RIGHT")
	elseif (orien=="vert") then
		MaggzWM.main:SetSize(30,135)
		MaggzWM.main.moverLeft:SetSize(30,20)
		MaggzWM.main.moverRight:SetSize(30,20)
		MaggzWM.main.moverLeft:SetPoint("BOTTOM",MaggzWM.main,"TOP")
		MaggzWM.main.moverRight:SetPoint("TOP",MaggzWM.main,"BOTTOM")
	end
end
function MaggzWM:orienFormat(dir)
	for k,v in pairs(MaggzWM.flare) do v:ClearAllPoints() end
	MaggzWM.flareClear:ClearAllPoints()
	MaggzWM.main.moverLeft:ClearAllPoints()
	MaggzWM.main.moverRight:ClearAllPoints()
	if (dir==1) then -- Normal
		MaggzWM.flare["Square"]:SetPoint("LEFT", MaggzWM.main, "LEFT",5,0)
		for i = 2,5 do MaggzWM.flare[i]:SetPoint("LEFT", MaggzWM.flare[i-1], "RIGHT") end
		MaggzWM.flareClear:SetPoint("LEFT",MaggzWM.flare["Star"],"RIGHT",3,0)
		MaggzWM:frameFormat("horiz")
	elseif (dir==2) then -- Backwards
		MaggzWM.flare["Star"]:SetPoint("LEFT",MaggzWM.main,"LEFT",5,0)
		for i = 4,1,-1 do MaggzWM.flare[i]:SetPoint("LEFT",MaggzWM.flare[i+1],"RIGHT") end
		MaggzWM.flareClear:SetPoint("LEFT",MaggzWM.flare["Square"],"RIGHT",3,0)
		MaggzWM:frameFormat("horiz")
	elseif (dir==3) then -- Normal vertical
		MaggzWM.flare["Square"]:SetPoint("TOP", MaggzWM.main, "TOP",0,-5)
		for i = 2,5 do MaggzWM.flare[i]:SetPoint("TOP",MaggzWM.flare[i-1], "BOTTOM") end
		MaggzWM.flareClear:SetPoint("TOP",MaggzWM.flare["Star"],"BOTTOM",0,-3)
		MaggzWM:frameFormat("vert")
	elseif (dir==4) then -- Backwards vertical
		MaggzWM.flare["Star"]:SetPoint("TOP", MaggzWM.main, "TOP",0,-5)
		for i = 4,1,-1 do MaggzWM.flare[i]:SetPoint("TOP", MaggzWM.flare[i+1], "BOTTOM") end
		MaggzWM.flareClear:SetPoint("TOP",MaggzWM.flare["Square"],"BOTTOM",0,-3)
		MaggzWM:frameFormat("vert")
	end
end
function MaggzWM:orien(dir)
	if (combatWait==false) then
		if not (UnitAffectingCombat("player")) then
			if (MaggzWMDB.flipped==false) and (MaggzWMDB.vertical==false) then
				MaggzWM:orienFormat(1)
			elseif (MaggzWMDB.flipped==true) and (MaggzWMDB.vertical==false) then 
				MaggzWM:orienFormat(2)	
			elseif (MaggzWMDB.flipped==false) and (MaggzWMDB.vertical==true) then
				MaggzWM:orienFormat(3)
			elseif (MaggzWMDB.flipped==true) and (MaggzWMDB.vertical==true) then
				MaggzWM:orienFormat(4)
			end
		end
	else
		return
		--print("MaggzWM has SecureActionButtons which cannot be changed during combat!!!")
	end
end
local function Maggzcrf()
	if (MaggzMarkerDB.crf==true) then
		CompactRaidFrameManager:UnregisterAllEvents()
		CompactRaidFrameManager:Hide()
		CompactRaidFrameContainer:UnregisterAllEvents()
		CompactRaidFrameContainer:Hide()
	elseif (MaggzMarkerDB.crf==false) then
		CompactRaidFrameManager:RegisterAllEvents()
		CompactRaidFrameContainer:RegisterAllEvents()
		if (IsInRaid() or IsInGroup()) then
			CompactRaidFrameManager:Show()
			--CompactRaidFrameContainer:Show()
		end
	end
end
function MaggzAI:crfToggle()
	MaggzMarkerDB.crf = not MaggzMarkerDB.crf
	Maggzcrf()
	checkUpdate()
end
function MaggzMarker:bgToggle()
	MaggzMarkerDB.bgHide = not MaggzMarkerDB.bgHide
	backgroundVisibility()
	checkUpdate()
end
function MaggzWM:bgToggle()
	MaggzWMDB.bgHide = not MaggzWMDB.bgHide
	backgroundVisibility()
	checkUpdate()
end
function MaggzAI:bgToggle()
	MaggzAIDB.bgHide = not MaggzAIDB.bgHide
	backgroundVisibility()
	checkUpdate()
end
local raidScaleText
function MaggzMarker:scale(frame)
	if frame == nil then return end
	MaggzMarkerDB.scale = (frame:GetValue());
	raidScaleText:SetFormattedText("%s: %d%s", L["Raid Markers scale"],math.floor(MaggzMarkerDB.scale*100),"%")
	MaggzMarker.main:SetScale(MaggzMarkerDB.scale);
end
local raidAlphaText
function MaggzMarker:alpha(frame)
	if frame == nil then return end
	MaggzMarkerDB.alpha = (frame:GetValue());
	raidAlphaText:SetFormattedText("%s: %d%s",L["Raid Markers alpha"],math.floor(MaggzMarkerDB.alpha*100),"%")
	MaggzMarker.main:SetAlpha(MaggzMarkerDB.alpha);
end
local raidSpaceText
function MaggzMarker:space(frame)
	if frame == nil then return end
	MaggzMarkerDB.iconSpace = (frame:GetValue());
	raidSpaceText:SetFormattedText("%s: %d",L["Icon spacing"],MaggzMarkerDB.iconSpace)
	MaggzMarker:orien()
end
local worldScaleText
function MaggzWM:scale(frame)
	if frame == nil then return end
	MaggzWMDB.scale = (frame:GetValue());
	worldScaleText:SetFormattedText("%s: %d%s", L["World Markers scale"],math.floor(MaggzWMDB.scale*100),"%")
	MaggzWM.main:SetScale(MaggzWMDB.scale);
end
local worldAlphaText
function MaggzWM:alpha(frame)
	if frame == nil then return end
	MaggzWMDB.alpha = (frame:GetValue());
	worldAlphaText:SetFormattedText("%s: %d%s", L["World Markers alpha"],math.floor(MaggzWMDB.alpha*100),"%")
	MaggzWM.main:SetAlpha(MaggzWMDB.alpha);
end
-----------------------------------------
function MaggzWM:retex(tex)
	if (tex==1) then
		for k,v in pairs(MaggzWM.flare) do v:SetNormalTexture("interface\\minimap\\partyraidblips") end
		MaggzWM.flare["Square"]:GetNormalTexture():SetTexCoord(0.75,0.875,0,0.25)
		MaggzWM.flare["Triangle"]:GetNormalTexture():SetTexCoord(0.25,0.375,0,0.25)
		MaggzWM.flare["Diamond"]:GetNormalTexture():SetTexCoord(0,0.125,0.25,0.5)
		MaggzWM.flare["Cross"]:GetNormalTexture():SetTexCoord(0.625,0.75,0,0.25)
		MaggzWM.flare["Star"]:GetNormalTexture():SetTexCoord(0.375,0.5,0,0.25)
	else
		for k,v in pairs(MaggzWM.flare) do v:SetNormalTexture("interface\\targetingframe\\ui-raidtargetingicons") end
		MaggzWM.flare["Square"]:GetNormalTexture():SetTexCoord(0.25,0.5,0.25,0.5)
		MaggzWM.flare["Triangle"]:GetNormalTexture():SetTexCoord(0.75,1,0,0.25)
		MaggzWM.flare["Diamond"]:GetNormalTexture():SetTexCoord(0.5,0.75,0,0.25)
		MaggzWM.flare["Cross"]:GetNormalTexture():SetTexCoord(0.5,0.75,0.25,0.5)
		MaggzWM.flare["Star"]:GetNormalTexture():SetTexCoord(0,0.25,0,0.25)
	end
end
function MaggzMarker:getLoc()
	local point, relativeTo, relPt, xOff, yOff = MaggzMarker.main:GetPoint()
	MaggzMarkerDB.x = xOff
	MaggzMarkerDB.y = yOff
	MaggzMarkerDB.relPt = relPt
end
function MaggzWM:getLoc()
	local point, relativeTo, relPt, xOff, yOff = MaggzWM.main:GetPoint()
	MaggzWMDB.x = xOff
	MaggzWMDB.y = yOff
	MaggzWMDB.relPt = relPt
end
function MaggzAI:getLoc()
	local point, relativeTo, relPt, xOff, yOff = MaggzAI.main:GetPoint()
	MaggzAIDB.x = xOff
	MaggzAIDB.y = yOff
	MaggzAIDB.relPt = relPt
end
function MaggzMarker:detach()
	
end
function MaggzMarker:retach()

end

-- Slash command handler

SLASH_MAGGZMARKER1, SLASH_MAGGZMARKER2 = '/mm', '/maggzmarker';
local function handler(msg, editbox)
	if msg == "lock" then
		MaggzMarker:lockToggle()
		MaggzWM:lockToggle()
		MaggzAI:lockToggle()
	elseif msg == "reset" then
		MaggzMarker:reset()
	elseif msg == "show" then
		MaggzMarkerDB.shown=true; MaggzWMDB.shown=true; MaggzAIDB.shown=true; visibility()
	elseif msg == "hide" then
		MaggzMarkerDB.shown=false; MaggzWMDB.shown=false; MaggzAIDB.shown=false; visibility()
	elseif msg == "clamp" then
		MaggzMarker:clampToggle(); MaggzWM:clampToggle(); MaggzAI:clampToggle()
	elseif msg == "rc" or msg == "rolecheck" then
		if ( ((UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) and GetNumGroupMembers() > 0) or (UnitIsGroupLeader("player") and GetNumGroupMembers() > 0) ) then
			InitiateRolePoll();
			print("MaggzMarker Rolecheck Initiated!");
		else
			print("MaggzMarker Rolecheck Failed! You do not have permission......");
		end
	elseif msg == "options" then
		InterfaceOptionsFrame_OpenToCategory(MaggzMarker.options)
	else
		print("-------------------------------------------------------");
		print("MaggzMarker / commands list:");
		print("rc or rolecheck - performs a rolecheck");
		print("reset - resets all MaggsMarker items position to default");
		print("lock - locks all MaggsMarker items in place");	
		print("clamp - clamps all MaggzMarker items to screen");
		print("options - opens MaggzMarker options and settings window");	
		print("show - shows all MaggzMarker items");
		print("hide - hides all MaggzMarker items");
		print("-------------------------------------------------------");	
	end
end
SlashCmdList["MAGGZMARKER"] = handler;

--Options panel
local lastCB
local function cNewO(parent,text,func)
	local f = CreateFrame("CheckButton",nil,parent,"UICheckButtonTemplate")
	if not lastCB then f:SetPoint("TOPLEFT",parent,20,-160) else f:SetPoint("TOP",lastCB,"BOTTOM",0,-5) end
	f:SetScript("OnClick",func)
	f.text = f:CreateFontString(nil,"OVERLAY","ChatFontNormal")
	f.text:SetPoint("LEFT",f,"RIGHT")
	f.text:SetText(text)
	lastCB = f
	return f
end
local b = "|cffffd200"
local cbt = "UICheckButtonTemplate"
if not InterfaceOptionsFrame:IsMovable() then InterfaceOptionsFrame:SetMovable(true); InterfaceOptionsFrame:SetScript("OnMouseDown", function(self) self:StartMoving() end); InterfaceOptionsFrame:SetScript("OnMouseUp", function(self) self:StopMovingOrSizing() end) end
local header = options:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
header:SetPoint("TOPLEFT", options, 20,-20)
header:SetText(mM)
local version = options:CreateFontString(nil, "OVERLAY", "ChatFontSmall")
version:SetPoint("TOPLEFT", options,30,-50)
version:SetFormattedText("%s%s:|r %s",b,L["Version"],GetAddOnMetadata("MaggzMarker", "Version"))
local desc = options:CreateFontString(nil, "OVERLAY", "ChatFontSmall")
desc:SetPoint("TOPLEFT", version,0,-20)
desc:SetFormattedText("%s%s:|r %s",b,L["About"],GetAddOnMetadata("MaggzMarker", "Notes"))
local author = options:CreateFontString(nil, "OVERLAY", "ChatFontSmall")
author:SetPoint("TOPLEFT", desc,0,-20)
author:SetFormattedText("%s%s:|r Maggz - Turalyon",b,L["Author"])
--local credits = options:CreateFontString(nil, "OVERLAY", "ChatFontSmall")
--credits:SetPoint("TOPLEFT",author,0,-20)
--credits:SetFormattedText("%s%s:",b,L["Translation credits"])
--local LANG1 = options:CreateFontString(nil, "OVERLAY", "ChatFontSmall")
--Lang1:SetPoint("TOPLEFT",credits,10,-20)
--Lang1:SetText("|cff69ccf0LANG-enUS|r - NAME(S)")
local mainopt = options:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
mainopt:SetPoint("TOPLEFT", options, 20,-140)
mainopt:SetText(L["Aggro indicator and Compact raid frame options"])
local optionsCrf = cNewO(options,L["Show Compact Raid Frame"],function() MaggzAI:crfToggle() end)
local optionsShow = cNewO(options,L["Show Aggro Indicator"],function() MaggzAIDB.shown = not MaggzAIDB.shown; visibility() end)
local optionsLock = cNewO(options,L["Lock Aggro Indicator"],function() MaggzAI:lockToggle() end)
local optionsClamp = cNewO(options,L["Clamp Aggro Indicator"],function() MaggzAI:clampToggle() end)
local optionsBG = cNewO(options,L["Hide Aggro Background"],function() MaggzAI:bgToggle() end)
local optionsAGS = cNewO(options,L["Play Sound for Initial Threat"],function() MaggzAIDB.sound = not MaggzAIDB.sound; end)
lastCB = nil


keyBinds = CreateFrame("Button","MaggzMarkerKeyBindings",options,"GameMenuButtonTemplate");
keyBinds:SetSize(100,20);
keyBinds:SetText(L["Key bindings"]);
keyBinds:SetPoint("BOTTOMRIGHT",options,-10,10);
keyBinds:SetScript("OnClick",function(self)
	KeyBindingFrame_LoadUI();
	KeyBindingFrame.mode = 1;
	ShowUIPanel(KeyBindingFrame);
end);


local lastCB
local function cNew(parent,text,func)
	local f = CreateFrame("CheckButton",nil,parent,"UICheckButtonTemplate")
	if not lastCB then f:SetPoint("TOPLEFT",parent,20,-50) else f:SetPoint("TOP",lastCB,"BOTTOM",0,-5) end
	f:SetScript("OnClick",func)
	f.text = f:CreateFontString(nil,"OVERLAY","ChatFontNormal")
	f.text:SetPoint("LEFT",f,"RIGHT")
	f.text:SetText(text)
	lastCB = f
	return f
end

-- Raid marker frame
MaggzMarker.raidMark = CreateFrame("Frame", "MaggzMarkerRaidMarker", options)
MaggzMarker.raidMark.name = L["Raid marker"]
MaggzMarker.raidMark.parent = MaggzMarker.options.name
local raid = MaggzMarker.raidMark
InterfaceOptions_AddCategory(raid)
local raidHeader = raid:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
raidHeader:SetPoint("TOPLEFT", raid, 20,-20)
raidHeader:SetFormattedText("%s - %s",mM,L["Raid marker"])
local raidShow = cNew(raid,L["Show frame"],function() MaggzMarkerDB.shown = not MaggzMarkerDB.shown; visibility() end)
local raidLock = cNew(raid,L["Lock frame"],function() MaggzMarker:lockToggle() end)
local raidClamp = cNew(raid,L["Clamp to screen"],function() MaggzMarker:clampToggle() end)
local raidFlip = cNew(raid,L["Reverse icons"],function() MaggzMarkerDB.flipped = not MaggzMarkerDB.flipped; MaggzMarker:orien() end)
local raidVert = cNew(raid,L["Display vertically"],function() MaggzMarkerDB.vertical = not MaggzMarkerDB.vertical; MaggzMarker:orien() end)
local raidParty = cNew(raid,L["Hide when alone"],function() MaggzMarkerDB.partyShow = not MaggzMarkerDB.partyShow; visibility() end)
local raidTarget = cNew(raid,L["Show only with a target"],function() MaggzMarkerDB.targetShow = not MaggzMarkerDB.targetShow; visibility() end)
local raidAssist = cNew(raid,L["Hide without assist (in a raid)"],function() MaggzMarkerDB.assistShow = not MaggzMarkerDB.assistShow; visibility() end)
local raidBG = cNew(raid,L["Hide background"],function() MaggzMarker:bgToggle() end)
local raidTool = cNew(raid,L["Enable tooltips"],function() MaggzMarkerDB.tooltips = not MaggzMarkerDB.tooltips end)
local raidScale = CreateFrame("Slider","raidScale",raid,"OptionsSliderTemplate")
lastCB = nil
raidScale:SetPoint("TOPRIGHT",raid,-20,-60)
raidScale:SetSize(180,16)
raidScale:SetMinMaxValues(0.5,1.5)
raidScale:SetValue(1)
raidScale:SetValueStep(0.01)
raidScale:SetOrientation("HORIZONTAL")
raidScale:SetScript("OnValueChanged", function(self) MaggzMarker:scale(self) end)
_G["raidScaleLow"]:SetText("50%")
_G["raidScaleHigh"]:SetText("150%")
raidScaleText = _G["raidScaleText"]
local raidAlpha = CreateFrame("Slider","raidAlpha",raid,"OptionsSliderTemplate")
raidAlpha:SetPoint("TOP",raidScale,"BOTTOM",0,-25)
raidAlpha:SetSize(180,16)
raidAlpha:SetMinMaxValues(0,1)
raidAlpha:SetValue(1)
raidAlpha:SetValueStep(0.01)
raidAlpha:SetOrientation("HORIZONTAL")
raidAlpha:SetScript("OnValueChanged", function(self) MaggzMarker:alpha(self) end)
_G["raidAlphaLow"]:SetText("0%")
_G["raidAlphaHigh"]:SetText("100%")
raidAlphaText = _G["raidAlphaText"]
local raidSpace = CreateFrame("Slider","raidSpace",raid,"OptionsSliderTemplate")
raidSpace:SetPoint("TOP",raidAlpha,"BOTTOM",0,-25)
raidSpace:SetSize(180,16)
raidSpace:SetMinMaxValues(-15,15)
raidSpace:SetValue(0)
raidSpace:SetValueStep(1)
raidSpace:SetOrientation("HORIZONTAL")
raidSpace:SetScript("OnValueChanged", function(self) MaggzMarker:space(self) end)
_G["raidSpaceLow"]:SetText("-15")
_G["raidSpaceHigh"]:SetText("15")
raidSpaceText = _G["raidSpaceText"]

-- World marker frame
MaggzMarker.worldMark = CreateFrame("Frame", "MaggzMarkerWorldMarks", options)
MaggzMarker.worldMark.name = L["World markers"]
MaggzMarker.worldMark.parent = MaggzMarker.options.name
local world = MaggzMarker.worldMark
InterfaceOptions_AddCategory(world)
local worldHeader = world:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
worldHeader:SetPoint("TOPLEFT", world, 20,-20)
worldHeader:SetFormattedText("%s - %s",mM,L["World markers"])
worldShow = cNew(world,L["Show frame"],function() MaggzWMDB.shown = not MaggzWMDB.shown; visibility() end)
local worldLock = cNew(world,L["Lock frame"],function() MaggzWM:lockToggle() end)
local worldClamp = cNew(world,L["Clamp to screen"],function() MaggzWM:clampToggle() end)
local worldFlip = cNew(world,L["Reverse icons"],function() MaggzWMDB.flipped = not MaggzWMDB.flipped; MaggzWM:orien() end)
local worldVert = cNew(world,L["Display vertically"],function() MaggzWMDB.vertical = not MaggzWMDB.vertical; MaggzWM:orien() end)
local worldParty = cNew(world,L["Hide when alone"],function() MaggzWMDB.partyShow = not MaggzWMDB.partyShow; visibility() end)
local worldAssist = cNew(world,L["Hide without assist (in a raid)"],function() MaggzWMDB.assistShow = not MaggzWMDB.assistShow; visibility() end)
local worldBG = cNew(world,L["Hide background"],function() MaggzWM:bgToggle() end)
local worldTool = cNew(world,L["Enable tooltips"],function() MaggzWMDB.tooltips = not MaggzWMDB.tooltips end)
lastCB= nil

local worldScale = CreateFrame("Slider","worldScale",world,"OptionsSliderTemplate")
worldScale:SetPoint("TOPRIGHT",world,-20,-60)
worldScale:SetSize(180,16)
worldScale:SetMinMaxValues(0.5,1.5)
worldScale:SetValue(1)
worldScale:SetValueStep(0.01)
worldScale:SetOrientation("HORIZONTAL")
worldScale:SetScript("OnValueChanged", function(self) MaggzWM:scale(self) end)
_G["worldScaleLow"]:SetText("50%")
_G["worldScaleHigh"]:SetText("150%")
worldScaleText = _G["worldScaleText"]
local worldAlpha = CreateFrame("Slider","worldAlpha",world,"OptionsSliderTemplate")
worldAlpha:SetPoint("TOP",worldScale,"BOTTOM",0,-25)
worldAlpha:SetSize(180,16)
worldAlpha:SetMinMaxValues(0,1)
worldAlpha:SetValue(1)
worldAlpha:SetValueStep(0.01)
worldAlpha:SetOrientation("HORIZONTAL")
worldAlpha:SetScript("OnValueChanged", function(self) MaggzWM:alpha(self) end)
_G["worldAlphaLow"]:SetText("0%")
_G["worldAlphaHigh"]:SetText("100%")
worldAlphaText = _G["worldAlphaText"]
local worldTexText = world:CreateFontString(nil,"OVERLAY","ChatFontNormal")
worldTexText:SetPoint("TOPLEFT",worldAlpha,"BOTTOMLEFT",0,-20)
worldTexText:SetText(L["Display as"])
local worldTex = CreateFrame("Frame", "worldTex", world, "UIDropDownMenuTemplate")
worldTex:SetPoint("TOPLEFT",worldTexText,"BOTTOMLEFT",-20,-5)

local worldTexDrop = {
	{text=L["Blips"], value=1, func=(function() MaggzWM:retex(1); MaggzWMDB.worldTex=1; UIDropDownMenu_SetSelectedValue(worldTex, 1) end)},
	{text=L["Icons"], value=2, func=(function() MaggzWM:retex(2); MaggzWMDB.worldTex=2; UIDropDownMenu_SetSelectedValue(worldTex, 2) end)}
}
local function initialize(self, level)
	local val = UIDROPDOWNMENU_MENU_VALUE
	local info = UIDropDownMenu_CreateInfo()
	for k,v in pairs(worldTexDrop) do
		info = UIDropDownMenu_CreateInfo()
		info.text = v.text
		info.value = v.value
		info.func = v.func
		UIDropDownMenu_AddButton(info, level)
   end
end
UIDropDownMenu_Initialize(worldTex, initialize)
UIDropDownMenu_SetWidth(worldTex, 100);
UIDropDownMenu_SetButtonWidth(worldTex, 124)
UIDropDownMenu_JustifyText(worldTex, "LEFT")
UIDropDownMenu_SetSelectedValue(worldTex, MaggzWMDB.worldTex)

checkUpdate = function()
    optionsCrf:SetChecked(MaggzMarkerDB.crf)
	optionsShow:SetChecked(MaggzAIDB.shown)
	optionsLock:SetChecked(MaggzAIDB.locked)
	optionsClamp:SetChecked(MaggzAIDB.clamped)
	optionsBG:SetChecked(MaggzAIDB.bgHide)
	optionsAGS:SetChecked(MaggzAIDB.sound)
	raidShow:SetChecked(MaggzMarkerDB.shown)
	raidLock:SetChecked(MaggzMarkerDB.locked)
	raidClamp:SetChecked(MaggzMarkerDB.clamped)
	raidFlip:SetChecked(MaggzMarkerDB.flipped)
	raidVert:SetChecked(MaggzMarkerDB.vertical)
	raidParty:SetChecked(MaggzMarkerDB.partyShow)
	raidTarget:SetChecked(MaggzMarkerDB.targetShow)
	raidAssist:SetChecked(MaggzMarkerDB.assistShow)
	raidBG:SetChecked(MaggzMarkerDB.bgHide)
	raidTool:SetChecked(MaggzMarkerDB.tooltips)
	raidScaleText:SetFormattedText("%s: %d%s", L["Raid Markers scale"],math.floor(MaggzMarkerDB.scale*100),"%")
	_G[raidScale:GetName()]:SetValue(MaggzMarkerDB.scale)
	raidAlphaText:SetFormattedText("%s: %d%s",L["Raid Markers alpha"],math.floor(MaggzMarkerDB.alpha*100),"%")
	_G[raidAlpha:GetName()]:SetValue(MaggzMarkerDB.alpha)
	raidSpaceText:SetFormattedText("%s: %d",L["Icon spacing"],MaggzMarkerDB.iconSpace)
	_G[raidSpace:GetName()]:SetValue(MaggzMarkerDB.iconSpace)
	worldShow:SetChecked(MaggzWMDB.shown)
	worldLock:SetChecked(MaggzWMDB.locked)
	worldClamp:SetChecked(MaggzWMDB.clamped)
	worldFlip:SetChecked(MaggzWMDB.flipped)
	worldVert:SetChecked(MaggzWMDB.vertical)
	worldParty:SetChecked(MaggzWMDB.partyShow)
	worldAssist:SetChecked(MaggzWMDB.assistShow)
	worldBG:SetChecked(MaggzWMDB.bgHide)
	worldTool:SetChecked(MaggzWMDB.tooltips)
	worldScaleText:SetFormattedText("%s: %d%s", L["World Markers scale"],math.floor(MaggzWMDB.scale*100),"%")
	_G[worldScale:GetName()]:SetValue(MaggzWMDB.scale)
	worldAlphaText:SetFormattedText("%s: %d%s", L["World Markers alpha"],math.floor(MaggzWMDB.alpha*100),"%")
	_G[worldAlpha:GetName()]:SetValue(MaggzWMDB.alpha)
	MaggzWM:retex(MaggzWMDB.worldTex)
	UIDropDownMenu_SetSelectedValue(worldTex,MaggzWMDB.worldTex)
end
function MaggzMarker:reset()
	for k,v in pairs(mM_defaults) do MaggzMarkerDB[k] = mM_defaults[k] end
	for k,v in pairs(mWM_defaults) do MaggzWMDB[k] = mWM_defaults[k] end
	for k,v in pairs(mAI_defaults) do MaggzAIDB[k] = mAI_defaults[k] end
	MaggzMarker.main:ClearAllPoints()
	MaggzMarker.main:SetPoint("CENTER", UIParent)
	MaggzWM.main:ClearAllPoints()
	MaggzWM.main:SetPoint("CENTER", UIParent,0,50)
	MaggzAI.main:ClearAllPoints()
	MaggzAI.main:SetPoint("CENTER", UIParent,-20,-50)
	clamp()
	backgroundVisibility()
	visibility()
	lock()
	MaggzMarker:orien()
	checkUpdate()
end
MaggzMarker.options.default = MaggzMarker:reset() 

-- OnEvent
local event = CreateFrame("Frame")
event:RegisterEvent("ADDON_LOADED")
event:SetScript("OnEvent", function(self,event,addon,...)
	if (event=="ADDON_LOADED") then
		if (addon=="MaggzMarker") then
			for k,v in pairs(mM_defaults) do if MaggzMarkerDB[k] == nil then MaggzMarkerDB[k] = mM_defaults[k] end end
			for k,v in pairs(mWM_defaults) do if MaggzWMDB[k] == nil then MaggzWMDB[k] = mWM_defaults[k] end end
			for k,v in pairs(mAI_defaults) do if MaggzAIDB[k] == nil then MaggzAIDB[k] = mAI_defaults[k] end end
			if (MaggzMarkerDB.isLocked) then MaggzMarkerDB = {}; MaggzMarker_reset() end
			if (MaggzMarkerDB.x==0) and (MaggzMarkerDB.y==0) then 
				local point, relativeTo, relPt, xOff, yOff = MaggzMarker.main:GetPoint()
				MaggzMarkerDB.x = xOff
				MaggzMarkerDB.y = yOff
				MaggzMarkerDB.relPt = relPt
			else
				MaggzMarker.main:SetPoint("CENTER", UIParent, MaggzMarkerDB.relPt, MaggzMarkerDB.x, MaggzMarkerDB.y)
			end
			if (MaggzWMDB.x==0) and (MaggzWMDB.y==50) then
				local point, relativeTo, relPt, xOff, yOff = MaggzWM.main:GetPoint()
				MaggzWMDB.x = xOff
				MaggzWMDB.y = yOff
				MaggzWMDB.relPt = relPt
			else
				MaggzWM.main:SetPoint("CENTER", UIParent, MaggzWMDB.relPt, MaggzWMDB.x, MaggzWMDB.y)
			end	
			--
			if (MaggzAIDB.x==0) and (MaggzAIDB.y==50) then
				local point, relativeTo, relPt, xOff, yOff = MaggzAI.main:GetPoint()
				MaggzAIDB.x = xOff
				MaggzAIDB.y = yOff
				MaggzAIDB.relPt = relPt
			else
				MaggzAI.main:SetPoint("CENTER", UIParent, MaggzAIDB.relPt, MaggzAIDB.x, MaggzAIDB.y)
			end		
			MaggzMarker.main:SetScale(MaggzMarkerDB.scale)
			MaggzMarker.main:SetAlpha(MaggzMarkerDB.alpha)
			
			MaggzWM.main:SetScale(MaggzWMDB.scale)
			MaggzWM.main:SetAlpha(MaggzWMDB.alpha)
			
			clamp()
			backgroundVisibility()
			visibility()
			lock()
			MaggzMarker:orien()
			MaggzWM:orien()
			checkUpdate()
		end
	elseif (event=="PLAYER_REGEN_DISABLED") then 
		combatWait = true
	elseif (event=="GROUP_ROSTER_UPDATE") or (event=="PLAYER_TARGET_CHANGED") or (event=="PARTY_CONVERTED_TO_RAID") then
		visibility()
	elseif (event=="PLAYER_REGEN_ENABLED") then
		combatWait = false
		visibility()
		MaggzWM:orien()
-- AI event handler
	elseif (event=="UNIT_THREAT_SITUATION_UPDATE") then 
		local status, statustxts = UnitThreatSituation("player"), { "low on threat",  "overnuking", "losing threat", "tanking securely" }
		--print("ThreatMod:", status, statustxts);
		if status and status >= 2 then
				aImaintxt:Show()
				if (MaggzAIDB.sound==true) then
				--PlaySoundFile("Sound\\Creature\\Illidan\\BLACK_Illidan_04.wav", "Master")
				--PlaySoundFile("Sound\\Creature\\Sisters\\CR_Sisters_Aggro01.ogg", "Master")
				--PlaySoundFile("Sound\\Creature\\Sisters\\CR_Sisters_Slay02.ogg", "Master")
				PlaySound("LevelUpSound", "Master")
				end
		else
		    aImaintxt:Hide()
		end	
--
	elseif (event=="PLAYER_LOGIN") then
		DEFAULT_CHAT_FRAME:AddMessage("|CFF0042FF"..MAGGZMARKER_VER_TITLE.."|r |CFFFF0303Loaded|r |CFFFFFF01enjoy!|r")
		--DEFAULT_CHAT_FRAME:AddMessage("")
	end
end)

event:RegisterEvent("GROUP_ROSTER_UPDATE")
event:RegisterEvent("PARTY_CONVERTED_TO_RAID")
event:RegisterEvent("PLAYER_TARGET_CHANGED")
event:RegisterEvent("PLAYER_REGEN_ENABLED")
event:RegisterEvent("PLAYER_REGEN_DISABLED")
event:RegisterEvent("UPDATE_BINDINGS")
event:RegisterEvent("UNIT_THREAT_SITUATION_UPDATE")
event:RegisterEvent("PLAYER_LOGIN")