 # THE SOFTWARE.
 
 LINK_WARNING_AMOUNT = 20
+LINK_PREVIEW_THRESHOLD = 10
 BLACKLIST = /^javascript/i
 FILTERS = {
 						all: ".*",

onMessageHandler = (request, sender, sendResponse) ->
	if request.type == "verifySelection"
		@links = request.links
		for own key, value of FILTERS
			@linksForFilter[key] = filterLinks(value)
			updateContextMenu(key)
	# todo: store current selection for optimization (in case it asks for the same extension again next time)
	# todo: show up to three links directly (+ all)

filterLinks = (filter) ->
	link for link in @links.reverse() when link.match(new RegExp(filter, "i")) and not link.match(BLACKLIST)

	chrome.contextMenus.update(id, {title:title, enabled:links.length > 0})

onClickHandler = (info, tab) ->
	previewLink = @previewLinks[info.menuItemId]
	if previewLink
		openLink(previewLink, tab)
	else
		links = @linksForFilter[info.menuItemId]
		if links.length <= LINK_WARNING_AMOUNT or confirm(chrome.i18n.getMessage("selection_alert_tooManyLinks", [links.length]))
			for link in links
				openLink(link, tab)

openLink = (url, currentTab) ->
	chrome.tabs.create({url:url,index:currentTab.index+1})

chrome.runtime.onInstalled.addListener(onInstalledHandler)
chrome.extension.onMessage.addListener(onMessageHandler)
