$(document).ready(function() {
	
	var cats = ['contest', 'portfolio'];		
	var processResults = function(res) {

		for (var j=0; j<cats.length; j++) {
			var cat = cats[j];
			$('#latest-comments-'+cat).html('');
			var comments = res[cat];
			for (var i=0; i<comments.length; i++) {
				var cmt = comments[i];
				var el = $('<div class="comment"><h5><a href=""></a></h5><p><span></span><strong></strong></p></div>');
				el.hover(function() { $(this).addClass('hover')}, function() { $(this).removeClass('hover') });
				el.click(function() { document.location.href = WEB_ROOT + $(this).find('a').attr('href').substring(1) });
				var d = GC.parseDate(cmt['date']);
				var shorttitle = cmt.title;
				if (shorttitle.length > 36) {
					shorttitle = shorttitle.substring(0, 36) + '...';
				}
				el.find('a').text(shorttitle).attr('href', cmt.url).attr('title', d.toString()+': '+cmt.title);
				el.find('p span').text(cmt.ago + ' / ' + (cmt.username.length > 16 ? cmt.username.substring(0, 16) + '...' : cmt.username) + ' / ');
				el.find('p strong').text(cmt.replies);
				$('#latest-comments-'+cat).append(el);
			}
		}
	};

	$('.latest-comments li a').click(function() {
		var cat = $(this).attr('href').substring(1);
		$(this).parent().addClass('active').siblings().removeClass('active');
		$('#latest-comments-'+cat).show().siblings('div').hide();
		return false;
	});

	var updateLatestComments = function() {
		GC.callAction('latest_comments/get', processResults);
	};

	updateLatestComments();
	setInterval(updateLatestComments, 120 * 1000 /* 2 min */);
});
