GuildRaidSnapShot_SnapShots = {};

GuildRaidSnapShot_Loot = {};

GuildRaidSnapShot_Notes = {};

GRSS_Calendar = {};

GRSS_Systems = {};

GRSS_Full_DKP = {};

GRSS_Bosses = {"Lucifron","Magmadar","Gehennas","Garr","Baron Geddon","Shazzrah","Sulfuron Harbinger","Golemagg the Incinerator","Ragnaros","Doom Lord Kazzak","Azuregos","Lethon","Emeriss","Onyxia","Taerar","Ysondre","Razorgore the Untamed","Vaelastrasz the Corrupt","Flamegor","Ebonroc","Firemaw","Chromaggus","Broodlord Lashlayer","Nefarian","Prophet Skeram","Lord Kri","Battleguard Sartura","Princess Huhuran","Fankriss the Unyielding","Viscidus","Ouro","C'Thun","Emperor Vek'nilash","Emperor Vek'lor","Anub'Rekhan","Grand Widow Faerlina","Maexxna","Feugen","Gluth","Gothik the Harvester","Grobbulus","Heigan the Unclean","Highlord Mograine","Instructor Razuvious","Lady Blaumeux","Loatheb","Noth the Plaguebringer","Patchwerk","Sapphiron","Sir Zeliek","Stalagg","Thaddius","Thane Korth'azz","Ossirian the Unscarred","Moam","Kurinnaxx","General Rajaxx","Buru the Gorger","Ayamiss the Hunter","Bloodlord Mandokir","Gahz'ranka","Gri'lek","Hakkar","Hazza'rah","High Priest Thekal","High Priest Venoxis","High Priestess Arlokk","High Priestess Jeklik","High Priestess Mar'li","Jin'do the Hexxer","Renataki","Wushoolay","The Crone","Hyakiss the Lurker","Julianne","Maiden of Virtue","Moroes","Netherspite","Nightbane","Prince Malchezaar","Rokad the Ravager","Romulo","Shade of Aran","Shadikith the Glider","Terestian Illhoof","The Big Bad Wolf","The Curator","Gruul the Dragonkiller","Magtheridon","High King Maulgar","Fathom-Lord Karathress","Hydross the Unstable","Lady Vashj","Leotheras the Blind","Morogrim Tidewalker","The Lurker Below","Al'ar","High Astromancer Solarian","Kael'thas Sunstrider","Void Reaver","Doomwalker","Attumen the Huntsman","Illidan Stormrage","Gathios the Shatterer","High Nethermancer Zerevor","Lady Malande","Veras Darkshadow","Essence of Anger","Gurtogg Bloodboil","Illidari Council","Teron Gorefiend","High Warlord Naj'entus","Mother Shahraz","Shade of Akama","Supremus","Anetheron","Archimonde","Azgalor","Kaz'rogal","Rage Winterchill","Nalorakk","Akil'zon","Jan'alai","Halazzi","Hex Lord Malacrass","Zul'jin","Kalecgos","Sathrovarr the Corruptor","Brutallus","Felmyst","Lady Sacrolash","Grand Warlock Alythess","M'uru","Entropius","Kil'jaeden","Kel'Thuzad","Sartharion","Archavon the Stone Watcher","Malygos"};

GRSS_FastBossLookup = {};	--This gets initialized with the mod

GRSS_Ignore = {"Onyxian","Onyxia's Elite Guard","Maexxna Spiderling","Patchwerk Golem","Hakkari","Son of Hakkar"," slain by ","Nightbane .+",".+the Crone","Netherspite Infernal","Ember of Al'ar","Sartharion Twilight Whelp","Sartharion Twilight Egg"};

GRSS_Yells = {};

GRSS_LootIgnore = {"Hakkari Bijou","Alabaster Idol","Amber Idol","Azure Idol","Jasper Idol","Lambent Idol","Obsidian Idol","Onyx Idol","Vermillion Idol","Lava Core","Fiery Core","Large .+ Shard","Small .+ Shard","Nexus Crystal","Wartorn .+ Scrap","Badge of Justice","Cosmic Infuser","^Devastation$","Infinity Blade","Phaseshift Bulwark","Warp Slicer","Staff of Disintegration","Netherstrand Longbow","Nether Spike","Bundle of Nether Spikes","Emblem of Heroism","Emblem of Valor","Abyss Crystal"};

GRSS_Yells["Majordomo Executus"] = "Impossible!.+I submit!";

GRSS_Yells["Attumen the Huntsman"] = "Always knew.+the hunted";

GRSS_BossEmote = {};

GRSS_BossEmote["Chess"] = "doors of the Gamesman's Hall";

GRSS_ZoneIgnore = {"Arathi Basin","Alterac Valley","Eye of the Storm","Warsong Gulch"};

GRSS_Auto = 1;

GRSS_LootCap = 1;

GRSS_Bidding = 0;

GRSS_Rolling = 0;

GRSS_HighBid = 0;

GRSS_HighBidder = "";

GRSS_CurrentSort = "total";

GRSS_BidStyle = "Silent Auction";

GRSS_CurrentItem = "";

GRSS_ItemHistory = {};

GRSS_TakeScreenshots = 0;

GRSS_CurrentLootDate = "";

GRSS_CurrentLootIndex = 0;

GRSS_LastCommand = {};

GRSS_ItemPrices = {};

GRSS_RaidFilter = "All";

GRSS_ClassFilter = "All";

GRSS_ItemQueue = {};

GRSS_ItemBoxOpen = 0;

GRSS_WipeCauser = "Unknown";

GRSS_LastWipeTime = 0;

GRSS_LastSnapshot = 0;

GRSS_PopupPoints = 0;

GRSS_PopupLootDate = "";

GRSS_PopupLootIndex = "";

GRSS_WaitListRequest = {}; --This is functioning as a Queue of WaitingList requests

GRSS_WaitListRequestBoxOpen = 0;

GRSS_NumberedSystems = {};

GRSS_Loot = {};

GRSS_WaitingList = {};

GRSS_RaidSignups = {};

GRSS_InviteType = "Waiting List";

GRSS_RollNumber = 1000;

GRSS_AutoWipe = 1;

GRSS_PendingSnapShotName = ""

GRSS_CurrentCalendarIndex = 1;

GRSS_LootPromptInCombat = 0;



GRSS_NextTime = nil;

GRSS_Period = 60;

GRSS_ReadyForTimer = false;



GRSS_Prefix = "GRSS: ";



GRSS_Guild = {};

GRSS_DKP = {};

GRSS_Bids = {};

GRSSCurrentSystem = "";

GRSSCurrentAction = "DKP Standings";

GRSSHelpMsg = {

	"!help = This help menu",

	"!dkp = Your current DKP",

	"!dkp name [name2 name3...] = the DKP for the player 'name' (and name2 and name3) (i.e. !dkp Joe Fred)",

	"!items = Your item history",

	"!items name = Item history for player 'name' (i.e. !items Joe)",

	"!bid X = Bid X points on the current item",

	"!dkpclass class [class2 class3...] = the current standings for the 'class' (and 'class2' and 'class3') (i.e. !dkpclass mage warlock)",

	"!dkpraid X = the current standings for DKP System X for current raid members (to get the list of appropriate Systems, just type !dkpraid)",

	"!dkpall X = the current standings for DKP System X (to get the list of appropriate Systems, just type !dkpall)",

	"!price itemname = the price of the item 'itemname' (parts of names work too, i.e. '!price felheart' will retrieve the prices of all items with 'felheart' in the name)",

	"!waitlist = Add me to the Waiting List",

};



local GRSSVersion = "0.993";

local GRSSUsage = {

	"Type |c00ffff00/grss <snapshotname>|r to take a snapshot (ex: |c00ffff00/grss Kel'Thuzad|r)",

	"|c00ffff00/grss reset|r to delete all your snapshots",

	"|c00ffff00/grss show|r to bring up the DKP standings/bidding/rolling screen",

	"|c00ffff00/grss invite|r to bring up the Waiting List/Auto-Invite screen",

	"|c00ffff00/grss starttimer|r start Periodic Snapshots",

	"|c00ffff00/grss stoptimer|r to disable Periodic Snapshots",

	"|c00ffff00/grss noauto|r to disable auto-snapshot",

	"|c00ffff00/grss yesauto|r to enable auto-snapshot",

	"|c00ffff00/grss yesscreenshot|r to make snapshots also take a screenshot",

	"|c00ffff00/grss noscreenshot|r to make snapshots NOT take a screenshot",

	"|c00ffff00/grss yesloot|r to make Loot received (Blue and better) prompt for points spent",

	"|c00ffff00/grss noloot|r to disable the loot prompt when items are received",

	"|c00ffff00/grss yeswipe|r to enable the snapshot prompt when a wipe occurs",

	"|c00ffff00/grss nowipe|r to disable the snapshot prompt when a wipe occurs",

	"|c00ffff00/grss yeslootcombat|r allows the loot prompt to pop up in combat",

	"|c00ffff00/grss nolootcombat|r forces the loot prompt to wait until out of combat (recommended unless you're having problems)",

	"==========================================",

	"Members can also send you tells like the following for information (you can even send yourself a tell for this info, too)",

	};



local GRSS_Colors={

	--[[["ff9d9d9d"] = "grey",

	["ffffffff"] = "white",

	["ff00ff00"] = "green",

	["ff0070dd"] = "rare",]]--

	["ffa335ee"] = "epic",

	["ffff8000"] = "legendary"

};



local GRSS_ChatFrame_OnEvent_Original = nil;



function GRSS_IsWhisperGRSSWhisper(w)

	if string.find(w,"^!dkp") or string.find(w,"^!items") or string.find(w,"^!info") or string.find(w,"^!commands") or string.find(w,"^!price") or string.find(w,"^!waitlist") then

		return true;

	else

		return false;

	end

end





function GRSS_IsOutgoingWhisperGRSSWhisper(w)

	if string.find(w,"^"..GRSS_Prefix) then

		return true;

	else

		return false;

	end

end





function GRSS_SetItemRef(link,text,button)

	if GRSSCurrentAction ~= "DKP Standings" and GRSS_Bidding~=1 and GRSS_Rolling~=1 then

		GRSSItemName:SetText(text);

	end

end



