Mys.KMAView = function(config)
{
	Mys.KMAView.superclass.constructor.call(this, config);
};

Ext.extend(Mys.KMAView, Mys.TemplateView, {
	id : "item_id",
	
	className : "mys-view-item",
	
	elTemplate : (new Ext.Template([
		'<div class="{bodyClassName}"></div>',
		'<table class="mys-view-footer" width="100%" border="0" cellspacing="0" cellpadding="0">',
			'<tr>',
				'<td height="8"></td>',
			'</tr>',
			'<tr>',
				'<td style="padding-left:10px;"><a name="reply" class="mys-reply-total txt1244b"></a><img src="/images/common/line_devidec.gif" width="9" height="9" hspace="5" align="absmiddle" /></td>',
				'<td align="right" style="padding-right:5px;"><a class="mys-item-agree" href="javascript:void(0);" onfocus="blur()"><img src="/images/common/btn_recom.gif" width="54" height="17" border="0" /></a><img src="/images/common/line_devide.gif" width="1" height="16" /><a class="mys-item-print" href="javascript:void(0);" onfocus="blur()"><img class="mys-item-scrap-img"src="/images/common/btn_print.gif" width="64" height="17" border="0" /></a><img src="/images/common/line_devide.gif" width="1" height="16" /><a class="mys-item-scrap" href="javascript:void(0);" onfocus="blur()"><img class="mys-item-scrap-img"src="/images/common/btn_scrabview.gif" width="64" height="17" border="0" /></a></td>',
			'</tr>',
			'<tr>',
				'<td height="5"></td>',
			'</tr>',
		'</table>',
		'<div class="mys-reply-list"></div>',
		'<table class="mys-pre-next" cellspacing="0" cellpadding="0" width="100%">',
			'<tr>',
				'<td class="c" style="padding:15px 0 20px 0" colspan="2"><a href="javascript:;" class="mys-item-list"><img src="/images/mem_ceo/btn_list.gif"></a></td>',
			'</tr>',
		'</table>',
		'<div class="{loadingClassName}"><img class="mys-loading-img" src="/images/common/loadings.gif"/></div>'
	])).compile(),
	
	bodyTemplate : (new Ext.Template([
		'<table width="100%" border="0" cellspacing="0" cellpadding="0">',
			'<tr>',
				'<td colspan="1">',
					'<div id="view_top_left">{admin}</div>',
				'</td>',
				'<td colspan="1">',
					'<div id="view_top_right">',
			    	'<a class="mys-item-list" onfocus="blur()" href="javascript:;"><img width="47" hspace="5" height="17" src="/images/common/btn_list.gif"/></a>',
					'</div>',
				'</td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" style="border:solid 5px #e8e8e8; padding:5px 0 5px 0"><table width="100%" border="0" cellspacing="0" cellpadding="0">',
					'<tr>',
						'<td width="50" height="30" align="center" class="mys-item-agree"><div class="score" title="추천수">{agree_count}</div><span><img src="/images/chuchun.gif" width="19" height="11"></span></td>',
						'<td width="5" bgcolor="#e8e8e8">&nbsp;</td>',
						'<td style="padding-left:10px" class="subject">{title}</td>',
						'<td width="67" align="center" class="view_event_resv">{acceptUp}</td>',
					'</tr>',
				'</table></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">',
					'<tr>',
						'<td width="55"><img src="../images/common/stit_schedule.gif" width="39" height="13" hspace="5" /></td>',
						'<td class="txt1266">{dateRange}</td>',
						'<td width="50"><img src="/images/overseas/stit_limit.gif" width="53" height="13" /></td>',
						'<td width="190" class="txt1266">{applyDate}</td>',
					'</tr>',
				'</table></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="1" bgcolor="#e8e8e8"></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">',
					'<tr>',
						'<td width="55"><img src="../images/common/stit_place.gif" width="39" height="13" hspace="5" /></td>',
						'<td class="txt1266">{location}</td>',
						'<td width="50" ><img class = "replytitle" src="../images/common/stit_comment.gif" width="39" height="13" /></td>',
						'<td width="190" class="num mys-item-reply"><span>{reply_count}</span><a href="#reply"><img src="../images/common/btn_viewcomment.gif" width="47" height="15" hspace="5" border="0" align="absmiddle" /></a></td>',
					'</tr>',
				'</table></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="1" bgcolor="#e8e8e8"></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">',
					'<tr>',
						'<td width="55"><img src="../images/common/stit_speaker.gif" width="39" height="13" hspace="5" /></td>',
						'<td class="txt1266">{lecturer}</td>',
						'</tr>',
				'</table></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="1" bgcolor="#e8e8e8"></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="30" style="padding:8px 0 8px 0" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">',
					'<tr>',
						'<td width="55" valign="top"><img src="../images/common/stit_summary.gif" width="39" height="13" hspace="5" /></td>',
						'<td class="subcontent">{summary}</td>',
						'</tr>',
				'</table></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" height="2" bgcolor="#e8e8e8"></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" align="right" style="padding-right: 5px;height : 30px;"><a href="{linkURL}" class="s11 c88 link">{linkURL}</a> <a class="copy" href="javascript:void(0);"><img src="/images/common/btn_coppyaddress.gif" width="49" height="17" hspace="5" border="0" align="absmiddle" /></a></td>',
			'</tr>',
			'<tr>',
				'<td colspan="2" class="contents"><div class="view-static-contents">{content}</div></td>',
			'</tr>',
			'{acceptDown}',
			'<tr>',
				'<td colspan="2" height="20">&nbsp;</td>',
			'</tr>', 
		'</table>',
		'{fileTable}',
		'<table style="margin : 4px 0px 0px 0px;"width="100%" border="0" cellspacing="0" cellpadding="0">',
		'<tr>',
			'<td width="43" height="30" align="center" bgcolor="#f0f0f1"><img src="../images/common/icon_tag.gif" width="28" height="13" /></td>',
			'<td bgcolor="#f0f0f1" class="txt1244">{tags}</td>',
		'</tr>',
		'</table>'
	])).compile(),
	
	footerTemplate : (new Ext.Template([

	])).compile(),
/*
	fileTemplate : (new Ext.Template('<div class="added_file_box">{file_type}<img class="added_file_img1"src="/images/common/icon_file.gif" width="10" height="13" align="absmiddle" /><a class="added_file_link" href="{url}" target="_blank">{name}</a><a class="added_file_img2_a" href="{url}" target="_blank"><img class="added_file_img2"src="/images/common/btn_filedown.gif" width="47" height="15" hspace="0" border="0" align="absmiddle" /></a></div>')).compile(),
	fileTableTemplate : (new Ext.Template([
		'<table>',
			'<tr>',
				'<td style="width:49px; padding : 8px 0px; background:#e8e8e8;"><img src="/images/common/stit_file.gif" width="49" height="9" /></td>',
				'<td class="file">{files}</td>',
			'</tr>',
		'</table>'
	])).compile(),
*/

	fileTemplate : (new Ext.Template([
		'<li>{file_type}</li>',
		'<li class="btnready"><a href="{url}">{btn_down}</a></li>'
	])).compile(),

	fileTableTemplate : (new Ext.Template([
		'<div class="edu_view">',
			'<ul>',
			'{files}',
			'</ul>',
		'</div>'
	])).compile(),
	
	tagTemplate : (new Ext.Template('<a class="txt1166" href="/search/search.htm?keyword={keyword}" value={tag_id}>{tag_name}</a>')).compile(),
	modifyURL : "/admin/k_semi_write.htm",
	
	createTags : function(tag)
	{
		var tag = this.safeArray(tag);
		
		for(var i = 0, tags = []; i < tag.length; i++)
		{
			tag[i].keyword = tag[i].tag_name.encode();
			tags.push(this.tagTemplate.apply(tag[i]));
		}
		
		tagHTML = tags.length ? tags.join(", ") : "태그없음";
		
		return tagHTML;
	},
	
	createFiles : function(data)
	{
		for(var i = 1, files = [], isFirst = true; i <= 5; i++)
		{
			if(data["file" + i])	
				files.push(this.fileTemplate.apply({url : data["file" + i], name : data["file" + i].split("/").pop().decode()}));
		}
		
		return files.length ? this.fileTableTemplate.apply({files : files.join('<div class="under-line"></div>')}) : '';
	},
	
	createAdmin : function(data)
	{
		if(data.board_id==18)
			this.modifyURL = "/admin/leadership_notice_write.htm";
		if(Global.Session.isAdmin())
		{
			var tag = this.safeArray(data.tag);
			
			for(var i = 0, tags = []; i < tag.length; i++)
				tags.push(tag[i].tag_name);
			var admin = '' +
				'<a href="' + this.modifyURL + '" onfocus="blur()"><img src="/images/common/btn_writesemi.gif" width="57" height="17" hspace="5" /></a>' +
				'<a href="' + this.modifyURL + '?item_id=' + data.item_id + '" onfocus="blur()"><img src="/images/common/btn_fixsemi.gif" width="32" height="17" /></a>' +
				'<img src="/images/common/line_devidebtn.gif" width="1" height="17" />' +
				'<a href="#" onfocus="blur()" class="mys-item-del"><img src="/images/common/btn_delsemi.gif" width="32" height="17" /></a>' +
				' <img src="/images/common/line_devidebtn.gif" width="1" height="17" /> ' +
				'<span class="show-index">인덱스에 보이기</span><input type="checkbox" class="mys-item-default" ' + (data.default_flag ? 'checked' : '') + '/>' +
				' <img src="/images/common/line_devidebtn.gif" width="1" height="17" /> ' +
				'<form class="mys-tag-modify" style="display:inline" onsubmit="return false;"><input type="text" name="tag" value="' + tags.join(", ") + '"/><button type="submit">수정</button></form>';
			return admin;
		}
		else
			return '';
	},
	
	createLink : function(data)
	{
		return "http://" + location.hostname + ViewManager.getViewURL(data);
	},
	
	createAcceptUp : function(data)
	{
		if(data.accept_url)
			return '<a href="' + data.accept_url + '"><img src="../images/common/btn_applysemis.gif"  width="57" height="17" hspace="5" border="0" /></a>';
	},
	
	createAcceptDown : function(data)
	{
		if(data.accept_url)
			return '<tr><td colspan="2" align="center"><a href="' + data.accept_url + '"><img src="../images/k_semi/btn_applysemi.gif" width="126" height="30" border="0" /></a></td></tr>';
	},
	
	prepareData : function(data)
	{
		data.page = (Math.floor(this.safeNumber(data.start) /10) + 1) || this.page;
		
		data.className = this.className;
		
		data.title = data.title || "제목없음";
		data.summary = data.summary || "내용없음";
		
		data.read_count = this.safeNumber(data.read_count);
		data.agree_count = this.safeNumber(data.agree_count);
		data.reply_count = this.safeNumber(data.reply_count);
		
		var startDate = Date.parseDate(data.start_date, "Y-m-d H:i:s");
		var endDate = Date.parseDate(data.end_date, "Y-m-d H:i:s");
		if(startDate && endDate)
		{
			data.dateRange = startDate.format("y년 n월 j일");
			
			if(startDate.format("ymd") == endDate.format("ymd"))
				data.dateRange += " " + startDate.format("H:i") + " ~ " + endDate.format("H:i");
			else if(startDate.format("ym") == endDate.format("ym"))
				data.dateRange += " ~ " + endDate.format("j일");
			else if(startDate.format("y") == endDate.format("y"))
				data.dateRange += " ~ " + endDate.format("n월 j일");
			else
				data.dateRange += " ~ " + endDate.format("y년 n월 j일");
		}
		
		var applyDate = Date.parseDate(data.apply_date, "Y-m-d H:i:s");
		if(applyDate)
			data.applyDate = applyDate.format("y년 n월 j일 h시");
		
		data.tags = this.createTags(data.tag);
		data.fileTable = this.createFiles(data);
		data.admin = this.createAdmin(data);
		data.linkURL = this.createLink(data);
		data.acceptUp = this.createAcceptUp(data);
		data.acceptDown = this.createAcceptDown(data);
		
		return data;
	},
	
	render : function(el)
	{
		if(Mys.KMAView.superclass.render.call(this, el) === false)
			return false;
		
		this.replyList = new Mys.ReplyList;
		this.replyList.render(this.el.child(".mys-reply-list"));
		this.replyTotalEl = this.el.child(".mys-reply-total");
		this.replyList.on("update", function(list, data, totalRow){
			if(this.replyTotalEl)
				this.replyTotalEl.update('댓글(' + (totalRow || 0) + ')');
		}, this);
		
		this.footer = this.el.child(".mys-view-footer");
		
		if(this.footer)
		{
			this.footer.select(".mys-item-agree").on("click", this.agree, this);
			this.footer.select(".mys-item-scrap").on("click", this.scrap, this);
			this.footer.select(".mys-item-print").on("click", this.print, this);
		}
		this.preNextEl = this.el.child(".mys-pre-next", true);
	},
	
	safeSize : function(el)
	{
		if(!el)
			return;
		var cwidth = el.getWidth();
		
		el.select("img").each(function(el){
			var width = el.getWidth();
			
			if(width > cwidth)
			{
				el.setWidth(cwidth);
				el.setHeight(Math.floor((cwidth / width) * el.getHeight()));
			}
		}, this);
	},
	
	setEvents : function(el)
	{
		this.safeSize(el.child(".view-static-contents"));
		
		el.select(".mys-item-agree").on("click", this.agree, this);
		this.el.select(".mys-item-list").each(function(el){
			el.dom.href = ViewManager.getURL(this.data) + "#state=list&page=" + (this.data.page || 1) + "&board_id=" + this.data.board_id;
		}, this);
		
		if(Global.Session.isAdmin())
		{
			this.el.child(".mys-item-del").on("click", this.del, this);
			this.el.child(".mys-item-default").on("click", this.setDefault, this);
			this.el.child(".mys-tag-modify").on("submit", this.modifyTag, this);
		}
		
		if(Ext.isIE)
		{
			this.linkEl = el.child(".link");
			
			this.copyEl = el.child(".copy");
				
			this.copyEl.on("click", this.copy, this);
		}
		if(this.replyList)
		{
			this.replyList.setItemData(this.data);
			this.replyList.update(this.data.reply, this.data.reply_total);
		}
	},
	
	showTagForm : function()
	{
		this.el.child(".mys-tag-btn").setStyle("display", "none");
		this.el.child(".mys-tag-list").setStyle("display", "none");
		this.el.child(".mys-tag-modify").setStyle("display", "");
	},
	
	hideTagForm : function()
	{
		this.el.child(".mys-tag-btn").setStyle("display", "");
		this.el.child(".mys-tag-list").setStyle("display", "");
		this.el.child(".mys-tag-modify").setStyle("display", "none");
	},
	
	modifyTag : function()
	{
		Global.modelItem.modifyTag(this.data.item_id, this.el.child(".mys-tag-modify").dom.tag.value, function(){window.location.reload();});
	},
	
	agree : function()
	{
		if(!Global.Session.checkLogin(ViewManager.getViewURL(this.data)))
			return ;
		
		if(this.data.agree_date)
		{
			alert("이미 추천하였습니다.");
			return false;
		}
		Global.modelItem.agree(this.data.item_id, this.onAgree, this);
	},
	
	copy : function()
	{
		if(this.linkEl)
		{
			window.clipboardData.setData("Text", this.linkEl.dom.innerHTML.entitiesDecode());
			alert("복사되었습니다. 붙여넣기 또는 Ctrl + V를 눌러 원하는 곳에 넣어주세요");
		}
	},
	
	onAgree : function(data)
	{
		if(data && data.response.code == 100)
		{
			var ac = this.el.child(".score");
			
			ac.update(this.safeNumber(ac.dom.innerHTML) + 1);
			
			this.data.agree_date = 1;
			
			alert("추천하였습니다.");
		}
	}
	
	,scrap : function(e, el) {
		if(!Global.Session.checkLogin(ViewManager.getViewURL(this.data)))
			return ;
		
		if(this.data.scrap_date)
		{
			alert("이미 스크랩하였습니다.");
			return false;
		}
		
		if(!window.ScrapDlg)
			ScrapDlg = new Mys.Scrap();
		
		ScrapDlg.setData(this.data);
		ScrapDlg.show(el, "br-tr");
	}
	
	,print : function()
	{
		var url = "event_print.htm?item_id=" + this.data.item_id;
		window.open(url,"" , "popup=1,width=800, height=768, top=0, left=0, location=0, toolbar=0, directories=0, status=0, menubar=1, scrollbars=1, resizable=1");
	},
	
	goList : function()
	{
		if(!KMAList.rendered)
			KMAList.render("kma-list");
		
    KMAList.pager.setPage(Math.floor(this.safeNumber(this.start) /10) + 1);
    
		this.el.setStyle("display", "none");
		
    KMAList.el.show();
	},
	
	loadView : function(itemID, loadStart)
	{
		var list;
		
		if(KMAList.rendered)
			KMAList.el.setStyle("display", "none");
		
		this.render("kma-view");
		
		this.page = KMAList.pager.page;
		
		this.startLoading();
		
		this.el.show();
		
		if(loadStart)
			list = KMAList;
		
		Global.modelItem.get(itemID, list, this.onLoadView, this);
	},
	
	onLoadView : function(data)
	{
		if(data && data.response.code == 100)
		{
			this.update(data.response);
			
			if(window.relationList && Ext.get("relation-list"))
			{
				//최신 댓글 -> 관련 후기
				if(!relationList.rendered)
					relationList.render("relation-list");
				//css 가 넘 더러워서 일단 자바로 마진잡음 by saint
				Ext.get("relation-list").dom.style.marginBottom = "5px";
				
				relationList.load(data.response);
				relationList.el.show();
			}
		}
	},
	
	del : function(){
		if(confirm("정말 삭제하시겠습니까?")){
			Global.modelItem.del(this.data.item_id, function(data){
				if(data && data.response.code == 100)
					this.goList();
			}, this);
		}
	},
	
	setDefault : function(e, el)
	{
		Global.modelItem.setDefault(this.data.item_id, el.checked, function(data){
			if(data && data.response.code == 100)
				alert((el.checked ? "설정" : "해제") + "하였습니다.");
		}, this);
	},
	
	startLoading : function()
	{
		if(Mys.KMAView.superclass.startLoading.call(this) === false)
			return false;
		
		if(this.replyList)
			this.replyList.el.dom.style.display = "none";
		if(this.footer)
			this.footer.dom.style.display = "none";
		if(this.preNextEl)
			this.preNextEl.style.display = "none";
	},
	
	endLoading : function()
	{
		if(Mys.KMAView.superclass.endLoading.call(this) === false)
			return false;
		
		if(this.replyList)
			this.replyList.el.dom.style.display = "";
		if(this.footer)
			this.footer.dom.style.display = "";
		if(this.preNextEl)
			this.preNextEl.style.display = "";
	}
});
