$(document).ready(function(){
	$("input[name='comment-submit']").click(function(){
		if($("input[name='comment_title']").val() == ''){
			alert('Please enter comment title');
			return false;
		}
		if($("textarea[name='comment_body']").val() == ''){
			alert('Please enter comment');
			return false;
		}
		
		var btn = $(this);
		btn.addClass('disable_submit').attr('disabled', true);
		form_data = $('#comment-form').serialize();
		//comment_total = $('#comment_total').length;
		
		$.ajax({
			data: form_data,
			type: 'post',
			//dataType: 'json',
			url: '/comments/comment_submit',
			beforeSend: function(){
				btn.after('<div id="loading"></div>');
			},
			error: function(){
				alert('error');
				btn.removeClass('disable_submit').attr('disabled', false);
			},
			success: function(data){
				$('#no_comment').remove();
				if(data != ''){
					$('.comments').append(data);
				}
				$('#loading').remove();
				var comment = parseInt($('#comment_total').text());
				commente = comment + 1;
				$('#comment_total').text(commente);
				$('#comment-form')[0].reset();
				btn.removeClass('disable_submit').attr('disabled', false);
				
			}
		});
		return false;
	});
	
	$('.comments .article').hover(
		function(){
			$(this).children('.edit').show();
		},
		function(){
			$(this).children('.edit').hide();
		}
	);
	
	$('.edit').click(function(){
		me = $(this);
		comment_id = $(this).children('a').attr('rel');
		$.ajax({
			data: 'comment_id='+comment_id,
			type: 'post',
			dataType: 'json',
			url: '/comments/comment_delete',
			beforeSend: function(){
				me.prepend('<div id="loading"></div>');
			},
			success: function(data){
				if(data.status = 1){
					$('#comment-'+comment_id).remove();
					alert('Comment deleted');
				}else{
					alert('Can not delete fail');
				}
				$('#loading').remove();
			}
		});
	});
});