function GuildRaidSnapShot_OnLoad()



	--GRSSChangeSystem(0);

	SlashCmdList["GuildRaidSnapShot"] = GuildRaidSnapShot_SlashHandler;

	SLASH_GuildRaidSnapShot1 = "/grss";

	SLASH_GuildRaidSnapShot1 = "/GRSS";



	for i,v in pairs(GRSS_Bosses) do

		GRSS_FastBossLookup[v] = v;

	end



	this:RegisterEvent("PLAYER_REGEN_ENABLED");

	this:RegisterEvent("PLAYER_REGEN_DISABLED");

	this:RegisterEvent("PLAYER_DEAD");

	this:RegisterEvent("CHAT_MSG_COMBAT_FRIENDLY_DEATH");

	this:RegisterEvent("CHAT_MSG_COMBAT_HOSTILE_DEATH");

	this:RegisterEvent("PLAYER_TARGET_CHANGED");

	this:RegisterEvent("CHAT_MSG_LOOT");

	this:RegisterEvent("CHAT_MSG_MONSTER_YELL");

	this:RegisterEvent("CHAT_MSG_RAID_BOSS_EMOTE");

	this:RegisterEvent("PLAYER_GUILD_UPDATE");

	this:RegisterEvent("GUILD_ROSTER_UPDATE");

	this:RegisterEvent("CHAT_MSG_WHISPER");

	this:RegisterEvent("CHAT_MSG_SYSTEM");

	this:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");

	this:RegisterEvent("LOOT_OPENED");

	this:RegisterEvent("RAID_ROSTER_UPDATE");

	this:RegisterEvent("VARIABLES_LOADED");

	this:RegisterEvent("CALENDAR_OPEN_EVENT");

	this:RegisterEvent("CALENDAR_UPDATE_EVENT");

	this:RegisterEvent("CALENDAR_UPDATE_EVENT_LIST");

	this:RegisterEvent("CALENDAR_NEW_EVENT");

	this:RegisterEvent("ADDON_LOADED");

	DEFAULT_CHAT_FRAME:AddMessage("GuildRaidSnapShot (By DKPSystem.com) Version "..GRSSVersion.." loaded. ");

	DEFAULT_CHAT_FRAME:AddMessage("Type |c00ffff00/grss|r to get a list of options for GuildRaidSnapShot");



	--GRSS_ChatFrame_OnEvent_Original = ChatFrame_OnEvent;

        --ChatFrame_OnEvent = GRSS_ChatFrame_OnEvent_Hook;

	ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER",GRSS_IsWhisperGRSSWhisper); -- incoming whispers

	ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER_INFORM",GRSS_IsOutgoingWhisperGRSSWhisper); --outgoing whispers



	hooksecurefunc("SetItemRef",GRSS_SetItemRef);



	StaticPopupDialogs["GRSS_ITEMPOINTS"] = {

		text = "How many points did |c00ffff00%s|r spend on %s",

		button1 = "OK",

		button2 = "Cancel",

		OnAccept = function()

			points = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			GuildRaidSnapShot_Loot[GRSS_PopupLootDate][GRSS_PopupLootIndex].points = points;

			GRSS_RecordCurrentPlayerReceivedItem(points);

		end,

		EditBoxOnEnterPressed = function()

			points = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			GuildRaidSnapShot_Loot[GRSS_PopupLootDate][GRSS_PopupLootIndex].points = points;

			GRSS_RecordCurrentPlayerReceivedItem(points);

			this:GetParent():Hide();

		end,

		OnShow = function()

			GRSS_ItemBoxOpen = 1;	

			getglobal(this:GetName().."EditBox"):SetText(GRSS_PopupPoints);

		end,	

		OnHide = function()

			if table.getn(GRSS_ItemQueue) > 0 then

				GRSS_NextItemPopup();

			else

				GRSS_ItemBoxOpen = 0;

			end

		end,

		timeout = 0,

		whileDead = 1,

		hideOnEscape = 1,

		hasEditBox = 1,

	};

	StaticPopupDialogs["GRSS_WAITINGLIST"] = {

		text = "Name of person(s) to add to waiting list? If adding more than one, seperate by a space",

		button1 = "OK",

		button2 = "Cancel",

		OnAccept = function()

			name = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			GRSS_AddNameToWaitingList(name);

			

		end,

		EditBoxOnEnterPressed = function()

			name = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			GRSS_AddNameToWaitingList(name);

			this:GetParent():Hide();

		end,

		OnShow = function()

			getglobal(this:GetName().."EditBox"):SetText("");

		end,

		timeout = 0,

		whileDead = 1,

		hideOnEscape = 1,

		hasEditBox = 1,

	};

	StaticPopupDialogs["GRSS_RAIDCHECK"] = {

		text = "There are %s items and %s raids recorded right now.  Would you like to keep this data (and apply any expenditures to the current standings) or would you like to purge all the data.  You should ONLY purge if you've already uploaded your snapshots.",

		button1 = "Purge",

		button2 = "Keep",

		OnAccept = function()

			GuildRaidSnapShot_SnapShots = {};

			GuildRaidSnapShot_Loot = {};

			GRSSPrint("Snapshots Purged");	

		end,

		OnCancel = function()

			GRSS_RecalcSpentLoot();

		end,

		timeout = 0,

		whileDead = 1,

	};

	StaticPopupDialogs["GRSS_TIMERCHECK"] = {

		text = "The timer is currently running to take snapshots.  The next snapshot is set to be taken in |c00ffff00%s minutes|r.  Continue the periodic snapshots, or Stop the Timer?",

		button1 = "Continue",

		button2 = "Stop Timer",

		OnAccept = function()

			GRSS_ReadyForTimer = true;

			local mins = GRSS_NextTime - GetTime();

			if mins > 0 then

				mins = math.floor(mins / 60);

				GRSSPrint("The next snapshot wlll be in "..mins.." minutes");

			end

		end,

		OnCancel = function()

			GRSS_NextTime = nil;

			GRSS_ReadyForTimer = true;

			GRSSPrint("Snapshot Timer Stopped");

		end,

		timeout = 0,

		whileDead = 1,

	};



	StaticPopupDialogs["GRSS_STARTTIMER"] = {

		text = "How many minutes to between snapshots?",

		button1 = "OK",

		button2 = "Cancel",

		OnAccept = function()

			mins = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			if tonumber(mins) == nil then

				GRSSPrint(mins.." isn't exactly a number, is it?");

			else

				GRSS_StartTimer(mins);

			end

		end,

		EditBoxOnEnterPressed = function()

			mins = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			if tonumber(mins) == nil then

				GRSSPrint(mins.." isn't exactly a number, is it?");

			else

				GRSS_StartTimer(mins);

			end

			this:GetParent():Hide();

		end,

		OnShow = function()

			getglobal(this:GetName().."EditBox"):SetText(GRSS_Period);

		end,

		timeout = 0,

		whileDead = 1,

		hideOnEscape = 1,

		hasEditBox = 1,

	};



	StaticPopupDialogs["GRSS_WIPE"] = {

		text = "Was this a wipe? If so, and if you want to take a snapshot, enter the name of the snapshot and click 'Record Wipe', otherwise, click Cancel",

		button1 = "Record Wipe",

		button2 = "Cancel",

		OnAccept = function()

			name = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			GRSS_TakeSnapShot(name);

		end,

		EditBoxOnEnterPressed = function()

			name = getglobal(this:GetParent():GetName().."EditBox"):GetText();

			GRSS_TakeSnapShot(name);

			this:GetParent():Hide();

		end,

		OnShow = function()

			getglobal(this:GetName().."EditBox"):SetText("Wipe on "..GRSS_WipeCauser);

		end,

		timeout = 0,

		whileDead = 1,

		hideOnEscape = 1,

		hasEditBox = 1,

	};



	StaticPopupDialogs["GRSS_AUTOWAITLIST"] = {

		text = "|c00ffff00%s|r is requesting to be added to the Wait List.  Accept or Deny?",

		button1 = "Accept",

		button2 = "Deny",

		OnAccept = function()

			local last = table.getn(GRSS_WaitListRequest);

			local name = GRSS_WaitListRequest[last];

			GRSS_AddNameToWaitingList(name);

			GRSS_SendWhisper("You've been added to the waiting list","WHISPER",nil,name);

		end,

		OnCancel = function()

			local last = table.getn(GRSS_WaitListRequest);

			local name = GRSS_WaitListRequest[last];

			GRSS_SendWhisper("Your request to be added to the waiting list has been denied","WHISPER",nil,name);

		end,

		OnHide = function()

			table.remove(GRSS_WaitListRequest);

			if table.getn(GRSS_WaitListRequest) > 0 then

				GRSS_NextWaitListRequest();

			else

				GRSS_WaitListRequestBoxOpen = 0;

			end

		end,

		onShow = function()

			GRSS_WaitListRequestBoxOpen = 1;

		end,

		timeout = 0,

		whileDead = 1,

	};

	

end



function GRSS_GetCalendar()

	

	local curmonth,curyear;

	_,curmonth,_,curyear = CalendarGetDate();

	if(GRSS_Calendar == nil) then

		GRSS_Calendar = {};

	end



	local keepers = {}

	for m = -1,1 do

		for d=1,31 do

			local numevents = CalendarGetNumDayEvents(0,d);

			--GRSSPrint("Num Events:"..numevents);

			if numevents > 0 then

				for e=1,numevents do

					

					local title, hour, minute, caltype,_,eventtype = CalendarGetDayEvent(m,d,e);

					if eventtype > 0 then

						if caltype=="GUILD" or caltype=="PLAYER" then

							month = curmonth + m;

							year = curyear;

							if(month < 0) then

								year = year - 1;

								month = 12;

							elseif(month > 12) then

								year = year + 1;

								month = 1;

							end



							local found = GRSS_FindEvent(title,year,month,d,hour,minute);

							if found == nil then

								local event = {

										title = title,

										monthoffset = m,

										month = month,

										year = year,

										day = d,

										eventindex = e,

										hour = hour,

										minute = minute,

										eventtype = eventtype

									}

								table.insert(GRSS_Calendar,event);

								found = table.getn(GRSS_Calendar);



							end

							keepers[found] = found;

						end

					end

					

				end

			end

		end

	end



	for i,v in pairs(GRSS_Calendar) do

		if keepers[i]==nil then

			GRSS_Calendar[i] = nil;

		end

	end

end



function GRSS_FindEvent(title,year,month,day,hour,minute)

	for i,e in pairs(GRSS_Calendar) do

		if(e.title == title and e.month==month and e.day==day and e.hour==hour and e.minute==minute and e.year==year) then

			return i;

		end

	end

	return nil;

end



function GRSS_StoreEventDetails()

	local title, desc, creator,eventtype,_,maxsize,_,_,month,day,year,hour,minute = CalendarGetEventInfo();



	local i = GRSS_FindEvent(title,year,month,day,hour,minute);

	if i ~= nil then

		GRSS_Calendar[i].description = desc;

		GRSS_Calendar[i].creator = creator;

		GRSS_Calendar[i].maxsize = maxsize;

		local numinvites = CalendarEventGetNumInvites();

		local invites = {}

		for ii = 1,numinvites do

			local name,level,className,_,inviteStatus,modStatus = CalendarEventGetInvite(ii);

			local inv = {

				name = name,

				level = level,

				class = className,

				status = inviteStatus

			};

			table.insert(invites,inv);

		end



		GRSS_Calendar[i].invites = invites;

	end

end



function GRSS_StartTimer(mins)

	GRSS_Period = mins;

	GRSS_NextTime = GetTime() + (GRSS_Period*60);

	GRSS_TakeSnapShot("Periodic Snapshot");

	GRSSPrint("Periodic Snapshots Started.  Next Snapshot will be in "..mins.." minutes");

end



function GRSS_StopTimer()

	GRSS_Period = nil;

	GRSS_NextTime = nil;

	GRSSPrint("Periodic Snapshots Halted");

end



