if (navigator.userAgent.toLowerCase().indexOf('gecko') != -1)
	document.writeln('<link rel="stylesheet" type="text/css" href="styles/gecko.css" />');

if(window.document.all && !window.document.getElementById)
	window.document.getElementById = function() {
		return this.all[arguments[0]];
	}

if (document.all) {
	fnGetViewHeight = new Function("return window.document.body.clientHeight;");
	fnGetPageHeight = new Function("return window.document.body.scrollHeight;");
	fnGetScrollTop = new Function("return window.document.body.scrollTop;");
}
else {
	fnGetViewHeight = new Function("return window.innerHeight;");
	fnGetPageHeight = new Function("return window.document.height;");
	fnGetScrollTop = new Function("return window.pageYOffset;");
}

function fnGetPageLeft() {
	var iLeft = 0;
	for (var oElement = arguments[0]; oElement; oElement = oElement.offsetParent)
		iLeft += oElement.offsetLeft;
	return iLeft;
}

function fnGetPageTop() {
	var iTop = 0;
	for (var oElement = arguments[0]; oElement; oElement = oElement.offsetParent)
		iTop += oElement.offsetTop;
	return iTop;
}

var iMinHeight = 150;
var iFromBottom = 40;
var iNewTop = 0;
var bActive = false;

function fnTBInit() {
	iNewTop = Math.max(iMinHeight, (Math.max(iFromBottom - fnGetScrollTop(), 0) + fnGetScrollTop() + fnGetViewHeight() - iFromBottom));
	if (!bActive && ((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.toLowerCase().indexOf("mac") != -1 && fnGetScrollTop() > iFromBottom) || (fnGetPageHeight() - iFromBottom > fnGetViewHeight()))) {
		if (document.getElementById) {
			document.getElementById("topbutton").style.top = iNewTop +"px";
			document.getElementById("topbutton").style.display = "block";
		}
		bActive = true;
		fnTBMove();
	}
}

function fnTBChange() {
	if ((navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.toLowerCase().indexOf("mac") != -1 && fnGetScrollTop() > iFromBottom) || (fnGetPageHeight() - iFromBottom > fnGetViewHeight()))
		fnTBInit();
	else if (bActive) {
		bActive = false;
		document.getElementById("topbutton").style.display = "none";
	}
}

function fnTBMove() {
	if (bActive) {
		var iTop = document.getElementById("topbutton").offsetTop;
		if (iTop != iNewTop) {
			var iStepTop = Math.ceil((iNewTop - iTop) * 0.3);
			var iNextTop = (iStepTop > 0) ? iTop + Math.max(1, iStepTop) : iTop + Math.min(-1, iStepTop);
			document.getElementById("topbutton").style.top = iNextTop;
			setTimeout("fnTBMove();", 50);
		}
		else
			setTimeout("fnTBMove();", 250);
	}
}

window.onload = fnTBInit;
window.onresize = window.onscroll = fnTBChange;