Arcazoid = {
	initPayoff: function() {
		var el = document.getElementById("payoff");
		var repeat = 3;
		if (navigator.userAgent.indexOf("MSIE") > 0 && navigator.userAgent.indexOf("Opera") == -1 && parseInt(navigator.appVersion) < 7) { // hack for IE
			var src = el.currentStyle.backgroundImage;
			for (var i = 0; i < repeat; i++) {
				var img = document.createElement("img");
				img.src = src.substring(5,src.lastIndexOf("\""));
				el.appendChild(img);
			}
			el.style.background = "none";
		}
		el.x = 0;
		el.scroll = function() {
			if (this.x <= -Math.round(this.offsetWidth / repeat)) this.x = 0;
			this.style.marginLeft = this.x-- + "px";
			setTimeout(function() { el.scroll(); }, 50);
		};
		el.scroll();
	},
	
	initStatsBar: function() {
		var els = getChildElements(getChildElements(document.getElementById("statistics"))[0]);
		for (var i = 0, l = els.length; i < l; i++) {
			var el = els[i];
			el.items = el.getElementsByTagName("li");
			el.next = (i < l - 1) ? els[i + 1] : els[0];
			for (var j = 0, m = el.items.length; j < m; j++) {
				var item = el.items[j];
				item.parent = el;
				item.next = (j < m - 1) ? el.items[j + 1] : false;
				item.scrollIn = function() {
					var el = this;
					var x = Math.max(0, this.offsetLeft - 15);
					this.style.left = x + "px";
					if (x > 0) setTimeout(function() { el.scrollIn(); }, 50);
					else setTimeout(function() { el.scrollOut(); }, 1500);
				};
				item.scrollOut = function() {
					var el = this;
					var y = Math.min(this.offsetHeight, this.offsetTop + 2);
					this.style.top = y + "px";
					if (y < this.offsetHeight) setTimeout(function() { el.scrollOut(); }, 50);
					else {
						this.style.left = null;
						this.style.top = null;
						this.next ? this.next.scrollIn() : this.parent.scrollOut();
					}
				};
			}
			el.scrollIn = function() {
				var el = this;
				var x = Math.max(0, this.offsetLeft - 15);
				this.style.left = x + "px";
				if (x > 0) setTimeout(function() { el.scrollIn(); }, 50);
				else if (this.items.length > 0) setTimeout(function() { el.items[0].scrollIn(); }, 500);
				else setTimeout(function() { el.scrollOut(); }, 4000);
			};
			el.scrollOut = function() {
				var el = this;
				var x = Math.max(-this.offsetWidth, this.offsetLeft - 15);
				this.style.left = x + "px";
				if (x > -this.offsetWidth) setTimeout(function() { el.scrollOut(); }, 50);
				else {
					this.style.left = null;
					this.next.scrollIn();
				}
			};
		}
		els[0].scrollIn();
	},
	
	initPanels: function() {
		var els = document.body.getElementsByTagName("div");
		for (var i = 0; i < els.length; i++) {
			if (els[i].className != "panel") continue;
			els[i].getElementsByTagName("div")[0].onclick = function() {
				this.parentNode.className = (this.parentNode.className == "panel") ? "panel panel-collapse" : "panel";
			};
			if (els[i].id == "my-messages") els[i].className = "panel panel-collapse";
		}
	},
	
	processLinks: function() {
		var els = document.body.getElementsByTagName("a");
		for (var i = 0; i < els.length; i++) {
			if (els[i].rel == "external") els[i].target = "_blank";
		}
	},
	
	showNotification: function() {
		var el = document.getElementById("system-message");
		if (!el) return false;
		el.style.top = Math.min(document.documentElement.offsetHeight, document.body.offsetHeight) / 2 - el.offsetHeight / 2 + Math.max(document.documentElement.scrollTop, document.body.scrollTop) - 96 + "px";
		if (window.onresize != Arcazoid.showNotification)
			window.onresize = window.onscroll = Arcazoid.showNotification;
	},
	
	emphNewMessages: function() {
		var el = document.getElementById("my-messages-new");
		if (el && el.className.indexOf("one-or-more") > -1) {
			for (var i = 1; i <= 5; i++) {
				setTimeout(function() { el.style.visibility = "hidden"; }, 500 * i);
				setTimeout(function() { el.style.visibility = "inherit"; }, 500 * i + 250);
			}
		}
	}
};