function GRSS_Timer_OnUpdate()

	if GRSS_ReadyForTimer and GRSS_Period and GRSS_NextTime then

		local now = GetTime();

		if now > GRSS_NextTime then

			GRSS_TakeSnapShot("Periodic Snapshot");

			GRSSPrint("Next snapshot will be in "..GRSS_Period.." minutes");

			GRSS_NextTime = GetTime() + (GRSS_Period*60);

			--GRSS_TimerNotify = {};

		end

	end



		

	--GRSSPrint("update");

	if type(GRSS_GeneralTimer)=="function" then

		local now = GetTime();

		if now > GRSS_GeneralTimeout then

			GRSSPrint("Timeout");

			GRSS_GeneralTimer();



			GRSS_GeneralTimer = nil;

			GRSS_GeneralTimeout = nil;

		end

	end

end



function GRSS_StartGeneralTimer(seconds,fun)

	GRSS_GeneralTimer = fun;

	GRSS_GeneralTimeout = GetTime() + seconds;

end





function GRSS_NextItemPopup()

	if table.getn(GRSS_ItemQueue) > 0 then

		local i = table.getn(GRSS_ItemQueue);

		local item, name, points;

		GRSS_PopupLootDate = GRSS_ItemQueue[i].date;

		GRSS_PopupLootIndex = GRSS_ItemQueue[i].index;

		item = GRSS_ItemQueue[i].item;

		name = GRSS_ItemQueue[i].name;

		GRSS_PopupPoints = GRSS_ItemQueue[i].points;

		table.remove(GRSS_ItemQueue,i);

		StaticPopup_Show("GRSS_ITEMPOINTS",name,item);

	end

end



function GRSS_EnqueueItem(date,index,name,item,points)

	local temp = {};

	temp.date = date;

	temp.index = index;

	temp.name = name;

	temp.item = item;

	temp.points = points;

	table.insert(GRSS_ItemQueue,1,temp);

end



function GRSS_RecalcSpentLoot()

	for ld,daytable in pairs(GuildRaidSnapShot_Loot) do

		for lootindex,loottable in pairs(daytable) do

			if loottable.system == nil or GRSS_Systems[loottable.system]==nil then

				loottable.system = GRSSCurrentSystem

			end

			points = GRSSNumNilZero(loottable.points);

			if points ~= 0 then

				points = GRSSNumNilZero(loottable.points);

				foundname = false;

				for i,v in pairs(GRSS_Full_DKP[loottable.system]) do

					if string.lower(v.name)==string.lower(loottable.player) then

						v.spent = GRSSNumNilZero(v.spent) + points;

						foundname = true;

					end

				end

				if foundname == false then

					temp = {};

					temp.name = loottable.player;

					temp.class = "?";

					temp.spent = points;

					temp.earned = 0;

					temp.adj = 0;

					temp.rank = "?";

					temp.rankid = 0;

					table.insert(GRSS_Full_DKP[loottable.system],temp);

				end

			end

		end

	end

	GRSSChangeSystem(GRSSCurrentSystem);

end



function GRSS_PendingSnapshotCheck()

	local items = 0;

	local snapshots = table.getn(GuildRaidSnapShot_SnapShots);

	for i,v in pairs(GuildRaidSnapShot_Loot) do

		items = items + table.getn(v);

	end

	if snapshots > 0 or items > 0 then

		StaticPopup_Show("GRSS_RAIDCHECK",items,snapshots);

	end

end





function GRSS_TimerRunningCheck()

	if GRSS_NextTime then

		local mins = math.floor((GRSS_NextTime - GetTime())/60);

		if mins < 0 then

			mins = 0

		end	

		StaticPopup_Show("GRSS_TIMERCHECK",mins);

	else

		GRSS_ReadyForTimer = true;

	end

end



function GRSS_RecordCurrentPlayerReceivedItem(points)

	playername = string.lower(GuildRaidSnapShot_Loot[GRSS_PopupLootDate][GRSS_PopupLootIndex].player);

	normalplayername = GuildRaidSnapShot_Loot[GRSS_PopupLootDate][GRSS_PopupLootIndex].player;

	if tonumber(points) ~= nil and tonumber(points) ~= 0 then

		points = tonumber(points);

		foundname = false;

		for i,v in pairs(GRSS_Full_DKP[GRSSCurrentSystem]) do

			if string.lower(v.name)==playername then

				v.spent = GRSSNumNilZero(v.spent) + points;

				foundname = true;

			end

		end

		if foundname == false then

			temp = {};

			temp.name = normalplayername;

			temp.class = "?";

			temp.spent = points;

			temp.earned = 0;

			temp.adj = 0;

			table.insert(GRSS_Full_DKP[GRSSCurrentSystem],temp);

		end

		foundname = false;

		for i,v in pairs(GRSS_DKP) do

			if string.lower(v.name)==playername then

				v.spent = GRSSNumNilZero(v.spent) + points;

				v.earned = GRSSNumNilZero(v.earned);

				v.adj = GRSSNumNilZero(v.adj);

				if GRSS_Divide and GRSS_Divide[GRSSCurrentSystem] then

					v.total = v.earned/GRSSNumNilOne(v.spent);

				else

					v.total = v.earned + v.adj - v.spent;

				end

				foundname = true;

			end

		end

		if foundname == false then

			temp = {};

			temp.class = "?";

			temp.name = normalplayername;

			temp.spent = points;

			temp.earned = 0;

			temp.adj = 0;

			if GRSS_Divide and GRSS_Divide[GRSSCurrentSystem] then

				temp.total = 0

			else

				temp.total = -points;

			end

			table.insert(GRSS_DKP,temp);

		end

		temp = "Today: "..normalplayername.." <-- "..GuildRaidSnapShot_Loot[GRSS_PopupLootDate][GRSS_PopupLootIndex].item.." for "..points;



		if GRSS_ItemHistory[string.upper(playername)] ~= nil then

			table.insert(GRSS_ItemHistory[string.upper(playername)],temp);

		else

			GRSS_ItemHistory[string.upper(playername)] = {};

			GRSS_ItemHistory[string.upper(playername)][0] = temp;

		end

		GRSSScrollBar_Update();

	end

end



function GRSS_GetItemPoints(item)

	for i,v in pairs(GRSS_ItemPrices[GRSSCurrentSystem]) do

		if string.lower(item)==string.lower(v.name) then

			return v.points;

		end

	end

	return "";

end



--Sorts the Temp table GRSS_DKP

function GRSSSortBy(sort)

	GRSS_CurrentSort = sort;

	table.sort(GRSS_DKP,

		function(a,b)

			if sort=="spent" then

				if GRSSCurrentAction == "DKP Standings" then

					return GRSSNum(a.spent) > GRSSNum(b.spent);

				elseif GRSSCurrentAction == "Rolls" then

					return GRSSNum(a.roll) > GRSSNum(b.roll);

				elseif GRSSCurrentAction == "Bids" then

					return GRSSNum(a.bid) > GRSSNum(b.bid);

				end

			elseif sort=="rankid" then

				return GRSSNum(a.rankid) < GRSSNum(b.rankid);

			elseif a[sort]==b[sort] then

				return GRSSNum(a.total) > GRSSNum(b.total);

			elseif sort=="name" or sort=="class" then

				return (string.lower(a[sort]) < string.lower(b[sort]))

			else

				return (GRSSNum(a[sort]) > GRSSNum(b[sort]))

			end

		end

	);

	GRSSScrollBar_Update();

end



--Sorts the Full table generated by the downloader: GRSS_Full_DKP

function GRSSSortByFull(sort,sys)

	table.sort(GRSS_Full_DKP[sys],

		function(a,b)

			if sort=="spent" then

				if GRSSCurrentAction == "DKP Standings" then

					return GRSSNum(a.spent) > GRSSNum(b.spent);

				elseif GRSSCurrentAction == "Rolls" then

					return GRSSNum(a.roll) > GRSSNum(b.roll);

				elseif GRSSCurrentAction == "Bids" then

					return GRSSNum(a.bid) > GRSSNum(b.bid);

				end

			elseif a[sort]==b[sort] then

				return GRSSNum(a.earned) + GRSSNum(a.adj) - GRSSNum(a.spent) > GRSSNum(b.earned) + GRSSNum(b.adj) - GRSSNum(b.spent);

			elseif sort=="name" or sort=="class" then

				return (string.lower(a[sort]) < string.lower(b[sort]))

			else

				return (GRSSNum(a[sort]) > GRSSNum(b[sort]))

			end

		end

	);

end



function GRSSChangeSystem(sys)

	if sys ~= nil then

		local inc = 1;

		local i,v;

		local top = table.getn(GRSS_Full_DKP[sys]);

		GRSS_DKP = {};

		local RaidTable = GRSS_RaidTable();

		for i=1,top do

			if GRSS_RaidFilter == "All" or RaidTable[string.upper(GRSS_Full_DKP[sys][i].name)]==1 then

				if GRSS_ClassFilter == "All" or string.upper(GRSS_Full_DKP[sys][i].class) == string.upper(GRSS_ClassFilter) then

					GRSS_DKP[inc] = {};

					GRSS_DKP[inc].name = GRSS_Full_DKP[sys][i].name;

					GRSS_DKP[inc].class = GRSS_Full_DKP[sys][i].class;

					GRSS_DKP[inc].earned = GRSS_Full_DKP[sys][i].earned;

					GRSS_DKP[inc].spent = GRSS_Full_DKP[sys][i].spent;

					GRSS_DKP[inc].adj = GRSS_Full_DKP[sys][i].adj;

					if GRSS_Divide and GRSS_Divide[sys] then

						GRSS_DKP[inc].total = GRSS_DKP[inc].earned/GRSSNumNilOne(GRSS_DKP[inc].spent);

					else

						GRSS_DKP[inc].total = GRSS_DKP[inc].earned + GRSS_DKP[inc].adj - GRSS_DKP[inc].spent

					end

					GRSS_DKP[inc].bid = ""

					GRSS_DKP[inc].roll = ""

					GRSS_DKP[inc].rank = GRSS_Full_DKP[sys][i].rank;

					GRSS_DKP[inc].rankid = GRSS_Full_DKP[sys][i].rankid;

					inc = inc + 1;

				end

			end

		end

		GRSSScrollBar_Update();

		GRSSSortBy("total");

		GRSSScrollBar:Show();

		if table.getn(GRSS_NumberedSystems)==0 then

			for mysys in pairs(GRSS_Systems) do

				table.insert(GRSS_NumberedSystems,mysys);

			end

		end

	end

end





function GRSSGenerateBids()

	GRSS_Bids = {};

	local temp = {};

	local n = 24;

	local m = table.getn(GRSS_DKP);

	for i=1,n do

		temp = {};

		temp.name = GRSS_DKP[math.random(m)].name;

		temp.bid = math.random(1000);

		table.insert(GRSS_Bids,temp);

	end

	GRSSBidsScrollBar_Update();

end





function GRSSNum(v)

	v = tonumber(v);

	if v == nil then

		v = -9999999

	end

	return v;

end





function GRSSNumNilOne(v)

	v = tonumber(v);

	if v==nil or v==0 then

		v = 1;

	end

	return v;

end



function GRSSNumNilZero(v)

	v = tonumber(v);

	if v == nil then

		v = 0

	end

	return v;

