//CMS define
var CMS = {};
CMS.cms_list_path = 'json/list.json';
CMS.cms_article_root = 'json_article/';
CMS.allowed_type = '3,4';
CMS.title_status = [];

CMS.article_type_class = {
	2 : 'type_other'
,	3 : 'type_news'
,	4 : 'type_e'
,	5 : 'type_contest'
,	6 : 'type_server_status'
,	7 : 'type_announcement'
};

CMS.loadList = function(){
	var c = {
		id: ''
	,	lastLoadTime: 0
	,	list: []
	,	pageNumb: 1
	,	pageNow: 1
	,	linesPerPage: 10
	,	wordPerLine: 24
	,	html: ''
	};
	
	c.draw = function( page ){
		this.html = $("<ul/>");
		var i = 0, l=this.list.length, allowed = null;
		if(page>1 && page<=this.pageNumb){
			i += this.linesPerPage * ( page - 1 );
		}else if(page>this.pageNumb){
			i = this.linesPerPage * ( this.pageNumb - 1 );
		}
		if(this.list.length > 0){
			if(CMS.allowed_type){
				var t_allowed = CMS.allowed_type.split(',');
				allowed = [];
				for(var k=0; k<t_allowed.length; k++){
					allowed.push( t_allowed[k] - 0 );
				}
			}
			var appendHtml = '', j=this.linesPerPage;
			for(;i<l;i++){
				if(!j--){
					break;
				}
				if(allowed){
					if($.inArray(this.list[i].type, allowed) == -1){
						continue;
					}
				}
				if(this.list[i].title.length>this.wordPerLine){
					var showName = this.list[i].title.substr(0, this.wordPerLine) + '...';
				}else{
					showName = this.list[i].title;
				}
				
				CMS.title_status[i] = 'closed';
				appendHtml = '<table width="100%" cellpadding="0" cellspacing="0" class="cms_title_closed" id="cms_title_'+i+'" onClick="javascript:CMS.toggleListContent('+i+');"><tr><td><span class="new_img"><img onload="fixPNG(this)" src="images/png_' + CMS.article_type_class[this.list[i].type] + '.png"/></span><span class="news_t">' + showName + '</span> <span class="news_date">' + this.list[i].date + '</span> </td></tr></table> <div id="cms_content_'+i+'" style="display:none;" class="content"><span class="news_bg_t"></span><span class="news_bg_m">	<span class="news_img2"><img onload="fixPNG(this)" src="images/png_' + CMS.article_type_class[this.list[i].type] + '.png"/></span><span class="news_date2">' + this.list[i].date + '</span><div class="news_cotent">' + ( this.list[i].ab_content ? this.list[i].ab_content : this.list[i].title) + ' <br /><br /><span style="float:right;"><a href="' + this.list[i].href + '" title="' + this.list[i].title + '' + '">Read More</a></span></div></span><span class="news_bg_b"></span></div>';
				this.html.append( appendHtml );
			}
		}
		
	};
	c.show = function(){
		$(this.id).empty();
		$(this.id).html(this.html);
	};
	c.init = function(json, config){
		if(config.linesPerPage && config.linesPerPage>0){
			this.linesPerPage = config.linesPerPage;
		}
		if(config.wordPerLine && config.wordPerLine>0){
			this.wordPerLine = config.wordPerLine;
		}
		this.list = json;
		this.pageNow = 1;
		this.pageNumb = Math.ceil(this.list.length / this.linesPerPage) || 1;
		this.draw();
		this.show();
		this.lastLoadTime = new Date();
	};
	c.cache = '';
	c.requestting = 0;
	c.request = function(){
		c.requestting = 1;
		$.ajax({
			type: "GET",
			url: CMS.cms_list_path,
			dataType: 'json',
			timeout: 30000,
			success: function(json){
				c.cache = json;
				c.doDelayInit();
			}
		});
	};
	c.delayParams = [];
	c.delayInit = function(id, type, linesPerPage, wordPerLine){
		c.delayParams.push({
			id:id
		,	type:type
		,	linesPerPage:linesPerPage
		,	wordPerLine:wordPerLine
		});
	};
	c.doDelayInit = function(){
		var p = c.delayParams;
		for(var i=0; i<p.length; i++){
			var xid = '#' + p[i].id;
			c.id = xid;
			$(xid).empty();
			c.init(c.cache[p[i].type], {
				linesPerPage: p[i].linesPerPage
			,	wordPerLine:  p[i].wordPerLine
			});
		}
	};
	return function(id, type, linesPerPage, wordPerLine){
		var xid = '#' + id;
		c.id = xid;
		$(xid).empty();
		$(xid).append("Please wait for loading data!");
		if(!c.cache){
			c.delayInit(id, type, linesPerPage, wordPerLine);
			if(!c.requestting){
				c.request();
			}
		}else{
			c.init(c.cache[type], {
				linesPerPage: linesPerPage
			,	wordPerLine: wordPerLine
			});
		}
	};
}();
CMS.showOnlyListContent = function( idx ){
	for(var i = 0; i<CMS.title_status.length; i++){
		if(i != idx){
			$('#cms_title_'+i).removeClass('cms_title_opened');
			$('#cms_title_'+i).addClass('cms_title_closed');
			$('#cms_content_'+i).slideUp();
			CMS.title_status[i] = 'closed'
		}else{
			$('#cms_title_'+i).removeClass('cms_title_closed');
			$('#cms_title_'+i).addClass('cms_title_opened');
			$('#cms_content_'+i).slideDown();
			CMS.title_status[i] = 'opened';
		}
	}
};
CMS.toggleListContent = function( idx ){
	if(CMS.title_status[idx] == 'closed'){
		$('#cms_title_'+idx).removeClass('cms_title_closed');
		$('#cms_title_'+idx).addClass('cms_title_opened');
		$('#cms_content_'+idx).slideDown();
		CMS.title_status[idx] = 'opened';
	}else{
		$('#cms_title_'+idx).removeClass('cms_title_opened');
		$('#cms_title_'+idx).addClass('cms_title_closed');
		$('#cms_content_'+idx).slideUp();
		CMS.title_status[idx] = 'closed'
	}
};
CMS.loadArticle = function(){
	var c = {
		id_title: ''
	,	id_anchor: ''
	,	id_date: ''
	,	id_content: ''
	,	lastLoadTime: 0
	};
	c.init = function( json ){
		$(this.id_title).empty();
		$(this.id_title).append(json.title);
		$(this.id_anchor).empty();
		$(this.id_anchor).append(json.anchor);
		$(this.id_date).empty();
		$(this.id_date).append(json.date);
		$(this.id_content).empty();
		$(this.id_content).html(json.content);
		this.lastLoadTime = new Date();
	}
	return function(id_title, id_anchor, id_date, id_content){
		c.id_title = '#' + id_title;
		c.id_anchor = '#' + id_anchor;
		c.id_date = '#' + id_date;
		c.id_content = '#' + id_content;
		$(id_title).empty();
		$(id_title).append("Please wait for loading data!");
		var s = window.location.search;
		var url = CMS.cms_article_root + s.substr(1);
		$.ajax({
			type: "GET",
			url: url,
			dataType: 'json',
			timeout: 30000,
			success: function(json){
				c.init(json);
			},
			error: function(){
				redirect('/page_error.html');
			}
		});
	};
}();
