local toc, privateData = ...
local AddonId = toc.identifier

EM.UI.Inbox = {}
EM.Inbox = {}
EM.Inbox.Event = {}

local mathFloor = math.floor
local tableInsert = table.insert
local tableSort = table.sort
local stringFind = string.find
local stringLower = string.lower

EM.Event.Trigger.TakeAttachment, EM.Event.TakeAttachment = Utility.Event.Create(AddonId, "TakeAttachment")
EM.Event.Trigger.OpenMessage, EM.Event.OpenMessage = Utility.Event.Create(AddonId, "OpenMessage")

function EM.UI.Setup.Inbox()

	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings

	inboxTab.noMail = UI.CreateFrame("Text", AddonId .. "_Inbox_noMail", inboxTab)
		inboxTab.noMail:SetPoint("CENTER", inboxTab, "CENTER")
		inboxTab.noMail:SetFontSize(20)
		inboxTab.noMail:SetFontColor(1,1,1,1)
		inboxTab.noMail:SetLayer(10)
		inboxTab.noMail:SetText("No Mail")
		inboxTab.noMail:SetVisible(true)

	inboxTab.Grid = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid", inboxTab)
		inboxTab.Grid:SetPoint("TOPLEFT", inboxTab, "TOPLEFT", 10, 34)
		inboxTab.Grid:SetWidth(390)
		inboxTab.Grid:SetHeight(401)
		inboxTab.Grid:SetLayer(15)
		inboxTab.Grid:SetVisible(true)
		inboxTab.Grid:SetBackgroundColor(0,0,0,1)

		inboxTab.Grid:EventAttach(Event.UI.Input.Mouse.Wheel.Back, EM.Inbox.Event.WheelBack, AddonId .. "_Inbox_Grid_WheelBack")
		inboxTab.Grid:EventAttach(Event.UI.Input.Mouse.Wheel.Forward, EM.Inbox.Event.WheelForward, AddonId .. "_Inbox_Grid_WheelForward")

		inboxTab.Grid.Header = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header", inboxTab.Grid)
			inboxTab.Grid.Header:SetPoint("TOPLEFT", inboxTab, "TOPLEFT", 10, 5)
			inboxTab.Grid.Header:SetPoint("BOTTOMRIGHT", inboxTab.Grid, "TOPRIGHT")
			inboxTab.Grid.Header:SetLayer(15)
			inboxTab.Grid.Header:SetBackgroundColor(0,0,0,0)

			inboxTab.Grid.Header.icon = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_icon", inboxTab.Grid.Header)
				inboxTab.Grid.Header.icon:SetPoint("TOPLEFT", inboxTab.Grid.Header, "TOPLEFT", 1, 0)
				inboxTab.Grid.Header.icon:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Header, "BOTTOMLEFT", 22, 0)
				inboxTab.Grid.Header.icon:SetLayer(20)
				inboxTab.Grid.Header.icon:SetBackgroundColor(0,0,0,0)

			inboxTab.Grid.Header.name = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_name", inboxTab.Grid.Header)
				inboxTab.Grid.Header.name:SetPoint("TOPLEFT", inboxTab.Grid.Header.icon, "TOPRIGHT", 1, 0)
				inboxTab.Grid.Header.name:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Header.icon, "BOTTOMRIGHT", 79, 0)
				inboxTab.Grid.Header.name:SetLayer(20)
				inboxTab.Grid.Header.name:SetBackgroundColor(0,0,0,0)
				inboxTab.Grid.Header.name.Name = "name"

				inboxTab.Grid.Header.name.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Header_name_text", inboxTab.Grid.Header.name)
					inboxTab.Grid.Header.name.text:SetPoint("CENTER", inboxTab.Grid.Header.name, "CENTER")
					inboxTab.Grid.Header.name.text:SetFontSize(16)
					inboxTab.Grid.Header.name.text:SetFontColor(1,1,1,1)
					inboxTab.Grid.Header.name.text:SetLayer(25)
					inboxTab.Grid.Header.name.text:SetText("From")

			inboxTab.Grid.Header.subject = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_subject", inboxTab.Grid.Header)
				inboxTab.Grid.Header.subject:SetPoint("TOPLEFT", inboxTab.Grid.Header.name, "TOPRIGHT", 1, 0)
				inboxTab.Grid.Header.subject:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Header.name, "BOTTOMRIGHT", 209, 0)
				inboxTab.Grid.Header.subject:SetLayer(20)
				inboxTab.Grid.Header.subject:SetBackgroundColor(0,0,0,0)
				inboxTab.Grid.Header.subject.Name = "subject"

				inboxTab.Grid.Header.subject.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Header_subject_text", inboxTab.Grid.Header.subject)
					inboxTab.Grid.Header.subject.text:SetPoint("CENTER", inboxTab.Grid.Header.subject, "CENTER")
					inboxTab.Grid.Header.subject.text:SetFontSize(16)
					inboxTab.Grid.Header.subject.text:SetFontColor(1,1,1,1)
					inboxTab.Grid.Header.subject.text:SetLayer(25)
					inboxTab.Grid.Header.subject.text:SetText("Subject")

			inboxTab.Grid.Header.count = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_count", inboxTab.Grid.Header)
				inboxTab.Grid.Header.count:SetPoint("TOPLEFT", inboxTab.Grid.Header.subject, "TOPRIGHT", 1, 0)
				inboxTab.Grid.Header.count:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Header.subject, "BOTTOMRIGHT", 19, 0)
				inboxTab.Grid.Header.count:SetLayer(20)
				inboxTab.Grid.Header.count:SetBackgroundColor(0,0,0,0)

			inboxTab.Grid.Header.date = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Header_date", inboxTab.Grid.Header)
				inboxTab.Grid.Header.date:SetPoint("TOPLEFT", inboxTab.Grid.Header.count, "TOPRIGHT", 1, 0)
				inboxTab.Grid.Header.date:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Header.count, "BOTTOMRIGHT", 60, 0)
				inboxTab.Grid.Header.date:SetLayer(20)
				inboxTab.Grid.Header.date:SetBackgroundColor(0,0,0,0)
				inboxTab.Grid.Header.date.Name = "date"

				inboxTab.Grid.Header.date.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Header_date_text", inboxTab.Grid.Header.date)
					inboxTab.Grid.Header.date.text:SetPoint("CENTER", inboxTab.Grid.Header.date, "CENTER")
					inboxTab.Grid.Header.date.text:SetFontSize(16)
					inboxTab.Grid.Header.date.text:SetFontColor(1,1,1,1)
					inboxTab.Grid.Header.date.text:SetLayer(25)
					inboxTab.Grid.Header.date.text:SetText("Expiration")


		inboxTab.Grid.Header.Sorted = "name"
		inboxTab.Grid.Header.SortedDirection = "down"

		inboxTab.Grid.Body = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body", inboxTab.Grid)
			inboxTab.Grid.Body:SetPoint("TOPLEFT", inboxTab.Grid, "TOPLEFT")
			inboxTab.Grid.Body:SetPoint("BOTTOMRIGHT", inboxTab.Grid, "BOTTOMRIGHT")
			inboxTab.Grid.Body:SetLayer(20)
			inboxTab.Grid.Body:SetBackgroundColor(0,0,0,0)

			for i = 1, 20, 1 do

				inboxTab.Grid.Body[i] = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i, inboxTab.Grid.Body)
					if i == 1 then
						inboxTab.Grid.Body[i]:SetPoint("TOPLEFT", inboxTab.Grid.Body, "TOPLEFT", 1, 1)
					else
						inboxTab.Grid.Body[i]:SetPoint("TOPLEFT", inboxTab.Grid.Body[i-1], "BOTTOMLEFT", 0, 1)
					end
					inboxTab.Grid.Body[i]:SetWidth(388)
					inboxTab.Grid.Body[i]:SetHeight(19)
					inboxTab.Grid.Body[i]:SetLayer(25)
					inboxTab.Grid.Body[i]:SetBackgroundColor(0,0,0,0)
					inboxTab.Grid.Body[i].slot = i
					inboxTab.Grid.Body[i].displayed = i

					inboxTab.Grid.Body[i].icon = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_icon", inboxTab.Grid.Body[i])
						inboxTab.Grid.Body[i].icon:SetPoint("TOPLEFT", inboxTab.Grid.Body[i], "TOPLEFT")
						inboxTab.Grid.Body[i].icon:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i], "BOTTOMLEFT", 21, 0)
						inboxTab.Grid.Body[i].icon:SetLayer(30)
						inboxTab.Grid.Body[i].icon:SetBackgroundColor(0.133,0.133,0.133,1)

						inboxTab.Grid.Body[i].icon.texture = UI.CreateFrame("Texture", AddonId .. "_Inbox_Grid_Body_" .. i .. "_icon_texture", inboxTab.Grid.Body[i].icon)
							inboxTab.Grid.Body[i].icon.texture:SetPoint("CENTER", inboxTab.Grid.Body[i].icon, "CENTER")
							inboxTab.Grid.Body[i].icon.texture:SetWidth(17)
							inboxTab.Grid.Body[i].icon.texture:SetHeight(17)
							inboxTab.Grid.Body[i].icon.texture:SetLayer(35)

					inboxTab.Grid.Body[i].name = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_name", inboxTab.Grid.Body[i])
						inboxTab.Grid.Body[i].name:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].icon, "TOPRIGHT", 1, 0)
						inboxTab.Grid.Body[i].name:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].icon, "BOTTOMRIGHT", 79, 0)
						inboxTab.Grid.Body[i].name:SetLayer(30)
						inboxTab.Grid.Body[i].name:SetBackgroundColor(0.133,0.133,0.133,1)

						inboxTab.Grid.Body[i].name.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Body_" .. i .. "_name_text", inboxTab.Grid.Body[i].name)
							inboxTab.Grid.Body[i].name.text:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].name, "TOPLEFT", 2, -1)
							inboxTab.Grid.Body[i].name.text:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].name, "BOTTOMRIGHT")
							inboxTab.Grid.Body[i].name.text:SetFontSize(14)
							inboxTab.Grid.Body[i].name.text:SetFontColor(1,1,1,1)
							inboxTab.Grid.Body[i].name.text:SetLayer(35)

					inboxTab.Grid.Body[i].subject = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_subject", inboxTab.Grid.Body[i])
						inboxTab.Grid.Body[i].subject:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].name, "TOPRIGHT", 1, 0)
						inboxTab.Grid.Body[i].subject:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].name, "BOTTOMRIGHT", 209, 0)
						inboxTab.Grid.Body[i].subject:SetLayer(30)
						inboxTab.Grid.Body[i].subject:SetBackgroundColor(0.133,0.133,0.133,1)

						inboxTab.Grid.Body[i].subject.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Body_" .. i .. "_subject_text", inboxTab.Grid.Body[i].subject)
							inboxTab.Grid.Body[i].subject.text:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].subject, "TOPLEFT", 2, -1)
							inboxTab.Grid.Body[i].subject.text:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].subject, "BOTTOMRIGHT")
							inboxTab.Grid.Body[i].subject.text:SetFontSize(14)
							inboxTab.Grid.Body[i].subject.text:SetFontColor(1,1,1,1)
							inboxTab.Grid.Body[i].subject.text:SetLayer(35)

					inboxTab.Grid.Body[i].count = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_count", inboxTab.Grid.Body[i])
						inboxTab.Grid.Body[i].count:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].subject, "TOPRIGHT", 1, 0)
						inboxTab.Grid.Body[i].count:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].subject, "BOTTOMRIGHT", 19, 0)
						inboxTab.Grid.Body[i].count:SetLayer(30)
						inboxTab.Grid.Body[i].count:SetBackgroundColor(0.133,0.133,0.133,1)

						inboxTab.Grid.Body[i].count.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Body_" .. i .. "_count_text", inboxTab.Grid.Body[i].count)
							inboxTab.Grid.Body[i].count.text:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].count, "TOPLEFT", 2, -1)
							inboxTab.Grid.Body[i].count.text:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].count, "BOTTOMRIGHT")
							inboxTab.Grid.Body[i].count.text:SetFontSize(14)
							inboxTab.Grid.Body[i].count.text:SetFontColor(1,1,1,1)
							inboxTab.Grid.Body[i].count.text:SetLayer(35)

					inboxTab.Grid.Body[i].date = UI.CreateFrame("Frame", AddonId .. "_Inbox_Grid_Body_" .. i .. "_date", inboxTab.Grid.Body[i])
						inboxTab.Grid.Body[i].date:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].count, "TOPRIGHT", 1, 0)
						inboxTab.Grid.Body[i].date:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].count, "BOTTOMRIGHT", 60, 0)
						inboxTab.Grid.Body[i].date:SetLayer(30)
						inboxTab.Grid.Body[i].date:SetBackgroundColor(0.133,0.133,0.133,1)

						inboxTab.Grid.Body[i].date.text = UI.CreateFrame("Text", AddonId .. "_Inbox_Grid_Body_" .. i .. "_date_text", inboxTab.Grid.Body[i].date)
							inboxTab.Grid.Body[i].date.text:SetPoint("TOPLEFT", inboxTab.Grid.Body[i].date, "TOPLEFT", 2, 0)
							inboxTab.Grid.Body[i].date.text:SetPoint("BOTTOMRIGHT", inboxTab.Grid.Body[i].date, "BOTTOMRIGHT")
							inboxTab.Grid.Body[i].date.text:SetFontSize(12)
							inboxTab.Grid.Body[i].date.text:SetFontColor(1,1,1,1)
							inboxTab.Grid.Body[i].date.text:SetLayer(35)
							inboxTab.Grid.Body[i].date.text:SetText("12345")
							inboxTab.Grid.Body[i].date.text:SetText("")

				inboxTab.Grid.Body[i]:EventAttach(Event.UI.Input.Mouse.Right.Click, EM.Inbox.Event.RightClick,  AddonId .. "_Inbox_Grid_Body_" .. i .. "_RightClick")
				inboxTab.Grid.Body[i]:EventAttach(Event.UI.Input.Mouse.Left.Click, EM.Inbox.Event.LeftClick,  AddonId .. "_Inbox_Grid_Body_" .. i .. "_LeftClick")

			end

	EM.Inbox.Grid = inboxTab.Grid
	EM.Inbox.Grid.MarkedCount = 0

	inboxTab.ScrollBar = UI.CreateFrame("RiftScrollbar", "inboxScrollBar", inboxTab.Grid)
		inboxTab.ScrollBar:SetOrientation("vertical")
		inboxTab.ScrollBar:SetPoint("TOPLEFT", inboxTab.Grid, "TOPRIGHT", 9, 0)
		inboxTab.ScrollBar:SetHeight(401)
		inboxTab.ScrollBar:SetWidth(15)
		inboxTab.ScrollBar:SetRange(1, 1)
		inboxTab.ScrollBar:SetEnabled(true)
		inboxTab.ScrollBar:SetVisible(true)
		inboxTab.ScrollBar:EventAttach(Event.UI.Scrollbar.Change, EM.Inbox.Event.ScrollbarChange,  AddonId .. "_Inbox_ScrollBar_Change")

	inboxTab.btnDeleteAll = UI.CreateFrame("RiftButton", "inboxDeleteAll", inboxTab)
		inboxTab.btnDeleteAll:SetText("Delete All")
		inboxTab.btnDeleteAll:SetPoint("BOTTOMLEFT", inboxTab, "BOTTOMLEFT", 2, 2)
		inboxTab.btnDeleteAll:SetEnabled(false)
		inboxTab.btnDeleteAll:EventAttach(Event.UI.Button.Left.Press, EM.Inbox.Event.DeleteAll_LeftPress, "inboxDeleteAll_LeftPress")

	inboxTab.btnTakeAll = UI.CreateFrame("RiftButton", "inboxTakeAll", inboxTab)
		inboxTab.btnTakeAll:SetText("Take All")
		inboxTab.btnTakeAll:SetPoint("BOTTOMRIGHT", inboxTab, "BOTTOMRIGHT", -2, 2)
		inboxTab.btnTakeAll:SetEnabled(false)
		inboxTab.btnTakeAll:EventAttach(Event.UI.Button.Left.Press, EM.Inbox.Event.TakeAll_LeftPress, "inboxTakeAll_LeftPress")

	inboxTab.btnTakeSelected = UI.CreateFrame("RiftButton", "inboxTakeSelected", inboxTab)
		inboxTab.btnTakeSelected:SetText("Take Selected")
		inboxTab.btnTakeSelected:SetPoint("BOTTOMLEFT", inboxTab.btnTakeAll, "TOPLEFT", 0, 6)
		inboxTab.btnTakeSelected:SetEnabled(false)
		inboxTab.btnTakeSelected:EventAttach(Event.UI.Button.Left.Press, EM.Inbox.Event.TakeSelected_LeftPress, "inboxTakeSelected_LeftPress")

	inboxTab.btnDeleteSelected = UI.CreateFrame("RiftButton", "inboxDeleteSelected", inboxTab)
		inboxTab.btnDeleteSelected:SetText("Delete Selected")
		inboxTab.btnDeleteSelected:SetPoint("BOTTOMRIGHT", inboxTab.btnDeleteAll, "TOPRIGHT", 0, 6)
		inboxTab.btnDeleteSelected:SetEnabled(false)
		inboxTab.btnDeleteSelected:EventAttach(Event.UI.Button.Left.Press, EM.Inbox.Event.DeleteSelected_LeftPress, "inboxDeleteSelected_LeftPress")

	inboxTab.btnTakeCustom = UI.CreateFrame("RiftButton", "inboxTakeCustom", inboxTab)
		inboxTab.btnTakeCustom:SetText("Take Custom")
		inboxTab.btnTakeCustom:SetPoint("BOTTOMCENTER", inboxTab, "BOTTOMCENTER", 0, 2)
		inboxTab.btnTakeCustom:SetEnabled(false)
		inboxTab.btnTakeCustom:EventAttach(Event.UI.Button.Left.Press, EM.Inbox.Event.TakeCustom_LeftPress, "inboxTakeCustom_LeftPress")

	inboxTab.btnDeleteCustom = UI.CreateFrame("RiftButton", "inboxDeleteCustom", inboxTab)
		inboxTab.btnDeleteCustom:SetText("Delete Custom")
		inboxTab.btnDeleteCustom:SetPoint("BOTTOMLEFT", inboxTab.btnTakeCustom, "TOPLEFT", 0, 6)
		inboxTab.btnDeleteCustom:SetEnabled(false)
		inboxTab.btnDeleteCustom:EventAttach(Event.UI.Button.Left.Press, EM.Inbox.Event.DeleteCustom_LeftPress, "inboxDeleteCustom_LeftPress")

	EM.UI.Created = true
	EM.Created.Inbox = true
	EM.Event.Trigger.UICreated(Inspect.Mail.List())