end







function GRSSScrollBar_Update()

	local line; -- 1 through 10 of our window to scroll

	local lineplusoffset; -- an index into our data calculated from the scroll offset

	local earned,sent,adj;

	FauxScrollFrame_Update(GRSSScrollBar,table.getn(GRSS_DKP),10,16);

	for line=1,10 do

		lineplusoffset = line + FauxScrollFrame_GetOffset(GRSSScrollBar);

		if lineplusoffset <= table.getn(GRSS_DKP) then

			if GRSSCurrentAction == "DKP Standings" then

				spent = GRSS_DKP[lineplusoffset].spent;

			elseif GRSSCurrentAction == "Rolls" then

				spent = GRSS_DKP[lineplusoffset].roll;

			elseif GRSSCurrentAction == "Bids" then

				spent = GRSS_DKP[lineplusoffset].bid;

			end



			if GRSSCurrentAction == "DKP Standings" then

				earned = GRSS_DKP[lineplusoffset].earned;

				adj = GRSS_DKP[lineplusoffset].adj;

			else

				earned = "";

				adj = "";

			end



			getglobal("GRSSRow"..line.."FieldPlayer"):SetText(GRSS_DKP[lineplusoffset].name);

			getglobal("GRSSRow"..line.."FieldClass"):SetText(GRSS_DKP[lineplusoffset].class);

			getglobal("GRSSRow"..line.."FieldRank"):SetText(GRSS_DKP[lineplusoffset].rank);

			getglobal("GRSSRow"..line.."FieldEarned"):SetText(earned);

			getglobal("GRSSRow"..line.."FieldSpent"):SetText(spent);

			getglobal("GRSSRow"..line.."FieldAdj"):SetText(adj);

			if tonumber(GRSS_DKP[lineplusoffset].total) ~= nil then

				getglobal("GRSSRow"..line.."FieldDKP"):SetText(string.format("%.2f",GRSS_DKP[lineplusoffset].total));

			else

				getglobal("GRSSRow"..line.."FieldDKP"):SetText(GRSS_DKP[lineplusoffset].total);

			end

			getglobal("GRSSRow"..line.."FieldPlayer"):Show();

			getglobal("GRSSRow"..line.."FieldClass"):Show();

			getglobal("GRSSRow"..line.."FieldRank"):Show();

			getglobal("GRSSRow"..line.."FieldEarned"):Show();

			getglobal("GRSSRow"..line.."FieldSpent"):Show();

			getglobal("GRSSRow"..line.."FieldAdj"):Show();

			getglobal("GRSSRow"..line.."FieldDKP"):Show();

			getglobal("GRSSRow"..line.."FieldHighlight"):Show();



		else

			getglobal("GRSSRow"..line.."FieldPlayer"):Hide();

			getglobal("GRSSRow"..line.."FieldClass"):Hide();

			getglobal("GRSSRow"..line.."FieldRank"):Hide();

			getglobal("GRSSRow"..line.."FieldEarned"):Hide();

			getglobal("GRSSRow"..line.."FieldSpent"):Hide();

			getglobal("GRSSRow"..line.."FieldAdj"):Hide();

			getglobal("GRSSRow"..line.."FieldDKP"):Hide();	

			getglobal("GRSSRow"..line.."FieldHighlight"):Hide();



		end

	end

end



function GRSSBidsScrollBar_Update()

	local line; -- 1 through 10 of our window to scroll

	local lineplusoffset; -- an index into our data calculated from the scroll offset

	local earned,sent,adj;

	FauxScrollFrame_Update(GRSSBidsScrollBar,table.getn(GRSS_Bids),23,16);

	--GRSSPrint("size: "..table.getn(GRSS_Bids));

	for line=1,23 do

		lineplusoffset = line + FauxScrollFrame_GetOffset(GRSSBidsScrollBar);

		if lineplusoffset <= table.getn(GRSS_Bids) then

			getglobal("GRSSBidsRow"..line.."FieldPlayer"):SetText(GRSS_Bids[lineplusoffset].name);

			getglobal("GRSSBidsRow"..line.."FieldBid"):SetText(GRSS_Bids[lineplusoffset].bid);	

			--GRSSPrint("Printing on line "..line..": "..GRSS_Bids[lineplusoffset].name.." - "..GRSS_Bids[lineplusoffset].bid);

			getglobal("GRSSBidsRow"..line.."FieldPlayer"):Show();

			getglobal("GRSSBidsRow"..line.."FieldBid"):Show();

			getglobal("GRSSBidsRow"..line.."Delete"):Show();

			getglobal("GRSSBidsRow"..line.."FieldHighlight"):Show();

		else

			getglobal("GRSSBidsRow"..line.."FieldPlayer"):Hide();

			getglobal("GRSSBidsRow"..line.."FieldBid"):Hide();

			getglobal("GRSSBidsRow"..line.."FieldHighlight"):Hide();

			getglobal("GRSSBidsRow"..line.."Delete"):Hide();

		end

	end

end



function nn(v)

	if v == nil then

		return "nil"

	else

		return v;

	end

end



function GuildRaidSnapShot_OnEvent(event,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)

	if(event == "CHAT_MSG_MONSTER_YELL") then

		if GRSS_Auto==1 and GRSS_Yells[arg2] ~= nil then

			if string.find(arg1,GRSS_Yells[arg2]) ~= nil then

				if InCombatLockdown() then

					GRSS_PendingSnapShotName = arg2;

				else

					GRSS_TakeSnapShot(arg2);

				end

			end

		end

	elseif(event == "CHAT_MSG_RAID_BOSS_EMOTE") then

		if GRSS_Auto == 1 then

			for i,v in ipairs(GRSS_BossEmote) do

				if string.find(arg1,v) then

					if InCombatLockdown() then

						GRSS_PendingSnapShotName = i;

					else

						GRSS_TakeSnapShot(i);

					end

				end

			end

		end

	elseif(event == "PLAYER_REGEN_ENABLED") then

		if GRSS_PendingSnapShotName ~= "" then

			GRSS_TakeSnapShot(GRSS_PendingSnapShotName);

			GRSS_PendingSnapShotName = "";

		end

		if table.getn(GRSS_ItemQueue) > 0 then

			GRSS_NextItemPopup();

		end

		if table.getn(GRSS_WaitListRequest)>0 then

			GRSS_NextWaitListRequest()

		end

	elseif(event=="PLAYER_DEAD" or event=="CHAT_MSG_COMBAT_FRIENDLY_DEATH") then

		GRSS_WipeCheck();

	elseif(event=="COMBAT_LOG_EVENT_UNFILTERED") then

		if(arg2=="PARTY_KILL" or arg2=="UNIT_DIED") then

			if not IsActiveBattlefieldArena() then

				if GRSS_FastBossLookup[arg7] ~= nil and GRSS_Auto == 1 then

					if InCombatLockdown() then

						GRSS_PendingSnapShotName = arg7;

					else

						GRSS_TakeSnapShot(arg7);

					end

				end

			end

			--GRSSPrint("Killed: "..nn(arg2)..":"..nn(arg3)..":"..nn(arg4)..":"..nn(arg5)..":"..nn(arg7)..":"..nn(arg8));

		end

	elseif(event=="PLAYER_TARGET_CHANGED") then

		local name = UnitName("target");

		if name ~= nil and GRSS_FastBossLookup[name]~= nil then

			GRSS_WipeCauser = name;

		end

	elseif(event == "PLAYER_GUILD_UPDATE") then

		GuildRoster();

	elseif(event == "VARIABLES_LOADED") then

		GRSSInviteScrollBar_Update();

		GRSS_PendingSnapshotCheck();

		GRSS_TimerRunningCheck();

	elseif(event == "CALENDAR_OPEN_EVENT" or event=="CALENDAR_UPDATE_EVENT" or event=="CALENDAR_UPDATE_EVENT_LIST" or event=="CALENDAR_NEW_EVENT") then

		GRSS_GetCalendar();

		GRSS_StoreEventDetails();

	elseif(event == "GUILD_ROSTER_UPDATE") then

		GRSS_TakeGuildOnly();

	elseif(event == "CHAT_MSG_LOOT") then

		GRSS_CaptureLoot(arg1);

	elseif(event == "RAID_ROSTER_UPDATE") then

		GRSS_RaidChange(arg1,arg2);

	elseif(event == "LOOT_OPENED")  then

		GRSS_GetLoot();

	elseif(event == "CHAT_MSG_WHISPER") then

		GRSS_ProcessWhisper(arg2,arg1,this.language);

	elseif(event == "CHAT_MSG_SYSTEM") then

		if GRSS_Rolling==1 then

			--GRSSPrint("Roll"..GRSS_RollNumber);

			local name, roll;

			for name, roll in string.gmatch(arg1, "(%a+) rolls (%d+) %(1%-"..GRSS_RollNumber.."%)") do

				GRSS_RecordRoll(name, roll);

				GRSSSortBy("roll");



			end

		end

	elseif(event == "ADDON_LOADED") then

		UIDropDownMenu_SetText(GRSSRollNumberDropDown,"1-"..GRSSNumNilZero(GRSS_RollNumber));

	end

end



function GRSS_IsZoneIgnored()

	local zone = GetRealZoneText();

	for i,v in pairs(GRSS_ZoneIgnore) do

		if v == zone then

			return 1;

		end

	end

	return nil;

end



function GRSS_WipeCheck()

	if GRSS_Auto and GRSS_AutoWipe==1 and not IsActiveBattlefieldArena() and not GRSS_IsZoneIgnored() then

		local num = GetNumRaidMembers();

		local is_dead,online,dead,alive,name;

		dead = 0;

		if num>1 then

			for i = 1, num do

				name,_,_,_,_,_,_,online,is_dead = GetRaidRosterInfo(i);

				if is_dead or not online then

					dead = dead + 1

				end

			end

		end

		if (dead/num) >= 0.7 then

			if(GetTime() > GRSS_LastWipeTime+120) then

				GRSS_LastWipeTime = GetTime();

				StaticPopup_Show("GRSS_WIPE");

			end

		end

	end

end



function GRSS_RecordRoll(name,roll)

	local itemname = GRSSItemName:GetText()

	for i,v in pairs(GRSS_DKP) do

		if string.lower(v.name)==string.lower(name) then

			if GRSS_DKP[i].roll == "" or GRSS_DKP[i].roll == nil then

				GRSS_DKP[i].roll = roll;

				GRSS_SendWhisper("Your roll of "..roll.." for "..itemname.." has been recorded","WHISPER",nil,name);

			end

			return;

		end

	end

	temp = {};

	temp.name = name;

	temp.class = "?";

	temp.spent = "";

	temp.earned = "";

	temp.adj = "";

	temp.roll = roll;

	temp.total = "?";

	GRSS_SendWhisper("Your roll of "..roll.." for "..itemname.." has been recorded","WHISPER",nil,name);

	table.insert(GRSS_DKP,1,temp);

	--GRSSScrollBar_Update();

end





function GRSS_SendWhisper(msg,msgtype,lang,to)

	SendChatMessage(GRSS_Prefix..msg,msgtype,lang,to);	

