var section = []; // list of anchors added to each entry
var cursection = null;
var oldsection = null;
var refpage = document.referrer;
var abletoscroll = true;

//	Get the event in a browser-happy way.
function getEvent(e) {
	return (e ? e : window.event);
}

//	Get the keycode from the event in a browser-happy way.
function getKeycode(e) {
	if (e && !e.ctrlKey && !e.metaKey && e.keyCode) {
		return e.keyCode;
	}
	if (e && !e.ctrlKey && !e.metaKey && e.which) {
		return e.which;
	}
	return null;
}

//	Get the target of the event in a browser-happy way.
function getTarget(e) {
	if (e && e.target) {
		return e.target;
	}
	if (e && e.srcElement) {
		return e.srcElement;
	}
	return null;
}

//	Process key presses.
function navkey(e) {
	var e = getEvent(e);
	var et = getTarget(e);
	var kc = getKeycode(e);

	if (!e || !et || !kc) {
		//	Don't know why they don't all exist, but error out safely.
		return true;
	}
	else if ((et.nodeName.toLowerCase() == 'input') || (et.nodeName.toLowerCase() == 'textarea')) {
		return true;
	}

	//	NOTE: if you want any of these keycodes to override things like Mozilla's find-as-you-type, then you need
	//	to return false after completing your task.
	if (kc == 46) {
		nextArticle();
		return false;
	}
	else if (kc == 44) {
		prevArticle();
		return false;
	}
	else if (kc == 13) {
		if (et.nodeName.toLowerCase() != 'a') {
			followPost();
			return false;
		}
	}
	else if (kc == 59) {
		prevPage();
		return false;
	}
}

// insert anchor tags at the top of each blog entry
function getentries() {
	var entry = 0;
	var div = document.getElementsByTagName('div');
	for (var i=0; i<div.length; i++) {
		if ((div[i].className == "post") || (div[i].className == "commentbody")) {
			if (div[i].className == "post") {
				var pdiv = div[i].getElementsByTagName('div');
				for (var p=0; p<pdiv.length; p++) {
					if (pdiv[p].className == "title") {
						adiv = pdiv[p].getElementsByTagName('a');
						div[i].permalink = adiv[1];
					}
				}
			}
			section[section.length] = div[i];
		}
	}
}

// Determine the offset from the top of the page
function offsetTop(obj) {
	var y = 0;
	for (; obj; obj=obj.offsetParent) {
		y += obj.offsetTop;
		//	if (obj.scrollTop) y += obj.scrollTop;
	}
	return y;
}

// Get the value of the top left corner of the current viewport relative to the whole page
function getYScroll() {
	var yscroll;
	if (self.pageYOffset) // all except Explorer
	{ yscroll = self.pageYOffset; }
	else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
	{ yscroll = document.documentElement.scrollTop; }
	else if (document.body) // all other Explorers
	{ yscroll = document.body.scrollTop; }
	return yscroll;
}

// scroll back to the previous article
function prevArticle() {
	var ypos = getYScroll();
	var oldsection = cursection;
	for (var i=section.length; --i>=0;) {
		if (offsetTop(section[i]) < ypos) {
			window.scroll(0,offsetTop(section[i]));
			cursection = i;
			//	check to see if we were able to scroll to this post.
			abletoscroll = (getYScroll() == offsetTop(section[i]));
			setFocus();
			break;
		}
	}
}

// advance to the next article
function nextArticle() {
	var ypos = getYScroll();
	var oldsection = cursection;
	for (var i=0; i<section.length; i++) {
		if (offsetTop(section[i]) > ypos) {
			window.scroll(0,offsetTop(section[i]));
			cursection = i;
			//	check to see if we were able to scroll to this post.
			abletoscroll = (getYScroll() == offsetTop(section[i]));
			setFocus();
			break;
		}
	}
}

//	Set the browser focus to the current post.
function setFocus() {
	if (cursection >= 0) {
		if (section[cursection].permalink && (section[cursection].permalink != null)) {
			var anchor = section[cursection].getElementsByTagName('a')[1];
			anchor.focus();
		}
	}
	return true;
}

// Follow a post's link to its permanent archive page
function followPost() {
	if ((cursection != null) && (cursection >= 0) && (section[cursection].permalink) && (section[cursection].permalink != null)) {
		window.location = section[cursection].permalink.href;
	}
}

//	Return to the referring page.
function prevPage() {
	if (refpage != '') {
		window.location = refpage;
	}
}

// hook events
document.onkeypress = navkey;
window.onload = getentries;