end

function EM.Inbox.Event.DeleteAll_LeftPress(handle)
	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings
	if EM.Temp.PopupTest then
		for slot, data in ipairs(EM.Mail.List.Grid) do
			if type(data.attachmentCount)=="number" and data.attachmentCount > 0 then
				print("Message titled " .. data.subject .. " from " .. data.from .. " may contain Attachments!")
				print("Stopped Deletion Process!")
				EM.Temp.PopupTest = false
				return
			end
			if data.marked then
				data.marked = false
			end
			key = data.id
			EasyMailTable[EM.PlayerName].Inbox[key].deleted = os.date("%x %X", Inspect.Time.Server())
			Command.Mail.Delete(key)
		end
		EM.Inbox.Fill()
		EM.Temp.PopupTest = false
	elseif EM.Temp.PopupTest == nil then
		inboxTab.btnTakeSelected:SetEnabled(true)
		inboxTab.btnDeleteSelected:SetEnabled(true)
		EM.Temp.PopupTest = false
	else
		EM.Utility.Popup(2, "Really Delete all messages?", EM.Inbox.Event.DeleteAll_LeftPress)
		inboxTab.btnTakeSelected:SetEnabled(false)
		inboxTab.btnDeleteSelected:SetEnabled(false)
	end
end

function EM.Inbox.Event.DeleteSelected_LeftPress(handle)
	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings
	local tempDeleteList = {}
	if EM.Temp.PopupTest then
		for slot, data in ipairs(EM.Mail.List.Grid) do
			if data.marked then
				if type(data.attachmentCount)=="number" and data.attachmentCount > 0 then
					print("Message titled " .. data.subject .. " from " .. data.from .. " may contain Attachments!")
					print("Stopped Deletion Process!")
					EM.Temp.PopupTest = false
					return
				end
				data.marked = false
				key = data.id
				EasyMailTable[EM.PlayerName].Inbox[key].deleted = os.date("%x %X", Inspect.Time.Server())
				table.insert(tempDeleteList, key)
			end
		end
		for idx, key in ipairs(tempDeleteList) do
			Command.Mail.Delete(key)
		end
		EM.Inbox.Fill()
		EM.Temp.PopupTest = false
	elseif EM.Temp.PopupTest == nil then
		inboxTab.btnTakeSelected:SetEnabled(true)
		inboxTab.btnDeleteSelected:SetEnabled(true)
		EM.Temp.PopupTest = false
	else
		EM.Utility.Popup(2, "Really Delete the Selected messages?", EM.Inbox.Event.DeleteSelected_LeftPress)
		inboxTab.btnTakeSelected:SetEnabled(false)
		inboxTab.btnDeleteSelected:SetEnabled(false)
	end
