Mys.KMAItem = function(data, config)
{
	Mys.KMAItem.superclass.constructor.call(this, data, config);
};

Ext.extend(Mys.KMAItem, Mys.TemplateItem, {
	id : "item_id",
	
	titleSize : 455,
	titleUnit : 16,
	summarySize : 515,
	summaryUnit : 12,
	useImage : true,
	
	template : (new Ext.Template([
		'<table class="{className}" id="{className}-{item_id}" width="100%" border="0" cellspacing="0" cellpadding="0">',
			'<tr>',
				'{imageTD}',
				'<td style="padding : 0px;"colspan="{colspan}"><table width="100%" border="0" cellspacing="0" cellpadding="0">',
					'<tr>',
						'<td><table>',
						'<tr>',
							'<td width="100%" height="38" align="center"  class="agree-count"><div style="border:solid 5px #e7e7e7" title="추천수"><span class="score">{agree_count}</span></div></td>',
						'</tr>',
						'<tr>',
							'<td><a href="javascript:void(0);" class="mys-item-agree" onfocus="blur()"><img style="padding-bottom : 1px;"src="/images/common/btn_recoms.gif" width="50" height="17" border="0" /></a><br/><a href="javascript:void(0);" class="mys-item-scrap" onfocus="blur()"><img src="/images/common/btn_scrabreviews.gif" width="50" height="17" border="0" /></a></td>',
						'</tr>',
						'</table></td>',
						'<td valign="top"><table width="{width}" border="0" cellpadding="0" cellspacing="0">',
							'<tr>',
								'<td height="30" colspan="2" style="padding-left:2px"><div style="position:relative"><nobr class="mys-event-subject"><a href="{viewURL}" class="subject" {target}>{title}</a></nobr><div class="admin_fix_doc">{admin}</div></div></td>',
							'</tr>',
							'<tr class="center-tag-box">',
								'<td width="50%" height="20" class="k_semi_date s11 c66"><div><img class="mys-myitem-tag-img" src="/images/common/icon_date.gif" width="24" height="13" hspace="5" align="absmiddle" /><span>{dateRange}</span></div></td>',
								'<td width="50%" class="k_semi_locate s11 c66"><div width="100%"><img class="mys-myitem-tag-img" src="/images/common/{iconSpeaker}" width="24" height="13" hspace="5" align="absmiddle" /><nobr>{lecturer}</nobr></div></td>',
							'</tr>',
							'<tr>',
							'<td  colspan="3" height="38" valign="top" class="h14"><div class="summary">{summary}</div></td>',
							'</tr>',
						'</table></td>',
					'</tr>',
				'</table></td>',
			'</tr>',
			'<tr>',
						'<td height="17" colspan="6"><table class="mys-tag-footer" border="0" cellspacing="0" cellpadding="0">',
							'<tr class="mys-tag-footer-tr" style="padding-left : 3px;">',
								'<td width="5%"><img class="mys-tag-footer-tr-img"src="/images/common/icon_tag.gif" width="28" height="13" /></td>',
								'<td width="70%"class="s11 c66" ><span class="mys-event-tag-contents">{tags}</span></td>',
								'<td width="5%"><img class="click-img" src="/images/common/icon_view.gif" width="26" height="13" /></td>',
								'<td width="5%" class="num"><span>{read_count}</span></td>',
								'<td width="15%"><table><td width="30%" class="mys-item-reply"><img class="click-img" src="/images/common/icon_comment.gif" width="26" height="13" /></td>',
								'<td width="70%"><span class="num">{reply_count}</span><img class="click-img" src="/images/common/icon_trir.gif" width="3" height="5" hspace="5" vspace="5" align="absmiddle" /></td>',
							'</tr>',
						'</table></td>',
			'</tr>',
		'</table>'
	])).compile(),
	
	tagTemplate : (new Ext.Template('<a class="txt1166" href="/search/search.htm#state=list&keyword={keyword}" value={tag_id}>{tag_name}</a>')).compile(),
	
	createTags : function(tag)
	{
		var length = -2, overTagHTML = "";
		tag = this.safeArray(tag);
		
		for(var i = 0, tags = [], overTags = []; i < tag.length; i++)
		{
			length += tag[i].tag_name.getByte() + 2;
			
			tag[i].keyword = tag[i].tag_name.encode();
			
			if(length < this.list.tagLength)
				tags.push(this.tagTemplate.apply(tag[i]));
			else
				overTags.push(tag[i].tag_name);
			
			length += 2;
		}
		
		if(overTags.length)
		{
			overTagHTML = '<span title="' + overTags.join(", ") + '">...</span>';
			tags.push(overTagHTML);
		}
		
		tagHTML = tags.length ? tags.join(", ") : "태그없음";
		
		return tagHTML;
	},
	
	createAdmin : function(data)
	{
		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);
			admin = '' +
				'<a href="' + ViewManager.getModifyURL(data) + '?item_id=' + data.item_id + '" onfocus="blur()"><img src="/images/common/btn_fixsemi.gif" width="32" height="17" /></a>' +
				'<a href="javascript:;" onfocus="blur()" class="mys-item-del"><img src="/images/common/btn_delsemi.gif" width="32" height="17" /></a>';
			return admin;
		}
		else
			return '';

	},
	
	prepareData : function(data)
	{
		data = Mys.KMAItem.superclass.prepareData.call(this, data);
		
		data.titleAll = (data.title || "").replace('"', "");
		
		data.viewURL = ViewManager.getViewURL(data);
		
		if(this.useImage)
		{
			if(data.image)
			{
				data.imageTD = '<td class="thumbb"><a href="' + data.viewURL + '" ' + (data.target || "") + '><img class="mys-item-image" src="/images/common/img_blank.gif"/></a></td>';
				data.colspan = 3;
				data.width = 477;
				this.titleSize = 475;
				this.summarySize = 465;
			}
			else
			{
				data.imageTD = '';
				data.colspan = 1;
				data.width = 596;
				this.titleSize = 580;
				this.summarySize = 580;
			}
		}
		
		if(!data.lecturer)
		{
			data.lecturer = data.location;
			data.iconSpeaker = "icon_place.gif"
		}
		else
		{
			data.iconSpeaker = "icon_speaker.gif"
		}
		if(data.title)
			data.title = data.title.substrPixel(this.titleSize, this.titleUnit, 1, true);
		else
			data.title = "제목없음";
		
		if(data.summary)
			data.summary = data.summary.substrPixel(this.summarySize, this.summaryUnit, 2);
		else
			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);
		
		data.admin = this.createAdmin(data);
		
		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일");
			
		}
		
		data.tags = this.createTags(data.tag);
		
		return data;
	},
	
	changeImage : function(el)
	{
		if(!el)
			return;
		
		//이미지 처리
		var image = new Image(), target = el;
		
		Ext.fly(image).on("load", function(e, el){
			(target.dom || target).src = this.data.image;
		}, this);
		
		image.src = this.data.image;
	},
	
	setEvents : function(el)
	{
		this.changeImage(this.el.child(".mys-item-image", true));
		
		el.child(".mys-item-reply").on("mouseover", this.reply, this);	
		el.child(".mys-item-reply").on("mouseout", this.hidereply, this);
		
		el.child(".mys-item-agree").on("click", this.agree, this);
		el.child(".agree-count").on("click", this.agree, this);
		
		el.child(".mys-item-scrap").on("click", this.scrap, this);
		
/*		el.child(".subject").on("click", function(){
			this.list.goView(this.data)
		}, this);*/
		
		if(Global.Session.isAdmin())
		{
			this.el.child(".mys-item-del").on("click", this.del, this);
		}
	},
	
	del : function()
	{
		if(confirm("정말 삭제하시겠습니까?"))
		{
			Global.modelItem.del(this.data.item_id, function(data){
				if(data && data.response.code == 100)
					this.list.refresh();
			}, this);
		}
	},

	reply : function()
	{
		if (!this.list.replyPopup)
			this.list.replyPopup = new Mys.replyPopup();
		
		this.list.replyPopup.popup(this.data, this.el,"tl-bl");
	},	
	
	hidereply : function()
	{
		if (this.list.replyPopup)
			this.list.replyPopup.hide();
	},
		
	agree : function()
	{
		if(!Global.Session.checkLogin(location.pathname + ""))
			return ;
		
		if(this.data.agree_date)
		{
			alert("이미 추천하였습니다.");
			return false;
		}
		
		Global.modelItem.agree(this.data.item_id, this.onAgree, this);
	},
	
	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(location.pathname + ""))
			return ;
		
		if(this.data.scrap_date)
		{
			alert("이미 스크랩하였습니다.");
			return false;
		}
		
		if(!window.ScrapDlg)
			ScrapDlg = new Mys.Scrap();
		
		ScrapDlg.setData(this.data);
		ScrapDlg.show(el, "bl-tl");
	}
});
