
window.onload = function() {
	var url = "xml.php";
	var option = {
		method :'get',
		onComplete : function(org) {
			var xml = org.responseXML;
			List.show(xml);
		}
	}
	var obj = new Ajax.Request(url, option);
}

var List = {
	"VERSION" :'0.2.0',
	parse : function(xml) {
		var root = xml.documentElement;

		var list = [];

		if (null != root) {

			if (root.hasChildNodes()) {

				var forums = root.childNodes;
				
				for (var i=0; i < forums.length; i++) {
					var node = forums[i];
					
					var forumObj;
					if (node.nodeType == 1) {
						// forum
						forumObj = new Forums();
						
						forumObj.id = node.getAttribute("id");
						
						var ele = node.childNodes;
						
						for (var j=0; j < ele.length; j++) {
							var child = ele[j];
							
							// item
							if (child.nodeType == 1) {
								
								var dd = child.childNodes;
								
								var obj = new Topics();
								for (var k=0; k < dd.length; k++) {
									var e = dd[k];
									
									if (e.nodeType == 1 && e.nodeName == "topic") {
										obj.topicId = DOM.getText(e);
									} else if (e.nodeType == 1 && e.nodeName == "title") {
										obj.title = DOM.getText(e);
									} else if (e.nodeType == 1 && e.nodeName == "date") {
										obj.date = DOM.getText(e);
									}
								}
								forumObj.setTopics(obj);
							}
						}
						list.push(forumObj);
					}
				}
			}
		}
		
		return list;
	},

	show : function(xml) {
		var list = this.parse(xml);

		if (list.length == 0) {
			return "";
		}

		var size = list.length;
		
		for (var i=0; i < size; i++) {
			var forum = list[i];
			
			var str = new StringBuffer();
			
			var topicsList = forum.getTopics();
			
			var lsize = topicsList.length;
			
			str.append('<h4>トピックスへのショートカット</h4>');
			str.append('<ol>');
			for (var j=0; j < lsize; j++) {
				var obj = topicsList[j];
	
				str.append('<li>');
				str.append('<p>');
				str.append('<a href="viewtopic.php?f=');
				str.append(forum.id);
				str.append('&amp;t=');
				str.append(obj.topicId);
				str.append('">');
				str.append(obj.title);
				str.append('</a>&nbsp;&nbsp;');
				str.append('(').append(obj.date).append(')');
				str.append('</p>');
				str.append('</li>');
			}
			str.append('</ol>');
			
			$('l_' + forum.id).innerHTML = str.toString();
		}
	}
}

/**
 * Topics Object define
 */
function Topics() {
	this.topicId = null;
	this.title = null;
	this.date = null;
}

/**
 * Forum Class
 */
function Forums() {
	this.id = null;
	this.topicsList = [];
}

Forums.prototype.setTopics = function(obj) {
	this.topicsList.push(obj);
	return this;
}

Forums.prototype.getTopics = function() {
	return this.topicsList;
}

/**
 * Utilies
 */
var DOM = {
	getText : function(node) {
		var child = node.childNodes;

		for ( var i = 0; i < child.length; i++) {
			var d = child[i];
			if (d.nodeType == 3 || d.nodeType == 4) {
				return d.nodeValue;
			}
		}
	}
}

//StringBuffer for Javascript
function StringBuffer() {
	this.buffer = [];
}

StringBuffer.prototype.append = function(str) {
	this.buffer.push(str);
	return this;
}

StringBuffer.prototype.toString = function() {
	return this.buffer.join("");
}
