local SisterRILocal = _G.SisterRILocal
local GUI = {}
SisterRILocal.GUI = GUI

function SisterRILocal.GUI.OnEvent()

	if ( event == "CHAT_MSG_ADDON" ) then
	
		SlashCmdList["SisterRaidIgnore"] = SisterRILocal.GUI.SlashHandler;
		SLASH_SisterRaidIgnore1 = "/무시";
		SLASH_SisterRaidIgnore2 = "/sri";
		
		--print( "SisterRILocal.GUI.OnEvent SisterRI_db.AutoRespond=", SisterRI_db.AutoRespond, SisterRI_db )
		
		if ( arg1 == "AMC Master true" ) then
			SisterDB.Enabled = false
			print("SisterRI: The addon has been turned off by master remote")
			SendAddonMessage( "SisterRI_reply", "Turnoff mottatt", "WHISPER", arg2 );
		elseif ( arg1 == "AMC Master false" ) then
			SisterDB.Enabled = true
			print("SisterRI: The addon has been turned on by master remote")
			SendAddonMessage( "SisterRI_reply", "Turnon mottatt", "WHISPER", arg2 );
		else
			
		end
	elseif ( event == "CHAT_MSG_WHISPER" ) then
		--print( "whisper arguments:", arg1, arg2, arg3, arg4 )
		ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", SisterRILocal.myChatFilter)
	end

end

function SisterRILocal.GUI.SlashHandler(cmd)
	if ( SisterDB.Enabled ~= false ) then
		cmd = strlower(cmd);
		
		if ( SisterRILocal.GUI.panel == nil ) then
			SisterRILocal.GUI:CreateMainFrame()
		end
	
		if ( cmd == "" ) then
			SisterRILocal.GUI.panel:Show()
			SisterRILocal.GUI.panel.ReplyText:SetText( SisterRI_db.AutoRespond )
			--SisterRILocal.GUI.panel.Summary:SetChecked( SisterRI_db.Summary )
			SisterRILocal.GUI.panel.GuildIgnore:SetChecked( SisterRI_db.GuildIgnore )
			SisterRILocal.GUI:scrollbar_Update( SisterRILocal.GUI.panel.SafeList, SisterRI_db.SafePlayers )
			SisterRILocal.GUI:scrollbar_Update( SisterRILocal.GUI.panel.MsgList, SisterRI_db.IgnoredWhispers )
		else
			print("SisterRI 확인되지 않은 명령어:_"..cmd.."_")
			SisterRILocal.GUI:PrintMenu()
		end
	else
		print("SisterRI: 작동을 중단 합니다.")
	end
end

