$(function(){
	$("#message_detail").rte({
		css: ['/css/rte_default.css'],
		width: 450,
		height: 120,
		controls_rte: rte_toolbar,
		controls_html: html_toolbar
	});
	loadContent();
});
function loadContent(){
	var usr = $("#result_group").attr("name");
	var bid = $("#blog_space").attr("name");
	$.ajax({
	type: "GET",
	url: '/blog/comment/'+usr+'/'+bid,
	data: "rand="+Math.random(),
	beforeSend: function(){$("#comment_space").empty().height(100).html("<img src='/images/spin.gif' style='position:absolute; top:30px; right:370px;' />"); },
	success: function(msg){ $("#comment_space").empty().height('').html(msg); addAction(); }
	});
}
function addAction(){
	$(".del_blog a").click(function(event){
		event.preventDefault();
		if(confirm('ต้องการลบ BLOG ใช่ไหม?')){
			window.location = ""+$(this).attr("href");
		}
	});
	delAction(".del_comment a");
	$("#get_commnet").text("คำตอบ ("+$("#comment_size").text()+")");
	$("#comment_form").submit(function(event){
		event.preventDefault();
		$(this).addTextarea();
		if($.trim($('#comment_form textarea').val()).length<2  || $.trim($('#comment_form textarea').val())=='<P>&nbsp;</P>'){
			alert('กรอกข้อมูลด้วยครับ');
		}else{
		$("#comment_form").ajaxSubmit({
				beforeSubmit: function(){
					$("#comment_form :submit").hide().after("<img src='/images/spin.gif' />");
				},
				success: function(msg){ 
					$("#comment_space").empty().height('').html(msg);
					$("#comment_form :submit").show().next("img").remove();
					$("#comment_form").clearTextarea();
					addNewAction();
				}
			});
		}
	});
	addSpan();
}
function addNewAction(){
	$("#get_commnet").text("คำตอบ ("+$("#comment_size").text()+")");
	delAction(".del_comment a");
	addSpan();
}
function delAction(arg){
		$(arg).click(function(event){
		event.preventDefault();
		if(!confirm('คุณต้องการลบความคิดเห็นนี้ใช่หรือไม่'))
			return;
		var tmp_ct = $(this).parents('.comment_content');
		var tmp_name = $(this).attr('name');
		$.ajax({
		type: "GET",
		url: ""+$(this).attr("href"),
		data: "rand="+Math.random(),
		beforeSend: function(){$('a:[name='+tmp_name+']').hide(); $('a:[name='+tmp_name+']').after("<img src='/images/spin.gif' />");},
		success: function(msg){
				if(msg.substring(0, 2)=='OK'){
					tmp_ct.remove();
				}
			}
		});
	});
}
