$(function(){
	if($('#contribute-form').length){
		// add ability to reply to comments
		var activeCancel = null;

		$('<div class="replyLink">Reply</div>').click(function(){
			var replyLink = $(this);
			var comment = replyLink.parent();

			if(replyLink.html() == 'Reply'){
				$('#contribute-form').appendTo(comment.parent());
				$('#contribute-parent').val(comment.attr('data-comment-id'));
				replyLink.html('Cancel');
				if(activeCancel != null){
					activeCancel.html('Reply');
				}
				activeCancel = replyLink;
			}
			else{
				$('#contribute-form').appendTo('#comments');
				$('#contribute-parent').val('0');
				replyLink.html('Reply');
			}

		}).appendTo($('.comment'));

		// hide antispam
		$('#contribute-question').val('42');
		$('#contribute-guard').css('display', 'none');
	}

	// format timestamps
	var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

	var showTimeTreshold = new Date() - 86400000;
	$('.timestamp').each(function(){
		var d = new Date(this.innerHTML);
		var buffer = [];

		buffer.push(d.getFullYear(), ' ', months[d.getMonth()], ' ', d.getDate());
		if(d >= showTimeTreshold){
			var minutes = d.getMinutes();
			if(minutes < 10){
				minutes = '0' + minutes;
			}
			var hours = d.getHours();
			if(hours < 10){
				hours = '0' + hours;
			}
			buffer.push(' at ', hours, ':', minutes);
		}

		this.innerHTML = buffer.join('');
	});
});