end

function EM.Inbox.Event.DeleteCustom_LeftPress(handle)
	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings
	local tempDeleteList = {}
	if EM.Temp.PopupTest then
		for slot, data in ipairs(EM.Mail.List.Grid) do
			if string.find(data.subject, settingsTab.txfDeleteCustom:GetText()) then
				if type(data.attachmentCount)=="number" and data.attachmentCount > 0 then
					print("Message titled " .. data.subject .. " from " .. data.from .. " may contain Attachments!")
					print("Stopped Deletion Process!")
					EM.Temp.PopupTest = false
					return
				end
				if data.marked then
					data.marked = false
				end
				key = data.id
				EasyMailTable[EM.PlayerName].Inbox[key].deleted = os.date("%x %X", Inspect.Time.Server())
				table.insert(tempDeleteList, key)
			end
		end
		for idx, key in ipairs(tempDeleteList) do
			Command.Mail.Delete(key)
		end
		EM.Inbox.Fill()
		EM.Temp.PopupTest = false
	else
		if settingsTab.txfDeleteCustom:GetText() == nil then
			EM.Utility.Popup(1, "Custom Search text is Empty, unable to complete action!")
		else
			EM.Utility.Popup(2, "Really Delete all messages with '" .. settingsTab.txfDeleteCustom:GetText() .. "' in Subject?", EM.Inbox.Event.DeleteCustom_LeftPress)
		end
	end
	for slot, data in ipairs(EM.Mail.List.Grid) do
		if data.marked then
			inboxTab.btnTakeSelected:SetEnabled(true)
			inboxTab.btnDeleteSelected:SetEnabled(true)
			return
		else
			inboxTab.btnTakeSelected:SetEnabled(false)
			inboxTab.btnDeleteSelected:SetEnabled(false)
		end
	end
