LINKSNAG_MAX_LINK_LENGTH = 32;
LINKSNAG_LINK_COLOR = "00FF00";

function linkSnagExportFrame_OnLoad()
	linkSnag_old_ChatFrame_OnEvent = ChatFrame_OnEvent;
	ChatFrame_OnEvent = linkSnag_new_ChatFrame_OnEvent;

	linkSnag_old_SetItemRef = SetItemRef;
	SetItemRef = linkSnag_new_SetItemRef;
end



function linkSnag_new_ChatFrame_OnEvent(orig_event)
	local msg = arg1;
	
	if(string.sub(orig_event, 1, 8) == "CHAT_MSG" and msg ~= nil) then
		msg = string.gsub(msg, "(%s?)(www%.[%w_/%.%?%%=~&-]+)(%s?)", linkSnag_linkGen);	-- www.whatever.com/dir/file.ext
		msg = string.gsub(msg, "(%s?)(%a+://[%w_/%.%?%%=~&-]+)(%s?)", linkSnag_linkGen);	-- http://sub.domain.com/dir/file.ext
		msg = string.gsub(msg, "(%s?)([_%w-%.~-]+@[_%w-]+%.[_%w-%.]+)(%s?)", linkSnag_linkGen);-- 192.168.0.1:12345/dir/file.ext
		msg = string.gsub(msg, "(%s?)(%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?[:%d%d?%d?%d?%d?]*[%w_/%.%?%%=~&-]*)(%s?)", linkSnag_linkGen);--foo@bar.com
		arg1 = msg;
	end
			
	linkSnag_old_ChatFrame_OnEvent(orig_event);
end 

function linkSnag_new_SetItemRef(link, text, button)
	if (string.sub(link, 1 , 3) == "ref") then
		linkSnagURL:SetText(string.sub(link,5));
		linkSnagExportFrame:Show();
		linkSnagURL:HighlightText();
		return;
	else
		linkSnag_old_SetItemRef(link, text, button);
	end
end

function linkSnag_linkGen(begSpace, URL, aftSpace)
	return begSpace .. "|cff" .. LINKSNAG_LINK_COLOR .. "|Href:" .. URL .. "|h[".. URL .."]|h|r" .. aftSpace;
end

function linkSnag_HideButton_onClick()
	linkSnagExportFrame:Hide();
end
