--------------------------------------------------------------------------
-- ChatScroll.lua 
--------------------------------------------------------------------------
--[[
ChatScroll

author: AnduinLothar    <karlkfi@cosmosui.org>

-ChatFrame Mouse Wheel Scroll

Change List
v2.2
- Added "Hide Chat Frame Scroll Buttons" Option
- Cleaned up the code
v2.1
- Prepared for Lua 5.1
- Removed Localization usage
v2.0
- No longer uses hidden frames, now uses script element hooks
- No longer supports PopUI or TransUI, I recomend using VisibilityOptions to hide the buttons.
- Added Shift-MouseWheelUp to to to the top of the chat window
- Added Shift-MouseWheelDown to to to the bottom of the chat window
- Added Scroll to Top Binding
v1.1
- Fixed negative framelevel bug in new patch
- Updated the TOC to 1300.
v1.1
- Initial Release

$Date: 2007-07-03 21:22:03 -0500 (Tue, 03 Jul 2007) $
$Rev: 5009 $

]]--

-- <= == == == == == == == == == == == == =>
-- => Function Definition
-- <= == == == == == == == == == == == == =>

ChatScroll = {};

-- For manually changing settings use these two variables (these will not override Khaos settings)
ChatScroll.Enabled = true;
ChatScroll.Hide_Buttons = false;

function ChatScroll.OnMouseWheel(chatframe, value)
	if ( ChatScroll.Enabled ) then
		if ( IsShiftKeyDown() ) then
			if ( value > 0 ) then
				chatframe:ScrollToTop()
			elseif ( value < 0 ) then
				chatframe:ScrollToBottom();
			end
		else
			if ( value > 0 ) then
				chatframe:ScrollUp();
			elseif ( value < 0 ) then
				chatframe:ScrollDown();
			end
		end
	else
		if ( value > 0 ) then
			ActionBar_PageUp();
		elseif ( value < 0 ) then
			ActionBar_PageDown();
		end
	end
end

function ChatScroll.ScrollToTop()
	SELECTED_DOCK_FRAME:ScrollToTop();
end

function ChatScroll.UpdateScrollButtons()
	ChatScroll.SetScrollButtonVisibility(ChatScroll.Hide_Buttons)
end

function ChatScroll.SetScrollButtonVisibility(toggle)
	if (toggle) then
		ChatScroll.Hide_Buttons = true;
		ChatFrameMenuButton:Hide()
	else
		ChatScroll.Hide_Buttons = false;
		ChatFrameMenuButton:Show()
	end
	
	local frameName
	for i=1, NUM_CHAT_WINDOWS do
		frameName = "ChatFrame"..i
		if (toggle) then
			getglobal(frameName.."UpButton"):Hide()
			getglobal(frameName.."DownButton"):Hide()
			getglobal(frameName.."BottomButton"):Hide()
		else
			getglobal(frameName.."UpButton"):Show()
			getglobal(frameName.."DownButton"):Show()
			getglobal(frameName.."BottomButton"):Show()
		end
	end
end

-- <= == == == == == == == == == == == == =>
-- => Execution
-- <= == == == == == == == == == == == == =>

-- Set up hooks
for i=1, NUM_CHAT_WINDOWS do
	local frame = getglobal("ChatFrame"..i);
	frame:EnableMouseWheel(1);
	local oldFunc = frame:GetScript("OnMouseWheel");
	if (type(oldFunc) == "function") then
		frame:SetScript("OnMouseWheel", function(self) oldFunc(self); ChatScroll.OnMouseWheel(self, arg1); end);
	else
		frame:SetScript("OnMouseWheel", function(self) ChatScroll.OnMouseWheel(self, arg1); end);
	end
end
hooksecurefunc("FCF_UpdateButtonSide", ChatScroll.UpdateScrollButtons);

-- Update Button Visibility
ChatScroll.SetScrollButtonVisibility(ChatScroll.Hide_Buttons)

-- Register with Khaos
if (Khaos) then
	local optionSet = {
		id="ChatScroll";
		text=CHATSCROLL_CONFIG_HEADER;
		helptext=CHATSCROLL_CONFIG_HEADER_INFO;
		callback=function(checked) if (checked) then ChatScroll.Enabled = true; else ChatScroll.Enabled = nil; end end;
		feedback=function(checked) if (checked) then return CHATSCROLL_CHAT_ENABLED else return CHATSCROLL_CHAT_DISABLED end end;
		difficulty=1;
		default={checked=true};
		options={
			{
				id="Header";
				text=CHATSCROLL_CONFIG_HEADER;
				helptext=CHATSCROLL_CONFIG_HEADER_INFO;
				type=K_HEADER;
			};
			{
				id="HideChatButtons";
				text=CHATSCROLL_BUTTONS_HIDE;
				helptext=CHATSCROLL_BUTTONS_HIDE_INFO;
				type=K_TEXT;
				check=true;
				callback=function(state) ChatScroll.SetScrollButtonVisibility(state.checked) end;
				default={checked=false};
				feedback=function(state) if (state.checked) then return CHATSCROLL_BUTTONS_HIDDEN else return CHATSCROLL_BUTTONS_SHOWN end end;
			};
		};
	};
	Khaos.registerOptionSet(
		"chat",
		optionSet
	);
end