end

function EM.Inbox.Event.TakeAll_LeftPress(handle)
	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings
	local tempAll = {}
	for slot, data in ipairs(EM.Mail.List.Grid) do
		if data.marked then
			data.marked = false
		end
		table.insert(tempAll, data.id)
	end
	EM.Event.Trigger.OpenMessage(tempAll)
	inboxTab.btnTakeSelected:SetEnabled(false)
	inboxTab.btnDeleteSelected:SetEnabled(false)
end

function EM.Inbox.Event.TakeSelected_LeftPress(handle)
	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings
	local tempSelected = {}
	for slot, data in ipairs(EM.Mail.List.Grid) do
		if data.marked then
			data.marked = false
			table.insert(tempSelected, data.id)
		end
	end
	EM.Event.Trigger.OpenMessage(tempSelected)
	inboxTab.btnTakeSelected:SetEnabled(false)
	inboxTab.btnDeleteSelected:SetEnabled(false)
end

function EM.Inbox.Event.TakeCustom_LeftPress(handle)
	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings
	local tempCustom = {}
	for slot, data in ipairs(EM.Mail.List.Grid) do
		if (settingsTab.txfTakeCustom and settingsTab.txfTakeCustom:GetText() == nil) or
		(EM.Settings.Inbox and EM.Settings.Inbox.Custom.Take.value == nil) then
			EM.Utility.Popup(1, "Custom Search text is Empty, unable to complete action!")
		else
			if string.find(data.subject, EM.Settings.Inbox.Custom.Take.value) then
				if data.marked then
					data.marked = false
				end
				table.insert(tempCustom, data.id)
			end
		end
	end
	EM.Event.Trigger.OpenMessage(tempCustom)
	for slot, data in ipairs(EM.Mail.List.Grid) do
		if data.marked then
			inboxTab.btnTakeSelected:SetEnabled(true)
			inboxTab.btnDeleteSelected:SetEnabled(true)
			return
		else
			inboxTab.btnTakeSelected:SetEnabled(false)
			inboxTab.btnDeleteSelected:SetEnabled(false)
		end
	end
