/*
File: gargar.v1.00.js
Date: 2007/3/4
Script by Kenichi Hamada
Copyright(c) 2007 Kenichi Hamada
*/
function buttonGroupInit(p_oEvent) {
	this.on("checkedButtonChange", clickButton);
}

var oButtonGroup = new YAHOO.widget.ButtonGroup("buttongroup1", { oninit: { fn: buttonGroupInit } }); 

function clickButton(p_oEvent) {
	loadFile("./contents.xml", keyFromButton(p_oEvent.newValue.index) );
}

function keyFromButton(btno) {
	var keyword = "";
	switch (btno) {
		case 0:
		keyword = "新着";
		break;
		case 1:
		keyword = "おもちゃ";
		break;
		case 2:
		keyword = "セーバー";
		break;
		case 3:
		keyword = "ゲーム";
		break;
		case 4:
		keyword = "実験";
		break;
		default:
		keyword = "なし";
		break;
	}
	return keyword;
}

function loadFile(url,keyword) {
	contentKeyWord = keyword;		// グローバル変数に退避
	var today = new Date();
	url += '?date=' + today.getTime();
	new Ajax.Request(
		url,
		{
			method: 'get',
			onComplete: gotXml3
		}
	);
}

function gotXml3(obj) {
	var clist = new Array();
	var xmlData = obj.responseXML;
	var result = loadContentsToArray(xmlData,clist);
	if (result=="Ok") {
		$('randomview').innerHTML = parseContentsRandom(clist);
		$('listview').innerHTML = parseContents(clist,contentKeyWord);
	} else {
		$('listview').innerHTML = result;
	}
}

function parseContentsRandom(clist) {
	var i = randRange(0,clist.length-1);
	var viewText = '<span class="contents"><h2>Random チョイス</h2>こんなものもあります。<br />';
	viewText += '<a href="' + clist[i].url + '">' + clist[i].title + '</a> <span class="subdate">(' + clist[i].date.getFullYear() + '/' + (clist[i].date.getMonth()+1) + '/' +clist[i].date.getDate() + ')';
	if (clist[i].date.getTime()!=clist[i].update.getTime()) {
		viewText += '-(' + clist[i].update.getFullYear() + '/' + (clist[i].update.getMonth()+1) + '/' +clist[i].update.getDate() + ')</span><br />';
	} else {
		viewText += '</span><br />';
	}
	viewText += clist[i].detail + '<br />';
	viewText += '</span>';
	return viewText;
}

function parseContents(clist,keyword) {
	clist.sort(dateSort);
	var viewText = '<span class="contents"><br />';
	var kflag = 0;
	if (keyword=="新着") {
		kflag = 1;
	}
	var wflag = 0;
	for (var i=0 ; i<clist.length ; i++) {
		if (kflag==1) {
			wflag = 1;
		} else {
			wflag = 0;
			for (var k=0 ; k<clist[i].keyword.length ; k++) {
				if (clist[i].keyword[k]==keyword) {
					wflag = 1;
					break;
				}
			}
		}
		if (wflag==1) {
			viewText += '<a href="' + clist[i].url + '">' + clist[i].title + '</a> <span class="subdate">(' + clist[i].date.getFullYear() + '/' + (clist[i].date.getMonth()+1) + '/' +clist[i].date.getDate() + ')';
			if (clist[i].date.getTime()!=clist[i].update.getTime()) {
				viewText += '-(' + clist[i].update.getFullYear() + '/' + (clist[i].update.getMonth()+1) + '/' +clist[i].update.getDate() + ')</span><br />';
			} else {
				viewText += '</span><br />';
			}
			viewText += clist[i].detail + '<br /><span class="half"><br /></span>';
		}
	}
	viewText += '</span>';
	return viewText;
}

function dateSort(a,b) {
	return b.update.getTime()-a.update.getTime();
}

function loadContentsToArray(xmldata,clist) {
	var contents = xmldata.getElementsByTagName("Contents");
	if (contents.length==0) {
		return "No Contents";
	}
	var rootNode = contents[0];
	for (var chNode=rootNode.firstChild ; chNode!=null ; chNode=chNode.nextSibling) {
		switch (chNode.nodeName) {
			case "Data":
			var elm = chNode.firstChild;
			if (elm.nodeName!="Title") {
				return "No Title";
			}
			var titleName = elm.firstChild.nodeValue;

			elm = elm.nextSibling;
			if (elm.nodeName!="Detail") {
				return "No Detail";
			}
			var detailText = elm.firstChild.nodeValue;

			elm = elm.nextSibling;
			if (elm.nodeName!="Key") {
				return "No Key";
			}
			var keyWord = elm.firstChild.nodeValue;

			elm = elm.nextSibling;
			if (elm.nodeName!="Url") {
				return "No Url";
			}
			var urlAddr = elm.firstChild.nodeValue;

			elm = elm.nextSibling;
			if (elm.nodeName!="Date") {
				return "No Date";
			}
			var dateText = elm.firstChild.nodeValue;
			var updateText = elm.firstChild.nodeValue;

			elm = elm.nextSibling;
			if (elm.nodeName=="Update") {
				if (elm.firstChild) {
					updateText = elm.firstChild.nodeValue;
				}
			}

			var obj = new Object();
			obj.title = titleName;
			obj.detail = detailText;
			obj.keyword = keyWord.split(",");
			obj.url = urlAddr;
			obj.date = new Date(dateText);
			obj.update = new Date(updateText);
			clist.push(obj);
			break;
		}
	}
	return "Ok";
}

function randRange(minNum, maxNum) {
	return (Math.floor(Math.random()*(maxNum-minNum+1))+minNum);
}
