var wui;

function WUI()
{
	this._header;
	this._status;
	this._menu;
	this._content;
	this._info;
	this._footer;
	this._feed;
	this._feed_list	= [];
	this._feeds;
	this._sm	= [];

	this._idx		= 1;
	this._last_idx		= -1;
	this._feed_menu_id	= "home";
	this._last_A		= -1;

	this.add_info = function(name, content)
	{
		var tr	= document.createElement("tr");
		var th	= document.createElement("th");
		var sep	= document.createElement("td");
		var td	= document.createElement("td");

		if (name == "permalink") {
			content = location.href + content.substring(2,content.length);
		}

		th.innerHTML	= name;
		sep.innerHTML	= ":"
		td.innerHTML	= content;

		tr.appendChild(th);
		tr.appendChild(sep);
		tr.appendChild(td);

		this._info.appendChild(tr);
	}

	this.set_info = function(dom)
	{
		var i = 0;
		var e = dom.getElementsByTagName("head")[0];
		var meta;
		var m_name;
		var m_cntn;

		if (!e) {
			return true;
		}
		if (!this._info) {
			this._info = document.getElementById("wui_info_content");
		}

		this._info.innerHTML = "";

		i = dom.getElementsByTagName("meta");
		if (i.length <= 0) {
			return true;
		}

		for (i = 0; e && i < e.childNodes.length; i++) {
			if ((e.childNodes.item(i).nodeName == "meta")
			|| (e.childNodes.item(i).nodeName == "META")) {
				meta = e.childNodes.item(i);

				m_name = meta.getAttribute("name");
				m_cntn = meta.getAttribute("content");

				this.add_info(m_name, m_cntn);
			}
		}

		this.add_info("permalink", _m[this._idx].link);
	}

	this.set_iframe_content = function(iframe, url)
	{
		this.get_before();

		iframe.height		= 0;
		iframe.scrollHeight	= 0;
		iframe.offsetHeight	= 0;
		iframe.contentWindow.document.body.scrollHeight	= 0;

		iframe.src = url;
	}

	this.set_content = function(divel, url, is_set_info)
	{
		var req = this.get(url);

		divel.innerHTML = req._v.responseText;

		if (!is_set_info || !req._xml) {
			return;
		}

		this.set_info(req._xml);
	}

	this.menu_init = function()
	{
		var i = 0;
		var A;
		var B;
		var pid;
		var div;
		var span;

		while (i < _m.length) {
			A		= _m[i].gid;
			B		= 0;
			pid		= _m[i].pid;

			div		= document.createElement("div");
			div.id		= "menu-"+ A;
			div.className	= "menu"

			this._sm[A]			= new Array();
			this._sm[A][B]			= document.createElement("div");
			this._sm[A][B].className	= "submenu"
			this._sm[A][B].style.zIndex	= B;
			this._sm[A][B].id		= _m[i].pid;

			while (i < _m.length && _m[i].gid == A) {
				if (pid != _m[i].pid) {
					pid = _m[i].pid;

					if (A != 0) {
						this._sm[A][B].style.display = "none";
					}

					div.appendChild(this._sm[A][B]);

					B++;
					this._sm[A][B]		= document.createElement("div");
					this._sm[A][B].className	= "submenu"
					this._sm[A][B].style.zIndex	= B;
					this._sm[A][B].id		= _m[i].pid;
				}

				if (this._sm[A][B].childNodes.length > 0) {
					this._sm[A][B].innerHTML += " &bull; ";
				}

				span		= document.createElement("span");
				span.id		= "menuitem-"+ i;
				span.className	= "menuitem";
				span.innerHTML	+= _m[i].title;
				span.setAttribute("onclick",
					"wui_menu_onclick(wui,"+ i +","+ A +")");

				this._sm[A][B].appendChild(span);

				i++;
			}
			if (A != 0) {
				this._sm[A][B].style.display = "none";
			}

			div.appendChild(this._sm[A][B]);
			this._menu.appendChild(div);
		}
	}
}

WUI.prototype.get_before = function()
{
	if (this._status) {
		this._status.style.visibility = "visible";
	}
}

WUI.prototype.get_after = function()
{
	if (this._status) {
		this._status.style.visibility = "hidden";
	}
}

/**
 * @return	:
 *	< 200	: success.
 *	< -1	: system fail.
 *	< *	: http return code.
 */