end

function EM.UI.Inbox.OpenMessage(hEvent, list)
	if Inspect.Interaction().mail == true then
		if list then
			EM.Temp.list = list
		end
		if EM.Temp.list then
			local key = EM.Temp.list[1] or nil
			if key then
				Command.Mail.Open(key, EM.UI.Callback.Open)
			else
				EM.Inbox.Fill()
			end
		end
	else
		EM.Utility.Popup(1, "You have moved to far from the Mail Box!!!")
		return
	end
end

function EM.UI.Callback.Open(status, message)
	if not status then
		for count,data in ipairs(EM.Mail.List.Original) do
			EM.Temp.Opened = EM.Messages[data.id]
			if not EM.Temp.Opened then return end
			if EM.Temp.Opened.cod then
				EasyMailTable[EM.PlayerName].Inbox[data.id].paidCoD = EM.Temp.Opened.cod
				Command.Mail.Pay(data.id, EM.UI.Callback.Pay)
			elseif EM.Temp.Opened.attachments ~= nil then
				EM.Event.Trigger.TakeAttachment()
			else
				table.remove(EM.Temp.list, 1)
				EM.Event.Trigger.OpenMessage()
			end
		end
	else
		if message then
			print("Message Open Command Failed with message: " .. message)
		else
			print("Message Open Command Failed with no message.")
		end
	end