end



function GRSS_ProcessWhisper(from,msg,lang)

	temp = {};

	if(string.find(msg,"^!waitlist")) then

		if not GRSS_IsNameQueuedOnWaitList(from) then

			GRSS_QueueWaitListRequest(from)

		end

		if GRSS_IsNameOnWaitingList(from) then

			GRSS_SendWhisper("You're already on the waiting list","WHISPER",lang,from);

		elseif InCombatLockdown() then

			GRSS_SendWhisper("Your request to be added to the waiting list has been received, but I am currently in combat.  When I am out of combat, I will be able to respond to your request","WHISPER",lang,from);

		elseif GRSS_WaitListRequestBoxOpen==0 then

			GRSS_NextWaitListRequest()

		end

	elseif(string.find(msg,"^!bid")) then

		if GRSS_Bidding == 1 then

			s,e,amt = string.find(msg,"^!bid%s*(%d*)");

			temp.name = from;

			if amt=="" then

				amt = "blank";

			else

				amt = tonumber(amt);

			end

			temp.bid = amt;

			table.insert(GRSS_Bids,temp);

			GRSSBidsScrollBar_Update();

			if GRSS_BidStyle=="Silent Auction" or GRSS_BidStyle=="Open Auction" then

				if tonumber(amt)~=nil and tonumber(amt) > GRSS_HighBid then

					GRSS_SendWhisper("You are now the high bidder with "..amt,"WHISPER",lang,from);

					GRSS_HighBid = tonumber(amt);

					if GRSS_HighBidder ~= from and GRSS_HighBidder ~= "" then

						GRSS_SendWhisper("You are no longer the high bidder.  The high bid is now "..GRSS_HighBid..".","WHISPER",nil,GRSS_HighBidder);	

					end

					GRSS_HighBidder = from;

					if GRSS_BidStyle=="Silent Auction" then

						SendChatMessage("The high bid is now "..amt,"RAID");

					else

						SendChatMessage(GRSS_HighBidder.." is now the high bidder with "..amt,"RAID");

					end

				elseif tonumber(amt)~=nil and tonumber(amt) < GRSS_HighBid then

					GRSS_SendWhisper(amt.." is too low","WHISPER",lang,from);

				elseif tonumber(amt)~=nil and tonumber(amt) == GRSS_HighBid then

					GRSS_SendWhisper("Your bid of "..amt.." ties you with the high bidder", "WHISPER", lang, from);

				else

					GRSS_SendWhisper("Your bid was not understood.  Please specify a number. (ie: !bid 365)","WHISPER",lang,from);

				end

			elseif GRSS_BidStyle=="Silent Non-Auction" then

				GRSS_SendWhisper("Your bid of "..amt.." has been recorded","WHISPER",lang,from);

			end

			local found=false;

			for i,v in pairs(GRSS_DKP) do

				if string.lower(v.name)==string.lower(from) then

					if tonumber(GRSS_DKP[i].bid)==nil or (tonumber(amt) ~= nil and tonumber(GRSS_DKP[i].bid) < tonumber(amt)) then

						GRSS_DKP[i].bid = amt;

					end

					found=true

				end

			end

			if found==false then

				temp = {};

				temp.name = from;

				temp.class = "?";

				temp.spent = "";

				temp.earned = "";

				temp.adj = "";

				temp.bid = amt;

				temp.total = "?";

				table.insert(GRSS_DKP,1,temp);

			end

			GRSSSortBy("bid");

			GRSSScrollBar_Update();

		else

			GRSS_SendWhisper("Sorry, bids are not being received right now", "WHISPER", lang, from);

		end

	elseif(string.find(msg,"^!dkpclass")) then

		s,e,class = string.find(msg,"^!dkpclass%s+(.+)");	

		if class==nil or class == "" then

			GRSS_SendWhisper("You need to specify a class (i.e. !dkpclass mage)", "WHISPER", lang, from);

		else

			found = false;

			local classes = {};

			for w in string.gmatch(string.upper(class),"(%a+)") do

				classes[w]=w;

			end

			if string.find(string.lower(class),"death%s*knight") then

				classes["DEATH KNIGHT"]="DEATH KNIGHT";

			end

			for sys in pairs(GRSS_Systems) do

				GRSSSortByFull("total",sys);

				for i,v in pairs(GRSS_Full_DKP[sys]) do

					if classes[string.upper(v.class)] then

						local total;

						if GRSS_Divide[sys] == nil then

							total = tonumber(v.earned) + tonumber(v.adj) - tonumber(v.spent)

						else

							total = GRSSNumNilZero(v.earned) / GRSSNumNilOne(v.spent);

						end

						GRSS_SendWhisper("("..sys..") "..v.name.."("..v.class.."): "..total,"WHISPER",lang,from);

						found=true;

					end

				end

			end

			if found==false then

				GRSS_SendWhisper("Sorry, no players were found with the class "..class,"WHISPER",lang,from);

			end

		end

	elseif(string.find(msg,"^!dkpraid")) then

		s,e,sysid = string.find(msg,"^!dkpraid%s+(.+)%s*");

		if sysid==nil or sys=="" or GRSS_NumberedSystems[GRSSNumNilZero(sysid)]==nil then

			GRSS_SendWhisper("You'll need to specify one of the following DKP Systems in the following form...","WHISPER",lang,from);

			for sysid,sys in pairs(GRSS_NumberedSystems) do

				GRSS_SendWhisper("For "..sys..": !dkpraid "..sysid,"WHISPER",lang,from);

			end

		else

			sys = GRSS_NumberedSystems[GRSSNumNilZero(sysid)];

			GRSSSortByFull("total",sys);

			RaidTable = GRSS_RaidTable();

			GRSS_SendWhisper("Sending DKP for "..sys.." (only showing Raid Members)","WHISPER",lang,from);

			for i,v in pairs(GRSS_Full_DKP[sys]) do

				if RaidTable[string.upper(v.name)]==1 then

					local total = GRSSNumNilZero(v.earned) + GRSSNumNilZero(v.adj) - GRSSNumNilZero(v.spent)

					GRSS_SendWhisper(v.name.."("..v.class.."): "..total,"WHISPER",lang,from);

					found=true;

				end

			end

		end

	elseif(string.find(msg,"^!dkpall")) then

		s,e,sysid = string.find(msg,"^!dkpall%s+(.+)%s*");

		if sysid==nil or sys=="" or GRSS_NumberedSystems[GRSSNumNilZero(sysid)]==nil then

			GRSS_SendWhisper("You'll need to specify one of the following DKP Systems in the following form...","WHISPER",lang,from);

			for sysid,sys in pairs(GRSS_NumberedSystems) do

				GRSS_SendWhisper("For "..sys..": !dkpall "..sysid,"WHISPER",lang,from);

			end

		else

			local count = 0;

			sys = GRSS_NumberedSystems[GRSSNumNilZero(sysid)];

			GRSSSortByFull("total",sys);

			GRSS_SendWhisper("Sending DKP for "..sys.." (Only showing top 40)","WHISPER",lang,from);

			for i,v in pairs(GRSS_Full_DKP[sys]) do

				if count < 40 then

					local total;

					if GRSS_Divide[sys] == nil then

						total = GRSSNumNilZero(v.earned) + GRSSNumNilZero(v.adj) - GRSSNumNilZero(v.spent)

					else

						total = GRSSNumNilZero(v.earned) / GRSSNumNilOne(v.spent);

					end

					--local total = GRSSNumNilZero(v.earned) + GRSSNumNilZero(v.adj) - GRSSNumNilZero(v.spent)

					GRSS_SendWhisper(v.name.."("..v.class.."): "..total,"WHISPER",lang,from);

					found=true;

					count = count + 1

				end

			end

		end

	elseif(string.find(msg,"^!dkp")) then

		s,e,name = string.find(msg,"^!dkp%s*(.*)");

		if name == "" then

			name = from;

		end

		tosend = GRSSGetPlayerDKP(name);

		for i,v in pairs(tosend) do

			GRSS_SendWhisper(v, "WHISPER", lang, from);

		end

		if table.getn(tosend)==0 then

			GRSS_SendWhisper("No player named "..name.." found", "WHISPER", lang, from);

		end

	elseif(string.find(msg,"^!items")) then

		s,e,name = string.find(msg,"^!items%s*(.*)%s*");

		if name == "" then

			playername = string.upper(from);

		else

			playername = string.upper(name);

		end

		if GRSS_ItemHistory[playername] ~= nil then

			for i,v in pairs(GRSS_ItemHistory[playername]) do

				GRSS_SendWhisper(v,"WHISPER",lang,from);

			end

		else

			GRSS_SendWhisper("There is no item history for "..playername,"WHISPER",lang,from);

		end

	elseif(string.find(msg,"^!price%s")) then

		s,e,itemraw = string.find(msg,"^!price%s+(.*)%s*");

		s,e,color,item = string.find(itemraw, "|c(%x+)|Hitem:%d+:%d+:%d+:%d+|h%[(.-)%]|h|r");

		if not s then

			item = itemraw

		end

		found = false;

		for sys in pairs(GRSS_Systems) do

			for i,v in pairs(GRSS_ItemPrices[sys]) do

				if string.find(string.lower(v.name),string.lower(item)) then

					if GRSSNumNilZero(v.points) > 0 then

						GRSS_SendWhisper("("..sys..") "..v.name..": "..v.points.." points","WHISPER",lang,from);

						found = true;

					end

				end

			end

		end

		if found == false then

			GRSS_SendWhisper("Sorry no item by the name of '"..item.." was found","WHISPER",lang,from);

		end



	elseif(string.find(msg,"^!help") or string.find(msg,"^!%?") or string.find(msg,"^!info") or string.find(msg,"^!commands")) then

		if string.upper(from) == string.upper(UnitName("player")) then

			for i,v in pairs(GRSSHelpMsg) do

				GRSSPrint(v);

			end

		else

			for i,v in pairs(GRSSHelpMsg) do

				GRSS_SendWhisper(v,"WHISPER",lang,from);

			end

		end

	end

end



function GRSSGetPlayerDKP(players)

	local dkp = {};

	for sys in pairs(GRSS_Systems) do

		for i,v in pairs(GRSS_Full_DKP[sys]) do

			for name in string.gmatch(players,"(%a+)") do

				if string.lower(v.name)==string.lower(name) then

					local total;

					if GRSS_Divide[sys] == nil then

						total = GRSSNumNilZero(v.earned) + GRSSNumNilZero(v.adj) - GRSSNumNilZero(v.spent)

					else

						total = GRSSNumNilZero(v.earned) / GRSSNumNilOne(v.spent);

					end

					--total = GRSSNumNilZero(v.earned) + GRSSNumNilZero(v.adj) - GRSSNumNilZero(v.spent);

					table.insert(dkp,"("..sys..") "..name.."("..v.class.."): "..total);

				end

			end

		end

	end

	return dkp;

end



