/**
 * 리플을 제어하는 클래스
 * 
 * @author Firen
 * @version 0.0.0.5
 */
var Reply = function(num){
	var out = "<div id='reply[" + num + "]' class='reply'><p><center><img src='/images/loading.gif' /><br />Now Loading...</center></p></div>";
	document.write(out);
	this.num = num;
	this.div = document.getElementById("reply[" + num + "]");
	this.load();
};

/**
 * 로딩 화면을 출력함
 */
Reply.prototype.loading = function(){
	this.div.innerHTML = "<p><center><img src='/images/loading.gif' /><br />Now Loading...</center></p>";
}

/**
 * 리플을 불러들임
 */
Reply.prototype.load = function(){
	var num = this.num;
	var div = this.div;
	var reply = this;
	this.loading();
	new Ajax.Request("/board/reply.php", {
		parameters:'id=' + num,
		onComplete:function(responseHttpObj){
			var doc	= responseHttpObj.responseXML.getElementsByTagName("replys")[0];
			var replys	= doc.getElementsByTagName("reply");
			var write	= parseInt(doc.getAttribute("write"));
			
			if(!doc){
				div.innerHTML = "리플을 가저오는데 실패하였습니다.";
				return false;
			}
			
			var out = "";
			var id, top, nick, reg, text;
			var style = "";
			if(replys.length > 0){
				out = "<table>";
				for(var i=0; i<replys.length; i++){
					id = replys[i].getAttribute("id");
					top = parseInt(replys[i].getAttribute("top"));
					user = replys[i].getAttribute("user");
					name = replys[i].getAttribute("name");
					nick = replys[i].getAttribute("nick");
					del = replys[i].getAttribute("del");
					reg = replys[i].getAttribute("reg");
					text = reply.nodeValue(replys[i]);
					text = text.replace(/\n/g, "<br />");
					name = name && name != "null" ? name : nick;
					
					if(!top)
						text = "<img src='../images/btn_reply.gif' /> " + text;
					
					if(i + 1 == replys.length)
						style = " nonunderline";
					
					out += "<tr>" +
							"<td class='name" + style + "'>";
					
					out += user ? "<a href='/member/profile.php?id=" + user + "' target='_blank'>" + name + "</a>" : name;
					
					out +=
							"</td>" +
							"<td class='text" + style + "'>" + text + " <span class='reg'>(" + reg + ")</span>" + "</td>" +
							"<td class='btn" + style + "'>";
					
					if(write && top)
						out += 
							"<a title='리플 달기' href='#' onclick='return reply[" + num + "].openReply(" + id + ");'><img src='../images/btn_reply_reply.gif' /></a> ";
					
					if(del)
						out += 
							"<a title='수정' href='#' onclick='return reply[" + num + "].openUpdate(" + id + ");'><img src='../images/btn_reply_edit.gif'/></a> " +
							"<a title='삭제' href='#' onclick='return reply[" + num + "].del(" + id + ");'><img src='../images/btn_reply_del.gif'/></a> ";
					out +=
							"</td>" +
						"</tr>";
				}
				out += "</table>";
			}else if(!write)
				div.style.display = "none";
			
			if(write){
				out +=
					"<form id='formReply" + num + "' method='post' onsubmit='return reply[" + num + "].insert();'>" +
						"<textarea name='text'></textarea>" +
						"<input type='image' src='../images/btn_check.gif' />" +
					"</form>";
			}
			
			div.innerHTML = out;
		}
	});
}

/**
 * 리플을 삽입함
 * 
 * @param num
 */
Reply.prototype.insert = function(num){
	var textarea = this.div.getElementsByTagName("textarea")[0].value;
	var reply = this;
	if(textarea)
		new Ajax.Request("/board/replyInsertProcess.php", {
			parameters:Form.serialize("formReply" + this.num) + "&id=" + reply.num,
			onComplete:function(responseHttpObj){
				var doc = responseHttpObj.responseText;
				
				if(doc)
					alert(doc);
				
				reply.load();
			}
		});
	else
		alert("리플을 입력해 주세요");
	return false;
}

/**
 * 리플 창을 띄움
 * 
 * @param id
 */
Reply.prototype.openReply = function(id){
	window.open("/board/replyInsert.php?id=" + this.num + "&reply=" + id, 'window', "width=500, height=280, location=no, directories=no, resizable=no, status=no, toolbar=no, menubar=no, scrollbars=no");
	
	return false;
}

/**
 * 업데이트 창을 띄움
 * 
 * @param id
 */
Reply.prototype.openUpdate = function(id){
	window.open("/board/replyUpdate.php?id=" + this.num + "&reply=" + id, 'window', "width=500, height=280, location=no, directories=no, resizable=no, status=no, toolbar=no, menubar=no, scrollbars=no");
	
	return false;
}

/**
 * 리플을 삭제함
 */
Reply.prototype.del = function(num){
	if(!confirm('삭제 하시겠습니까?'))
		return false;
	var reply = this;
	this.loading();
	new Ajax.Request("/board/replyDelete.php", {
		parameters:'id=' + num,
		onComplete:function(responseHttpObj){
			var doc = responseHttpObj.responseText;

			if(doc)
				alert(doc);
			
			reply.load();
		}
	});
	return false;
}

/**
 * 주어진 엘레멘탈의 값을 반환
 */
Reply.prototype.nodeValue = function(element) {
	if(element.text)
		return element.text;
	return element.textContent;
}
