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

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

onClickHandler = (info, tab) ->
	links = @linksForFilter[info.menuItemId]
	if links.length <= LINK_WARNING_AMOUNT or confirm(chrome.i18n.getMessage("selection_alert_tooManyLinks", [links.length]))
		for link in links
			chrome.tabs.create({url:link,index:tab.index+1})

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

onMessageHandler = (request, sender, sendResponse) ->
	if request.type == "verifySelection"
		@links = request.links
		updateContextMenus()

updateContextMenus = ->
	renderPreviewContextMenus()
	for own key, value of FILTERS
		@linksForFilter[key] = filterLinks(value)
		updateContextMenu(key)

renderPreviewContextMenus = ->
	clearPreviewContextMenus()
	@previewAmount = Math.min(@links.length, LINK_PREVIEW_THRESHOLD)
	if @previewAmount > 0
		chrome.contextMenus.create({contexts:["all"], parentId:"parent", id:"preview_separator", type:"separator"})
		for i in [0..@previewAmount]
			@previewLinks["preview#{i}"] = @links[i]
			chrome.contextMenus.create({contexts:["all"], parentId:"parent", id:"preview#{i}", title:@links[i]})

clearPreviewContextMenus = ->
	chrome.contextMenus.remove("preview_separator")
	for i in [0..@previewAmount]
		chrome.contextMenus.remove("preview#{i}")
	@previewAmount = 0
	@previewLinks = {}

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