var past_the_end = 0;

function doPrevLnk() {
	var links = document.links;
	for(var i = 0; i < links.length; i++) {
		if((links[i].nodeName.toLowerCase() == 'a') && links[i].hasChildNodes()) {
			var children = links[i].childNodes;
			for (var j = 0; j < children.length; j++)
			{
				if(children[j].nodeType && (children[j].nodeType == 3) && (children[j].nodeValue == '<')) {
					window.self.location.href = links[i].href;
					return true;
				}
			}
		}
	}
}

function doNextLnk() {
	var links = document.links;
	for(var i = links.length-1; i >= 0; i--) {
		if((links[i].nodeName.toLowerCase() == 'a') && links[i].hasChildNodes()) {
			var children = links[i].childNodes;
			for (var j = 0; j < children.length; j++)
			{
				if(children[j].nodeType && (children[j].nodeType == 3) && (children[j].nodeValue == '>')) {
					window.self.location.href = links[i].href;
					return true;
				}
			}
		}
	}
}

doMouseWheel = function(e) {
  e = e ? e : window.event;
  var wheelData = e.detail ? e.detail * -1 : e.wheelDelta / 40;

	var scry = 0;
	var maxY = 1;
	if((!window.self.scrollY) && (window.self.document.documentElement || window.self.document.body)) {
		scry = (window.self.document.documentElement ? window.self.document.documentElement.scrollTop : window.self.document.body.scrollTop);
	} else if(window.self.scrollY) {
		scry = window.self.scrollY;
	}
	if((!window.self.scrollMaxY) && (window.self.document.documentElement || window.self.document.body)) {
		maxY = (window.self.document.documentElement ? (window.self.document.documentElement.scrollHeight - window.self.document.documentElement.clientHeight) : (window.self.document.body.scrollHeight - window.self.document.body.clientHeight));
	} else if(window.self.scrollMaxY) {
		maxY = window.self.scrollMaxY;
	}

	if((wheelData > 0) && (scry <= 0)) {
		past_the_end = past_the_end-1;
		if(past_the_end < -5) {
			doPrevLnk();
		}
	} else if((wheelData < 0) && (scry >= maxY)) {
		past_the_end = past_the_end+1;
		if(past_the_end > 7) {
			doNextLnk();
		}
	} else {
		past_the_end = 0;
	}
}

XBrowserAddHandlerNew(document,'mousewheel',doMouseWheel);
XBrowserAddHandlerNew(window,'mousewheel',doMouseWheel);
