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

EM.UI.Archive = {}
EM.Archive = {}
EM.Archive.Event = {}

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

function  EM.UI.Setup.Archive()

	local archiveTab = EM.UI.Archive

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

		archiveTab.Grid:EventAttach(Event.UI.Input.Mouse.Wheel.Back, EM.Archive.Event.WheelBack, AddonId .. "_Archive_Grid_WheelBack")
		archiveTab.Grid:EventAttach(Event.UI.Input.Mouse.Wheel.Forward, EM.Archive.Event.WheelForward, AddonId .. "_Archive_Grid_WheelForward")

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

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

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

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

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

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

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

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

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


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

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

			for i = 1, 20, 1 do

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

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

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

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

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

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

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

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

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

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

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

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

			end

	archiveTab.ScrollBar = UI.CreateFrame("RiftScrollbar", "archiveScrollBar", archiveTab.Grid)
		archiveTab.ScrollBar:SetOrientation("vertical")
		archiveTab.ScrollBar:SetPoint("TOPLEFT", archiveTab.Grid, "TOPRIGHT", 8, -4)
		archiveTab.ScrollBar:SetHeight(409)
		archiveTab.ScrollBar:SetWidth(15)
		archiveTab.ScrollBar:SetRange(1, 1)
		archiveTab.ScrollBar:SetThickness(150)
		archiveTab.ScrollBar:SetEnabled(true)
		archiveTab.ScrollBar:SetVisible(true)
		archiveTab.ScrollBar:EventAttach(Event.UI.Scrollbar.Change, EM.Archive.Event.ScrollbarChange,  AddonId .. "_Archive_ScrollBar_Change")

	archiveTab.FilterOptions = UI.CreateFrame("Text", "FilterOptions", archiveTab)
		archiveTab.FilterOptions:SetPoint("TOPLEFT", archiveTab.Grid, "BOTTOMLEFT", 0, 6)
		archiveTab.FilterOptions:SetText("Options:")
		archiveTab.FilterOptions:SetFontSize(16)
		archiveTab.FilterOptions:SetFontColor(1,1,1,1)

	archiveTab.radioSelected1 = "Search"

	archiveTab.SearchRadio = UI.CreateFrame("Frame", "SearchRadio", archiveTab)
		archiveTab.SearchRadio:SetPoint("TOPLEFT", archiveTab.FilterOptions, "TOPRIGHT", 10, 3)
		archiveTab.SearchRadio:SetHeight(19)
		archiveTab.SearchRadio:SetWidth(60)
		archiveTab.SearchRadio.Name = "Search"
		archiveTab.SearchRadio:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if archiveTab.radioSelected1 == "Filter" then
				archiveTab.SearchRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				archiveTab.FilterRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.radioSelected1 = "Search"
			end
		end, "LeftClick")

		archiveTab.SearchRadio.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", archiveTab.SearchRadio)
			archiveTab.SearchRadio.Texture:SetPoint("CENTERLEFT", archiveTab.SearchRadio, "CENTERLEFT")
			archiveTab.SearchRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")

		archiveTab.SearchRadio.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", archiveTab.SearchRadio)
			archiveTab.SearchRadio.Text:SetPoint("CENTERLEFT", archiveTab.SearchRadio.Texture, "CENTERRIGHT")
			archiveTab.SearchRadio.Text:SetText("Search")

	archiveTab.FilterRadio = UI.CreateFrame("Frame", "FilterRadio", archiveTab)
		archiveTab.FilterRadio:SetPoint("TOPLEFT", archiveTab.SearchRadio, "TOPRIGHT", 5, 0)
		archiveTab.FilterRadio:SetHeight(19)
		archiveTab.FilterRadio:SetWidth(84)
		archiveTab.FilterRadio.Name = "Filter"
		archiveTab.FilterRadio:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if archiveTab.radioSelected1 == "Search" then
				archiveTab.FilterRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				archiveTab.SearchRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.radioSelected1 = "Filter"
			end
		end, "LeftClick")

		archiveTab.FilterRadio.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", archiveTab.FilterRadio)
			archiveTab.FilterRadio.Texture:SetPoint("CENTERLEFT", archiveTab.FilterRadio, "CENTERLEFT")
			archiveTab.FilterRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")

		archiveTab.FilterRadio.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", archiveTab.FilterRadio)
			archiveTab.FilterRadio.Text:SetPoint("CENTERLEFT", archiveTab.FilterRadio.Texture, "CENTERRIGHT")
			archiveTab.FilterRadio.Text:SetText("Filter")

	archiveTab.radioSelected2 = "Received"

	archiveTab.ReceivedMailRadio = UI.CreateFrame("Frame", "ReceivedMailRadio", archiveTab)
		archiveTab.ReceivedMailRadio:SetPoint("TOPLEFT", archiveTab.FilterRadio, "TOPRIGHT", 4, 0)
		archiveTab.ReceivedMailRadio:SetHeight(19)
		archiveTab.ReceivedMailRadio:SetWidth(91)
		archiveTab.ReceivedMailRadio.Name = "Received"
		archiveTab.ReceivedMailRadio:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if archiveTab.radioSelected2 == "Sent" then
				archiveTab.ReceivedMailRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				archiveTab.SentMailRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.radioSelected2 = "Received"
				EM.NewDataTable.Used = "Inbox"
				EM.UI:SearchArchive("", true)
				archiveTab.SearchField:SetText("")
				EM.Archive.Populate(0)
			end
		end, "LeftClick")

		archiveTab.ReceivedMailRadio.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", archiveTab.ReceivedMailRadio)
			archiveTab.ReceivedMailRadio.Texture:SetPoint("CENTERLEFT", archiveTab.ReceivedMailRadio, "CENTERLEFT")
			archiveTab.ReceivedMailRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")

		archiveTab.ReceivedMailRadio.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", archiveTab.ReceivedMailRadio)
			archiveTab.ReceivedMailRadio.Text:SetPoint("CENTERLEFT", archiveTab.ReceivedMailRadio.Texture, "CENTERRIGHT")
			archiveTab.ReceivedMailRadio.Text:SetText("Received Mail")

	archiveTab.SentMailRadio = UI.CreateFrame("Frame", "SentMailRadio", archiveTab)
		archiveTab.SentMailRadio:SetPoint("TOPLEFT", archiveTab.ReceivedMailRadio, "TOPRIGHT", 15, 0)
		archiveTab.SentMailRadio:SetHeight(19)
		archiveTab.SentMailRadio:SetWidth(68)
		archiveTab.SentMailRadio.Name = "Sent"
		archiveTab.SentMailRadio:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if archiveTab.radioSelected2 == "Received" then
				archiveTab.SentMailRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				archiveTab.ReceivedMailRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.radioSelected2 = "Sent"
				EM.NewDataTable.Used = "Sent"
				EM.UI:SearchArchive("", true)
				archiveTab.SearchField:SetText("")
				EM.Archive.Populate(0)
			end
		end, "LeftClick")

		archiveTab.SentMailRadio.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", archiveTab.SentMailRadio)
			archiveTab.SentMailRadio.Texture:SetPoint("CENTERLEFT", archiveTab.SentMailRadio, "CENTERLEFT")
			archiveTab.SentMailRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")

		archiveTab.SentMailRadio.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", archiveTab.SentMailRadio)
			archiveTab.SentMailRadio.Text:SetPoint("CENTERLEFT", archiveTab.SentMailRadio.Texture, "CENTERRIGHT")
			archiveTab.SentMailRadio.Text:SetText("Sent Mail")

	archiveTab.SearchText = UI.CreateFrame("Text", "SearchText", archiveTab)
		archiveTab.SearchText:SetPoint("TOPLEFT", archiveTab.FilterOptions, "BOTTOMLEFT", 5, 5)
		archiveTab.SearchText:SetLayer(5)
		archiveTab.SearchText:SetText("Search:")
		archiveTab.SearchText:SetFontSize(13)
		archiveTab.SearchText:SetFontColor(1,1,1,1)
		archiveTab.SearchText:SetWidth(45)

	archiveTab.SearchField = UI.CreateFrame("RiftTextfield", "SearchField", archiveTab)
		archiveTab.SearchField:SetPoint("TOPLEFT", archiveTab.SearchText, "TOPRIGHT", 1, 1)
		archiveTab.SearchField:SetHeight(18)
		archiveTab.SearchField:SetWidth(148)
		archiveTab.SearchField:SetLayer(5)
		archiveTab.SearchField:SetBackgroundColor(0,0,0,1)
		archiveTab.SearchField:EventAttach(Event.UI.Input.Key.Up,
			function(handle, hEvent, key)
				if key == "Return" then
					EM.NewDataTable.Used = "Filter"
					EM.UI:SearchArchive(archiveTab.SearchField:GetText(), false)
					archiveTab.SearchField:SetKeyFocus(false)
				end
			end, "SearchField_KeyUp")

	archiveTab.SearchFrame = UI.CreateFrame("Frame", "SearchFrame", archiveTab)
		archiveTab.SearchFrame:SetPoint("CENTER", archiveTab.SearchField, "CENTER")
		archiveTab.SearchFrame:SetHeight(20)
		archiveTab.SearchFrame:SetWidth(150)
		archiveTab.SearchFrame:SetLayer(4)
		archiveTab.SearchFrame:SetBackgroundColor(0.4,0.4,0.4,1)

	archiveTab.SearchCancel = UI.CreateFrame("Texture", "SearchCancel", archiveTab)
		archiveTab.SearchCancel:SetPoint("CENTERLEFT", archiveTab.SearchFrame, "CENTERRIGHT", 5, 0)
		archiveTab.SearchCancel:SetTexture(AddonId, "gfx/iconCancel.png")
		archiveTab.SearchCancel:SetWidth(15)
		archiveTab.SearchCancel:SetHeight(15)
		archiveTab.SearchCancel:EventAttach(Event.UI.Input.Mouse.Left.Click,
			function()
				EM.UI:SearchArchive("", true)
			end, "SearchCancel_LeftClick")

	archiveTab.radioSelected3 = "name"

	archiveTab.NameRadio = UI.CreateFrame("Frame", "NameRadio", archiveTab)
		archiveTab.NameRadio:SetPoint("TOPLEFT", archiveTab.SearchCancel, "TOPRIGHT", 10, 0)
		archiveTab.NameRadio:SetHeight(19)
		archiveTab.NameRadio:SetWidth(49)
		archiveTab.NameRadio.Name = "Name"
		archiveTab.NameRadio:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if archiveTab.radioSelected3 ~= "name" then
				archiveTab.NameRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				archiveTab.SubjectRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.DateRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.radioSelected3 = "name"
				if archiveTab.SearchField:GetText() ~= "" then
					EM.UI:SearchArchive(archiveTab.SearchField:GetText(), false)
				end
			end
		end, "LeftClick")

		archiveTab.NameRadio.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", archiveTab.NameRadio)
			archiveTab.NameRadio.Texture:SetPoint("CENTERLEFT", archiveTab.NameRadio, "CENTERLEFT")
			archiveTab.NameRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")

		archiveTab.NameRadio.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", archiveTab.NameRadio)
			archiveTab.NameRadio.Text:SetPoint("CENTERLEFT", archiveTab.NameRadio.Texture, "CENTERRIGHT")
			archiveTab.NameRadio.Text:SetText("Name")

	archiveTab.SubjectRadio = UI.CreateFrame("Frame", "SubjectRadio", archiveTab)
		archiveTab.SubjectRadio:SetPoint("TOPLEFT", archiveTab.NameRadio, "TOPRIGHT", 7, 0)
		archiveTab.SubjectRadio:SetHeight(19)
		archiveTab.SubjectRadio:SetWidth(56)
		archiveTab.SubjectRadio.Name = "Subject"
		archiveTab.SubjectRadio:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if archiveTab.radioSelected3 ~= "subject" then
				archiveTab.SubjectRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				archiveTab.NameRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.DateRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.radioSelected3 = "subject"
				if archiveTab.SearchField:GetText() ~= "" then
					EM.UI:SearchArchive(archiveTab.SearchField:GetText(), false)
				end
			end
		end, "LeftClick")

		archiveTab.SubjectRadio.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", archiveTab.SubjectRadio)
			archiveTab.SubjectRadio.Texture:SetPoint("CENTERLEFT", archiveTab.SubjectRadio, "CENTERLEFT")
			archiveTab.SubjectRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")

		archiveTab.SubjectRadio.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", archiveTab.SubjectRadio)
			archiveTab.SubjectRadio.Text:SetPoint("CENTERLEFT", archiveTab.SubjectRadio.Texture, "CENTERRIGHT")
			archiveTab.SubjectRadio.Text:SetText("Subject")

	archiveTab.DateRadio = UI.CreateFrame("Frame", "DateRadio", archiveTab)
		archiveTab.DateRadio:SetPoint("TOPLEFT", archiveTab.SubjectRadio, "TOPRIGHT", 7, 0)
		archiveTab.DateRadio:SetHeight(19)
		archiveTab.DateRadio:SetWidth(42)
		archiveTab.DateRadio.Name = "Date"
		archiveTab.DateRadio:EventAttach(Event.UI.Input.Mouse.Left.Click, function()
			if archiveTab.radioSelected3 ~= "date" then
				archiveTab.DateRadio.Texture:SetTexture(AddonId, "gfx/radiobutton_selected.png")
				archiveTab.NameRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.SubjectRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")
				archiveTab.radioSelected3 = "date"
				if archiveTab.SearchField:GetText() ~= "" then
					EM.UI:SearchArchive(archiveTab.SearchField:GetText(), false)
				end
			end
		end, "LeftClick")

		archiveTab.DateRadio.Texture = UI.CreateFrame("Texture", "SendMoneyRadio2_Texture", archiveTab.DateRadio)
			archiveTab.DateRadio.Texture:SetPoint("CENTERLEFT", archiveTab.DateRadio, "CENTERLEFT")
			archiveTab.DateRadio.Texture:SetTexture(AddonId, "gfx/radiobutton.png")

		archiveTab.DateRadio.Text = UI.CreateFrame("Text", "SendMoneyRadio2_Text", archiveTab.DateRadio)
			archiveTab.DateRadio.Text:SetPoint("CENTERLEFT", archiveTab.DateRadio.Texture, "CENTERRIGHT")
			archiveTab.DateRadio.Text:SetText("Date")

	EM.Archive.Populate(0)

