Mys.KMAList = function(config)
{
	Mys.KMAList.superclass.constructor.call(this, config);
	
	this.on("move", function(start, maxRow){
		this.onMove();
    
		Global.modelItem.getList(this.boardID, this.boardType, this.order, this.dir, this.basis, this.timePoint, this.range, this.startDate, this.endDate, null, start, maxRow, this.keyword, this.scriptFlag, this.defaultFlag, this.cate, function(data){
			if(data && data.response.code == 100)
				this.update(data.response.item, data.response.total);
			else
				this.empty();
		}, this)
	}, this);
};

Ext.extend(Mys.KMAList, Mys.TemplateList, {
	itemClass : Mys.KMAItem,
	tagLength : 50,
	
	headerTemplate : (new Ext.Template([
		'<table width="100%" border="0" cellspacing="0" cellpadding="0">',
			'<tr id="top-menu-list">',
				'<td height="50">',
					'<table align="center" border="0" cellspacing="0" cellpadding="0">',
						'<tr>',
							'<td width="115" align="center" class="sort" style="border:solid 1px #cecece" onmouseover="MM_showHideLayers(\'option2\',\'\',\'show\')" onmouseout="MM_showHideLayers(\'option2\',\'\',\'hide\')"><a href="#" value="range">{sortText}일순</a><img src="../images/blank.gif"/></td>',
							'<td width="10"></td>',
							'<td width="115" align="center" class="sort" style="border:solid 1px #cecece"><a href="#state=list&order=agree_count&dir=desc" value="agree_count">추천순</a><img src="../images/blank.gif"/></td>',
							'<td width="10"></td>',
							'<td width="115" align="center" class="sort" style="border:solid 1px #cecece"><a href="#state=list&order=reply_count&dir=desc" value="reply_count">댓글순</a><img src="../images/blank.gif"/></td>',
							'<td width="10"></td>',
							'<td width="115" align="center" class="sort" style="border:solid 1px #cecece"><a href="#state=list&order=read_count&dir=desc" value="read_count">조회순</a><img src="../images/blank.gif"/></td>',
						'</tr>',
					'</table>',
					'<div id="option2" class="sort-select" onmouseover="MM_showHideLayers(\'option2\',\'\',\'show\')" onmouseout="MM_showHideLayers(\'option2\',\'\',\'hide\')" >',
						'<nobr onmouseover="this.style.backgroundColor=\'#8ec9d0\'" onmouseout="this.style.backgroundColor=\'#dceef0\'"><img src="/images/overseas/dot_sort.gif"/><a href="#state=list&range=all&time_point={timePoint}&basis={basis}&order={order}&dir={dir}" value="all">전체</a></nobr>',
						'<nobr onmouseover="this.style.backgroundColor=\'#8ec9d0\'" onmouseout="this.style.backgroundColor=\'#dceef0\'"><img src="/images/overseas/dot_sort.gif"/><a href="#state=list&range=today&time_point={timePoint}&basis={basis}&order={order}&dir={dir}" value="today">오늘</a></nobr>',
						'<nobr onmouseover="this.style.backgroundColor=\'#8ec9d0\'" onmouseout="this.style.backgroundColor=\'#dceef0\'"><img src="/images/overseas/dot_sort.gif"/><a href="#state=list&range=week&time_point={timePoint}&basis={basis}&order={order}&dir={dir}" value="week">일주일 이내</a></nobr>',
						'<nobr onmouseover="this.style.backgroundColor=\'#8ec9d0\'" onmouseout="this.style.backgroundColor=\'#dceef0\'"><img src="/images/overseas/dot_sort.gif"/><a href="#state=list&range=month&time_point={timePoint}&basis={basis}&order={order}&dir={dir}" value="month">한달 이내</a></nobr>',
						'<nobr onmouseover="this.style.backgroundColor=\'#8ec9d0\'" onmouseout="this.style.backgroundColor=\'#dceef0\'"><img src="/images/overseas/dot_sort.gif"/><a href="#state=list&range=year&time_point={timePoint}&basis={basis}&order={order}&dir={dir}" value="year">일년 이내</a></nobr>',
//						'<nobr onmouseover="this.style.backgroundColor=\'#8ec9d0\'" onmouseout="this.style.backgroundColor=\'#dceef0\'"><img src="/images/overseas/dot_sort.gif"/><a href="#" value="past">지난 {sortText}</a></nobr>',
					'</div>',
				'</td>',
			'</tr>',
		'</table>'
	])).compile(),
	
	elTemplate : (new Ext.Template([
		'<div class="{headerClassName}"></div>',
		'<div class="{bodyClassName}"></div><div style="text-align : center;" class="{emptyClassName}">내용이 없습니다.</div><div class="{loadingClassName}"><img class="mys-loading-img" src="/images/common/loadings.gif"/></div><div class="{pagerClassName}"></div>'
	])).compile(),
	
	setEvents : function()
	{
		if(!this.headerTemplate)
			return false;
		
		var options2 = this.el.child("#option2");
		
		if(options2)
			this.rangeEl = options2.select("a").on("click", this.onRangeClick, this);
		
		this.sortEl = this.headerEl.select(".sort");
		
		this.sortEl.each(function(el){
			var a = el.child("a");
			
			if(a.dom.getAttribute("value") == "range")
				this.rangeSortEl = a;
			else
				el.child("a").on("click", this.onSortClick, this);
		}, this);
	},
	
	onSortClick : function(e, el)
	{
		//this.setSort(e.getTarget("a").getAttribute("value"));
	},
	
	onRangeClick : function(e, el)
	{
		//this.setRange(e.getTarget("a").getAttribute("value") || "all");
	},
	
	setSort : function(order)
	{
		if(!order)
			return ;
		
		this.order = order;
		this.dir = "desc";
		
		this.pager.first();
	},
	
	setRange : function(range)
	{
		if(!range)
			return;
		
		this.range = range;
		this.order = this.orderValue;
		this.dir = this.tab == "future" ? "asc" : "desc";
		
		this.pager.first();
	},
	
	onSorted : function(value)
	{
		if(!this.sortEl)
			return;
		var value = value || this.order;
		
		this.sortEl.each(function(el){
			if(el.child("a").dom.getAttribute("value") == value)
				el.addClass("active");
			else
				el.removeClass("active");
		}, this);
	},
	
	onRanged : function()
	{
		if(!this.rangeEl)
			return;
		
		this.rangeEl.each(function(el){
			if(el.dom.getAttribute("value") == this.range)
				this.rangeSortEl.update(el.dom.innerHTML == "전체" ? (this.sortText + "일순") : el.dom.innerHTML);
		}, this);
		
		this.onSorted("range");
	},
	
	onMove : function()
	{
		if(!this.headerTemplate)
			return ;
		
		var s = parseHash(window.location.hash);
		
		if(s.range)
			this.onRanged();
		else if(s.order)
			this.onSorted();
	},
	
	loadList : function(page)
	{
		if(KMAView.rendered)
			KMAView.el.setStyle("display", "none");
		
		this.render("kma-list");
		
		this.pager.setPage(parseInt(page) || 1);
		
    this.el.show();
    
    if(window.relationList && relationList.rendered)
    	relationList.el.setStyle("display", "none");
	},
	
	goView : function(data, loadStart)
	{
		if(!ViewManager.isViewURL(data))
		{
			ViewManager.goView(data, loadStart);
			return;
		}
		
		if(this.rendered)
			this.el.setStyle("display", "none");
		
		KMAView.render("kma-view");
		
		KMAView.start = (this.pager.page - 1) * this.maxRow;
		
		KMAView.loadView(data.item_id, loadStart);
		
		KMAView.el.show();
	}
});