
// @param	string
// @return	number

String.prototype.startsWith = function(str){
	return this.indexOf(str) === 0;			// .indexOf() is case sensitive
};


var	keyboard = function(e){

	if (keyboardCache){
		return null;
	}

	switch (e.keyCode){	// keycode table, http://unixpapa.com/js/key.html
		case 0:
		case 16:
		case 17:
		case 18:
		case 91:
		case 92:
		case 93:
		case 219:
		case 220:
		case 224:							// command, branded keys
			keyboardCache = e.keyCode;			// being lazy with setTimeout,
			window.setTimeout('keyboardCache = null', 400);
			break;								// but accurate in many cases
		case 27:							// esc

			if (!colofon.active){
				document.activeElement.blur();

				if (app.entries){				// reset cached focus
					app.entries.lastly = null;
				}

			} else {
				colofon.toggle(e);
			}

			break;
		case 65:							// a
			redirect(document.getElementById('pots'));
			break;
		case 67:							// c
			colofon.toggle(e);
			break;
		case 72:							// h
			redirect(document.getElementById('prev'));
			break;
		case 73:							// i
			redirect(document.getElementById('home'));
			break;
		case 74:							// j

			if (app.pageType === 'index'){
				shiftFocus(1);
			}

			break;
		case 75:							// k

			if (app.pageType === 'index'){
				shiftFocus(-1);
			}

			break;
		case 76:							// l
			redirect(document.getElementById('next'));
			break;
		case 79:							// o
			redirect(document.activeElement);
			break;
	}
},
	keyboardCache = null;


// @param	number or object
// @return	number or redirect

var redirect = function(el){

	if (el === 1){							// = "open (o)" link

		if (app.entries.lastly === null){
			return null

		} else {
			el = app.entries[app.entries.lastly];
		}
	}

	if (!el || !el.href || el.href.startsWith('javascript:void')){
		
		if (el.href === 'javascript:void(colofon.toggle())'){
			colofon.toggle();
		}
		
		return null;
	}

	el = el.href;

	if (el.search(/^(?:https?\:\/\/\S*)/i) === -1){
		el = !el.startsWith('/')
			? 'http://' + self.location.host + '/' + el
			: 'http://' + self.location.host + el;
	}

	self.location.href = el;
};


// @param	number
// @param	number

var shiftFocus = function(n, click){
	var current = app.entries.lastly;

	if (current === null){

		if (app.entries.length === 1){
			current = 0;

		} else {
			current = n === 1
				? 0
				: app.entries.length - 1;
		}

	} else if (	(	!click
				&&	document.activeElement !== app.entries[current]
				)
			|	(	click
				&&	app.entries.time < new Date().getTime()
				)
	){										// re-focus on cached focus
		current = current;

	} else if (current === 0){

		if (app.entries.length !== 1){
			current = n === 1
				? current + n
				: app.entries.length - 1;
		}

	} else if (current === app.entries.length - 1){
		current = n === 1
			? 0
			: current + n;
	} else {
		current+= n;
	}

	app.entries.lastly = current;
	app.entries.tab = null;
	app.entries[current].focus();
};


var focused = function(e){
	var	entries,
		i,
		l,
		target;
	e.stopPropagation();

	if (app.entries.tab){
		entries = app.entries;
		i = 0;
		l = entries.length + 1;
		target = e.target;

		while (--l){

			if (entries[i] === target){
				app.entries.lastly = i;
				break;

			} else {
				++i;
			}
		}

	} else {
		app.entries.tab = 1;
	}
};


var blurred = function(e){
	e.stopPropagation();
	app.entries.time = new Date().getTime() + 400;
};


// @param	number
// @param	number
// @param	string

var flimflam = function(page, total, path){
	var	a,
		f = document.createDocumentFragment(),
		i = 1,
		pagination = document.getElementById('paginate'),
		span;

	++total;
	while (--total){

		if (i === page){
			span = document.createElement('span');
			span.innerHTML = i;				// html: &nbsp; or &#160; unicode: \U00A0
			f.appendChild(span);
			f.appendChild(document.createTextNode(' '));

		} else {
			a = document.createElement('a');
			a.innerHTML = i;
			a.href = path + i + '/';
			f.appendChild(a);
			f.appendChild(document.createTextNode(' '));
		}

		++i;
	}

	pagination.innerHTML = '';
	pagination.appendChild(f);
};