end

function EM.UI:SearchArchive(searchValue, reset)

	local archiveTab = EM.UI.Archive
	local searchList = {}
	local searchValue = stringLower(searchValue)
	local col = archiveTab.radioSelected3

	EM.NewDataTable.Filter = {}

	if reset then
		archiveTab.SearchField:SetText("")
		if archiveTab.radioSelected2 == "Received" then
			EM.NewDataTable.Used = "Inbox"
		elseif archiveTab.radioSelected2 == "Sent" then
			EM.NewDataTable.Used = "Sent"
		end
	else
		if archiveTab.radioSelected2 == "Received" then
			searchList = EM.NewDataTable.Inbox
			if col == "name" then
				col = "from"
			elseif col == "date" then
				col = "deleted"
			end
		elseif archiveTab.radioSelected2 == "Sent" then
			searchList = EM.NewDataTable.Sent
			if col == "name" then
				col = "to"
			elseif col == "date" then
				col = "timesent"
			end
		end
		if searchValue ~= "" then
			for position, data in ipairs(searchList) do
				if data[col] then
					if archiveTab.radioSelected1 == "Search" then
						if stringFind(stringLower(data[col]), searchValue) ~= nil then
							tableInsert(EM.NewDataTable.Filter, data)
						end
					elseif archiveTab.radioSelected1 == "Filter" then
						if stringFind(stringLower(data[col]), searchValue) == nil then
							tableInsert(EM.NewDataTable.Filter, data)
						end
					end
				end
			end
		end
	end

	EM.Archive.Populate(0)
