var they={
  relatedTagsLimit:3,
  relatedPostsLimit:18,


	loadRelated:function() {

    var idlist=[];
    idlist.push($("body").attr("class").replace(/post/,""));





		$('#tags a').each(function() {
			var tagName=$(this).attr('rel');

			var b=0;
			$.get('/tagged/'+tagName,function(data) {
				$(data).find('.post').each(function() {
					var pid=$(this).attr('id').replace(/post/,"");
					var e=0;
					if (e<=5 && b<=they.relatedPostsLimit && idlist.indexOf(pid)==-1) {
						idlist.push(pid);
						e++;
						b++;
						$(this).appendTo($('#related'));
					}
				});

			});

		});

		$('#related').show();


	},

	trimTags:function() {
		$a=$('#tags a:last');
		$a.html($a.html().replace(', ',''));
	}
};

$(document).ready(function() {
	$('body#perm').each(function() {
		they.loadRelated();
		they.trimTags();
	});
});