$(function(){
	$("#txt_msg").topCorner();
	$("#txt_msg").bottomCorner();
	$("#fml").submit(function(event){
		event.preventDefault();
		var tmp_id = this.id;
		$(this).addTextarea();
		if($.trim($('#'+tmp_id+' textarea').val()).length==0 || $.trim($('#'+tmp_id+' textarea').val())=='<P>&nbsp;</P>'){
			alert('กรุณากรอกข้อมูล');
		}else{
			$("#fml").ajaxSubmit({
				beforeSubmit: function(){
				$("#send_msg").append("<img src='/images/spin.gif' />").find('input').hide();
				},
				success: function(msg){
					$("#send_msg").find('img').remove();
					$("#send_msg input").show();$("#msg_list").prepend(msg); addNewAction(); 
					$("#"+tmp_id).clearTextarea();
				}
			});
		}
	});
	$("#message").rte({
		css: ['/css/rte_default.css'],
		width: 650,
		height: 60,
		controls_rte: rte_toolbar,
		controls_html: html_toolbar
	});
		loadContent("/profile/searchmb/"+$("#userid").val());
		$(".rmb a").click(function(){
			window.location.reload();
		});
});
function loadContent(arg){
	$.ajax({
	type: "GET",
	url: encodeURI(arg),
	data: "rand="+Math.random(),
	beforeSend: function(){$("#msg_list").empty().html('<div class="wait_friend"></div>'); $("html, body").animate({scrollTop:0}, 400);},
	success: function(msg){ $("#msg_list").html(msg); addAction(); }
	});
}
function addAction(){
	commentAction(".fcl form");
	reloadMsg(".reload_msg");
	hideComment(".comment_msg");
	$(".prvpage, .nextpage").click(function(){
		loadContent($(this).attr("alt"));
	});
	$(".subpage a,  #lnpage a").click(function(event){
		event.preventDefault();
		loadContent($(this).attr("href"));
	});
	deleteTopic($("body"));
	deleteComment($("body"));
	addSpan();
	gotopage();
}
function addNewAction(){
	commentAction(".fcl:first form");
	reloadMsg(".reload_msg:first");
	addSpan();
	deleteTopic($(".msg_list_content:first"));
}
function hideComment(arg){
	$(arg).each(function(){
		var tmp_s = $(this).children('.comment_msg_det').size();
		if(tmp_s>3){
			$(this).find('.sam').remove().end().prepend('<div class="sam link_color"><a href="javascript:void(0)">แสดงทั้งหมด ('+(tmp_s)+' ความคิดเห็น)</a></div>');
			$(this).children('.comment_msg_det:lt('+(tmp_s-3)+')').hide();
			$(this).find('.sam a').click(function(event){
				event.preventDefault();
				if($(this).text()=='ซ่อนความคิดเห็น'){
					hideComment($(this).parents('div.comment_msg'));
				}else{
					$(this).parents('div.comment_msg').children('.comment_msg_det').show();
					$(this).text("ซ่อนความคิดเห็น");
				}
			});
		}
	});
}
function reloadMsg(arg){
	$(arg).click(function(event){
		event.preventDefault();
		var tmp_msg = $(this).attr("name");
		$.ajax({
		type: "GET",
		url: '/profile/newcomment/'+$('#userid').val()+'/'+tmp_msg+'/',
		data: "rand="+Math.random(),
		beforeSend: function(){$("div:[name=c"+tmp_msg+"]").empty().height(80).html("<img src='/images/spin.gif' style='position:absolute; top:10px; right:330px;' />").next().hide();},
		success: function(msg){ $("div:[name=c"+tmp_msg+"]").height('').empty().html(msg).next().show(); hideComment("div:[name=c"+tmp_msg+"]"); addSpan(); deleteComment($(".msg_list_content:[name=t"+tmp_msg+"]")); $("#b"+tmp_msg).show(); $("#c"+tmp_msg).hide();
		}
		});
	});
}
function commentAction(arg){
	$(arg).prev("input").click(function(){
		var tmp_frm=$(this).next('form').show();
		$('body', $('iframe', tmp_frm).document()).focus();
		$(this).hide();
	});
	$(".cancelme", arg).click(function(){
		$(this).parents('form').hide().prev('input').show();
	});
	$(".tcm", arg).rte({
		css: ['/css/rte_default.css'],
		width: 300,
		height: 45,
		controls_rte: rte_toolbar,
		controls_html: html_toolbar
	});
	$(arg).submit(function(event){
		var tmp_id = this.id;
		$(this).addTextarea();
		if($.trim($("#"+tmp_id+" textarea").val()).length==0  || $.trim($('#'+tmp_id+' textarea').val())=='<P>&nbsp;</P>'){
			alert('กรุณากรอกข้อมูล');
		}else{
			$(this).ajaxSubmit({
				beforeSubmit: function(){
				$("#"+tmp_id).append("<img src='/images/spin.gif' />").find('.bcm').hide();
				},
				success: function(msg){
					$("#"+tmp_id).find('img').remove();
					$('.enable',"#"+tmp_id).trigger('click');
					$("#"+tmp_id+" .bcm").show();
					$("div:[name*="+tmp_id+"]").empty().html(msg);
					hideComment("div:[name*="+tmp_id+"]");
					$("#"+tmp_id).clearTextarea();
					addSpan();
					deleteComment($(".msg_list_content:[name=t"+tmp_id.substring(1)+"]"));
					$("#"+tmp_id).hide().prev('input').show();
				}
			});
		}
		event.preventDefault();
	});
	$(arg).hide();
}
function deleteTopic(arg){
	$(".deltopic",arg).click(function(event){
		var tmp_msg = $(this).attr("name");
		event.preventDefault();
		if(!confirm('ต้องการลบใช่หรือไม่')){
			return;
		}
		$.ajax({
		type: "GET",
		url: '/profile/deletetopic/'+$('#userid').val()+'/'+tmp_msg+'/',
		data: "rand="+Math.random(),
		beforeSend: function(){$("div:[name=t"+tmp_msg+"]").empty().height(120).html("<img src='/images/spin.gif' style='position:relative; top:30px; left:250px;' />");},
		success: function(msg){ $("div:[name=t"+tmp_msg+"]").remove(); }
		});
	});
}
function deleteComment(arg){
	$(".delcomment",arg).click(function(event){
		var tmp_msg = $(this).attr("name");
		event.preventDefault();
		if(!confirm('ต้องการลบใช่หรือไม่')){
			return;
		}
		$.ajax({
		type: "GET",
		url: '/profile/deletecomment/'+$('#userid').val()+'/'+tmp_msg+'/',
		data: "rand="+Math.random(),
		beforeSend: function(){$("div:[name=m"+tmp_msg+"]").empty().height(30).html("<img src='/images/spin.gif' style='position:relative; top:3px; left:200px;' />");},
		success: function(msg){ $("div:[name=m"+tmp_msg+"]").remove(); }
		});
	});
}
