var l33t = {
	$: function(obj) {
		return document.getElementById(obj);
	},
	boot: function boot(text, time) {
		var progress = '';
		for (var i = 0; i < time; i++) {
			progress += '_';
		}
		this.$('progress').innerHTML = progress;
		this.$('message').innerHTML  = text;
	},
	init: function init() {
		this.$('layout').style.display = 'none';
		this.get('profile');
		setTimeout(function() { l33t.boot('INITIALIZING SYSTEM...', 2); }, 500);
		setTimeout(function() { l33t.boot('LOADING USER PROFILE...', 4); }, 1000);
		setTimeout(function() { l33t.boot('ALLOCATING RESOURCES...', 6); }, 2000);
		setTimeout(function() { l33t.boot('READING MEMORY BUFFER...', 8); }, 3500);
		setTimeout(function() { l33t.boot('BOOT SEQUENCE COMPLETE', 10); }, 4500);
		var t = 0;
		for (var i = 20; i >= 0; i--) {
			if (window.ActiveXObject) {
				setTimeout("l33t.$('splash').style.filter = 'alpha(opacity=" + (i*5) + ")';", 5000+(t*50));
			} else {
				setTimeout("l33t.$('splash').style.opacity = " + (i*0.05) + ";", 5000+(t*50));
			}
			t++;
			if (i < 1) {
				setTimeout(function() {
					l33t.$('splash').style.display = 'none';
					l33t.$('layout').style.display = 'block';
				}, 5500+(t*50));
			}
		}
		setInterval(function() { (Math.random()*10 > 5)? l33t.$('ping').innerHTML = '.' : l33t.$('ping').innerHTML = ':'; }, 300);
		setInterval(function() { (Math.random()*10 > 5)? l33t.$('rpc').innerHTML = '.' : l33t.$('rpc').innerHTML = ':'; }, 400);
		setInterval(function() { (Math.random()*10 > 5)? l33t.$('rest').innerHTML = '.' : l33t.$('rest').innerHTML = ':'; }, 500);
		setInterval(function() { (Math.random()*10 > 5)? (Math.random()*10 > 5)? l33t.$('level').innerHTML = '_' : l33t.$('level').innerHTML = '__' : (Math.random()*10 > 5)? l33t.$('level').innerHTML = '___' : l33t.$('level').innerHTML = '____'; }, 100);
		
		var options = this.$('menu').getElementsByTagName('li'),
			link    = null;
		for (var j = 0; j < options.length; j++) {
			link = options[j].getElementsByTagName('a')[0];
			link.onmouseover = l33t.monitor.on;
			link.onmouseout  = l33t.monitor.off;
			link.onclick     = function() {
				if (this.title.toLowerCase() !== 'execute') {
					return l33t.get(this.title.toLowerCase());
				}
				location.href = 'mailto:contact@jameswatts.info?subject=' + encodeURIComponent('[insert subject here]') + '&body=' + encodeURIComponent('[insert message here]');
			};
		}
	},
	call: function call(data, callback) {
		l33t.$('state').innerHTML = 'LOADING...';
		var request = new XMLHttpRequest();
		var method  = 'GET';
		var url     = '/content/' + data + '.html?cache=' + Math.random();
		request.open(method, url, true);
		request.callback = callback;
		request.onerror  = function(e) {
			l33t.$('state').innerHTML = 'ERROR 500';
		};
		request.onProgress = function(e) {
			this.recieved = e.position;
			this.total    = e.totalSize;
			this.complete = (e.position/e.totalSize)*100;
		};
		request.onreadystatechange = function(e) {
			if (this.readyState == 4) {
				if (this.status == 200) {
					if (typeof this.callback === 'function') {
						this.callback();
					}
				} else {
					this.response = {
						text  : this.responseText,
						status: this.statusText
					};
					l33t.$('state').innerHTML = 'ERROR 404';
				}
			}
		};
		request.send(null);
		return request;
	},
	get: function get(data) {
		this.$('content').innerHTML = '';
		var call = new this.call(data, function() {
			l33t.$('content').innerHTML = this.responseText;
			l33t.$('state').innerHTML   = '';
		});
		return call;
	},
	monitor: {
		on  : function on() {
			l33t.$('monitor-on').style.display  = 'block';
			l33t.$('monitor-off').style.display = 'none';
		},
		off : function off() {
			l33t.$('monitor-off').style.display = 'block';
			l33t.$('monitor-on').style.display  = 'none';
		}
	}
};

