--[[

	Scorpio's AddOns

	By Scorpio of <Sisu> http://sisuguild.fi

]]

-- Local variables
local SAD_Saved_UnitFrame_OnEnter = nil;

-- OnLoad
function SAD_ShowUnitGuild_OnLoad()

	this:RegisterEvent("UPDATE_MOUSEOVER_UNIT");
	SlashCmdList["SAD_SHOW_UNIT_GUILD"] = SAD_BuffDuration_SlashCommandHandler;
	SLASH_SAD_SHOW_UNIT_GUILD1 = "/sadshowguild";
	this:RegisterEvent("ADDON_LOADED");

end

-- Slash command handler
function SAD_BuffDuration_SlashCommandHandler(msg)

	-- make all lowercase to ease checking
	msg = string.lower(msg);

	if((string.find(msg, 'enable')) or (string.find(msg, 'true'))) then
	
		SAD_ShowUnitGuild_Enable(1);
		
	elseif((string.find(msg, 'disable')) or (string.find(msg, 'false'))) then
	
		SAD_ShowUnitGuild_Enable(0);
	
	else

		local helptext = "/sadshowguild enable|true|disable|false";
		
		SAD_Output(helptext);
	
	end

end

-- Enable ShowUnitGuild
function SAD_ShowUnitGuild_Enable(toggle)

	if (toggle == 1) then
	
		if(SAD_Saved_UnitFrame_OnEnter == nil) then
			SAD_Saved_UnitFrame_OnEnter = UnitFrame_OnEnter;
			UnitFrame_OnEnter = SAD_ShowUnitGuild_UnitFrame_OnEnter;
		end
			
		SAD_Trace("ShowUnitGuild Enabled");
			
	else
	
		if(SAD_Saved_UnitFrame_OnEnter ~= nil) then
			UnitFrame_OnEnter = SAD_Saved_UnitFrame_OnEnter;
			SAD_Saved_UnitFrame_OnEnter = nil;
		end
			
		SAD_Trace("ShowUnitGuild Disabled");
			
	end

	SAD_SHOW_UNIT_GUILD = toggle;
	
end

-- Event handler
function SAD_ShowUnitGuild_OnEvent(event)

	if ( event == "UPDATE_MOUSEOVER_UNIT" ) then
	
		SAD_ShowUnitGuild_Tooltip("mouseover");
		
	end

	if( (event == "ADDON_LOADED") and (arg1 == "SAD_ShowUnitGuild") ) then

		SAD_ShowUnitGuild_Enable(SAD_SHOW_UNIT_GUILD);

	end
	
end

-- Show guild in target tooltip box
function SAD_ShowUnitGuild_UnitFrame_OnEnter()

	if(SAD_Saved_UnitFrame_OnEnter ~= nil) then
		SAD_Saved_UnitFrame_OnEnter();
		SAD_ShowUnitGuild_Tooltip("target");
	end
	
end

-- Helper for Tooltip
function SAD_ShowUnitGuild_Tooltip(entrytarget)

	if(entrytarget) then
	
		local guildname = GetGuildInfo(entrytarget);
			
		if(guildname) then
			
			GameTooltip:AddLine("<"..guildname..">");
			local gth = GameTooltip:GetHeight();
			GameTooltip:SetHeight(gth + 12);
			
		end
	
	end
	
end