var colofon = {

	toggle: function(e){

		if (!this.active){
			this.cachedFocus = app.pageType === 'intex'
				? app.entries.lastly || document.activeElement
				: document.activeElement;
			document.activeElement.blur();

			this.cachedScroll = {
				'x': window.pageXOffset,
				'y': window.pageYOffset
			};

			this.append();
			return null;
		}

		if (typeof(e) === 'object'){
			e.stopPropagation();

			if (	e.target.href !== undefined	// = (X)
				||	(	e.keyCode !== undefined	// = esc
					&&	e.keyCode !== 0		// when successively selecting text,
					)						// Chrome tends to behave this way
			){
				this.deselect();
			}
		}

		if (!window.getSelection().toString()){
			this.el.style.display = 'none';	// otherwise, Opera won't redraw
			window.setTimeout('colofon.dispose()', 1);
		}
	},

	deselect: function(){

		if (document.selection){
			document.selection.empty();
		}

		window.getSelection().removeAllRanges();
	},

	dispose: function(){					// cause of Opera redraw thang
		window.clearTimeout('colofon.dispose()');
		document.activeElement.blur();
		document.body.removeChild(this.el);
		this.el.style.display = '';			// cause of Opera redraw thang
		window.scrollTo(this.cachedScroll.x, this.cachedScroll.y);
		delete this.active;

		if (this.cachedFocus){
			this.cachedFocus.focus();
		}
	},

	append: function(){

		if (!this.boundToggle){
			this.boundToggle = this.bind(this, this.toggle);
			this.el.addEventListener('click', colofon.boundToggle, false);
		}

		document.body.appendChild(this.el);
		this.active = 1;
	},

	el: (function(){
		var	el = document.createElement('p'),
			str1 = '. <a href=https://github.com/bingobongo/>GitHub</a>.<br>',
			str2 = ' <a href=/speelplaats/2011/07/04/saft/>Saft</a>.</small></span><br>\
		<a href=javascript:void(null)>✖</a>';
		el.setAttribute('id', 'colofon', 0);

		if (navigator.language.toLowerCase().indexOf('de') === 0){
			el.innerHTML = '<span>Dieses Land wird von Markus Mayer bestellt.<br>\
		<small>Anzutreffen hier: Gentzgasse&nbsp;52/2/7, 1180&nbsp;Wien.<br>\
		Die Handynummer ist +43.69981917820 und die<br>\
		E-Mail-Adresse <i>pachter</i> an diese Domain' + str1 + 'Der Inhalt wird verwaltet mit' + str2;

		} else if (navigator.language.toLowerCase().indexOf('en') === 0){
			el.innerHTML = '<span>This land is sown by Markus Mayer.<br>\
		<small>Meet him: Gentzgasse&nbsp;52/2/7, 1180&nbsp;Vienna.<br>\
		His mobile number is +43.69981917820 and the<br>\
		email address <i>pachter</i> at this domain' + str1 + 'Content managed with' + str2;

		} else {
			el.innerHTML = '<span>Dit akker werd zaaid van Markus Mayer.<br>\
		<small>Hier ontmoet je hem: Gentzgasse&nbsp;52/2/7, 1180&nbsp;Wenen.<br>\
		Zijn mobiele nummer is +43.69981917820 en het<br>\
		emailadres <i>pachter</i> op dit domein' + str1 + 'Inhoud beheerd met' + str2;
		}

		return el;
	})(),

	// @param	object
	// @param	method

	bind: function(object, method){
		return function(){
			return method.apply(object, arguments);
		}
	}

};


var domready = function(){
	var	entries,
		i,
		l,
		pageType = app.pageType = document.body.className;
	document.removeEventListener('DomContentLoaded', domready, false);

	if (pageType === 'index'){
		entries = app.entries = document.getElementById(pageType).getElementsByTagName('a');
		i = 0;
		l = entries.length + 1;

		while (--l){
			entries[i].addEventListener('blur', blurred, false);
			entries[i].addEventListener('focus', focused, false);
			++i;
		}

		app.entries.lastly =
		app.entries.time = null,
		app.entries.tab = 1;
	}

	document.addEventListener('keydown', keyboard, false);
},
	app = {};


document.addEventListener('DOMContentLoaded', domready, false);