function GuildRaidSnapShot_SlashHandler(msg)

	local newboss;

	msglower = string.lower(msg);

	if msglower == "" or msglower=="help" then

		for i,v in pairs(GRSSUsage) do

			DEFAULT_CHAT_FRAME:AddMessage(v);

		end

		for i,v in pairs(GRSSHelpMsg) do

			local tellcolor = "|c00ffff00";

			local endcolor = "|r";

			v = string.gsub(v,"^(.+) = (.+)",tellcolor.."%1"..endcolor.." = %2");

			DEFAULT_CHAT_FRAME:AddMessage(v);

		end

	elseif msglower == "reset" then

		GuildRaidSnapShot_SnapShots = {};

		GuildRaidSnapShot_Loot = {};

		GRSSPrint("Snapshots Purged");

	elseif msglower == "noauto" then

		GRSS_Auto = 0;

		GRSSPrint("Auto Snapshots Disabled");

	elseif msglower == "yesauto" then

		GRSS_Auto = 1;

		GRSSPrint("Auto Snapshots Enabled");

	elseif msglower == "yesloot" then

		GRSS_LootCap = 1;

		GRSSPrint("Looting will now result in popups prompting for points");

	elseif msglower == "noloot" then

		GRSS_LootCap = 0;

		GRSSPrint("Looting will no longer result in popups prompting for points");

	elseif msglower == "yeslootcombat" then

		GRSS_LootPromptInCombat = 1;

		GRSSPrint("Loot prompts will now pop up immediately");

	elseif msglower == "nolootcombat" then

		GRSS_LootPromptInCombat = 0;

		GRSSPrint("Loot prompts will now wait to pop once you're out of combat");

	elseif msglower == "show" then

		GRSS:Show();

	elseif msglower == "invite" then

		GRSSInvite:Show();

	elseif msglower == "starttimer" then

		StaticPopup_Show("GRSS_STARTTIMER");

	elseif msglower == "stoptimer" then

		GRSS_StopTimer();

	elseif msglower == "yesscreenshot" then

		GRSSPrint("Screenshots of the raid will be taken when snapshots are initiated");

		GRSS_TakeScreenshots = 1

	elseif msglower == "noscreenshot" then

		GRSS_TakeScreenshots = nil

		GRSSPrint("Screenshots of the raid will no longer be taken when snapshots are initiated");

	elseif msglower == "yeswipe" then

		GRSS_AutoWipe = 1;

		GRSSPrint("Wipe Detection enabled");

	elseif msglower == "nowipe" then

		GRSS_AutoWipe = 0;

		GRSSPrint("Wipe Detection disabled");

	elseif GetNumRaidMembers() > 0 or GetNumPartyMembers() > 0 then

		GRSS_TakeSnapShot(msg);

	else

		GRSSPrint("You're not in a raid");

	end

end



function GRSS_CaptureLoot(msg)

	if GetNumRaidMembers()>0 or GetNumPartyMembers()>0 then

		local s, e, player, link = string.find(msg, "([^%s]+) receives loot: (.+)%.");

		if(player == nil) then

			s, e, link = string.find(msg, "You receive loot: (.+)%.");

			if(link ~= nil) then

				player = UnitName("player");

			end

		end

		--GRSS_Auto = link;

		if(link and player) then

			local s, e, color, item = string.find(link, "|c(%x+)|Hitem[:%d%-]+|h%[(.-)%]|h|r");

			--GRSSPrint("color"..color..": item"..item);

			if(color and item and GRSS_Colors[color]) then

				-- Checking to see if the item listed is one we should ignore

				for i,v in pairs(GRSS_LootIgnore) do

					if string.find(item,v) then

						return;

					end

				end

				local lootdate = date("%Y-%m-%d");

				if(GuildRaidSnapShot_Loot[lootdate] == nil) then

					GuildRaidSnapShot_Loot[lootdate] = {};

				end

				local iteminfo = {};

				iteminfo.player = player;

				iteminfo.item = item;

				iteminfo.RealmName = GetRealmName();

				iteminfo.date = date("%Y-%m-%d %H:%M:%S");

				table.insert(GuildRaidSnapShot_Loot[lootdate],iteminfo);

				if GRSS_LootCap == 1 then

					GRSS_CurrentLootDate = lootdate;

					lootindex = table.getn(GuildRaidSnapShot_Loot[lootdate]);

					if GRSS_HighBid==0 then

						points = GRSS_GetItemPoints(GuildRaidSnapShot_Loot[lootdate][lootindex].item);

					else

						points = GRSS_HighBid;

					end	

					GRSS_EnqueueItem(lootdate,lootindex,player,link,points);

					if GRSS_ItemBoxOpen==0 and (not(InCombatLockdown()) or GRSS_LootPromptInCombat==1) then

						GRSS_NextItemPopup();

					end

				end

			end

		end

	end

end





function GRSS_TakeSnapShot(name)

	local SnapShotName = name.." "..date("%Y-%m-%d %H:%M:%S");

	if GRSS_LastSnapshot == nil then

		GRSS_LastSnapshot = 0

	end

	if GuildRaidSnapShot_SnapShots[SnapShotName]==nil and GetTime() > GRSS_LastSnapshot+4 then

		GRSS_LastSnapshot = GetTime();

		GuildRaidSnapShot_SnapShots[SnapShotName] = {};	

		GuildRaidSnapShot_SnapShots[SnapShotName].RealmName = GetRealmName();

		

		local OnlineRaid,RaidList = GRSS_RaidCommaList();

		GuildRaidSnapShot_SnapShots[SnapShotName]["Raid"] = RaidList;

		

		local OnlineGuild,GuildList = GRSS_GuildCommaList();

		GuildRaidSnapShot_SnapShots[SnapShotName]["Guild"] = GuildList;



		local OnlineWaitingList,WaitingList = GRSS_WaitCommaList();

		GuildRaidSnapShot_SnapShots[SnapShotName]["WaitList"] = WaitingList;



	

		GRSSPrint("SnapShot Taken: "..SnapShotName.." (Guild: "..OnlineGuild.." | Raid: "..OnlineRaid.." | Waiting List: "..OnlineWaitingList..")");

		if GRSS_TakeScreenshots==1 then

			Screenshot();

		end

	end

end



function GRSS_TakeGuildOnly()

	local members="", level,class,online,rank,notes;

	GRSS_Guild = {};

	local n = GetNumGuildMembers(true);

	local NumOnline = 0;

	for i = 1, n do

		MemName,rank,_,level,class,_,notes,_,_,online = GetGuildRosterInfo(i);

		if notes == nil then

			notes = "";

		end

		GRSS_Guild[MemName] = level .. ";" .. class..";"..rank..";;;"..notes;

	end



end



function GRSS_TakeGuildNotes()

	local members="", notes,num;

	num =0;

	GuildRoster();

	GRSS_Guild = {};

	local n = GetNumGuildMembers(true);

	local NumOnline = 0;

	GuildRaidSnapShot_Notes = {};

	for i = 1, n do

		MemName,_,_,_,_,_,notes = GetGuildRosterInfo(i);

		if notes ~= "" then

			num = num + 1;

			GuildRaidSnapShot_Notes[MemName] = notes;

		end

	end

	GRSSPrint("Guild notes Snapshot Taken: "..num.." Guild Notes Captured");

end



function GRSS_RaidCommaList()

	local members="";

	local NumOnline=0;

	local zone="";

	local con;

	local n = GetNumRaidMembers();

	if GetNumRaidMembers()>1 then

		for i = 1, n do

			MemName,_,_,_,_,_,zone,Online = GetRaidRosterInfo(i);

			if Online==1 then

				if zone==nil then

					zone = ""

				end

			else

				zone = "Offline";

			end

			members = members..MemName..":"..zone;

			if i ~= n then

				members = members .. ", "

			end

			NumOnline=NumOnline+1;

		end

		if GRSS_TakeScreenshots==1 then

			if InCombatLockdown() then

				GRSSPrint("You're currently in combat, so I can't open the raid window for this snapshot");

			else

				ToggleFriendsFrame(1);

				ToggleFriendsFrame(5);

			end

		end

	else

		n = GetNumPartyMembers();

		zone = "";

		for i = 1, n do

			MemName = UnitName("party"..i);

			if UnitIsConnected("party"..i) then

				con=1

			else

				con=0

			end

			if MemName ~= nil and con==1 then

				members = members..MemName..":"..zone..", ";



				NumOnline = NumOnline + 1

			end

		end

		members = members..UnitName("player")..":"..zone;

		NumOnline = NumOnline + 1;

	end

	return NumOnline,members

end



function GRSS_GuildCommaList()

	local members="";

	local online;

	local zone="";

	GuildRoster();

	local n = GetNumGuildMembers(false);

	local NumOnline = 0;

	for i = 1, n do

		MemName,_,_,_,_,zone,_,_,online = GetGuildRosterInfo(i);

		if zone==nil then

			zone = ""

		end

		if online==1 then

			members = members..MemName .. ":" .. zone;

			if i ~= n then

				members = members .. ", "

			end

			NumOnline = NumOnline + 1;

		end

	end

	return NumOnline,members;

end





function GRSS_WaitCommaList()

	local num = table.getn(GRSS_WaitingList);

	local waitlist = "";

	for i = 1, num do

		waitlist = waitlist..GRSS_WaitingList[i]

		if i<num then

			waitlist = waitlist..", ";

		end

	end

	return num,waitlist;

end		





function GRSS_RaidTable()

	local members={};

	local NumOnline=0;

	local con;

	local n = GetNumRaidMembers();

	if GetNumRaidMembers()>1 then

		for i = 1, n do

			MemName,_,_,_,_,_,_,Online = GetRaidRosterInfo(i);

			if Online==1 then

				members[string.upper(MemName)]=1;

			end

		end

	else

		n = GetNumPartyMembers();

		zone = "";

		for i = 1, n do

			MemName = UnitName("party"..i);

			if UnitIsConnected("party"..i) then

				con=1

			else

				con=0

			end

			if MemName ~= nil and con==1 then

				members[string.upper(MemName)]=1;

			end

		end

		members[string.upper(UnitName("player"))]=1;

	end

	return members;

end



function GRSS_GuildTable()

	local members={};

	local online;

	GuildRoster();

	local n = GetNumGuildMembers(false);

	local NumOnline = 0;

	for i = 1, n do

		MemName,_,_,_,_,_,_,_,online = GetGuildRosterInfo(i);

		if online==1 then

			members[string.upper(MemName)]=1;

		end

	end

	return members;

end



function GRSSSystemDropDown_OnLoad()

	GRSS_Initialize_Data();

	GRSSChangeSystem(GRSSCurrentSystem);

	UIDropDownMenu_Initialize(this, GRSSSystemDropDown_Initialize);

	if GRSS_Systems[GRSSCurrentSystem]~=nil then

		UIDropDownMenu_SetText(GRSSSystemDropDown,GRSSCurrentSystem);

	else

		UIDropDownMenu_SetText(GRSSSystemDropDown,"Select System");

	end

	UIDropDownMenu_SetWidth(GRSSSystemDropDown,130);

end



function GRSSActionDropDown_OnLoad()

	UIDropDownMenu_Initialize(this, GRSSActionDropDown_Initialize);

	UIDropDownMenu_SetText(GRSSActionDropDown,"DKP Standings");

	UIDropDownMenu_SetWidth(GRSSActionDropDown,110);