Arcazoid.Avatar = function(id, set) {
	var avatar = this;
	this.id = id;
	this.set = set ? set : {type: "wm", face: 0, hair: 0, trunk: 0, legs: 0, feet: 0};
	
	this.setSet = function(set) {
		avatar.setType(set.type);
		for (var o in set) if (o != "type") avatar.setPart(o, set[o]);
	};
	
	this.setType = function(type) {
		var el = document.getElementById(id);
		var els = el.getElementsByTagName("div");
		for (var o in avatar.set) avatar.set[o] = 0;
		avatar.set.type = type;
		el.className = "avatar avatar-" + type;
		for (var i = 0; i < els.length; i++)
			if (els[i].className.indexOf("part") > -1)
				avatar.setPart(els[i].className.substring(5), 0); 
		document.getElementById(id + "-set-type").value = type;
	};
	
	this.setPart = function(part, i) {
		if (part == "bare" || part == "head") return false;
		var el = document.getElementById(id + "-" + part);
		var last = Arcazoid.Avatar.ini[avatar.set.type][part] - 1;
		if (i < 0) avatar.set[part] = last;
		else if (i > last) avatar.set[part] = 0;
		else avatar.set[part] = i;
		el.style.backgroundPosition = -25 * avatar.set[part] + "px 0";
		document.getElementById(id + "-set-" + part).value = avatar.set[part];
	};
	
	this.choosePreviousType = function() {
		var type = null;
		for (var t in Arcazoid.Avatar.ini) {
			if (type != null && t == avatar.set.type) break;
			type = t;
		}
		avatar.setType(type);
	};
	this.chooseNextType = function() {
		var type = null;
		var match = false;
		for (var t in Arcazoid.Avatar.ini) {
			if (type == null || match) type = t;
			match = (t == avatar.set.type);
		}
		avatar.setType(type);
	};
	this.choosePreviousPart = function(part) {
		avatar.setPart(part, avatar.set[part] - 1);
	};
	this.chooseNextPart = function(part) {
		avatar.setPart(part, avatar.set[part] + 1);
	};
};
Arcazoid.Avatar.getRandomSet = function() {
	var ini = Arcazoid.Avatar.ini;
	var types = [];
	for (var o in ini) types[types.length] = o;
	var type = types[Math.floor(Math.random() * types.length)];
	var set = {type: type};
	for (var o in ini[type]) set[o] = Math.floor(Math.random() * ini[type][o]);
	return set;
};
Arcazoid.Avatar.ini = {
	wm: {face: 5, hair: 11, trunk: 7, legs: 7, feet: 5},
	bm: {face: 5, hair: 11, trunk: 7, legs: 7, feet: 5},
	am: {face: 5, hair: 11, trunk: 7, legs: 7, feet: 5},
	wf: {face: 5, hair: 9, trunk: 7, legs: 7, feet: 7},
	bf: {face: 5, hair: 9, trunk: 7, legs: 7, feet: 7}
};
var avatar = new Arcazoid.Avatar("avatar");

function getChildElements(el, s) {
	var nodes = el.childNodes;
	var els = [];
	for (var i = 0, l = nodes.length; i < l; i++)
		if (nodes[i].nodeType == 1 && (!s || nodes[i].tagName.toLowerCase() == s)) els[els.length] = nodes[i];
	return els;
};

function queryElementById(id, fn) {
	var el = document.getElementById(id);
	if (!el && !window.loaded) setTimeout(function() { queryElementById(id, fn); }, 200);
	else if (el && fn) fn();
}

window.loaded = false;
window.onload = window.onstop = function() {
	this.loaded = true;
};

queryElementById("statistics", Arcazoid.initStatsBar);
queryElementById("payoff", Arcazoid.initPayoff);
queryElementById("my-messages-new", Arcazoid.emphNewMessages);
queryElementById("footer", Arcazoid.initPanels);
queryElementById("footer", Arcazoid.processLinks);
queryElementById("footer", Arcazoid.showNotification);
queryElementById("avatar", function() {
	avatar.setSet(Arcazoid.Avatar.getRandomSet());
});