Mys.ResttechItem = function(data, config)
{
	Mys.ResttechItem.superclass.constructor.call(this, data, config);
};


Ext.extend(Mys.ResttechItem, Mys.VacationItem, {
	refClassName : "ref-valuation-item",
	template : (new Ext.Template([
		'<div class="{className}" id="{className}-{reply_id}" >',
			'<span class="nick">{refIcon}{nick}</span><span class="time">{insert_date}</span><br/>',
			'<div class="text">{text} {hasRefBtn}{hasDelBtn}</div>',
		'</div>',
	])).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 valign="top" class="nick-box"><label class="ref-nick-label">닉네임 : </label><input name="nick" class="ref-nick" /></td>',
					'<td valign="top"><textarea name="reply_text" id="textfield2" class="ref-reply-text"></textarea></td>',
					'<td class="mys-Reply-Btn-box">',
						'<button type="submit" class="submit-btn"></button><br/>',
						'<button type="reset" class="reset-btn"></button></td>',
				'</tr>',
				'<tr>',
					'<td height="20" colspan="3" 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.ResttechItem.superclass.prepareData.call(this, data);
		
		data.refIcon = 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>';
			if(data.hasDelBtn)
				data.hasRefBtn += '<img src="/images/common/line_devidec.gif" width="9" height="9" />';
		}
		
		return data;
	},
	
	setEvents : function(el){
		Mys.ResttechItem.superclass.setEvents.call(this, el);
		
		if(this.data.reply_id != this.data.ref_reply_id)
			el.addClass(this.refClassName);
		el.select(".mys-reply-refBtn").on("click", this.onRefBtnClick, 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.reply_text).on("keyup", function(e, el){
				this.textLengthEl.update(el.value.getByte());
			}, this);
			
			this.refEl.on("submit", this.addRef, this);
			this.refEl.on("reset", this.hideRef, this);
		}
		
		this.isShowRef = true;
		
		this.refEl.dom.style.display = "block";
		this.refEl.dom.reply_text.focus();
	},
	
	hideRef : function()
	{
		if(!this.isShowRef)
			return;
		this.isShowRef = false;
		
		this.refEl.dom.style.display = "none";
		
		if(this.textLengthEl)
			this.textLengthEl.update(0);
	},
	
	addRef : function()
	{
		if(this.list.checkLogin && !Global.Session.checkLogin())
			return ;
		
		if(!this.refEl.dom.reply_text.value)
			alert("내용을 입력하세요");
		
		Global.modelEventReply.add(null, this.data.reply_id, this.refEl.dom.nick ? this.refEl.dom.nick.value : '', this.refEl.dom.reply_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.modelEventReply.del(this.data.reply_id, this.onDel, this);
	}
});