WUI.prototype.get = function(uri)
{
	return new function() {
		WUI.prototype.get_before();

		if (window.XMLHttpRequest) {
			this._v = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			this._v = new ActiveXObject("Microsoft.XMLHTTP");
		}

		if (this._v == null || this._v == undefined) {
			WUI.prototype.get_after();
			return;
		}

		this._v.open("GET", uri, false);
		this._v.send();

		if (this._v.status != 200) {
			WUI.prototype.get_after();
			return;
		}

		if (this._v.responseXML == null) {
			if (window.XMLHttpRequest) {
				var domparser = new DOMParser();
				this._xml = domparser.parseFromString(
								this._v.responseText
								, "text/xml");
			} else {
				this._xml = this._v.loadXML(this._v.responseText);
			}
		} else {
			this._xml = this._v.responseXML;
		}

		WUI.prototype.get_after();

		return;
	}
}

function wui_menu_onclick(o, idx, A)
{
	var e;
	var i	= 0;
	var pid	= _m[idx].pid;
	var id	= "";

	if (idx == o._last_idx) {
		return true;
	}

	o._idx = idx;

	/* set content if link is present */
	if (_m[idx].link != "") {
		o.set_iframe_content(o._content, _m[idx].link);
	}

	id	= _m[idx].id;
	e	= document.getElementById(id);
	if (e) {
		e.style.display = "block";
	}

	/* unset last selected item */
	if (A < o._last_A) {
		e = document.getElementById("menuitem-"+ o._last_idx);
		if (e) {
			e.className = "menuitem";
		}
	}

	o._last_A	= A;
	o._last_idx	= idx;

	/* set item selected */
	e = document.getElementById("menuitem-"+ idx);
	if (e) {
		e.className = "menuitem-selected";
	}

	for (i = idx - 1; i >= 0 && _m[i].pid == pid; i--) {
		e = document.getElementById("menuitem-"+ i);
		if (e) {
			e.className = "menuitem";
		}
	}
	for (i = idx + 1; i < _m.length && _m[i].pid == pid; i++) {
		e = document.getElementById("menuitem-"+ i);
		if (e) {
			e.className = "menuitem";
		}
	}

	/* hide others higher level menu */
	A++;
	if (A >= o._sm.length) {
		return true;
	}

	for (i = A; i < o._sm.length; i++) {
		e = document.getElementById("menu-"+ i);
		if (e) {
			if (i == A) {
				e.style.display = "block";
			} else {
				e.style.display = "none";
			}
		}
	}

	for (i = 0; i < o._sm[A].length; i++) {
		if (o._sm[A][i].id == id) {
			o._sm[A][i].style.display = "block";
		} else {
			o._sm[A][i].style.display = "none";
		}
	}
	for (++A; A < o._sm.length; A++) {
		for (i = 0; i < o._sm[A].length; i++) {
			o._sm[A][i].style.display = "none";
		}
	}

	return true;
}