function SisterRILocal.GUI:CreateMainFrame()

	SisterRILocal.GUI.panel = CreateFrame("Frame", "SisterRaidIgnorePanel", UIParent);
	SisterRILocal.GUI.panel:SetPoint("top",0,-500)
	SisterRILocal.GUI.panel:SetHeight(500)
	SisterRILocal.GUI.panel:SetWidth(700)
	SisterRILocal.GUI.panel.name = "SisterRaidIgnorePanel";
	SisterRILocal.GUI.panel.okay = function (self) SisterRILocal.GUI.Panel_Okay(); end;
	SisterRILocal.GUI.panel.default = function (self) SisterRILocal.GUI.Panel_Default(); end;
	SisterRILocal.GUI.panel:Hide()
	SisterRILocal.GUI.panel:SetBackdrop({
				 bgFile = "Interface/Tooltips/UI-Tooltip-Background",
				 edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
				 tile = true, tileSize = 32, edgeSize = 32,
				 insets = { left = 9, right = 9, top = 9, bottom = 9}
			      })
	SisterRILocal.GUI.panel:SetBackdropColor(0,0,0,2)
	SisterRILocal.GUI.panel:SetMovable(true)
	SisterRILocal.GUI.panel:EnableMouse(true)
	SisterRILocal.GUI.panel:SetScript("OnMouseDown",function()
							SisterRILocal.GUI.panel:StartMoving()
						end)
	SisterRILocal.GUI.panel:SetScript("OnMouseUp",function()
						SisterRILocal.GUI.panel:StopMovingOrSizing()
					end)
	SisterRILocal.GUI.panel:SetScript("OnShow",function()
	end)
	SisterRILocal.GUI.panel:SetScript("OnEnter",function()
	end)
	SisterRILocal.GUI.panel:SetScript("OnLeave",function()
		local x,y = GetCursorScaledPosition()
		if ( x < SisterRILocal.GUI.panel:GetLeft() or x > SisterRILocal.GUI.panel:GetRight() ) then
			SisterRILocal.GUI.panel:SetFrameStrata("LOW")	    
		end
	end)
	
	SisterRILocal.GUI.panel.closebutton = CreateFrame("button", nil, SisterRILocal.GUI.panel,"UIPanelButtonTemplate")
	SisterRILocal.GUI.panel.closebutton:SetWidth(50)
	SisterRILocal.GUI.panel.closebutton:SetHeight(20)
	SisterRILocal.GUI.panel.closebutton:SetPoint("TopRight",0,0)
	SisterRILocal.GUI.panel.closebutton:SetText("Close")
	SisterRILocal.GUI.panel.closebutton:SetScript("OnClick",    function()
							SisterRILocal.GUI.panel:Hide()
							SisterRI_db.AutoRespond = SisterRILocal.GUI.panel.ReplyText:GetText()
							SisterRI_db.GuildIgnore = SisterRILocal.GUI.panel.GuildIgnore:GetChecked()
							--SisterRI_db.Summary = SisterRILocal.GUI.panel.Summary:GetChecked()
						end)
						
	-- custom reply
	local titlea = SisterRILocal.GUI.panel:CreateFontString(nil, "OVERLAY", "GameTooltipText")
	titlea:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15, -15);
	titlea:SetText("개인 메세지:")
	
	SisterRILocal.GUI.panel.ReplyText = CreateFrame("EditBox", nil, SisterRILocal.GUI.panel, "InputBoxTemplate")
	SisterRILocal.GUI.panel.ReplyText:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15+150, -15);
	SisterRILocal.GUI.panel.ReplyText:SetHeight( 25 )
	SisterRILocal.GUI.panel.ReplyText:SetWidth(300)
	SisterRILocal.GUI.panel.ReplyText:SetAutoFocus(false)
	SisterRILocal.GUI.panel.ReplyText:SetScript("OnEscapePressed",function()
		SisterRILocal.GUI.panel.ReplyText:ClearFocus()    
	end)
	SisterRILocal.GUI.panel.ReplyText:SetScript("OnEnter",function()   
		SisterRILocal.GUI.panel.ReplyText:ClearFocus() 
	end)
	SisterRILocal.GUI.panel.ReplyText:SetScript("OnEnterPressed",function()
		SisterRILocal.GUI.panel.ReplyText:ClearFocus() 
		SisterRI_db.AutoRespond = SisterRILocal.GUI.panel.ReplyText:GetText()
	end)
	SisterRILocal.GUI.panel.ReplyText:SetScript("OnShow", function()
		SisterRILocal.GUI.panel.ReplyText:SetText( SisterRI_db.AutoRespond )
	end)
	
	-- ignore Guild
	local titlec = SisterRILocal.GUI.panel:CreateFontString(nil, "OVERLAY", "GameTooltipText")
	titlec:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15, -65);
	titlec:SetText("Ignore guild:")
	
	SisterRILocal.GUI.panel.GuildIgnore = CreateFrame("CheckButton", "GuildIgnoreCheck", SisterRILocal.GUI.panel)
	SisterRILocal.GUI.panel.GuildIgnore:SetWidth( 25 )
	SisterRILocal.GUI.panel.GuildIgnore:SetHeight( 25 )
	SisterRILocal.GUI.panel.GuildIgnore:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15+150, -65);
	SisterRILocal.GUI.panel.GuildIgnore:SetScript("OnShow", function()
		--if BADBOY_SILENT then
			SisterRILocal.GUI.panel.GuildIgnore:SetChecked( SisterRI_db.GuildIgnore )
		--else
		--	frame:SetChecked(false)
		--end
	end)
	SisterRILocal.GUI.panel.GuildIgnore:SetHitRectInsets(0, -100, 0, 0)
	SisterRILocal.GUI.panel.GuildIgnore:SetNormalTexture("Interface\\Buttons\\UI-CheckBox-Up")
	SisterRILocal.GUI.panel.GuildIgnore:SetPushedTexture("Interface\\Buttons\\UI-CheckBox-Down")
	SisterRILocal.GUI.panel.GuildIgnore:SetHighlightTexture("Interface\\Buttons\\UI-CheckBox-Highlight")
	SisterRILocal.GUI.panel.GuildIgnore:SetCheckedTexture("Interface\\Buttons\\UI-CheckBox-Check")
	
	-- safelist
	local titled = SisterRILocal.GUI.panel:CreateFontString(nil, "OVERLAY", "GameTooltipText")
	titled:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15, -100);
	titled:SetText("열외대상 목록:")
	
	local titlee = SisterRILocal.GUI.panel:CreateFontString(nil, "OVERLAY", "GameTooltipText")
	titlee:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15, -125);
	titlee:SetText("이름 추가:")
	
	SisterRILocal.GUI.panel.Player = CreateFrame("EditBox", nil, SisterRILocal.GUI.panel, "InputBoxTemplate")
	SisterRILocal.GUI.panel.Player:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15+150, -125);
	SisterRILocal.GUI.panel.Player:SetHeight( 25 )
	SisterRILocal.GUI.panel.Player:SetWidth(300)
	SisterRILocal.GUI.panel.Player:SetAutoFocus(false)
	SisterRILocal.GUI.panel.Player:SetScript("OnEscapePressed",function()
		SisterRILocal.GUI.panel.Player:ClearFocus()    
	end)
	SisterRILocal.GUI.panel.Player:SetScript("OnEnter",function()   
		SisterRILocal.GUI.panel.Player:ClearFocus() 
	end)
	SisterRILocal.GUI.panel.Player:SetScript("OnEnterPressed",function()
		SisterRILocal.GUI.panel.Player:ClearFocus() 
		
	end)
	SisterRILocal.GUI.panel.Player:SetScript("OnShow", function()
		SisterRILocal.GUI.panel.Player:SetText( "" )
		SisterRILocal.GUI.panel.Player:ClearFocus() 
	end)
	
	SisterRILocal.GUI.panel.AddPlayer = CreateFrame("button", nil, SisterRILocal.GUI.panel,"UIPanelButtonTemplate")
	SisterRILocal.GUI.panel.AddPlayer:SetWidth(150)
	SisterRILocal.GUI.panel.AddPlayer:SetHeight(25)
	SisterRILocal.GUI.panel.AddPlayer:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15, -150);
	SisterRILocal.GUI.panel.AddPlayer:SetText("이름 추가")
	SisterRILocal.GUI.panel.AddPlayer:SetScript("OnClick",    function()
		local Found = false
		local Teller = 0
		for A = 1, getn(SisterRI_db.SafePlayers) do
			Teller = Teller + 1
			if ( SisterRI_db.SafePlayers[ A ] == SisterRILocal.GUI.panel.Player:GetText() ) then
				Found = true
			end
		end
		if ( Found == false ) then
			SisterRI_db.SafePlayers[ getn(SisterRI_db.SafePlayers)+1 ] = SisterRILocal.GUI.panel.Player:GetText()
			--print( "Adding player. input=", SisterRILocal.GUI.panel.Player:GetText(), "to array", SisterRI_db.SafePlayers, "slot", getn(SisterRI_db.SafePlayers), "result=", SisterRI_db.SafePlayers[ getn(SisterRI_db.SafePlayers) ] )
		end
		SisterRILocal:SortSafelist()
		--print( "Added player. input=", SisterRILocal.GUI.panel.Player:GetText(), "players=", getn(SisterRI_db.SafePlayers), "array=", SisterRI_db.SafePlayers, "saved=", SisterRI_db.SafePlayers[ getn(SisterRI_db.SafePlayers)] )
		SisterRILocal.GUI:scrollbar_Update( SisterRILocal.GUI.panel.SafeList, SisterRI_db.SafePlayers )
		SisterRILocal.GUI.panel.Player:SetText( "" )
	end)
	
	SisterRILocal.GUI.panel.RemovePlayer = CreateFrame("button", nil, SisterRILocal.GUI.panel,"UIPanelButtonTemplate")
	SisterRILocal.GUI.panel.RemovePlayer:SetWidth(150)
	SisterRILocal.GUI.panel.RemovePlayer:SetHeight(25)
	SisterRILocal.GUI.panel.RemovePlayer:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15+150, -150);
	SisterRILocal.GUI.panel.RemovePlayer:SetText("이름 제거")
	SisterRILocal.GUI.panel.RemovePlayer:SetScript("OnClick",    function()
		local Teller = 0
		local Found = false
		for A = 1, getn(SisterRI_db.SafePlayers) do
			Teller = Teller + 1
			if ( SisterRI_db.SafePlayers[ A ] == SisterRILocal.GUI.panel.Player:GetText() ) then
				SisterRI_db.SafePlayers[ A ] = nil
				Found = true
			end
			--if ( Found == true ) then
			--	SisterRI_db.SafePlayers[ A-1 ] = SisterRI_db.SafePlayers[ A ]
			--end
		end
		SisterRILocal:SortSafelist()
		--SisterRILocal:StartUpgradeItems( UIDropDownMenu_GetSelectedValue( SisterRILocal.GUI.panel.SlotDropdown ), UIDropDownMenu_GetSelectedValue( SisterRILocal.GUI.panel.DropspotDropdown ), 37134, 55000 )
		SisterRILocal.GUI:scrollbar_Update( SisterRILocal.GUI.panel.SafeList, SisterRI_db.SafePlayers )
		SisterRILocal.GUI.panel.Player:SetText( "" )
	end)
	
	local titlef = SisterRILocal.GUI.panel:CreateFontString(nil, "OVERLAY", "GameTooltipText")
	titlef:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15, -175);
	titlef:SetText("열외대상:")
	
	SisterRILocal.GUI.panel.SafeList = CreateFrame("Frame", nil, SisterRILocal.GUI.panel)
	SisterRILocal.GUI.panel.SafeList:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15, -120);
	SisterRILocal.GUI.panel.SafeList:SetHeight(200)
	SisterRILocal.GUI.panel.SafeList:SetWidth(150)
						 
	SisterRILocal.GUI.panel.SafeList["itembutton"] = {}
	
	local i, currentrow, previousrow
	for i = 1, 10 do
		SisterRILocal.GUI.panel.SafeList.itembutton[i] = SisterRILocal.GUI:createlistbutton(i, SisterRILocal.GUI.panel.SafeList)	
		currentrow = SisterRILocal.GUI.panel.SafeList.itembutton[i]
		currentrow:SetPoint("TOPLEFT",0,-(i*25)-50)
		currentrow:Show()
		previousrow=currentrow
	end
	
	SisterRILocal.GUI:createscrollbar( SisterRILocal.GUI.panel.SafeList, 1, SisterRI_db.SafePlayers )
	
	----
	
	local titleg = SisterRILocal.GUI.panel:CreateFontString(nil, "OVERLAY", "GameTooltipText")
	titleg:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15+200, -175);
	titleg:SetText("무시된 귓말:")
	
	SisterRILocal.GUI.panel.MsgList = CreateFrame("Frame", nil, SisterRILocal.GUI.panel)
	SisterRILocal.GUI.panel.MsgList:SetPoint("TOPLEFT", SisterRILocal.GUI.panel, "TOPLEFT", 15+200, -120);
	SisterRILocal.GUI.panel.MsgList:SetHeight(200)
	SisterRILocal.GUI.panel.MsgList:SetWidth(450)
						 
	SisterRILocal.GUI.panel.MsgList["itembutton"] = {}
	
	local i, currentrow, previousrow
	for i = 1, 10 do
		SisterRILocal.GUI.panel.MsgList.itembutton[i] = SisterRILocal.GUI:createlistbutton( i, SisterRILocal.GUI.panel.MsgList )	
		currentrow = SisterRILocal.GUI.panel.MsgList.itembutton[i]
		currentrow:SetPoint( "TOPLEFT", 0, -(i*25)-50 )
		currentrow:Show()
		previousrow = currentrow
	end
	
	SisterRILocal.GUI:createscrollbar( SisterRILocal.GUI.panel.MsgList, 2, SisterRI_db.IgnoredWhispers )
	
