$(function() {
	$('.comment-reply').click(function(e) {
		if($(this).attr("login")=="true") {
			//please log in
			location.href="/user/login?RET="+window.location.href;
		} else {
			var author = $(this).closest('.comment-entry').find('.comment-author').text();
			$('#comment textarea').focus();
			$('#comment textarea').text("@" + author + "\n\n");
		}
		e.preventDefault();
	});
	like.do_init();
});

/* like/unlike handling */
var like = {
	comment_id:null,
	do_init:function() {
		like.comment_id = null;
		$('.comment-like').unbind();
		$('.comment-like').click(function(e) {
			like.do_like($(this).attr('id'));
			$(this).attr('disabled','true');
			e.preventDefault();
		});
		$('.comment-unlike').unbind();
		$('.comment-unlike').click(function(e) {
			like.do_unlike($(this).attr('id'));
			$(this).attr('disabled','true');
			e.preventDefault();
		});
	},
	do_like:function(comment_id){
		like.do_call(comment_id,"comment_like");
	},
	do_unlike:function(comment_id){
		like.do_call(comment_id,"comment_unlike");
	},
	do_call:function(comment_id,mode){
		var href = "/site/_comment_likes";
		like.comment_id = comment_id;
		$.ajax({
			type:'GET',
			dataType:'html',
			success:function(data){
				like.update_data(data);
			},
			beforeSend:function(){
			},
			url:href+"?mode="+mode+"&comment_id="+comment_id+"&ms="+new Date().getTime()
		});
	
	},
	update_data:function(data){
		$(".like-"+like.comment_id).html(data);
		like.do_init();
	}
};
