Idler = {
	version: 0.23,

	level: 0,

	init: function() {
		this.console.init();
		this.clock.init();

		this.read('motd');
	},

	read: function(file) {
		var pre = '/v/';

		if (/http:\/\/cos.rummik.com/.test(location.href))
			pre = 'core/sub/cos/';

		if (/http:\/\/localhost/.test(location.href))
			pre = '';

		Ajax.httpRequest(pre+'txt/'+file+'.txt', {
			complete: function(j) {
				Idler.print(j.substr(0, j.length-1));
			}
		});
	},

	functions: {
		start: function() {
			this.levelup(parseInt(Cookie.get('start')), parseInt(Cookie.get('level')));
		},

		levelup: function(start, level) {
			if (Idler.level >= 100)
				return;

			if (typeof(start) != 'number' || isNaN(start))
				start = 0;

			if (typeof(level) != 'number' || isNaN(level))
				level = 1;

			Idler.level = (level <= Idler.level) ? Idler.level+1 : level;
			Idler.clock.reset();

			Cookie.set('level', Idler.level);

			var $n1 = 99310;
			var $n2 = 1.191;
			if (Idler.level < 50)
				Idler.clock.set(start, $n1*Math.pow($n2, Idler.level));
			else if (Idler.level < 75)
				Idler.clock.set(start, ($n1*Math.pow($n2, 50))+($n1*Math.pow($n2, Idler.level-25)));
			else
				Idler.clock.set(start, (($n1*Math.pow($n2, 50))*2)+($n1*Math.pow($n2, Idler.level-50)));

			if (start)
				Idler.print('You are currently level {level}.');
			else
				Idler.print('You\'ve leveled up!\nYou are now level {level}.');
		},
	},

	print: function(text) {
		var text  = text || '';
		var lines = text.split(/\n/);

		for (j in lines)
			this.console.buffer.push(lines[j]);
	},

	console: {
		buffer: new Array(),
		element: null,

		init: function() {
			this.element = document.getElementById('console');
			window.setInterval(function(){ Idler.console.update(); }, 150);
		},

		update: function() {
			if (this.buffer.length) {
				var a, match;
				var text  = this.buffer.shift() + "\n",
				    split = text.split(/(\[.+?::.+?\]|\{.+?\}|<.+?>)/);

				for (var i=0; i<split.length; i++) {
					if (i/2 == Math.round(i/2)) {
						this.element.appendChild(document.createTextNode(split[i]));
						continue;
					}

					if (/\[.+?::.+?\]/.test(split[i])) {
						a     = document.createElement('a');
						match = split[i].match(/\[(.+?)::(.+?)\]/);

						a.href = match[2];

						if (!/^javascript:/.test(match[2]))
							a.target = '_blank';

						a.appendChild(document.createTextNode('[' + match[1] + ']'));
						this.element.appendChild(a);
						continue;
					}

					if (/\{.+?\}/.test(split[i])) {
						match = split[i].match(/\{(.+?)\}/);

						if (/version|level/.test(match[1]))
							this.element.appendChild(document.createTextNode(Idler[match[1]]));
					}

					if (/<.+?>/.test(split[i])) {
						match = split[i].match(/<(.+?)>/);

						if (typeof(Idler.functions[match[1]]) != 'undefined')
							Idler.functions[match[1]]();
					}
				}

				this.element.scrollTop = this.element.scrollHeight + 30;
			}
		},

		clear: function() { // rewrite this to properly clear the contents of the console
			this.element.innerHTML = '<div></div>';
		}
	},

	clock: {
		time: 0,
		end: 0,

		start: new Date().getTime(),

		elements: {
			time: null,
			progress: null,
			percentage: null
		},

		init: function() {
			this.elements.time = document.getElementById('time');

			this.elements.progress = document.createElement('div');
			document.getElementById('progress').appendChild(this.elements.progress);

			this.elements.percentage = document.getElementById('percentage');

			window.setInterval(function(){ Idler.clock.update(); }, 87);
		},

		set: function(time, end) {
			this.start = new Date().getTime()-time;
			this.time = time;
			this.end = end;
		},

		reset: function() {
			this.time = 0;
			this.end = 0;
			this.start = new Date().getTime();
		},

		update: function() {
			var time, percentage;

			if (!this.end)
				return;

			time = new Date();

			this.time = time.getTime()-this.start;

			Cookie.set('start', this.time);

			if (this.time > this.end) {
				this.time = this.end;
				Idler.functions.levelup();
			}

			time.setTime(this.end-this.time);
			
			var hours        = time.getUTCHours() + ((time.getUTCDate()-1)*24),
			    minutes      = time.getUTCMinutes(),
			    seconds      = time.getUTCSeconds(),
			    milliseconds = time.getUTCMilliseconds(),
			    percent      = (this.time/this.end) * 100;
			
			if (percent > 100)
				percent = 100;

			hours        = (hours        < 100) ? '0'+hours        : hours;
			hours        = (hours        < 10)  ? '0'+hours        : hours;
			minutes      = (minutes      < 10)  ? '0'+minutes      : minutes;
			seconds      = (seconds      < 10)  ? '0'+seconds      : seconds;
			milliseconds = (milliseconds < 10)  ? milliseconds+'0' : milliseconds;
			milliseconds = (milliseconds < 100) ? milliseconds+'0' : milliseconds;

			percentage   = Math.round(percent);

			percentage   = (percentage   < 100) ? '0'+percentage   : percentage;
			percentage   = (percentage   < 10)  ? '0'+percentage   : percentage;

			this.elements.time.innerHTML = hours + ':' + minutes + ':' + seconds + '.' + milliseconds;
			this.elements.percentage.innerHTML = percentage + '%';
			this.elements.progress.style.width = percent + '%';
		}
	}
};

window.onload = function() {
	Idler.init();

	if (!(/https?:\/\/localhost/).test(location.href)) {
		var ga = document.createElement('script');

		ga.setAttribute('type', 'text/javascript');
		ga.setAttribute('src', 'http://www.google-analytics.com/ga.js');
		ga.setAttribute('onLoad', 'try{var pageTracker=_gat._getTracker("UA-4843546-1");pageTracker._setDomainName(".rummik.com");pageTracker._trackPageview();}catch(e){}');

		document.body.appendChild(ga);
	}
};
