PageNav = Class.create();
PageNav.prototype = {

 	initialize: function(containerId, contentPrefix, contentId, contentTag) {
 		this.container = $(containerId);
 		this.content = $(contentId);
 		
 		this.getElement(contentPrefix);

		if (this.container && this.item) {
			this.getPrevious();
			this.getNext();
			this.writeLinks();
		}
 	},

 	getElement: function(contentPrefix) {
 		var id = document.body.id;
 		if (document.body.className.indexOf(id)<0) {
	 		this.item = $(contentPrefix+id);
	 	}
 	},

 	getPrevious: function() {
 		this.previous = this.item.previousSibling;
 		while (this.previous && (!this.previous.tagName || this.previous.tagName.toLowerCase() != 'li')) this.previous = this.previous.previousSibling;

 		var first = this.item.parentNode.previousSibling;
 		while (first && (!first.tagName || first.tagName.toLowerCase() != 'h2')) first = first.previousSibling;
		if (!this.previous && first) this.previous = first;

		if (this.previous) {
			this.previous = this.writeWrappers(this.previous);
			Element.addClassName(this.previous, 'column first previous');
		}
	},

 	getNext: function() {
 		this.next = this.item.nextSibling;
 		while (this.next && (!this.next.tagName || this.next.tagName.toLowerCase() != 'li')) this.next = this.next.nextSibling;

		if (this.next) {
			this.next = this.writeWrappers(this.next);
			Element.addClassName(this.next, 'column last next');
		}
 	},

 	writeWrappers: function(node) {
		var clone = node.getElementsByTagName('a')[0].cloneNode(true);
		Element.addClassName(clone, 'arrow');

 		var wrapper = Builder.node('div', [clone]);
 		return wrapper;
 	},

 	writeLinks: function() {
 		this.container.appendChild(Builder.node('hr'));
 		if (this.previous && this.previous.nodeType == 1) this.container.appendChild(this.previous);
 		if (this.next && this.next.nodeType == 1) this.container.appendChild(this.next);
	}

}

Event.observe(window, 'load', function() {
	new PageNav('pagenav', 'sn-', 'subnav', 'li');
}, false);
