/*
 * WriteTopics.js - トップページMCMS対応箇所 表示用スクリプト
 */
if ( typeof(WriteTopics) == 'undefined' ) WriteTopics = function() {};

// WriteTopicsオブジェクト生成
var topics = new WriteTopics();

WriteTopics.prototype.XML_LIST = [
	'/xml/01.xml',
	'/xml/01_baseball.xml',
	'/xml/01_soccer.xml',
	'/xml/03.xml',
	'/xml/09.xml',
	'/xml/06.xml',
	'/xml/08.xml',
	'/xml/08_topics.xml',
	'/xml/11.xml',
	'/xml/12.xml',
	'/xml/13.xml',
	'/xml/13_press.xml',
	'/xml/14.xml'
];

// 表示箇所データID
WriteTopics.prototype.INFO_ID = 3;

// 表示箇所出力先div名
WriteTopics.prototype.INFO_DIV = 'topics';

// ターゲット区分
WriteTopics.prototype.TARGET_KIND_SELF  = '0';
WriteTopics.prototype.TARGET_KIND_BLANK = '1';
WriteTopics.prototype.TARGET_KIND_POPUP = '2';

// アーカイブ最古年度＋ファイル名
WriteTopics.prototype.START_YEAR   = 2005;
WriteTopics.prototype.ARCHIVE_NAME = 'main_';

// 「NEW」テキスト
WriteTopics.prototype.NEW_TEXT = '<div align="left"><font color="#cc0000">NEW</font></div>';
// アイコン画像HTML	
WriteTopics.prototype.ICON_IMG = '<td valign="top"><img width="7" vspace="3" height="7" border="0" src="../images/topics/icon_maru_s.gif"/></td>\n';
// 下線部HTML	
WriteTopics.prototype.UNDER_LINE = '<tr><td valign="top" align="center" colspan="4"><img width="505" height="4" border="0" src="images/line_blue.gif"/></td></tr>\n';

/*** Script Start  ***/
// OnLoad Function (Main)
onload = function () {
	var disp_year = (new Date()).getFullYear();

	// 年度が指定されていれば年度の取得
	if (location.search.length > 0) {
		choice = location.search.substring(1);
		choiceValue = choice.split('=');
		disp_year = choiceValue[1];
	}
	
	// XML URL Path
	var url;
	var info_list   = new Array();
	var count_info  = 0;
	
	for(var i=0; i<topics.XML_LIST.length; i++){
		// XML URL Path
		url = topics.XML_LIST[i];
		// Parse Each XMLs
		var http = new JKL.ParseXML(url);
		// 出力データの全配列化
		http.setOutputArrayAll();
		var data = http.parse();
		
		var id_list = new Array();
		// 全データ抽出
		if(!topics.isEmpty(data.itemList)){
			for(var j=0;j<data.itemList[0].item.length; j++){
				var item = data.itemList[0].item;
				for(var k=0;k<item[j].publishChk[0].id.length;k++){
					// 最新トピックス
					if(item[j].publishChk[0].id[k] == topics.INFO_ID){
						info_list[count_info] = item[j];
						count_info++;
					}
				}
			}
		}
	}
	// リストのソート
	info_list.sort(function(a, b)  {return b.date-a.date});
	// HTML書き出し
	topics.writeHTML(info_list, topics.INFO_DIV, disp_year);
}