end

function EM.UI.Callback.Pay(status, message)
	if not status then
		EM.Event.Trigger.OpenMessage()
	end
end

function EM.UI.Inbox.TakeAttachment(hEvent)
	if Inspect.Interaction().mail == true then
		local itemIDs = EM.Temp.Opened.attachments or nil
		if itemIDs and next(itemIDs) ~= nil then
			if EM.Utility.InventorySlotCheck() == true then
				Command.Mail.Take(EM.Temp.Opened.id, itemIDs, EM.UI.Callback.Take)
			else
				EM.Utility.Popup(1, "You have no free Inventory space!")
				return
			end
		else
			table.remove(EM.Temp.list, 1)
			EM.Event.Trigger.OpenMessage()
		end
	else
		EM.Utility.Popup(1, "You have moved to far from the Mail Box!!!")
		return
	end
end

function EM.UI.Callback.Take(status, message)
	if not status then
		EM.Temp.Opened = EM.Messages[EM.Temp.Opened.id]
		EM.Event.Trigger.TakeAttachment()
	end
end

function EM.Inbox.Populate(hEvent, id, status)

	EM.Mail.List.Grid = {}
	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings

	for id, details in pairs(EM.Messages) do
		table.insert(EM.Mail.List.Grid, EM.Messages[id])
	end

	if #EM.Mail.List.Grid == 0 then
		inboxTab.Grid:SetVisible(false)
		inboxTab.btnTakeAll:SetEnabled(false)
		inboxTab.btnDeleteAll:SetEnabled(false)
		inboxTab.btnTakeCustom:SetEnabled(false)
		inboxTab.btnDeleteCustom:SetEnabled(false)
		return
	end

	inboxTab.btnTakeAll:SetEnabled(true)
	inboxTab.btnDeleteAll:SetEnabled(true)

	if EM.Settings and EM.Settings.Inbox and EM.Settings.Inbox.Custom.Take.status then
		inboxTab.btnTakeCustom:SetEnabled(true)
	end
	if EM.Settings and EM.Settings.Inbox and EM.Settings.Inbox.Custom.Delete.status then
		inboxTab.btnDeleteCustom:SetEnabled(true)
	end

	table.sort(EM.Mail.List.Grid, function(m1, m2) return m1.expireUnix > m2.expireUnix end )

	if not inboxTab.Grid:GetVisible() then
		inboxTab.Grid:SetVisible(true)
	end

	EM.Inbox.Fill()

end