end



function GRSSBidStyleDropDown_OnLoad()

	UIDropDownMenu_Initialize(this, GRSSBidStyleDropDown_Initialize);

	if GRSS_BidStyle == "" then

		UIDropDownMenu_SetText(GRSSBidStyleDropDown,"Bidding Style");	

	else

		UIDropDownMenu_SetText(GRSSBidStyleDropDown,GRSS_BidStyle);

	end

	UIDropDownMenu_SetWidth(GRSSBidStyleDropDown,110);

end



function GRSSBidStyleDropDown_Initialize()

	local Actions = {"Silent Auction","Silent Non-Auction","Open Auction"};

	local info = {};

	

	info.notCheckable = nil;

	info.keepShownOnClick = nil;

	info.func = GRSS_ChangeBidStyle;

	for i,v in pairs(Actions) do

		info.text = v;

		info.value = v;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSS_ChangeBidStyle()

	UIDropDownMenu_SetText(GRSSBidStyleDropDown,this.value);

	GRSS_BidStyle = this.value;

end



function GRSSSpamDropDown_OnLoad()

	UIDropDownMenu_Initialize(this, GRSSSpamDropDown_Initialize);

	UIDropDownMenu_SetText(GRSSSpamDropDown,"Spam Chat");	

	UIDropDownMenu_SetWidth(GRSSSpamDropDown,110);

end



function GRSSSpamDropDown_Initialize()

	local Actions = {"RAID","PARTY","GUILD","SAY","OFFICER"};

	local info = {};

	

	info.notCheckable = nil;

	info.keepShownOnClick = nil;

	info.func = GRSS_ChangeSpam;

	for i,v in pairs(Actions) do

		info.text = v;

		info.value = v;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSS_ChangeSpam()

	local place = this.value;

	SendChatMessage("The following are for the DKP System: "..GRSSCurrentSystem,place);

	if table.getn(GRSS_DKP) > 40 then

		SendChatMessage("Only showing the top 40",place);

	end

	local count=0;

	for i,v in pairs(GRSS_DKP) do

		if count < 40 then

			GRSS_SendWhisper(v.name..": "..v.total,place);

			count = count + 1;

		end

	end

end





function GRSSRaidFilterDropDown_OnLoad()

	UIDropDownMenu_Initialize(this, GRSSRaidFilterDropDown_Initialize);

	if GRSS_Spam == "" then

		UIDropDownMenu_SetText(GRSSRaidFilterDropDown,"All");	

	else

		UIDropDownMenu_SetText(GRSSRaidFilterDropDown,GRSS_RaidFilter);

	end

	UIDropDownMenu_SetWidth(GRSSRaidFilterDropDown,90);

end



function GRSSRaidFilterDropDown_Initialize()

	local Actions = {"All","Raid Only"};

	local info = {};

	

	info.notCheckable = nil;

	info.keepShownOnClick = nil;

	info.func = GRSS_ChangeRaidFilter;

	for i,v in pairs(Actions) do

		info.text = v;

		info.value = v;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSS_ChangeRaidFilter()

	UIDropDownMenu_SetText(GRSSRaidFilterDropDown,this.value);

	GRSS_RaidFilter = this.value;

	GRSSChangeSystem(GRSSCurrentSystem);

end



function GRSSClassFilterDropDown_OnLoad()

	UIDropDownMenu_Initialize(this, GRSSClassFilterDropDown_Initialize);

	if GRSS_BidStyle == "" then

		UIDropDownMenu_SetText(GRSSClassFilterDropDown,"All");	

	else

		UIDropDownMenu_SetText(GRSSClassFilterDropDown,GRSS_ClassFilter);

	end

	UIDropDownMenu_SetWidth(GRSSClassFilterDropDown,90);

end



function GRSSClassFilterDropDown_Initialize()

	local Actions = {"All","Druid","Hunter","Mage","Paladin","Priest","Rogue","Shaman","Warlock","Warrior","Death Knight"};

	local info = {};

	

	info.notCheckable = nil;

	info.keepShownOnClick = nil;

	info.func = GRSS_ChangeClassFilter;

	for i,v in pairs(Actions) do

		info.text = v;

		info.value = v;

		UIDropDownMenu_AddButton(info);

	end

end





function GRSS_ChangeClassFilter()

	--GRSSPrint(this.value);

	UIDropDownMenu_SetText(GRSSClassFilterDropDown,this.value);

	--UIDropDownMenu_SetText(this.value,GRSSClassFilterDropDown);

	GRSS_ClassFilter = this.value;

	GRSSChangeSystem(GRSSCurrentSystem);

end



function GRSSRollNumberDropDown_OnLoad()

	

	UIDropDownMenu_Initialize(this, GRSSRollNumberDropDown_Initialize);

	--GRSSPrint(GRSS_RollNumber);

	UIDropDownMenu_SetText(GRSSRollNumberDropDown,"1-"..GRSSNumNilZero(GRSS_RollNumber));

	--UIDropDownMenu_SetText("1-"..GRSSNumNilZero(GRSS_RollNumber), GRSSRollNumberDropDown);	

	UIDropDownMenu_SetWidth(GRSSRollNumberDropDown,110);

end



function GRSSRollNumberDropDown_Initialize()

	local Actions = {"1-100","1-1000","1-1337"};

	local info = {};

	

	info.notCheckable = nil;

	info.keepShownOnClick = nil;

	info.func = GRSS_ChangeRollNumber;

	for i,v in pairs(Actions) do

		info.text = v;

		info.value = v;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSS_ChangeRollNumber()

	UIDropDownMenu_SetText(GRSSRollNumberDropDown,this.value);

	local s,e,num = string.find(this.value,"1%-(%d+)")

	GRSS_RollNumber = num;

end





function GRSSSystemDropDown_OnClick()

	GRSSCurrentSystem = this.value;

	UIDropDownMenu_SetText(GRSSSystemDropDown,this.value);

	this.isChecked=true;

	GRSSChangeSystem(GRSSCurrentSystem)

end





function GRSS_DeleteBid(line)

	lineplusoffset = line + FauxScrollFrame_GetOffset(GRSSBidsScrollBar);

	amount = GRSS_Bids[lineplusoffset].bid;

	name = GRSS_Bids[lineplusoffset].name;

	table.remove(GRSS_Bids,lineplusoffset);

	playerhigh = 0;

	totalhigh = 0;

	highbidder = "";

	for i,v in pairs(GRSS_Bids) do

		if string.lower(v.name)==string.lower(name) then

			if tonumber(v.bid)~=nil and tonumber(v.bid) > playerhigh then

				playerhigh = tonumber(v.bid);

			end

		end

		if tonumber(v.bid)~=nil and tonumber(v.bid) > totalhigh then

			totalhigh = tonumber(v.bid);

			highbidder = v.name;

		end

	end

	if GRSS_HighBid ~= totalhigh and GRSS_Bidding==1 then

		GRSS_HighBid = totalhigh;

		GRSS_HighBidder = highbidder;

		if GRSS_BidStyle=="Silent Auction" then

			SendChatMessage("The high bid has been changed to "..GRSS_HighBid,"RAID");

		elseif GRSS_BidStyle=="Open Auction" then

			SendChatMessage(highbidder.." is now the high bidder with "..totalhigh,"RAID");

		end

	end



	for i,v in pairs(GRSS_DKP) do

		if string.lower(v.name)==string.lower(name) then

			GRSS_DKP[i].bid = playerhigh;

		end

	end		

	GRSSBidsScrollBar_Update();

	GRSSScrollBar_Update();

end



function GRSSActionDropDown_Initialize()

	local Actions = {"DKP Standings","Rolls","Bids"};

	local info = {};

	

	info.notCheckable = nil;

	info.keepShownOnClick = nil;

	info.func = GRSS_DoAction;

	for i,v in pairs(Actions) do

		info.text = v;

		info.value = v;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSSShowDKP(DKP)

	if DKP == true then

		GRSSHeaderEarnedText:SetText("Earned");

		GRSSHeaderSpentText:SetText("Spent");

		GRSSHeaderAdjText:SetText("Adj");

		GRSSHeaderDKPText:SetText("Totl");

		GRSSRaidFilterDropDown:Show();

		GRSSClassFilterDropDown:Show();

		GRSSSpamDropDown:Show();

		GRSSLootDropDown:Hide();

	else

		GRSSRaidFilterDropDown:Hide();

		GRSSClassFilterDropDown:Hide();

		GRSSSpamDropDown:Hide();

		GRSSLootDropDown:Show();

	end



end



function GRSSShowRolls(Rolls)

	if Rolls == true then

		GRSSHeaderEarnedText:SetText("");

		GRSSHeaderSpentText:SetText("Roll");

		GRSSHeaderAdjText:SetText("");

		GRSSHeaderDKPText:SetText("Current DKP");

		GRSSToggleRolls:Show();

		GRSSClearRolls:Show();

		GRSSRollNumberDropDown:Show();

	else

		GRSSToggleRolls:Hide();

		GRSSClearRolls:Hide();

		GRSSRollNumberDropDown:Hide();

	end

end



function GRSSShowBids(Bids)

	if Bids == true then

		GRSSHeaderEarnedText:SetText("");

		GRSSHeaderSpentText:SetText("Highest Bid");

		GRSSHeaderAdjText:SetText("");

		GRSSHeaderDKPText:SetText("Current DKP");

		GRSSBids:Show();

		GRSSToggleBids:Show();

		GRSSClearBids:Show();

		GRSSBidStyleDropDown:Show();

	else

		GRSSBids:Hide();

		GRSSToggleBids:Hide();

		GRSSClearBids:Hide();

		GRSSBidStyleDropDown:Hide();

	end

end



function GRSS_ToggleRolls()

	local itemname = GRSSItemName:GetText()

	if GRSS_Rolling == 1 then

		GRSSToggleRolls:SetText("Start Rolls");

		GRSSRollNumberDropDown:Show();

		GRSS_Rolling = 0;

		GRSS_EnableFunctionalButtons(1);

		SendChatMessage("Rolling Ended on "..itemname..". No more rolls will be recorded", "RAID");

		SendChatMessage("Rolling Ended on "..itemname..". No more rolls will be recorded", "RAID_WARNING");			

	else

		GRSSToggleRolls:SetText("Stop Rolls");

		GRSSRollNumberDropDown:Hide();

		GRSS_Rolling = 1;

		GRSS_EnableFunctionalButtons(0);

		SendChatMessage("Rolling Started on "..itemname..".  If you are interested /random "..GRSS_RollNumber..".  Rolls with anything but "..GRSS_RollNumber.." will be ignored.", "RAID");

		SendChatMessage("Rolling Started on "..itemname..".  If you are interested /random "..GRSS_RollNumber..".", "RAID_WARNING");

	end

end





function GRSS_EnableFunctionalButtons(tf)

	if tf == 1 then

		GRSSClearBids:Enable();

		GRSSClearRolls:Enable();

		GRSSSystemDropDown:Show();

		GRSSActionDropDown:Show();

		GRSSItemName:Show();

		GRSSLootDropDown:Show();

	else

		GRSSClearBids:Disable();

		GRSSClearRolls:Disable();

		GRSSBidStyleDropDown:Hide();

		GRSSSystemDropDown:Hide();

		GRSSActionDropDown:Hide();

		GRSSItemName:Hide();

		GRSSLootDropDown:Hide();

	end