end

function EM.Archive.Populate(start)
	local archiveTab = EM.UI.Archive
	local listToUse = EM.NewDataTable[EM.NewDataTable.Used]

	for i = 1,20,1 do
		local value = start+i
		if listToUse[value] then
			if archiveTab.radioSelected2 == "Received" then
				archiveTab.Grid.Header.name.text:SetText("From")
				archiveTab.Grid.Header.date.text:SetText("Deleted")
				archiveTab.Grid.Body[i].icon.texture:SetVisible(true)
				if listToUse[value].spam then
					archiveTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/spam-mail.png")
					archiveTab.Grid.Body[i].subject.text:SetText(listToUse[value].subject)
					archiveTab.Grid.Body[i].name.text:SetFontColor(1,0,0,1)
					archiveTab.Grid.Body[i].subject.text:SetFontColor(1,0,0,1)
					archiveTab.Grid.Body[i].count.text:SetFontColor(1,0,0,1)
					archiveTab.Grid.Body[i].date.text:SetFontColor(1,0,0,1)
				elseif listToUse[value].cod and listToUse[value].read then
					archiveTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/open-mail-cod.png")
					archiveTab.Grid.Body[i].subject.text:SetText("CoD | " .. listToUse[value].subject)
					archiveTab.Grid.Body[i].name.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].subject.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].count.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].date.text:SetFontColor(0.286,0.4,1,1)
				elseif listToUse[value].cod and not listToUse[value].read then
					archiveTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/new-mail-cod.png")
					archiveTab.Grid.Body[i].subject.text:SetText("CoD | " .. listToUse[value].subject)
					archiveTab.Grid.Body[i].name.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].subject.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].count.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].date.text:SetFontColor(0.286,0.4,1,1)
				elseif not listToUse[value].read then
					archiveTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/new-mail.png")
					archiveTab.Grid.Body[i].subject.text:SetText(listToUse[value].subject)
					archiveTab.Grid.Body[i].name.text:SetFontColor(0.118,0.776,0.149,1)
					archiveTab.Grid.Body[i].subject.text:SetFontColor(0.118,0.776,0.149,1)
					archiveTab.Grid.Body[i].count.text:SetFontColor(0.118,0.776,0.149,1)
					archiveTab.Grid.Body[i].date.text:SetFontColor(0.118,0.776,0.149,1)
				else
					archiveTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/open-mail.png")
					archiveTab.Grid.Body[i].subject.text:SetText(listToUse[value].subject)
					archiveTab.Grid.Body[i].name.text:SetFontColor(0.788,0.788,0.788,1)
					archiveTab.Grid.Body[i].subject.text:SetFontColor(0.788,0.788,0.788,1)
					archiveTab.Grid.Body[i].count.text:SetFontColor(0.788,0.788,0.788,1)
					archiveTab.Grid.Body[i].date.text:SetFontColor(0.788,0.788,0.788,1)
				end
				archiveTab.Grid.Body[i].name.text:SetText(listToUse[value].from)
				archiveTab.Grid.Body[i].date.text:SetText(listToUse[value].deleted)
				if listToUse[value].attachmentCount then
					archiveTab.Grid.Body[i].count.text:SetText(tostring(listToUse[value].attachmentCount))
				else
					archiveTab.Grid.Body[i].count.text:SetText("")
				end
				archiveTab.Grid.Body[i].displayed = value
			elseif archiveTab.radioSelected2 == "Sent" then
				archiveTab.Grid.Header.name.text:SetText("To")
				archiveTab.Grid.Header.date.text:SetText("Sent")
				archiveTab.Grid.Body[i].icon.texture:SetVisible(true)
				if listToUse[value].cod then
					archiveTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/new-mail-cod.png")
					archiveTab.Grid.Body[i].subject.text:SetText("CoD | " .. listToUse[value].subject)
					archiveTab.Grid.Body[i].name.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].subject.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].count.text:SetFontColor(0.286,0.4,1,1)
					archiveTab.Grid.Body[i].date.text:SetFontColor(0.286,0.4,1,1)
				else
					archiveTab.Grid.Body[i].icon.texture:SetTexture(AddonId, "gfx/new-mail.png")
					archiveTab.Grid.Body[i].subject.text:SetText(listToUse[value].subject)
					archiveTab.Grid.Body[i].name.text:SetFontColor(0.788,0.788,0.788,1)
					archiveTab.Grid.Body[i].subject.text:SetFontColor(0.788,0.788,0.788,1)
					archiveTab.Grid.Body[i].count.text:SetFontColor(0.788,0.788,0.788,1)
					archiveTab.Grid.Body[i].date.text:SetFontColor(0.788,0.788,0.788,1)
				end
				archiveTab.Grid.Body[i].name.text:SetText(listToUse[value].to)
				archiveTab.Grid.Body[i].date.text:SetText(listToUse[value].timesent)
				if listToUse[value].attachmentCount then
					archiveTab.Grid.Body[i].count.text:SetText(tostring(listToUse[value].attachmentCount))
				else
					archiveTab.Grid.Body[i].count.text:SetText("")
				end
				archiveTab.Grid.Body[i].displayed = value
			end
		else
			archiveTab.Grid.Body[i].icon.texture:SetVisible(false)
			archiveTab.Grid.Body[i].subject.text:SetText("")
			archiveTab.Grid.Body[i].name.text:SetText("")
			archiveTab.Grid.Body[i].count.text:SetText("")
			archiveTab.Grid.Body[i].date.text:SetText("")
		end
	end
	if (#listToUse) > 20 then
		archiveTab.ScrollBar:SetRange(1, (#listToUse-19))
		archiveTab.ScrollBar:SetVisible(true)
	else
		archiveTab.ScrollBar:SetVisible(false)
	end
	if EM.UI.Archive.ScrollBar:GetPosition() ~= (start + 1) then
		EM.UI.Archive.ScrollBar:SetPosition(start + 1)
	end

	EM.Created.Archive = true

end

function EM.Archive.Event.RightClick(handle)

end

function EM.Archive.Event.LeftClick(handle)

end

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

function EM.Archive.Event.WheelForward(handle)
	local oldPosition = EM.UI.Archive.ScrollBar:GetPosition()
	local minRange, maxRange = EM.UI.Archive.ScrollBar:GetRange()
	local newPosition = mathFloor(oldPosition) - 3
	if newPosition < minRange then
		EM.UI.Archive.ScrollBar:SetPosition(minRange)
	else
		EM.UI.Archive.ScrollBar:SetPosition(newPosition)
	end
end

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