function EM.Inbox.Fill(start)

	local inboxTab = EM.UI.Inbox
	local settingsTab = EM.UI.Settings

	if not start or start == nil then
		start = EM.UI.Inbox.ScrollBar:GetPosition()-1
	end

	for i = 1,20,1 do
		local value = start+i
		if EM.Mail.List.Grid[value] then
			inboxTab.Grid.Body[i].icon.texture:SetVisible(true)
			if EM.Mail.List.Grid[value].spam then
				inboxTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/spam-mail.png")
				inboxTab.Grid.Body[i].subject.text:SetText(EM.Mail.List.Grid[value].subject)
				inboxTab.Grid.Body[i].name.text:SetFontColor(1,0,0,1)
				inboxTab.Grid.Body[i].subject.text:SetFontColor(1,0,0,1)
				inboxTab.Grid.Body[i].count.text:SetFontColor(1,0,0,1)
				inboxTab.Grid.Body[i].date.text:SetFontColor(1,0,0,1)
			elseif EM.Mail.List.Grid[value].gift and not EM.Mail.List.Grid[value].read then
				inboxTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/new-mail-gift.png.png")
				inboxTab.Grid.Body[i].subject.text:SetText(EM.Mail.List.Grid[value].subject)
				inboxTab.Grid.Body[i].name.text:SetFontColor(0.886,0.486,0.055,1)
				inboxTab.Grid.Body[i].subject.text:SetFontColor(0.886,0.486,0.055,1)
				inboxTab.Grid.Body[i].count.text:SetFontColor(0.886,0.486,0.055,1)
				inboxTab.Grid.Body[i].date.text:SetFontColor(0.886,0.486,0.055,1)
			elseif EM.Mail.List.Grid[value].gift then
				inboxTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/open-mail-gift.png")
				inboxTab.Grid.Body[i].subject.text:SetText(EM.Mail.List.Grid[value].subject)
				inboxTab.Grid.Body[i].name.text:SetFontColor(0.886,0.486,0.055,1)
				inboxTab.Grid.Body[i].subject.text:SetFontColor(0.886,0.486,0.055,1)
				inboxTab.Grid.Body[i].count.text:SetFontColor(0.886,0.486,0.055,1)
				inboxTab.Grid.Body[i].date.text:SetFontColor(0.886,0.486,0.055,1)
			elseif EM.Mail.List.Grid[value].cod and not EM.Mail.List.Grid[value].read then
				inboxTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/new-mail-cod.png")
				inboxTab.Grid.Body[i].subject.text:SetText("CoD | " .. EM.Mail.List.Grid[value].subject)
				inboxTab.Grid.Body[i].name.text:SetFontColor(0.286,0.4,1,1)
				inboxTab.Grid.Body[i].subject.text:SetFontColor(0.286,0.4,1,1)
				inboxTab.Grid.Body[i].count.text:SetFontColor(0.286,0.4,1,1)
				inboxTab.Grid.Body[i].date.text:SetFontColor(0.286,0.4,1,1)
			elseif EM.Mail.List.Grid[value].cod then
				inboxTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/open-mail-cod.png")
				inboxTab.Grid.Body[i].subject.text:SetText("CoD | " .. EM.Mail.List.Grid[value].subject)
				inboxTab.Grid.Body[i].name.text:SetFontColor(0.286,0.4,1,1)
				inboxTab.Grid.Body[i].subject.text:SetFontColor(0.286,0.4,1,1)
				inboxTab.Grid.Body[i].count.text:SetFontColor(0.286,0.4,1,1)
				inboxTab.Grid.Body[i].date.text:SetFontColor(0.286,0.4,1,1)
			elseif not EM.Mail.List.Grid[value].read then
				inboxTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/new-mail.png")
				inboxTab.Grid.Body[i].subject.text:SetText(EM.Mail.List.Grid[value].subject)
				inboxTab.Grid.Body[i].name.text:SetFontColor(0.118,0.776,0.149,1)
				inboxTab.Grid.Body[i].subject.text:SetFontColor(0.118,0.776,0.149,1)
				inboxTab.Grid.Body[i].count.text:SetFontColor(0.118,0.776,0.149,1)
				inboxTab.Grid.Body[i].date.text:SetFontColor(0.118,0.776,0.149,1)
			else
				inboxTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/open-mail.png")
				inboxTab.Grid.Body[i].subject.text:SetText(EM.Mail.List.Grid[value].subject)
				inboxTab.Grid.Body[i].name.text:SetFontColor(0.788,0.788,0.788,1)
				inboxTab.Grid.Body[i].subject.text:SetFontColor(0.788,0.788,0.788,1)
				inboxTab.Grid.Body[i].count.text:SetFontColor(0.788,0.788,0.788,1)
				inboxTab.Grid.Body[i].date.text:SetFontColor(0.788,0.788,0.788,1)
			end
			inboxTab.Grid.Body[i].name.text:SetText(EM.Mail.List.Grid[value].from)
			inboxTab.Grid.Body[i].date.text:SetText(EM.Mail.List.Grid[value].expire)
			if EM.Mail.List.Grid[value].attachmentCount then
				inboxTab.Grid.Body[i].count.text:SetText(tostring(EM.Mail.List.Grid[value].attachmentCount))
			else
				inboxTab.Grid.Body[i].count.text:SetText("")
			end
			inboxTab.Grid.Body[i].displayed = value
			if EM.Mail.List.Grid[value].marked then
				EM.Inbox.MarkRow(i, value, true)
			else
				EM.Inbox.MarkRow(i, value, false)
			end
		else
			inboxTab.Grid.Body[i].icon.texture:SetVisible(false)
			inboxTab.Grid.Body[i].subject.text:SetText("")
			inboxTab.Grid.Body[i].name.text:SetText("")
			inboxTab.Grid.Body[i].count.text:SetText("")
			inboxTab.Grid.Body[i].date.text:SetText("")
			inboxTab.Grid.Body[i].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[i].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[i].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[i].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[i].date:SetBackgroundColor(0.133,0.133,0.133,1)
		end
	end
	if (#EM.Mail.List.Grid) > 20 then
		EM.UI.Inbox.ScrollBar:SetRange(1, (#EM.Mail.List.Grid-19))
		EM.UI.Inbox.ScrollBar:SetVisible(true)
	else
		EM.UI.Inbox.ScrollBar:SetVisible(false)
	end
	if EM.UI.Inbox.ScrollBar:GetPosition() ~= (start + 1) then
		local min, max = EM.UI.Inbox.ScrollBar:GetRange()
		if max == 2 then
			EM.UI.Inbox.ScrollBar:SetPosition(min)
		elseif (start + 1) > max then
			EM.UI.Inbox.ScrollBar:SetPosition(max)
		else
			EM.UI.Inbox.ScrollBar:SetPosition(start + 1)
		end
	end
end

function EM.Inbox.MarkRow(row, displayed, status)

	local inboxTab = EM.UI.Inbox

	if status then
		EM.Mail.List.Grid[displayed].marked = true
		if EM.Mail.List.Grid[displayed].spam then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(1,1,1,1)
		elseif EM.Mail.List.Grid[displayed].gift and not EM.Mail.List.Grid[displayed].read then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(1,1,1,1)
		elseif EM.Mail.List.Grid[displayed].gift then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(1,1,1,1)
		elseif EM.Mail.List.Grid[displayed].cod and not EM.Mail.List.Grid[displayed].read then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(1,1,1,1)
		elseif EM.Mail.List.Grid[displayed].cod then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(1,1,1,1)
		elseif not EM.Mail.List.Grid[displayed].read then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(1,1,1,1)
		else
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(1,1,1,1)
			inboxTab.Grid.Body[row].name.text:SetFontColor(0,0,0,1)
			inboxTab.Grid.Body[row].subject.text:SetFontColor(0,0,0,1)
			inboxTab.Grid.Body[row].count.text:SetFontColor(0,0,0,1)
			inboxTab.Grid.Body[row].date.text:SetFontColor(0,0,0,1)
		end
	else
		EM.Mail.List.Grid[displayed].marked = false
		if EM.Mail.List.Grid[displayed].spam then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(0.133,0.133,0.133,1)
		elseif EM.Mail.List.Grid[displayed].gift and not EM.Mail.List.Grid[displayed].read then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(0.133,0.133,0.133,1)
		elseif EM.Mail.List.Grid[displayed].gift then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(0.133,0.133,0.133,1)
		elseif EM.Mail.List.Grid[displayed].cod and not EM.Mail.List.Grid[displayed].read then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(0.133,0.133,0.133,1)
		elseif EM.Mail.List.Grid[displayed].cod then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(0.133,0.133,0.133,1)
		elseif not EM.Mail.List.Grid[displayed].read then
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(0.133,0.133,0.133,1)
		else
			inboxTab.Grid.Body[row].icon:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].subject:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].count:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].date:SetBackgroundColor(0.133,0.133,0.133,1)
			inboxTab.Grid.Body[row].name.text:SetFontColor(0.788,0.788,0.788,1)
			inboxTab.Grid.Body[row].subject.text:SetFontColor(0.788,0.788,0.788,1)
			inboxTab.Grid.Body[row].count.text:SetFontColor(0.788,0.788,0.788,1)
			inboxTab.Grid.Body[row].date.text:SetFontColor(0.788,0.788,0.788,1)
		end
	end


