//
//	このJavascriptは、HTMLファイルで定義された以下のクラスに関連する処理を行う。
//	・linkListBasket
//


var elmLinkListBasket;

Event.observe(window, 'load', function(){
	elmLinkListBasket = document.getElementsByClassName('linkListBasket');
	refXmlTag = elmLinkListBasket[0].getAttribute('id').split(' ');

	new Ajax.Request('../' + refXmlTag[0] + '.xml', {
		method : 'get',
		parameters: '',
		onSuccess : XmlListUp
	});

}, false);


function XmlListUp(httpObj){
//	$('loaded-signal').innerText = 'XMLファイルを読み込んでいます...';
	var xmlObj            = httpObj.responseXML;
	for(i=0; i< xmlObj.getElementsByTagName('schedule').length; i++){
		for(j=0; j<elmLinkListBasket.length; j++){
			resultDivId = elmLinkListBasket[j].getAttribute('id');
			refXmlTag   = resultDivId.split(' ');

			if((refXmlTag.length == 3 && (refXmlTag[1].indexOf('calendar-') == 0 || refXmlTag[1].indexOf('history-') == 0) && xmlObj.getElementsByTagName('schedule')[i].parentNode.parentNode.tagName == refXmlTag[1] && xmlObj.getElementsByTagName('schedule')[i].parentNode.tagName == refXmlTag[2]) || (refXmlTag.length == 2 && (refXmlTag[1].indexOf('calendar-') == 0 || refXmlTag[1].indexOf('history-') == 0) && xmlObj.getElementsByTagName('schedule')[i].parentNode.parentNode.tagName == refXmlTag[1]) || (refXmlTag.length == 2 && (refXmlTag[1].indexOf('calendar-') != 0 || refXmlTag[1].indexOf('history-') != 0) && xmlObj.getElementsByTagName('schedule')[i].parentNode.tagName == refXmlTag[1])){
				var html                  = '';
				var article               = '';
				var year                  = '';
				var scheduleDate          = '';
				var date                  = '';
				var articleUrlObj         = xmlObj.getElementsByTagName('article-url')[i];
				var articleTitleObj       = xmlObj.getElementsByTagName('article-title')[i];
				if((articleUrlObj != null && articleUrlObj != undefined) || (articleTitleObj != null && articleTitleObj != undefined)){

					var articleUrl    = '';
					if(articleUrlObj != null && articleUrlObj != undefined && articleUrlObj.firstChild != null && articleUrlObj.firstChild != undefined){
						articleUrl        = articleUrlObj.firstChild.nodeValue;
					}

					var articleTitle  = '';
					if(articleTitleObj != null && articleTitleObj != undefined && articleTitleObj.firstChild != null && articleTitleObj.firstChild != undefined){
						articleTitle      = articleTitleObj.firstChild.nodeValue;
					}

					if(articleUrl != ''){
						article          += '<a target="_blank" href="' + articleUrl + '">' + articleTitle + '</a>';
					}
					else{
						article          += articleTitle;
					}

					var yearObj           = xmlObj.getElementsByTagName('year')[i];
					if(yearObj.childNodes.length > 0){
						year              = yearObj.firstChild.nodeValue;
					}

					var scheduleDateObj   = xmlObj.getElementsByTagName('schedule-date')[i];
					if(scheduleDateObj.childNodes.length > 0){
						scheduleDate      = scheduleDateObj.firstChild.nodeValue;
					}

					if(year != '' || scheduleDate != ''){
//						date              = year + scheduleDate + '<br/>';
						date              = year + scheduleDate + '　';
					}
					html                  = '<li class="linkListItem">' + date + article;

					var commentObj        = xmlObj.getElementsByTagName('comment')[i];
					if(commentObj.childNodes.length > 0){
						var comment       = commentObj.firstChild.nodeValue;
						html             += '　・・・・' + comment;
					}
					html                 += '</li>';

					$(resultDivId).innerHTML += html;
				}
			}
		}
	}

	$('loaded-signal').innerText = 'XMLファイルを読み込みました。';
	var sourceHash = self.location.hash;
	if(sourceHash != '')
	{
		location.hash  = '';
		location.hash  = sourceHash;
		window.scrollBy( 0, -10 );
	}
}