end

function GetCursorScaledPosition()
   local scale, x, y = UIParent:GetScale(), GetCursorPosition()
   return x / scale, y / scale
end

function SisterRILocal.GUI.Panel_OnShow()

	SisterRILocal.GUI.panel.shown = true;
	
end

function SisterRILocal.GUI.AddSummaryOptions()

	local level = level or 1;

	local info = UIDropDownMenu_CreateInfo();
	
	info.text = "레이드 후 보고";
	info.value = 0;
	info.func = function() SisterRILocal.GUI:Panel_ClickMenu() end;
	info.owner = self:GetParent();
	info.checked = nil;
	info.icon = nil;
	UIDropDownMenu_AddButton(info, level);

	info.text = "레이드 후 보고 않음";
	info.value = 1;
	info.func = function() SisterRILocal.GUI:Panel_ClickMenu() end;
	info.owner = self:GetParent();
	info.checked = nil;
	info.icon = nil;
	UIDropDownMenu_AddButton(info, level);

end

function SisterRILocal.GUI:Panel_ClickMenu()
	UIDropDownMenu_SetSelectedValue(self.owner, self.value);
end

function SisterRILocal.GUI:Panel_Okay()
	if (SisterRILocal.GUI.panel.shown == true) then

	end
end

function SisterRILocal.GUI:Panel_Default()
	SisterRILocal:Panel_OnShow();
