Mys.ReplyItem = function(data, config)
{
	Mys.ReplyItem.superclass.constructor.call(this, data, config);
};

Ext.extend(Mys.ReplyItem, Mys.TemplateItem, {
	id : "reply_id",
	className : "mys-reply-item",
	reportComment : null
	
	,template : (new Ext.Template([
	'<table class="{className}" id="{className}-{reply_id}" style="width:100%">',
		'<tr class="reply-box">',
			'<td  width="90" valign="top" style="padding-left:8px; padding-top:5px">',
				'<div class="name-box">{ref_icon}<span class="name">{nick}</span></div>',
			'</td>',
			'<td width="505" class="ttxt"><div class="reply-box-ttxt">{text}<span style="font-size : 11px;color : #999999;";>&nbsp;&nbsp;&nbsp;{replydate}</span></td>',
			'<td width="60" align="right" style="padding-right:5px">{hasDelBtn}{hasRefBtn}<a class="mys-reply-reportBtn" href="javascript:void(0);" title="신고하기"><img src="../images/common/btn_report.gif" width="10" height="9" border="0" /></a></td>',
		'</tr>',
	'</table>'
	])).compile(),
	
	refTemplate : (new Ext.Template([
		'<form class="mys-reply-ref" style="display:none;", onSubmit="return false;">',
			'<table width="100%" border="0" cellspacing="0" cellpadding="0">',
				'<tr>',
					'<td width="490" valign="top"><textarea name="text" id="textfield2"  style="margin: 2px 5px 0px 99px; width: 470px; height: 49px;"></textarea></td>',
					'<td class="mys-Reply-Btn-box">',
						'<img src="../images/common/btn_writerecomment.gif" class="btn_submit" style="border: medium none ;width:69px;height:25px;" />',
						'<img src="../images/common/btn_cancelrecomment.gif" class="btn_cancel" style="border: medium none ;width:69px;height:25px;" /></td>',
				'</tr>',
				'<tr>',
					'<td height="20" colspan="2" align="right" valign="top" class="txt1188" style="padding-right:5px"><span class="txt11f6b txt11f6c text-length">0</span><span class="txt11f6c">/600 bytes(한글 300자)</span></td>',
				'</tr>',
			'</table>',
		'</form>',
	])).compile(),
	
	isShowRef : false,
	
	prepareData : function(data)
	{
		
		data = Mys.ReplyItem.superclass.prepareData.call(this, data);
		
		data.text = Mys.getSafeString(data.text).replace(/\n/g, "<br/>");
		data.ref_icon = data.reply_id == data.ref_reply_id ? "" : '<img class="reply-icon" src="/images/common/icon_reply.gif" width="9" height="9" align="absmiddle" />';
		
		if(data.ref_reply_id == data.reply_id)
			data.hasRefBtn = '<a class="mys-reply-refBtn" href="javascript:void(0);" title="덧글달기"><img src="/images/common/btn_replys.gif" width="11" height="9" border="0" /></a><img src="/images/common/line_devidec.gif" width="9" height="9" />';
		
		if((Global.Session.isLogin() && Global.Session.getID() == data.user_id) || Global.Session.isAdmin())
			data.hasDelBtn = '<a class="mys-reply-delBtn" href="javascript:void(0);" title="삭제하기"><img src="/images/common/btn_deletes.gif" width="11" height="9" border="0" /></a><img src="/images/common/line_devidec.gif" width="9" height="9" />';
		
		var day =(new Date).format("Y-m-d");
		if(data.insert_date.indexOf(day)==-1) 
		{
			var replyMon = data.insert_date.substr(5,2);
			var replyDay = data.insert_date.substr(8,2);
			data.replydate = replyMon + "월 " + replyDay + "일";
		}
		else
		{
			var replyHour = data.insert_date.substr(11,2);
			var replyMin = data.insert_date.substr(14,2);
			data.replydate = replyHour + " : " + replyMin ;
		}
			
		
		return data;
	},
	
	setEvents : function(el)
	{
		//댓글 버튼
		el.select(".mys-reply-refBtn").on("click", this.onRefBtnClick, this);
		el.select(".mys-reply-delBtn").on("click", this.del, this);
		el.select(".mys-reply-reportBtn").on("click", this.clickReport, this);
	},
	
	onRefBtnClick : function()
	{
		if(this.isShowRef)
			this.hideRef();
		else
			this.showRef();
	},
	
	showRef : function()
	{
		if(this.isShowRef)
			return;
		
		if(!this.refEl)
		{
			this.refEl = this.refTemplate.insertAfter(this.el, {}, true);
			
			this.textLengthEl = this.refEl.child(".text-length");
			
			Ext.fly(this.refEl.dom.text).on("keyup", function(e, el){
				this.textLengthEl.update(el.value.getByte());
			}, this);
		}
		
		this.isShowRef = true;
		
		this.refEl.dom.style.display = "block";
		this.refEl.dom.text.focus();
		
		var btnSubmit = this.refEl.child(".btn_submit").on("click", this.addRef, this);
		var btnCancel = this.refEl.child(".btn_cancel").on("click", this.hideRef, this);
	},
	
	hideRef : function()
	{
		if(!this.isShowRef)
			return;
		this.isShowRef = false;
		
		this.refEl.reset();
		this.refEl.dom.style.display = "none";
		
		if(this.textLengthEl)
			this.textLengthEl.update(0);
	},
	
	addRef : function()
	{
		if(!Global.Session.checkLogin(ViewManager.getViewURL(this.list.itemData)))
			return ;
		
		if(!this.refEl.dom.text.value)
			alert("내용을 입력하세요");
		
		Global.modelReply.add(null, this.data.reply_id, this.refEl.dom.text.value, this.list.maxRow, this.onAddRef, this);
	},
	
	onAddRef : function(data)
	{
		if(!data || data.response.code != 100)
			return false;
		
		this.list.onMove(data);
	},
	
	del : function()
	{
		if(confirm("정말 삭제하시겠습니까?"))
			Global.modelReply.del(this.data.reply_id, this.onDel, this);
	},
	
	onDel : function(data)
	{
		if(data && data.response.code == 100)
			this.list.refresh();
	}
	,clickReport : function(e, el) {
		if(!Global.Session.checkLogin(ViewManager.getViewURL(this.list.itemData)))
			return ;
		
		if(!this.list.reportDlg){
			this.list.reportDlg = Ext.get('report_comment');
			this.list.reportDlg.child('.close-btn').on('click', this.closeReport, this);
			this.list.reportDlg.child('.cancel-btn').on('click', this.closeReport, this);
			this.list.reportDlg.child('.report-btn').on('click', this.reportComment, this);
		}
		if(!this.reportDlg)
			this.reportDlg = this.list.reportDlg;
		
		this.fillReport();
		this.reportDlg.show();
		this.reportDlg.alignTo(el, "br-tl");
	}
	,fillReport : function(){
		var contents = this.reportDlg.child('.reply-contents');
		var nick = this.reportDlg.child('.nick');
		var email = this.reportDlg.child('.email');
		var reportContents = this.reportDlg.child('#reportContents');

		reportContents.dom.value="";
		contents.dom.innerHTML = this.data.text;
		nick.dom.innerHTML = Global.Session.getNick();
		email.dom.innerHTML = Global.Session.getEmail();
	}
	,reportComment : function(){
		if(window.confirm("정말 신고 하시겠습니까?")){
			var reportContents = this.reportDlg.child('#reportContents');
			Global.modelReport.add(this.data.reply_id, reportContents.dom.value,
				this.onReport, this);
		}
	}
	,onReport : function(data){
		if(data && data.response.code == 100){
			alert('신고가 접수되었습니다.');
			this.closeReport();
		}else{
			//alert(data.response.message);
		}
	}
	,closeReport : function(e, el){
		this.reportDlg.hide();
	}
});

Mys.SimpleReplyItem = function(data, config)
{
	Mys.SimpleReplyItem.superclass.constructor.call(this, data, config);
};

Ext.extend(Mys.SimpleReplyItem, Mys.TemplateItem, {
	id : "reply_id",
	className : "mys-reply-item",
	reportComment : null,
	
	prepareData : function(data)
	{
		data = Mys.SimpleReplyItem.superclass.prepareData.call(this, data);
		
		data.ref_icon = data.reply_id == data.ref_reply_id ? "" : '<img class="reply-icon" src="../images/common/icon_reply.gif" width="9" height="9" align="absmiddle" />';
		
		return data;
	},
	template : (new Ext.Template([
	'<table class="{className}" id="{className}-{reply_id}">',
		'<tr class="reply-box">',
			'<td  width="90" valign="top" style="padding-left:10px; padding-top:9px">{ref_icon}',
				'<div><a href="javascript:void(0);" class="name">{nick}</a> <img src="../images/common/icon_trib.gif" width="5" height="3" vspace="5" align="absmiddle" /></div>',
			'</td>',
			'<td width="565" class="ttxt">{text}</td>',
		'</tr>',
	'</table>'
	])).compile()
});
