var tup;
var tdn;
var theinterval;
var theamt;

function doScrollUpReset() {
	window.clearInterval(tup);
	doMOverUp();
}

function doScrollDnReset() {
	window.clearInterval(tdn);
	doMOverDn();
}

function doPrevLnk() {
	window.clearInterval(tup);

	var tf = top.frames[1];
	var links = tf.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 == '<')) {
					tup=window.setInterval(doScrollUpReset,1700);
					tf.location.href = links[i].href;
					return true;
				}
			}
		}
	}
}

function doNextLnk() {
	window.clearInterval(tdn);

	var tf = top.frames[1];
	var links = tf.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 == '>')) {
					tdn=window.setInterval(doScrollDnReset,1700);
					tf.location.href = links[i].href;
					return true;
				}
			}
		}
	}
}

function doScrollUp() {
	var tf = top.frames[1];
	var scry = 0;
	if((!tf.scrollY) && (tf.document.documentElement || tf.document.body)) {
		scry = (tf.document.documentElement ? tf.document.documentElement.scrollTop : tf.document.body.scrollTop);
	} else if(tf.scrollY) {
		scry = tf.scrollY;
	}

	if(scry <= 0) {
		window.clearInterval(tup);
		tup=window.setInterval(doPrevLnk,1500);
	} else {
		tf.scrollBy(0,-theamt);
	}
}

function doScrollDn() {
	var tf = top.frames[1];
	var scry = 0;
	var maxY = 1;
	if((!tf.scrollY) && (tf.document.documentElement || tf.document.body)) {
		scry = (tf.document.documentElement ? tf.document.documentElement.scrollTop : tf.document.body.scrollTop);
	} else if(tf.scrollY) {
		scry = tf.scrollY;
	}
	if((!tf.scrollMaxY) && (tf.document.documentElement || tf.document.body)) {
		maxY = (tf.document.documentElement ? (tf.document.documentElement.scrollHeight - tf.document.documentElement.clientHeight) : (tf.document.body.scrollHeight - tf.document.body.clientHeight));
	} else if(tf.scrollMaxY) {
		maxY = tf.scrollMaxY;
	}

	if(scry >= maxY) {
		window.clearInterval(tdn);
		tdn=window.setInterval(doNextLnk,3000);
	} else {
		tf.scrollBy(0,theamt);
	}
}

doMOverUp = function() {
	tup=window.setInterval(doScrollUp,theinterval);
}

doMOutUp = function() {
	window.clearInterval(tup);
}

doMOverDn = function() {
	tdn=window.setInterval(doScrollDn,theinterval);
}

doMOutDn = function() {
	window.clearInterval(tdn);
}

doMOverPgLd = function() {
	var prtsize = readCookie('prtsize');
	if((prtsize == null) || (prtsize == 0)) {
	} else if(prtsize == 1) {
	} else {
		theinterval = 100;
		theamt = 20;
		//TODO: Move below the if else scrolling at all print sizes
		var curelem = document.getElementById("upscroll");
		XBrowserAddHandlerNew(curelem,'mouseover',doMOverUp);
		XBrowserAddHandlerNew(curelem,'mouseout',doMOutUp);

		curelem = document.getElementById("dnscroll");
		XBrowserAddHandlerNew(curelem,'mouseover',doMOverDn);
		XBrowserAddHandlerNew(curelem,'mouseout',doMOutDn);
	}
}

window.doMOverPgLdHdlr = doMOverPgLd;
XBrowserAddHandler(window,'load','doMOverPgLdHdlr');