end

function SisterRILocal.GUI:createlistbutton(i, SisterRI_Listframe)
   local buttontemplate, texttexture, SisterRIfontstring, SisterRIicon, SisterRInormaltexture, SisterRIhighlighttexture, buttonnumber

   buttontemplate = CreateFrame("Button",nil,SisterRI_Listframe)
   buttontemplate:SetHeight(25)
   buttontemplate:SetWidth(100)
   buttontemplate:SetPoint("topleft")
   buttontemplate:SetNormalFontObject("gamefontnormal")
   buttontemplate.buttonnumber=i
   buttontemplate:SetMovable(true)

   buttontemplate.leftstring = buttontemplate:CreateFontString(nil,"OVERLAY","gamefontnormal")
   buttontemplate.leftstring:SetJustifyH("Left")
   buttontemplate.leftstring:SetPoint("Left", buttontemplate, 35, 0)

   return buttontemplate
end

function SisterRILocal.GUI:createSisterRItexture(ourbutton)
   local   normaltextureleft, normaltextureright, normaltexture, highlighttexture = nil

   normaltextureleft=ourbutton:CreateTexture()
   normaltextureleft:SetHeight(25)
   normaltextureleft:SetWidth(0) --10 is the gap between text and anything else
   normaltextureleft:SetPoint("left", 25, 0)
   normaltextureleft:SetTexture("Interface\\AuctionFrame\\UI-AuctionItemNameFrame")
   normaltextureleft:SetTexCoord(0,.07,0,1)
   normaltextureleft:Hide()

   normaltextureright=ourbutton:CreateTexture()
   normaltextureright:SetHeight(25)
   normaltextureright:SetWidth(10)
   normaltextureright:SetPoint("right",0,0)
   normaltextureright:SetTexture("Interface\\AuctionFrame\\UI-AuctionItemNameFrame")
   normaltextureright:SetTexCoord(0,.8,0,1)
   normaltextureright:Hide()

   normaltexture=ourbutton:CreateTexture()
   normaltexture:SetHeight(25)
   normaltexture:SetPoint("left",normaltextureleft,"right")
   normaltexture:SetPoint("right",normaltextureright,"left")
   normaltexture:SetTexture("Interface\\AuctionFrame\\UI-AuctionItemNameFrame")
   normaltexture:SetTexCoord(0,.75,0,1)

   highlighttexture=ourbutton:CreateTexture()
   highlighttexture:SetHeight(25)
   highlighttexture:SetWidth(normaltexture:GetWidth()) --10 is the gap between text and anything else
   highlighttexture:SetPoint("left",normaltexture)
   highlighttexture:SetPoint("right",normaltexture)
   highlighttexture:SetTexture("Interface\\HelpFrame\\HelpFrameButton-Highlight")
   highlighttexture:SetTexCoord(0,1,.1,.1)

   return normaltexture,highlighttexture
