local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent",
function(self,event,...) 
	if f[event] then
		return f[event](...)
	end
end)

local origReadyCheckClick, origReadyCheckEnter, origReadyCheckLeave, origReadyCheckHide
local function RoleCheckReadyCheckClickModified(...)
	if IsModifierKeyDown() then
		InitiateRolePoll()
	else
		origReadyCheckClick(...)
	end
end
local function RoleCheckReadyCheckEnterModified(...)
	local self = ...
	f:RegisterEvent("MODIFIER_STATE_CHANGED")
	if IsModifierKeyDown() then
		self:SetText(ROLE_POLL)
		f.dirty = true
	else
		if origReadyCheckEnter then 
			origReadyCheckEnter(...)
		end
	end
end
local function RoleCheckReadyCheckLeaveModified(...)
	local self = ...
	f:UnregisterEvent("MODIFIER_STATE_CHANGED")
	if f.dirty then
		self:SetText(READY_CHECK)
		f.dirty = false
	end
	if origReadyCheckLeave then
		origReadyCheckLeave(...)
	end
end
local function RoleCheckReadyCheckHideModified(...)
	f:UnregisterEvent("MODIFIER_STATE_CHANGED")
	if origReadyCheckHide then
		origReadyCheckHide(...)
	end
end

function f.MODIFIER_STATE_CHANGED(...)
	if RaidFrameReadyCheckButton and RaidFrameReadyCheckButton:IsMouseOver() then
		if IsModifierKeyDown() then
			RaidFrameReadyCheckButton:SetText(ROLE_POLL)
			f.dirty = true
		else
			RaidFrameReadyCheckButton:SetText(READY_CHECK)
			f.dirty = false
		end
	end
end

function f.ADDON_LOADED(...)
	local addon = ...
	if addon == "RoleCheck" then
		if not SlashCmdList["ROLECHECK"] then -- make sure we don't overwrite default if Blizz decides to use same name
			SlashCmdList["ROLECHECK"] = function(msg)
				if ( ((IsRaidLeader() or IsRaidOfficer()) and GetNumRaidMembers() > 0) or (IsPartyLeader() and GetNumPartyMembers() > 0) ) then
					InitiateRolePoll();
				end
			end
			SLASH_ROLECHECK1="/rolecheck"
			SLASH_ROLECHECK2="/roc"
		end
	elseif addon == "Blizzard_RaidUI" then
		origReadyCheckClick = RaidFrameReadyCheckButton:GetScript("OnClick")
		origReadyCheckEnter = RaidFrameReadyCheckButton:GetScript("OnEnter")
		origReadyCheckLeave = RaidFrameReadyCheckButton:GetScript("OnLeave")
		origReadyCheckHide = RaidFrameReadyCheckButton:GetScript("OnHide")
		if origReadyCheckClick then
			RaidFrameReadyCheckButton:SetScript("OnClick",RoleCheckReadyCheckClickModified)
			RaidFrameReadyCheckButton:SetScript("OnEnter",RoleCheckReadyCheckEnterModified)
			RaidFrameReadyCheckButton:SetScript("OnLeave",RoleCheckReadyCheckLeaveModified)
			RaidFrameReadyCheckButton:SetScript("OnHide", RoleCheckReadyCheckHideModified)
		end
	end
end

--[[
ROLE_POLL_BEGIN -- show the summary window
ROLE_CHANGED_INFORM -- update with changes
RAID_ROSTER_UPDATE -- sync roster
8(-10-20) secure buttons for SetPartyAssignment("MAINTANK", unit) ClearPartyAssignment("MAINTANK", unit)
]]