end





function GRSS_ToggleBids()

	local player = UnitName("player");

	local itemname = GRSSItemName:GetText()

	if GRSS_Bidding == 1 then

		GRSSToggleBids:SetText("Start Bids");

		GRSS_Bidding = 0;

		GRSSBidStyleDropDown:Show();

		GRSS_EnableFunctionalButtons(1);

		SendChatMessage("Bidding Ended for "..itemname..". No more bids will be recorded", "RAID");

		SendChatMessage("Bidding Ended for "..itemname..".", "RAID_WARNING");

	else

		GRSSToggleBids:SetText("Stop Bids");

		GRSS_Bidding = 1;

		GRSSBidStyleDropDown:Hide();

		GRSS_HighBid = 0;

		GRSS_EnableFunctionalButtons(0);

		SendChatMessage("Bidding Started for "..itemname.."! To bid: '/w "..player.." !bid X' where X is the amount you wish to bid", "RAID");

		SendChatMessage("Bidding Started for "..itemname.."! To bid: '/w "..player.." !bid X' where X is the amount you wish to bid", "RAID_WARNING");

		

	end

end





function GRSS_ClearBids()

	GRSS_Bids = {};

	for i,v in pairs(GRSS_DKP) do

		GRSS_DKP[i].bid = ""

	end

	GRSSScrollBar_Update();

	GRSSBidsScrollBar_Update();

end



function GRSS_ClearRolls()

	for i,v in pairs(GRSS_DKP) do

		GRSS_DKP[i].roll = ""

	end

	GRSSScrollBar_Update();

end





function GRSS_DoAction()

	GRSSCurrentAction = this.value;

	UIDropDownMenu_SetText(GRSSActionDropDown,this.value);

	local DKP = false;

	local Rolls = false;

	local Bids = false;

	if this.value == "DKP Standings" then

		DKP = true;

	elseif this.value == "Rolls" then

		Rolls = true;

	elseif this.value == "Bids" then

		Bids = true;

	end

	if Rolls==true or Bids==true then

		GRSSItemName:Show();

	else

		GRSSItemName:Hide();

	end

	GRSSShowDKP(DKP);

	GRSSShowRolls(Rolls);

	GRSSShowBids(Bids);

	GRSSScrollBar_Update();

end



function GRSSSystemDropDown_Initialize()

	local info = {};



	if (not GRSS_Systems) then GRSS_Systems = {}; end;

	

	for index, sys in pairs(GRSS_Systems) do

		info.text = sys;

		info.value = sys;

		info.func = GRSSSystemDropDown_OnClick;

		info.notCheckable = nil;

		info.keepShownOnClick = nil;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSSLootDropDown_OnLoad()

	UIDropDownMenu_Initialize(this, GRSSLootDropDown_Initialize);

end



function GRSSLootDropDown_Initialize()

	local info = {};



	if (not GRSS_Loot) then GRSS_Loot = {}; end;

	

	for link in pairs(GRSS_Loot) do

		info.text = link;

		info.value = link;

		info.func = GRSSLootDropDownSelect;

		info.notCheckable = nil;

		info.keepShownOnClick = nil;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSSLootDropDown_OnClick()

	ToggleDropDownMenu(1, nil, GRSSLootDropDown, "GRSSItemName", 0, 0);

end



function GRSSLootDropDownSelect()

	GRSSItemName:SetText(this.value);

end



function GRSS_GetLoot()

	GRSS_Loot = {};

	for i = 1, GetNumLootItems() do

     		if LootSlotIsItem(i) then

       			local link = GetLootSlotLink(i);

       			local s, e, color, item = string.find(link, "|c(%x+)|Hitem[:%d%-]+|h%[(.-)%]|h|r");

			--GRSSPrint("color"..color..": item"..item);

			if(color and item and GRSS_Colors[color]) then

				GRSS_Loot[link]=link;

			end

     		end

   	end

	UIDropDownMenu_Initialize(GRSSLootDropDown, GRSSLootDropDown_Initialize);

end

















-- Begin the Waiting List/Invite Code





function GRSSInviteDropDown_OnLoad()

	UIDropDownMenu_Initialize(this, GRSSInviteDropDown_Initialize);

	UIDropDownMenu_SetText(GRSSInviteDropDown,"Waiting List");

	UIDropDownMenu_SetWidth(GRSSInviteDropDown,110);

end



function GRSSInviteDropDown_Initialize()

	local Actions = {"Waiting List","Signup Invites"};

	local info = {};

	

	info.notCheckable = nil;

	info.keepShownOnClick = nil;

	info.func = GRSS_ChangeInvite;

	for i,v in pairs(Actions) do

		info.text = v;

		info.value = v;

		UIDropDownMenu_AddButton(info);

	end

end



function GRSS_ChangeInvite()

	UIDropDownMenu_SetText(GRSSInviteDropDown,this.value);

	GRSS_InviteType = this.value;

	if GRSS_InviteType == "Waiting List" then

		GRSSInviteWaitingList:Show();

	else

		GRSSInviteWaitingList:Hide();

	end

	GRSSInviteScrollBar_Update();

end



function GRSSInviteScrollBar_Update()

	local line; -- 1 through 10 of our window to scroll

	local lineplusoffset; -- an index into our data calculated from the scroll offset

	if GRSS_InviteType == "Waiting List" then

		FauxScrollFrame_Update(GRSSInviteScrollBar,table.getn(GRSS_WaitingList),10,16);

		--GRSSPrint("size: "..table.getn(GRSS_Bids));

		for line=1,10 do

			lineplusoffset = line + FauxScrollFrame_GetOffset(GRSSInviteScrollBar);

			if lineplusoffset <= table.getn(GRSS_WaitingList) then

				getglobal("GRSSInviteRow"..line.."FieldItem"):SetText(GRSS_WaitingList[lineplusoffset]);

				getglobal("GRSSInviteRow"..line.."FieldItem"):Show();

				getglobal("GRSSInviteRow"..line.."Invite"):Show();

				getglobal("GRSSInviteRow"..line.."FieldHighlight"):Show();

				getglobal("GRSSInviteRow"..line.."Delete"):Show();

			else

				getglobal("GRSSInviteRow"..line.."FieldItem"):Hide();

				getglobal("GRSSInviteRow"..line.."FieldHighlight"):Hide();

				getglobal("GRSSInviteRow"..line.."Invite"):Hide();

				getglobal("GRSSInviteRow"..line.."Delete"):Hide();

			end

		end

	else

		FauxScrollFrame_Update(GRSSInviteScrollBar,table.getn(GRSS_RaidSignups),10,16);

		--GRSSPrint("size: "..table.getn(GRSS_Bids));

		for line=1,10 do

			lineplusoffset = line + FauxScrollFrame_GetOffset(GRSSInviteScrollBar);

			if lineplusoffset <= table.getn(GRSS_RaidSignups) then

				getglobal("GRSSInviteRow"..line.."FieldItem"):SetText(GRSS_RaidSignups[lineplusoffset].name);

				getglobal("GRSSInviteRow"..line.."FieldItem"):Show();

				getglobal("GRSSInviteRow"..line.."Invite"):Show();

				getglobal("GRSSInviteRow"..line.."FieldHighlight"):Show();

				getglobal("GRSSInviteRow"..line.."Delete"):Hide();

			else

				getglobal("GRSSInviteRow"..line.."FieldItem"):Hide();

				getglobal("GRSSInviteRow"..line.."FieldHighlight"):Hide();

				getglobal("GRSSInviteRow"..line.."Invite"):Hide();

				getglobal("GRSSInviteRow"..line.."Delete"):Hide();

			end

		end

	end

end





function GRSS_InviteLine(n)

	if GRSS_InviteType == "Waiting List" then

		local lineplusoffset = n + FauxScrollFrame_GetOffset(GRSSInviteScrollBar);

		local name = GRSS_WaitingList[lineplusoffset];

		InviteUnit(name);

	else

		local RaidTable = GRSS_RaidTable();

		local lineplusoffset = n + FauxScrollFrame_GetOffset(GRSSInviteScrollBar);

		for i,v in pairs(GRSS_RaidSignups[lineplusoffset].waiting) do

			GRSS_AddNameToWaitingList(v);

		end

		for i,v in pairs(GRSS_RaidSignups[lineplusoffset].pending) do

			GRSS_AddNameToWaitingList(v);

		end

		for i,v in pairs(GRSS_RaidSignups[lineplusoffset].approved) do

			InviteUnit(v);

		end

	end

end



function GRSS_DeleteInviteLine(n)

	if GRSS_InviteType == "Waiting List" then

		local lineplusoffset = n + FauxScrollFrame_GetOffset(GRSSInviteScrollBar);

		local name = GRSS_WaitingList[lineplusoffset];

		GRSS_RemoveFromWaitingList(name);

	end

end





function GRSS_RaidChange(arg1,arg2)

	--GRSSPrint("arg1: "..arg1);

	--GRSSPrint("arg2: "..arg2);

	local i=1;

	local RaidTable = GRSS_RaidTable();

	while i <= table.getn(GRSS_WaitingList) do

		if RaidTable[string.upper(GRSS_WaitingList[i])]==1 then

			table.remove(GRSS_WaitingList,i)

		else

			i = i + 1

		end

	end

	GRSSInviteScrollBar_Update();

end





function GRSS_RemoveFromWaitingList(name)

	local i = 1;

	while i <= table.getn(GRSS_WaitingList) do

		v = GRSS_WaitingList[i];

		if string.upper(v)==string.upper(name) then

			table.remove(GRSS_WaitingList,i);

		else

			i = i + 1

		end

	end

	GRSSInviteScrollBar_Update();

end



function GRSS_IsNameOnWaitingList(name)

	for i,v in pairs(GRSS_WaitingList) do

		if string.upper(name)==string.upper(v) then

			return true;

		end

	end

	return false;

end



function GRSS_AddToWaitingList()

	StaticPopup_Show("GRSS_WAITINGLIST");

end



function GRSS_AddNameToWaitingList(name)

	for n in string.gmatch(name,"(%a+)") do

		if not GRSS_IsNameOnWaitingList(n) then

			table.insert(GRSS_WaitingList,n);

		end

	end

	GRSSInviteScrollBar_Update();

end



function GRSS_IsNameQueuedOnWaitList(name)

	for i,v in pairs(GRSS_WaitListRequest) do

		if string.upper(name)==string.upper(v) then

			return true;

		end

	end

	return false;

end



function GRSS_QueueWaitListRequest(name)

	table.insert(GRSS_WaitListRequest,1,name);

end



function GRSS_NextWaitListRequest()

	local n = table.getn(GRSS_WaitListRequest);

	if n > 0 then

		local name = GRSS_WaitListRequest[n];

		StaticPopup_Show("GRSS_AUTOWAITLIST",name);

	end

	

end



function GRSSPrint(msg)

	DEFAULT_CHAT_FRAME:AddMessage("GRSS: "..msg);

end