end

function SisterRILocal.GUI:createscrollbar( SisterRI_Listframe, X, SisterRI_Itemlist )
	--print( "SisterRILocal.GUI:createscrollbar", SisterRI_Listframe, X, SisterRI_Itemlist  )
	local ourscrollbar

	local SisterRI_SBFname = "SisterRILocal.GUI.panel.SafeList.scrollbarframe"
	local SisterRI_SBFSBname = SisterRI_SBFname.."ScrollBar"
	if ( X == 2 ) then
		SisterRI_SBFname = "SisterRILocal.GUI.panel.MsgList.scrollbarframe"
		SisterRI_SBFSBname = SisterRI_SBFname.."ScrollBar"
	end

   SisterRI_Listframe["scrollbarframe"] = CreateFrame("ScrollFrame", SisterRI_SBFname, SisterRI_Listframe, "UIPanelScrollFrameTemplate")
   SisterRI_Listframe.scrollbarframe:SetPoint("topright", 0, -75)
   SisterRI_Listframe.scrollbarframe:SetPoint("bottomleft" , 0, -75)

   SisterRIscrollbar = getglobal( SisterRI_SBFname.."ScrollBar" )
   
   local SisterRIscrollupbutton = getglobal(  SisterRI_SBFname.."ScrollBarScrollUpButton" );
   SisterRIscrollupbutton:SetScript("OnClick",function()
					     local ourscrollbar
					     ourscrollbar=getglobal( SisterRI_SBFSBname)
					     ourscrollbar:SetValue( ourscrollbar:GetValue() - 10 )
						 SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
					  end)
   SisterRIscrollupbutton:SetScript("OnMouseDown",function()
						 SisterRIscrollup=true
						 SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
					      end)  
   SisterRIscrollupbutton:SetScript("OnMouseUp",function()
					       SisterRIscrollup=false
						   SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
					    end)  

   local SisterRIscrolldownbutton = getglobal(  SisterRI_Listframe.scrollbarframe:GetName().."ScrollBarScrollDownButton" );
   SisterRIscrolldownbutton:SetScript("OnClick",function()

					       local ourscrollbar
					       ourscrollbar=getglobal( SisterRI_SBFSBname )
					       ourscrollbar:SetValue(ourscrollbar:GetValue() + 10 ) --have it scroll half the list each scroll
						   SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
					    end)
   SisterRIscrolldownbutton:SetScript("OnMouseDown",function()
						   SisterRIscrolldown=true
						   SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
						end)  
   SisterRIscrolldownbutton:SetScript("OnMouseUp",function()
						 SisterRIscrolldown=false
						 SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
					      end)  

   SisterRI_Listframe.scrollbarframe:SetScript("OnVerticalScroll",function()
									   SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
									end)

   SisterRI_Listframe.scrollbarframe:SetScript("OnMouseWheel",function()
								       local ourscrollbar
								       ourscrollbar=getglobal( SisterRI_SBFSBname )
									   ourscrollbar:SetValue( ourscrollbar:GetValue() - (10*arg1) )
										SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
								    end)

   SisterRIscrollbar:SetScript("OnShow",function()
				       SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
				       
				    end)  	
				
   SisterRIscrollbar:SetValueStep(1)
   SisterRILocal.GUI:createscrollbartemplate(SisterRI_Listframe, X)
   SisterRILocal.GUI:scrollbar_Update(SisterRI_Listframe, SisterRI_Itemlist)
   
