$(function()
{
	$("a[@class^='flag_']").each(function()
	{
		$(this).click(function()
		{
			var type = $(this).attr("class").split('_')[1];
			var id_listing = $(this).attr("id").split('_')[1];

			var msg = '';

			if('spam' == type)
			{
				msg += '"spam"?';
				msg += "\r\nThe receiving of 10 flags will automatically remove the listing so please flag with care.";
			}
			else if('category' == type)
			{
				msg += '"wrong category"?';
				msg += "\r\nThe receiving of 10 wrong-category-flags will automatically remove the listing so please flag with care.";
			}
			else
			{
				msg += '"both"?';
			}

			var ans = confirm('Do you really want to mark this listing as ' + msg);

			if (ans)
			{
				$.ajax(
				{
					type: "POST",
					url: "flag.php",
					data: "id=" + id_listing + "&type=" + type,
					success: function(data)
					{
						alert(data);
					}
				});
			}

			return false;
		});
	});
});
