var _comment_container_id = '';
var _comment_data_id = '';
var _comment_user_id = '';

function Comment(opt) {
	this.form_container_id = opt.form_container_id ? opt.form_container_id : '';
	this.list_container_id = opt.list_container_id ? opt.list_container_id : '';
	this.data_id = opt.data_id ? opt.data_id : '';
	this.user_id = opt.user_id ? opt.user_id : '';
	this.list = null;
	this.page = 1;
	this.textarea = null;
	this.btn_submit = null;
}
Comment.prototype.load = function() {
	var me = this;

	var form_container = document.getElementById(me.form_container_id);
	if(form_container) {
		var cw = $(form_container).width();
		var ch = $(form_container).height();

		me.textarea = document.createElement('textarea');
		me.textarea.className = 'comment_form_textarea_off';
		me.textarea.style.width = (cw-($.browser.msie ? 8 : 8))+'px';
		me.textarea.value = '';
		me.textarea.onfocus = function() {
			this.value = '';
			this.className = 'comment_form_textarea_on';
		}
		me.textarea.onblur = function() {
			this.className = 'comment_form_textarea_off';
		}
		form_container.appendChild(me.textarea);
		if(login_id == '') {
			me.login_help = document.createElement('div');
			me.login_help.style.padding = '5px';
			me.login_help.innerHTML = '<a href="javascript:void(0)" onclick="login()" class="b">'+array_lang['1']+'</a> &nbsp;|&nbsp; <a href="/" class="b">'+array_lang['3']+'</a>';
			form_container.appendChild(me.login_help);
		}
		else {
			$(me.textarea).simpleautogrow();

			var form_footer = document.createElement('div');
			form_footer.className = 'comment_form_footer';

			me.btn_submit = document.createElement('input');
			me.btn_submit.className = 'button';
			me.btn_submit.type = 'button';
			me.btn_submit.value = array_lang['38'];
			$(me.btn_submit).click(function() {
				me.btn_submit.disabled = true;
				me.create();
			});
			form_footer.appendChild(me.btn_submit);

			me.loading = document.createElement('span');
			me.loading.innerHTML = '&nbsp;<img src="/img/spinner.gif" alt=""/>';
			me.loading.style.display = 'none';
			form_footer.appendChild(me.loading);

			form_container.appendChild(form_footer);
		}
	}
	var list_container = document.getElementById(me.list_container_id);
	me.list = document.createElement('div');
	me.list.className = 'comment_list';
	list_container.appendChild(me.list);

	comment_paging(me.list_container_id,me.data_id,me.user_id,1);
}
Comment.prototype.create = function() {
	var me = this;
	me.loading.style.display = 'inline';
	$.post('/com/comment_commit.php', {crud:'c',data_id:me.data_id,note:me.textarea.value}, function(r) {
		if(r == '401') {
			error_handler(r);
		}
		else {
			if(r == '412') {
				error_handler(r);
			}
			else {
				$('#comment_counter').text(parseInt($('#comment_counter').text(),10)+1);
				var tpage = document.getElementById('comment_paging_tpage').innerHTML;
				comment_paging(me.list_container_id,me.data_id,me.user_id,tpage);
			}
			me.textarea.value = '';
			me.textarea.style.height = '64px';
			me.btn_submit.disabled = false;
			me.loading.style.display = 'none';
		}
	});
}
function comment_paging(container_id,data_id,user_id,page) {
	container_id = container_id ? container_id : _comment_container_id;
	data_id = data_id ? data_id : _comment_data_id;
	user_id = user_id ? user_id : _comment_user_id;
	page = page ? page : 1;
	var cw = $('#'+container_id).width();
	$.get('/com/comment_paging.php?container_id='+container_id+'&data_id='+data_id+'&user_id='+user_id+'&container_width='+cw+'&page='+page,function(r) {
		document.getElementById(container_id).innerHTML = r;
		try {
			$('.reply_textarea_off').simpleautogrow();
		}
		catch(e) {
		}
		_comment_container_id = container_id;
		_comment_data_id = data_id;
		_comment_user_id = user_id;

		var el = document.getElementById('comment_counter');
		if(el) {
			var total = document.getElementById('comment_paging_total').innerHTML;
			el.innerHTML = (el.innerHTML.indexOf('(') > -1) ? '('+total+')' : total;
		}
	});
}
function comment_delete(img,comment_id,page) {
	if(confirm(array_lang['201'])) {
		img.src = '/img/spinner.gif';
		$.post('/com/comment_commit.php',{crud:'d',comment_id:comment_id},function(r) {
			if(r == '401') {
				error_handler(r);
			}
			else {
				if(r == '412') {
					error_handler(r);
				}
				else {
					//--------------------------------------------------------------------------
					// ¸¶ÀÌ ÆäÀÌÁö¿¡¼­ ÄÚ¸àÆ® ¸ñ·ÏÀ» º¸°í ÀÖÀ» ¶§...
					// ÄÚ¸àÆ®¸¦ »èÁ¦ÇÏ´Â °æ¿ì ÁÂÃø ¸ÞÀÎ ¸Þ´ºÀÇ ´ä±Û °¹¼ö Ã³¸®¸¦ À§ÇÑ ÆÄ¶ó¹ÌÅÍ
					//--------------------------------------------------------------------------
					var reply_counter = document.getElementById('reply_counter_'+comment_id) ? document.getElementById('reply_counter_'+comment_id).innerHTML : null;

					if($("#reply_form_"+comment_id)) $("#reply_form_"+comment_id).remove();
					//$('#reply_list_'+comment_id).fadeOut('slow',function() {
						$('#reply_list_'+comment_id).remove();
						$("#comment_"+comment_id).fadeOut('slow',function() {
							$("#comment_"+comment_id).remove();
							var s = $('#comment_counter').text();
							var parentheses = false;
							if(s.indexOf('(') > -1) {
								parentheses = true;
								s = intval(s);
							}
							s = number_format(parseInt(s,10)-1);
							s = parentheses ? '('+s+')' : s;
							$('#comment_counter').text(s);

							var tpage = intval(document.getElementById('comment_paging_tpage').innerHTML); // ÀüÃ¼ ÆäÀÌÁö
							if((page == 1 && tpage > 1) || page > 1) {
								//---------------------------------------------
								// ÇöÀç ÆäÀÌÁöÀÇ ¸ñ·ÏÀ» ¸ðµÎ »èÁ¦ÇÑ °æ¿ì Ã³¸®
								//---------------------------------------------
								var s = $('#comment_list_callback_container').text();
								if(trim(s) == '') comment_paging(null,null,null,page-1);
							}
							try {
								comment_delete_callback(comment_id,reply_counter);
							}
							catch(e) {
							}
						});
					//});
				}
			}
		});
	}
}