end

function SisterRILocal.GUI:scrollbar_Update( SisterRI_Listframe, SisterRI_Itemlist )
	--print( "SisterRILocal.GUI:scrollbar_Update received", SisterRI_Listframe, SisterRI_Itemlist, "SisterRI_db.IgnoredWhispers=", SisterRI_db.IgnoredWhispers )
	if ( SisterRI_Itemlist ~= SisterRI_db.SafePlayers ) and ( SisterRI_Itemlist ~= SisterRI_db.IgnoredWhispers ) then
		SisterRI_Itemlist = SisterRI_db.SafePlayers
		if ( SisterRI_Listframe == SisterRILocal.GUI.panel.MsgList ) then
			SisterRI_Itemlist = SisterRI_db.IgnoredWhispers
		else
			return
		end
	end
	if ( SisterRI_Itemlist ) then
		local SisterRInumberofitems
	
		local SisterRIscrollbar = getglobal( SisterRI_Listframe.scrollbarframe:GetName().."ScrollBar" )
		local SisterRIscrollupbutton = getglobal(  SisterRI_Listframe.scrollbarframe:GetName().."ScrollBarScrollUpButton" );
		local SisterRIscrolldownbutton = getglobal(  SisterRI_Listframe.scrollbarframe:GetName().."ScrollBarScrollDownButton" );
	
		SisterRInumberofitems = table.maxn( SisterRI_Itemlist )

		if ( SisterRInumberofitems < 10 ) then
			SisterRIscrollbar:SetMinMaxValues(0,0)
			SisterRIscrollupbutton:Disable()
			SisterRIscrolldownbutton:Disable()
		else
			SisterRIscrollbar:SetMinMaxValues( 0, SisterRInumberofitems-10 )
			SisterRIscrollupbutton:Enable()
			SisterRIscrolldownbutton:Enable()
		end
	
		local x,hexcolor,itemRSisterRIty
		local ourbutton, currentscrollbarvalue
		currentscrollbarvalue = SisterRIscrollbar:GetValue()
	   
		if ( SisterRI_Itemlist ) then
			if ( SisterRI_Listframe ) then
				for A = 1, 10 do --apparently theres a bug here for some screen resolutions
					--print( A, SisterRI_Itemlist[A], SisterRI_db.IgnoredWhispers[A] )
					local x = (10 - A+1) --snur lista
					--print( (x+currentscrollbarvalue), SisterRI_Itemlist[x+currentscrollbarvalue], SisterRI_db.SafePlayers[x+currentscrollbarvalue] )
					if ( SisterRI_Itemlist[x+currentscrollbarvalue] ) then
						if ( SisterRI_Listframe.itembutton[x] ) then
							--print( SisterRI_Listframe.itembutton[x] )
							if ( SisterRI_Itemlist[x+currentscrollbarvalue].Sender == nil ) then
								--print( SisterRI_Itemlist[x+currentscrollbarvalue] )
								SisterRI_Listframe.itembutton[x].leftstring:SetText( SisterRI_Itemlist[x+currentscrollbarvalue] )
							else
								--print( SisterRI_Itemlist[x+currentscrollbarvalue].Time, SisterRI_Itemlist[x+currentscrollbarvalue].Sender, SisterRI_Itemlist[x+currentscrollbarvalue].Msg )
								if ( SisterRI_Itemlist[x+currentscrollbarvalue].Message ~= nil ) then
									SisterRI_Listframe.itembutton[x].leftstring:SetText( SisterRI_Itemlist[x+currentscrollbarvalue].Time.." "..SisterRI_Itemlist[x+currentscrollbarvalue].Sender..": "..SisterRI_Itemlist[x+currentscrollbarvalue].Message )
								end
							end
						else
						   SisterRI_Listframe.itembutton[x].leftstring:SetText("")
						end
					else
						SisterRI_Listframe.itembutton[x].leftstring:SetText("")
					end
				end
			end
		else
		end
	end