// functions
WriteTopics.prototype.writeHTML = function(data_list, div_name, disp_year){
	var str = '';
	
	// ヘッダ
	str += '<table width=\'513\' border=\'0\' cellpadding=\'0\' cellspacing=\'0\'>\n';
	str += '<tr>\n';
	str += '<td width=\'15\'><img src=\'../images/common/clear.gif\' width=\'15\' height=\'5\' /></td>\n';
	str += '<td width=\'403\'><img src=\'../images/common/clear.gif\' width=\'403\' height=\'5\' /></td>\n';
	str += '<td width=\'65\'><img src=\'../images/common/clear.gif\' width=\'65\' height=\'5\' /></td>\n';
	str += '<td width=\'30\'><img src=\'../images/common/clear.gif\' width=\'30\' height=\'5\' /></td>\n';
	str += '</tr>\n';
	str += '<tr>\n';
	str += '<td colspan=\'4\' align=\'center\' valign=\'top\'>';
	str += '<img src=\'images/line_blue.gif\' width=\'505\' height=\'4\' border=\'0\' /></td>\n';
	str += '</tr>\n';
	
	var year_list  = new Array();
	var count_year = 0;
	var this_year;
	var base_year;
	// トピックス
	for(var i=0;i<data_list.length;i++){
		// 年度一覧リスト作成処理
		this_year = data_list[i].date[0].substring(0,4);
		if(i==0){
			if(this_year < disp_year){ disp_year = this_year };
			year_list[count_year] = data_list[i].date[0].substring(0,4);
			count_year++;
			base_year = this_year;
		}
		if(this_year != base_year){
			year_list[count_year] = data_list[i].date[0].substring(0,4);
			count_year++;
			base_year = data_list[i].date[0].substring(0,4);
		}
		
		// リスト用HTML設定
		str += topics.writeList(data_list[i], disp_year);
	}
	// フッタ
	str += '</table>\n';
		
	// HTML書き込み(div id=div_name)
	var element = document.getElementById(div_name);
	if(element){ element.innerHTML = str };
	
	// アーカイブ用リスト設定
	var year_str = '';
	var archive_no = parseInt(year_list[year_list.length-1], 10) - topics.START_YEAR;
	year_str += topics.yearList(year_list, archive_no);
	
	// HTML書き込み(div id='year_list')
	var element = document.getElementById('year_list');
	if(element){ element.innerHTML = year_str };
	// HTML書き込み(div id='year_list2')
	var element = document.getElementById('year_list2');
	if(element){ element.innerHTML = year_str };
}

/**
 * リスト用HTML設定関数 writeList
 */
WriteTopics.prototype.writeList = function(data_node, disp_year){
	var str = '';
	if(data_node.date[0].substring(0,4) == disp_year){
		str += '<tr>\n';
		str += topics.ICON_IMG;
		str += '<td valign="top">';
		
		// ターゲット(_selfの場合)
		if (data_node.target == topics.TARGET_KIND_SELF) {
			str += '<a href="javascript:Checker(\'' + data_node.url;
			str += '\')">';
			str += data_node.title;
		// ターゲット(ポップアップ以外の場合)
		} else {
			var target = (data_node.target == topics.TARGET_KIND_POPUP) ? '_self' : '_blank';
			str += '<a href="' + data_node.url + '" target="' + target + '">' + data_node.title;
		}
		str += '</a>';
		str += '</td>\n';
		
		// 日付
		str += '<td valign="bottom">';
		str += data_node.date[0].substring(0,4) + '.';
		str += data_node.date[0].substring(4,6) + '.';
		str += data_node.date[0].substring(6,8);
		str += '</td>\n';
		
		// NEWテキスト
		str += '<td valign="bottom" class="size12_16">';
		if(!topics.isEmpty(data_node.newflg) && data_node.newflg == '1'){
			str += topics.NEW_TEXT;
		}
		str += '</td>\n';
		str += '</tr>\n';
		// 下線
		str += topics.UNDER_LINE;
	}
	return str;
}

/**
 * アーカイブ用リスト設定関数 yearList
 */
WriteTopics.prototype.yearList = function(year_list, archive_no){
	var str = '';
	str += '<table cellspacing="0" cellpadding="0" border="0">\n';
	str += '<tbody>\n';
	str += '<tr>\n';
	str += '<td><img width="30" height="5" src="../images/common/clear.gif"/></td>\n';
	str += '<td>|　';
	
	for(var i=0;i<year_list.length;i++){
		str += '<a href="main.html?year=' + year_list[i] + '" target="main">';
		str += year_list[i] + '</a>　|　';
	}
	if(archive_no > 0){
		for(var j=archive_no;j>0;j--){
			str += '<a href="' + topics.ARCHIVE_NAME + (topics.START_YEAR + (j-1)) + '.html" target="main">';
			str += (topics.START_YEAR + (j-1)) + '</a>　|　';
		}
	}
	str += '</td>\n';
	str += '</tr>\n';
	str += '</tbody>\n';
	str += '</table>\n';
	return str;
}

/**
 * 空判定
 */
WriteTopics.prototype.isEmpty = function(obj){ return (obj == null || obj == undefined || obj == '')? true: false; }

