function UserBookmarkToggleLink(command, linkSelector, commands)
{
	next_command = command == 'add' ? 'del' : 'add'
	$(linkSelector).text(commands[next_command].text)
	$(linkSelector).fadeIn()
	return next_command
}

function UserBookmarkRemoveAd(command, linkSelector, links)
{
	$('li:has(.search_detail ' + linkSelector + ')').hide("slow")
	function decrease_count(selector)
	{
		cur_count = $(selector).text()
		if (cur_count > 0)
			$(selector).text(cur_count - 1)
		return cur_count <= 1
	}
	if (decrease_count('#current_span_count'))
		$('#no_user_bookmarks_message').show("slow")
	decrease_count('#total_count')
}

var userBookmarkLinksSetup = function(action, commands)
{
	$(function()
	{
		for ( var command in commands)
			$(".ad_user_bookmark_link[command=" + command + "]").text(commands[command].text)

		$(".ad_user_bookmark_link").css('display', 'inline');

		$(".ad_user_bookmark_link").click(function(event) {
			event.preventDefault()
			var commandId = $(this).attr("command")
			var adId = Number($(this).attr("index"))
			$(this).hide()
			$.getJSON(commands[commandId].url + '?ad=' + adId,
				function(result) {
					linkSelector = ".ad_user_bookmark_link[index=" + adId + "]"
					if (result.success)
					{
						commandId = action(commandId, linkSelector, commands)
						$(linkSelector).attr("command", commandId)
					} else {
						var message = result.error.substring(0,1).toUpperCase() +
							result.error.substring(1) + '.'
						var errorLogSelector = ".ad_user_bookmark_error[index=" + adId + "]"
						$(errorLogSelector).html(message)
						$(errorLogSelector).fadeIn()
					}
				}
			)
		})
	})
}
