function reply_textarea_focus(reply_data_id) {
	var t = document.getElementById('reply_textarea_'+reply_data_id);
	if(t.value == array_lang['203']+'...') t.value = '';
	t.className = 'reply_textarea_on';
	document.getElementById('reply_form_cmd_'+reply_data_id).style.display = 'block';
}
function reply_textarea_blur(reply_data_id) {
	var t = document.getElementById('reply_textarea_'+reply_data_id);
	if(t.value == '') {
		t.value = array_lang['203']+'...';
		t.className = 'reply_textarea_off';
		document.getElementById('reply_form_cmd_'+reply_data_id).style.display = 'none';
	}
}
function reply_paging(container_id,reply_data_id,page) {
	page = page ? page : 1;
	$.get('/com/reply_paging.php?container_id='+container_id+'&data_id='+reply_data_id+'&page='+page,function(r) {
		document.getElementById(container_id).innerHTML = r;
	});
}
function reply(reply_data_id) {
	if(user_id == '') {alert(array_lang['205']); return; } // '답글을 쓰려면 로그인해야 합니다.'
	var t = document.getElementById('reply_textarea_'+reply_data_id);
	if(t) {
		t.focus();
	}
	else {
		var w = $('#reply_list_'+reply_data_id).width()-75;
		$('#reply_list_'+reply_data_id).after(
			'<table id="reply_form_'+reply_data_id+'" border="0" cellpadding="0" cellspacing="0" class="reply_form">'+
			'<tr valign="top">'+
				'<td class="reply_form_left"><img src="'+user_sm+'" border="0" alt=""/></td>'+
				'<td class="reply_form_right">'+
					'<textarea id="reply_textarea_'+reply_data_id+'" style="width:'+w+'px;" onfocus="reply_textarea_focus(\''+reply_data_id+'\')" class="reply_textarea_off"></textarea>'+
					'<div id="reply_form_cmd_'+reply_data_id+'" class="reply_form_cmd">'+
						'<input type="button" value="&nbsp;'+array_lang['38']+'&nbsp;" id="reply_submit_'+reply_data_id+'" onclick="reply_submit(\''+reply_data_id+'\')" />'+
						'<input type="button" value="&nbsp;'+array_lang['30']+'&nbsp;" id="reply_cancel_'+reply_data_id+'" onclick="reply_cancel(\''+reply_data_id+'\')" />'+
					'</div>'+
				'</td>'+
			'</tr>'+
			'</table>'
		);
		$('#reply_textarea_'+reply_data_id).simpleautogrow();
		document.getElementById('reply_textarea_'+reply_data_id).focus();
	}
}
function reply_submit(reply_data_id) {
	var t = document.getElementById('reply_textarea_'+reply_data_id);
	var v = trim(t.value);
	if(v == '') { t.focus(); return; }

	var btn1 = document.getElementById('reply_submit_'+reply_data_id);
	var btn2 = document.getElementById('reply_cancel_'+reply_data_id);
	btn1.disabled = true;
	btn2.disabled = true;

	$.post('/com/reply_commit.php', {crud:'c',data_id:reply_data_id,note:v}, function(r) {
		if(r == '401') {
			error_handler(r);
		}
		else {
			if(r == '412') {
				error_handler(r);
			}
			else {
				reply_counter_update(reply_data_id,1);
				var sm = r.user_sm ? r.user_sm : '/img/anonymous.png';
				$('#reply_list_'+reply_data_id).append(
					'<table id="reply_'+r.reply_id+'" border="0" cellpadding="0" cellspacing="0" class="reply_item">'+
					'<tr valign="top">'+
					'<td class="reply_item_left"><img src="'+sm+'" border="0" alt=""/></td>'+
					'<td class="reply_item_right">'+
						'<a href="/user/?user_id='+r.user_id+'" class="reply_item_name">'+r.user_name+'</a> '+
						'<span class="reply_item_date">'+r.insert_date+'</span> '+
						' - <a href="javascript:void(0)" onclick="inedit(\'reply-'+r.reply_id+'-note\')">'+array_lang['21']+'</a>'+
						' - <a href="javascript:void(0)" onclick="reply_delete(\''+r.data_id+'\',\''+r.reply_id+'\')">'+array_lang['61']+'</a>'+
						'<div id="reply-'+r.reply_id+'-note" class="reply_item_note">'+r.note+'</div>'+
					'</td>'+
					'</tr>'+
					'</table>'
				);
			}
			t.value = '';
			btn1.disabled = false;
			btn2.disabled = false;
			document.getElementById('reply_textarea_'+reply_data_id).style.height = '24px';
			reply_textarea_blur(reply_data_id);
		}
	},'json');
}
function reply_cancel(reply_data_id) {
	document.getElementById('reply_textarea_'+reply_data_id).value = '';
	reply_textarea_blur(reply_data_id);
}
function reply_delete(reply_data_id,reply_id) {
	if(confirm(array_lang['201'])) {
		$.post('/com/reply_commit.php', {crud:'d',data_id:reply_data_id,reply_id:reply_id}, function(r) {
			if(r == '401') {
				error_handler(r);
			}
			else {
				if(r == '412') {
					error_handler(r);
				}
				else {
					reply_counter_update(reply_data_id,-1);
					$('#reply_'+reply_id).fadeOut('slow',function() {
						$('#reply_'+reply_id).empty().remove();
					});
				}
			}
		});
	}
}
function reply_counter_update(reply_data_id,amount) {
	var c = document.getElementById('reply_counter_'+reply_data_id);
	if(c) c.innerHTML = intval(c.innerHTML)+amount;

	var el = document.getElementById('reply_counter');
	if(el) el.innerHTML = '('+number_format(intval(el.innerHTML)+amount)+')';
}
function reply_more(reply_data_id) {
	$('#reply_more_'+reply_data_id).empty().remove();
	reply_paging('reply_list_'+reply_data_id,reply_data_id);
}