end

function EM.Inbox.Event.RightClick(handle)

	local inboxTab = EM.UI.Inbox
	local displayed = handle.displayed
	local slot = handle.slot


	if not EM.Mail.List.Grid[displayed] then return end
	if EM.Mail.List.Grid[displayed].marked then
		EM.Inbox.MarkRow(slot, displayed, false)
		EM.Inbox.Grid.MarkedCount = EM.Inbox.Grid.MarkedCount - 1
		if EM.Inbox.Grid.MarkedCount == 0 then
			inboxTab.btnTakeSelected:SetEnabled(false)
			inboxTab.btnDeleteSelected:SetEnabled(false)
		end
	else
		EM.Inbox.MarkRow(slot, displayed, true)
		inboxTab.btnTakeSelected:SetEnabled(true)
		inboxTab.btnDeleteSelected:SetEnabled(true)
		EM.Inbox.Grid.MarkedCount = EM.Inbox.Grid.MarkedCount + 1
	end
end

function EM.Inbox.Event.LeftClick(handle)
	local displayed = handle.displayed
	if not EM.Mail.List.Grid[displayed] then
		return
	else
		Command.Mail.Open(EM.Mail.List.Grid[displayed].id)
	end
end

function EM.Inbox.Event.WheelBack(handle)
	if (#EM.Mail.List.Grid) > 20 then
		local oldPosition = EM.UI.Inbox.ScrollBar:GetPosition()
		local minRange, maxRange = EM.UI.Inbox.ScrollBar:GetRange()
		local newPosition = mathFloor(oldPosition) + 3
		if newPosition > maxRange then
			EM.UI.Inbox.ScrollBar:SetPosition(maxRange)
		else
			EM.UI.Inbox.ScrollBar:SetPosition(newPosition)
		end
	end
end

function EM.Inbox.Event.WheelForward(handle)
	if (#EM.Mail.List.Grid) > 20 then
		local oldPosition = EM.UI.Inbox.ScrollBar:GetPosition()
		local minRange, maxRange = EM.UI.Inbox.ScrollBar:GetRange()
		local newPosition = mathFloor(oldPosition) - 3
		if newPosition < minRange then
			EM.UI.Inbox.ScrollBar:SetPosition(minRange)
		else
			EM.UI.Inbox.ScrollBar:SetPosition(newPosition)
		end
	end
end

function EM.Inbox.Event.ScrollbarChange(handle)
	local position = EM.UI.Inbox.ScrollBar:GetPosition()
	local minRange, maxRange = EM.UI.Inbox.ScrollBar:GetRange()
	local newPosition = mathFloor(position) - 1
	EM.Inbox.Fill(newPosition)
end

Command.Event.Attach(EM.Event.RefreshMail,			EM.Inbox.Populate, AddonId .. "_RefreshMail" )
Command.Event.Attach(EM.Event.TakeAttachment, 		EM.UI.Inbox.TakeAttachment, AddonId .. "_TakeAttachment" )
Command.Event.Attach(EM.Event.OpenMessage, 			EM.UI.Inbox.OpenMessage, AddonId .. "_OpenMessage" )