/* ----Pager------
To set up the pager, add a JS variable on your page:

var this.pagerContainerID = 'myContainerID';

Then make sure this container ONLY holds divs you want to page through, and that only 1 is not set to "display:none" when the page loads.
Any content masked by this function will not be visible to a javascript-less browser (i.e. cellphone)

2 buttons need to be included: one with ID nextButton calling getNextNode() and one with ID previousButton calling getPreviousNode() :
<input id="previousButton" type="button" value="Previous" onclick="getPreviousNode()"/>
<input id="nextButton" type="button" value="Next" onclick="getNextNode()"/>
*/
var NodePager = function(containerId, previousButtonSelector, nextButtonSelector, onLoad) {
	this.previousButtonSelector = previousButtonSelector;
	this.nextButtonSelector = nextButtonSelector;
	this.pagerContainerID = containerId;
	Ext.onReady(function() {
		this.setPagerButtons();
		if(typeof(onLoad) == 'function') onLoad(this);
	}, this);
}
NodePager.prototype.setPagerButtons = function() {
	var curr = this.getPagerIndex();
	var max = this.getPagerNodeMax() - 1;
	var prevs = Ext.select(this.previousButtonSelector).each(function(el) {
		el.dom.style.visibility = (curr <= 0)?'hidden':'visible';
	});
	var nexts = Ext.select(this.nextButtonSelector).each(function(el) {
		el.dom.style.visibility = (curr < max)?'visible':'hidden'
	});
}
NodePager.prototype.getPreviousNode = function() {
	var activeDiv = this.getActiveDiv();
	if(activeDiv) {
		if(this.getPreviousElement(activeDiv)) {
			activeDiv.style.display = 'none';
			this.getPreviousElement(activeDiv).style.display = 'block';
			this.setPagerButtons();
		}
	}
}

NodePager.prototype.getNextNode = function() {
	var activeDiv = this.getActiveDiv();
	if(activeDiv) {
		if(this.getNextElement(activeDiv)) {
			activeDiv.style.display = 'none';
			this.getNextElement(activeDiv).style.display = 'block';
			this.setPagerButtons();
		}
	}
}

NodePager.prototype.getActiveDiv = function() {
	var container = document.getElementById(this.pagerContainerID);
	var divs = this.getDirectChildrenByTagName(container, 'div');
	for(var i = 0; i < divs.length; i ++) {
		if(divs[i].style.display.search(/none/i) < 0){
			return divs[i];
		}
	}
}

NodePager.prototype.getNextElement = function(node) {
	if(!node) {
		return null;
	} else if(!node.nextSibling){
		return null;
	} else {
		var originalNode = node;
		do {
			node = node.nextSibling;
		} while(node && node.nextSibling && node.nodeName.search(/div/i) < 0)
		return (node && node.nodeName && node.nodeName.search(/div/i) >= 0 && node.innerHTML != originalNode.innerHTML)?node:null;
	}
}

NodePager.prototype.getPreviousElement = function(node) {
	if(!node) {
		return null;
	} else if(!node.previousSibling){
		return null;
	} else {
		var originalNode = node;
		do {
			node = node.previousSibling;
		} while(node.previousSibling && node.nodeName.search(/div/i) < 0)
		return (node && node.nodeName && node.nodeName.search(/div/i) >= 0 && node.innerHTML != originalNode.innerHTML)?node:null;
	}
}

NodePager.prototype.getPagerNodeMax = function() {
	var container = document.getElementById(this.pagerContainerID);
	return this.getDirectChildrenByTagName(container, 'div').length;
}

NodePager.prototype.getPagerIndex = function() {
	var container = document.getElementById(this.pagerContainerID);
	var activeDiv = this.getActiveDiv();
	var divs = this.getDirectChildrenByTagName(container, 'div');
	var retVal = -1;
	for(var i = 0; i < divs.length; i ++) {
		(activeDiv.innerHTML && divs[i].innerHTML == activeDiv.innerHTML)?retVal = i:null;
	}
	return retVal;
}
NodePager.prototype.getDirectChildrenByTagName = function(node, sTagName) {
	var retVal = new Array();
	if(!Array.prototype.push) {//IE5
		Array.prototype.push = function(oNode) { this[this.length] = oNode;};
	}
	for(var i = 0; i < node.childNodes.length; i ++) {
		(node.childNodes[i].nodeName && node.childNodes[i].nodeName.toLowerCase() == sTagName.toLowerCase())?retVal.push(node.childNodes[i]):null;
	}
	return retVal;
}
