// ========================================================
//   Lithmatic Company Site : NEWS Listing
//
//   2008.1.8 TLC ITPG  @author satoh
// ========================================================

//指定がない場合のデフォルトの表示リスト
var LIMIT_DEFAULT = 10;
// NEWアイコン
var NEW_ICON = "img/top/new_icon.gif";
// NEWアイコンの出現期間（日）
var NEW_ICON_APPEAR = 14;

var BN_PAGE = 'index.html'

//サイトツリー設定XML
var url3;

//XMLパース
var http3;
var data3;

var newsDivStr;
var listYear;

// ------------------------
// 初期化
// ------------------------
function init_news() {
	//サイトツリー設定XML
	url3 = baseUrl + "xml/latestNews.xml";
	//XMLパース
	http3 = new JKL.ParseXML( url3 );
	data3 = http3.parse();
}

// ------------------------
// XMLからデータ整形（カテゴリ、最新分限定数）
// ------------------------
function listLatestNewsDiv(_categ, _limit){
	try {
		if (_categ == null || _categ == ""){
			_categ = "all";
		}
		if (_limit == null || _limit == ""){
			_limit = LIMIT_DEFAULT;
		}

		//alert(_limit + " " + " " + _categ);
		newsDivStr = "";
		listCount = 0;

		for (i = 0; i < data3.news.item.length; i++ ) {
			if ( _categ != "all" && _categ != data3.news.item[i].category ){
				continue;
			}
			listCount ++;

			//ニュース加工
			newsDivStr += getNewsList(data3);

			if (listCount >= _limit ){
				break;
			}
		}

	} catch (e) {
		document.getElementById('news').innerHTML = "Error : " + e;
	}
}

// ------------------------
// XMLからデータ整形（カテゴリ、選択年別）
// ------------------------
function listNewsByYearDiv(_categ, _year){
	try {
		if (_categ == null || _categ == ""){
			_categ = "all";
		}
		if (_year == null || _year == ""){
			// 指定なしの場合は当年
			myD = new Date();
			myYear = myD.getYear();
			_year = (myYear < 2000) ? (myYear + 1900).toString() : myYear.toString();
		}

		newsDivStr = "";
		listYear = "";
		thisYear = "";

		for (i = 0; i < data3.news.item.length; i++ ) {
			// 年別のリンク作成
			if (thisYear != data3.news.item[i].pubY) {
				//if (thisYear != "") {
				//	listYear += MARK_SPLIT;
				//}
				thisYear = data3.news.item[i].pubY;
				if (_year != thisYear) {
					listYear += "<div class='sky'><a href='" + BN_PAGE + "?year=" + data3.news.item[i].pubY + "'>" + data3.news.item[i].pubY + "</a></div>";
				} else {
					listYear += "<div>" + data3.news.item[i].pubY + "</div>";
				}
			}

			if ( _year != data3.news.item[i].pubY || _categ != "all" && _categ != data3.news.item[i].category ){
				continue;
			}

			//ニュース加工
			newsDivStr += getNewsList(data3);

		}

	} catch (e) {
		document.getElementById('news').innerHTML = "Error : " + e;
	}
}

// ------------------------
// ニュースリストの加工
// ------------------------
function getNewsList(data3){
	try {
		myNewIcon = "<img src='" + baseUrl + NEW_ICON + "' class='img_bot mgL10'>";
		//URL生成、http から始まる場合は加工なし
		myUrl = getFormatUrl(data3.news.item[i].url);
		//曜日を算出
		myYoubi = getYoubi(data3.news.item[i].pubY, data3.news.item[i].pubMD);

		myNews = "";
		myNews += "<li class='arr_org'>" + data3.news.item[i].pubY + "/" + data3.news.item[i].pubMD.substr(0, 2) + "/" + data3.news.item[i].pubMD.substr(2, 2);
		myNews += "(" + myYoubi + ")";
		myNews += "<a href='" + myUrl + "'>" + data3.news.item[i].title + "</a>";
		if (appearNewIcon(data3.news.item[i].pubY, data3.news.item[i].pubMD)){
			myNews += myNewIcon;
		}
		myNews += "</li>";
		return myNews;

	} catch (e) {
		return "";
	}
}

// ------------------------
// NEWアイコン用に掲載日と今日の日付を比較する
// ------------------------
function appearNewIcon(y, md) {
	result = false;
	try {
		myMillisec = (new Date(parseInt(y, 10), parseInt(md.substr(0, 2), 10) - 1, parseInt(md.substr(2, 2), 10))).getTime();
		todayMillisec = (new Date()).getTime();
		if( todayMillisec - myMillisec < (24 * 60 * 60 * 1000) * NEW_ICON_APPEAR ) {
			result = true;
		}
		return result;

	} catch (e) {
		return result;
	}
}

// ------------------------
// 曜日を算出
// ------------------------
function getYoubi(y, md){
	myTbl = new Array("日","月","火","水","木","金","土");
	try {
		_date = new Date(parseInt(y, 10), parseInt(md.substr(0, 2), 10) - 1, parseInt(md.substr(2, 2), 10));
		_day = _date.getDay();
		return myTbl[_day];

	} catch (e) {
		return "";
	}
}

// ------------------------
// URL整形
// ------------------------
function getFormatUrl(orgUrl) {
	result = "#";
	try {
		if (orgUrl != null || orgUrl != "") {
			if (orgUrl.search(/http/i) != -1) {
				result = orgUrl;
			} else {
				result = baseUrl + orgUrl;
			}
		}
		return result;
	} catch (e) {
		return "";
	}
}


// ------------------------
// DIVにニュース書き出し
// ------------------------
function makeLatestNews(){
	try {
		article = "";
		if (newsDivStr == ""){
			article += "<p>該当するニュースは見つかりませんでした。</p>";
		} else {
			article += "<ul class='mgL10 mgB20'>";
			article += newsDivStr;
			article += "</ul>";
		}
		document.getElementById('news').innerHTML = article;

	} catch (e) {
		document.getElementById('news').innerHTML = "Error occured : " + e;
	}
}

// ------------------------
// DIVに存在する年のリンクを作成
// ------------------------
function makeYearList(){
	try {
		article = "";
		article += listYear;
		document.getElementById('bnYear').innerHTML = article;

	} catch (e) {
		document.getElementById('bnYear').innerHTML = "Error : " + e;
	}
}

// ------------------------
// ページロード時のメインメソッド
// param : categ -> XML
// ------------------------
function getLatestNews(categ, year, limit, pattern) {
	try {

		init_news();
		if (pattern == "latest"){
			listLatestNewsDiv(categ, limit);
		} else {
			listNewsByYearDiv(categ, year);
		}
		makeLatestNews();

	} catch(e) {
		document.write("Error： " + e);
	}
}

// ------------------------
// ページロード時のメインメソッド
// param : categ -> XML
// ------------------------
function getNewsBacknumber() {
	try {

		init_news();
		var oSearch = new CLocationSearch(location.search);
		myYear = oSearch.item('year');
		listNewsByYearDiv("", myYear);
		makeLatestNews();
		makeYearList();

	} catch(e) {
		document.write("Error： " + e);
	}
}





