/// 댓글    
$(".comment-post .textarea.enable").bind("paste", function(e) {
	e.preventDefault();
	var clipboardData = window.clipboardData || e.originalEvent.clipboardData;
	var text = clipboardData.getData("Text");
	text = text.replace(/\n/g, "<br>");
	
	if (document.selection) {
		document.selection.createRange().pasteHTML(text);
	}
	else {
		document.execCommand("insertHTML", false, text);
	}
});
    
$(".comment-post .textarea.enable").live("mousedown", function() {
	var self = $(this);
	if (self.attr("contentEditable") == undefined || self.attr("contentEditable") == "false" || self.attr("contentEditable") == "inherit") {
		self.next("a").show();
		self.attr("contentEditable", true).html("").focus();
	}
});

$(".comment-post form").live("submit", function(e) {
	e.preventDefault();
	//$("input[name=message]", this).val($(".textarea.enable", this).text());
	
	var self = $(this);
	var parent_id = $("input[name=parent_id]", this).val();

	var html = $(".textarea.enable", this).html().replace(/\n|\r/g, "");
	html = html.replace(/<(div|p)>/gi, "\n");
	html = html.replace(/<br\s?\/?>/gi, "");
	html = html.replace(/<\/(div|p)>/gi, "");
	html = html.replace(/^\s|\s$/g, "");
	var message = html;		


	$(".textarea.enable", this).text("");
	$.post("/api/add_comment.php", {"parent_id": parent_id, "message": message}, function(data) {
		$(data).appendTo(self.closest(".comments").find(".comment-each"));
		$("#loading").hide();
	});
});



$(".comment-each a").live("click", function(e) {
	e.preventDefault();
	if (!confirm("삭제하시겠습니까?")) {
		return false;	
	}
	
	var self = $(this);
	var id = $("input[name=comment_id]", $(this).closest("td")).val();
	$.post("/api/remove_comment.php", {"comment_id": id}, function(data) {	
		self.closest("table").remove();		
	});	
});
