﻿//<![CDATA[

/*
var objPage = new PageNavi();
var arrOp = {
 css:"reply_box"
,pager:objPage
};
*/
function reply(arrOp) {
	this.arrReply = undefined;
	this.intPage = 1;
	this.intPageSize = 10;
	this.intPageCount = 1;
	this.intRecordCount = 0;
	this.intIdx = null;
	this.blnNew = 0;

	for (var itm in arrOp) 
		eval ("this." + itm + "=arrOp." + itm);
}

reply.prototype.run = function() {
	if ((BD_INF.opt & 2) != 2) 
		return;

	if (MEMINF.login) {
		$("a.btn_upload_reply")
			.click(function() {
				return rep._save();
			})
			.keypress(function () {
				return this.click();
			});
		$("#txtReply")
			.keypress(function(e) {
				var key = _PAGEINFO.name == "MSIE" ? event.keyCode : e.which;
				if (key == 13) {
					$("a.btn_upload_reply").click();
					return false;
				}
				return true;
			});
		$("p.reply_write").show();
	}

	$("div.reply_box").show();
	this._get();
}

// 댓글 리스트 보여줌
reply.prototype._list = function() {
	if (!this.arrReply) {
		//alert("댓글 정보가 없습니다.");
		return;
	}
	var arr = this.arrReply;
	var str = "";
	for (var i = 0; i < arr.length; i++) {
		str += "<dl>"
		 + "<dt><label>" + arr[i].writer + "</label>"
		 + "<span>" + arr[i].writedate + "</span></dt>"
		 + "<dd><label>" + arr[i].contents + "</label>";

		// 자신이 쓴 댓글이 맞으면.. 조건 추가
		if ((MEMINF.login && MEMINF.memid == arr[i].id) || MEMINF.admin)
			str += "<a href=\"javascript:;\" "
				+ "onclick=\"" + this.id + "._delete(" + arr[i].idx + ");\">삭제</a>";

		str += "</dd></dl>";
	}
	$("div." + this.css)
		.find("dl").remove()
		.end()
		.find("label + img").after(str);

	$("label.reply_title").html("덧글 " + this.intRecordCount + "개");
	if (this.blnNew)
		$("label.reply_title").next().show();
}

// 페이징 보여줌
reply.prototype._page = function() {
	if (!this.pager) 
		return;

	with (this.pager) {
		currentPage = this.intPage;
		pageCount = this.intPageCount;
		goFunction = this.id + "._get";
		$("ul.paging").html(MakeNavi());
	}
}

// 댓글 리스트 가져옴
reply.prototype._get = function(intPage) {
	if (intPage)
		this.intPage = intPage;
	var obj = this;
//	$("div." + this.css + " dl").slideUp(0,function() {
		$.post(
			 "/board/ajax/getReplyList.asp"
			, { bid: BD_INF.bid
				, idx: obj.intIdx
				, page: obj.intPage
				, size: obj.intPageSize
			}
			, function(data) {
				// alert(data);
				eval("var result=" + data);
				if (result.err) {
					alert(result.err);
					return;
				}
				obj.arrReply = result.data;
				obj.intRecordCount = result.rc;
				obj.blnNew = result.isnew;
				obj.intPageCount = Math.floor((obj.intRecordCount - 1) / obj.intPageSize) + 1;
				obj._page();
				obj._list();
				obj._reset();
//				$("div." + this.css + " dl").hide(100, function() {
//					slideDown(300);
//				});
			}
		);
//	});
}

// 댓글 저장
reply.prototype._save = function() {
	var obj = this;
	if ($("#txtReply").val() == "") {
		alert("댓글이 입력되지 않았습니다.");
		$("#txtReply").focus();
		return false;
	}
	if (!confirm("정말 댓글을 등록하시겠습니까?")) 
		return false;
	$.post(
		 "/board/ajax/getUniqueCode.asp"
		,{	 bid:BD_INF.bid
			,idx:this.intIdx	}
		,function(data) {
			eval("var result=" + data);
			if (result.err) {
				alert(result.err);
				return false;
			}
			$.post(
				 "/board/ajax/replySave.asp"
				, { bid: BD_INF.bid
					, idx: obj.intIdx
					, contents: $("#txtReply").val()
					, code:result.msg
				}
				, function(data) {
					try {
						eval("var result=" + data);
						if (result.err) {
							alert(result.err);
							return false;
						}
						alert(result.msg);
						obj.intPage = 1;
						obj._get();
					}catch(e){alert("ㅇㅇ?");}
				}
			);
		}
	);
	return false;
}

// 댓글 삭제
reply.prototype._delete = function(intIdx) {
	if (!confirm("정말 삭제하시겠습니까?")) 
		return false;
	var obj = this;
	$.post(
		 "/board/ajax/replyDelete.asp"
		, {idx:intIdx}
		, function(data) {
			try {
				eval("var result=" + data)
				if (result.err) {
					alert(result.err);
					return;
				}
				alert(result.msg);
				obj._get();
			}catch(e) {}
		}
	);	
}

reply.prototype._reset = function() {
	$("#txtReply").val("");
}

//]]>