function WUIFeed(feed_list)
{
	this._v = new Array();
	this._o = "";

	this.init = function(feed_list)
	{
		if (feed_list == undefined) {
			return;
		}

		for (var i = 0; i < feed_list.length; i++) {
			this.get(feed_list[i]);
		}

		this._v.sort(this.sort);
		this.generate_output();
	}

	this.get = function(uri)
	{
		var req = WUI.prototype.get(uri);

		if (req._v.status != 200) {
			return req._v.status;
		}

		var xml		= req._xml;
		var type	= xml.firstChild;

		switch (type.nodeName) {
		case "feed":
			this.atom_parsing(xml);
			break;
		case "rss":
			var v = type.getAttribute("version");

			switch (v) {
			case "2.0":
				this.rss20_parsing(xml);
				break;
			default:
				console.warn("Unknown RSS version:"+ v);
				return;
			}
			break;
		default:
			console.warn("Unknown feed type:"+ type.nodeName);
			return;
		}

	}

	this.atom_parsing = function(xml)
	{
		var entries = xml.getElementsByTagName("entry");

		for (var i = 0; i < entries.length; i++) {
			var entry = {};

			entry.type = "atom";

			for (var c = 0; c < entries[i].childNodes.length; c++) {
				var child = entries[i].childNodes[c];

				if (child.nodeType != 1) {
					continue;
				}

				switch (child.nodeName) {
				case "link":
					entry[child.nodeName] = child.getAttribute("href");
					break;
				case "published":
					entry["my_date"] = new Date(child.textContent);
					/* no break */
				default:
					entry[child.nodeName] = child.textContent;
				}
			}

			this._v[this._v.length] = entry;
		}
	}

	this.rss20_str2date = function (str)
	{
		var mm_to_m	= {
				  Jan:"01", Feb:"02", Mar:"03", Apr:"04", May:"05"
				, Jun:"06", Jul:"07", Aug:"08", Sep:"09", Oct:"10"
				, Nov:"11", Dec:"12"
				};
		var arr_date	= str.split(" ");
		var d		= arr_date[1];
		var mm		= arr_date[2];
		var y		= arr_date[3];
		var time	= arr_date[4];
		var gmt_l	= arr_date[5].substring(0,3);
		var gmt_r	= arr_date[5].substring(3,5);
		var sdate	= "";

		sdate = y +"-"+ mm_to_m[mm] +"-"+ d +"T"+ time
			+ gmt_l +":"+ gmt_r;

		return new Date(sdate);
	}

	this.rss20_parsing = function(xml)
	{
		var entries = xml.getElementsByTagName("item");

		for (var i = 0; i < entries.length; i++) {
			var entry = {};

			entry.type = "rss20";

			for (var c = 0; c < entries[i].childNodes.length; c++) {
				var child = entries[i].childNodes[c];

				if (child.nodeType != 1) {
					continue;
				}
				if (child.nodeName == "pubDate") {
					entry["my_date"] = this.rss20_str2date(child.textContent);
				}

				entry[child.nodeName] = child.textContent;
			}

			this._v[this._v.length] = entry;
		}
	}

	this.sort = function(a, b)
	{
		return b.my_date - a.my_date;
	}

	this.generate_output = function()
	{
		for (var i = 0, v; i < this._v.length; i++) {
			v = this._v[i];

			switch(v.type) {
			case "atom":
				this._o	+= "<div class='activity'>"
					+ "<div class='activity_header'>"
					+ "<a href='"+ v.link +"'>"
					+ v.title
					+ "</a>"
					+ "<span class='activity_date'>"
					+ this.date2string(v.my_date)
					+ "</span>"
					+ "</div>"
					+ v.content
					+ "</div>";
				break;
			case "rss20":
				this._o	+= "<div class='activity'>"
					+ "<div class='activity_header'>"
					+ "<a href='"+ v.link +"'>"
					+ v.title
					+ "</a>"
					+ "<span class='activity_date'>"
					+ this.date2string(v.my_date)
					+ "</span>"
					+ "</div>"
					+ v.description
					+ "</div>";
				break;
			}
		}
	}

	this.date2string = function(date)
	{
		var month	= date.getUTCMonth() + 1;
		var day		= date.getUTCDate();
		var hour	= date.getUTCHours();
		var minute	= date.getUTCMinutes();
		var second	= date.getUTCSeconds();

		month	= (month < 10 ? "0" + month : month);
		day	= (day < 10 ? "0" + day : day);
		hour	= (hour < 10 ? "0" + hour : hour);
		minute	= (minute < 10 ? "0" + minute : minute);
		second	= (second < 10 ? "0" + second : second);

		return (date.getUTCFullYear() +"."+ month +"."+ day +" "
			+ hour +":"+ minute +":"+ second);
	}

	this.init(feed_list);
}

function wui_iframe_autoresize(id)
{
	var h;
	var w;
	var frame = document.getElementById(id);

	if (document.getElementById){
		h = frame.contentWindow.document.body.scrollHeight + 30;
	}

	frame.height		= h;
	frame.scrollHeight	= h;
	frame.offsetHeight	= h;

	if (wui != undefined) {
		wui.set_info(frame.contentDocument);
		wui.get_after();
	}
}

function wui_set_content_feed(div_id)
{
	if (div_id != undefined && div_id != "") {
		parent.wui._feed = document.getElementById(div_id);
		parent.wui._feed.innerHTML = parent.wui._feeds._o;
	}
}

MyWUI.prototype			= new WUI();
MyWUI.prototype.constructor	= MyWUI;
MyWUI.prototype.baseClass	= WUI.prototype.constructor;

function MyWUI(cfg)
{
	this._header	= document.getElementById("wui_header");
	this._status	= document.getElementById("wui_status");
	this._menu	= document.getElementById("wui_menu");
	this._content	= document.getElementById("wui_content");
	this._info	= document.getElementById("wui_info_content");
	this._footer	= document.getElementById("wui_footer");
	this._feed	= document.getElementById("my_activity");
	this._feed_list	= cfg.feed_list == undefined ? [] : cfg.feed_list;
	this._feeds	= new WUIFeed(this._feed_list);

	document.title	= cfg.title || "WUI!";

	if (cfg.header != undefined && cfg.header != "") {
		this.set_content(this._header, cfg.header, 0);
	}
	if (cfg.footer != undefined && cfg.footer != "") {
		this.set_content(this._footer, cfg.footer, 0);
	}

	this.menu_init();

	wui_menu_onclick(this, this._idx, _m[this._idx].gid);
}

function body_onload()
{
	wui = new MyWUI({
		title		:"kilabit"
	,	feed_list	:[
			"/my_github_feed.atom"
		,	"/my_reddit_feed.rss"
		,	"/my_codaset_k3pl_feed.atom"
		,	"/my_greader.atom"
		]
	});
}