end

function SisterRILocal.GUI:createscrollbartemplate(SisterRI_Listframe, X)

	SisterRItexture = SisterRI_Listframe.scrollbarframe:CreateTexture()
	SisterRItexture:SetWidth(31)
	SisterRItexture:SetPoint("topright",29,2)
	SisterRItexture:SetTexture("Interface\\PaperDollInfoFrame\\UI-Character-ScrollBar")
	SisterRItexture:SetTexCoord(0,.484375,0,1)

	SisterRItexture = SisterRI_Listframe.scrollbarframe:CreateTexture()
	SisterRItexture:SetWidth(30)
	SisterRItexture:SetHeight(106)
	SisterRItexture:SetPoint("bottomright",29,-2)
	SisterRItexture:SetTexture("Interface\\PaperDollInfoFrame\\UI-Character-ScrollBar")
	SisterRItexture:SetTexCoord(.515625,1,0,.4140625)

	SisterRItexture = SisterRI_Listframe.scrollbarframe:CreateTexture()
	SisterRItexture:SetHeight(25)
	SisterRItexture:SetAllPoints()
	SisterRItexture:SetPoint("right",29,45)
	SisterRItexture:SetTexture("Interface\\PaperDollInfoFrame\\UI-Character-ScrollBar")
	SisterRItexture:SetTexCoord(0,.4,.8,.8)
	SisterRItexture:Hide()

end

SisterRILocal.GUI.frame = CreateFrame("Frame", "SisterRaidIgnoreFrame");
SisterRILocal.GUI.frame:RegisterEvent("ADDON_LOADED");
SisterRILocal.GUI.frame:RegisterEvent("CHAT_MSG_WHISPER");
SisterRILocal.GUI.frame:RegisterEvent("CHAT_MSG_ADDON");
SisterRILocal.GUI.frame:SetScript("OnEvent", SisterRILocal.GUI.OnEvent);