window.onscroll = changeBothTargets;
window.onload = bodyOnLoad;
window.onresize = bodyOnResize;

function changeBothTargets(){
	initTopKnop();
}

function bodyOnLoad(){
	if(!document.all){
		NSWatchScroll();
	}
	initTopKnop();
}

function bodyOnResize(){
	if(document.layers){
		location.reload();
	}else{
		initTopKnop();
	}
}

if(document.all){
	getWindowHeight=new Function('return window.document.body.clientHeight;');
	getPageHeight=new Function('return window.document.body.scrollHeight;');
	getScrollY=new Function('return window.document.body.scrollTop;');
}else{
	getWindowHeight=new Function('return window.innerHeight;');
	getPageHeight=new Function('return window.document.height;');
	getScrollY=new Function('return window.pageYOffset;');
}

function getPageX(){
	var t=0;
	for(var l=arguments[0];l;l=l.offsetParent){
		t+=l.offsetLeft;
	}
	return t;
}

function getPageY(){
	var t=0;
	for(var l=arguments[0];l;l=l.offsetParent){
		t+=l.offsetTop;
	}
	return t;
}

if(window.document.layers){
	window.getPageX=function(){
		if(arguments[0].x)
			return arguments[0].x;
		var t=0;
		for(var l=arguments[0];l;l=l.offsetParent){
			t+=l.offsetLeft;
		}
		return t;
	};
	window.getPageY=function(){
		if(arguments[0].y)
			return arguments[0].y;
		var t=0;
		for(var l=arguments[0];l;l=l.offsetParent)
		{
			t+=l.offsetTop;
		}
		return t;
	};
}

var ScrollLastX=0;
var ScrollLastY=0;
function NSWatchScroll(){
	if(ScrollLastX!=window.pageXOffset||ScrollLastY!=window.pageYOffset){
		ScrollLastX=window.pageXOffset;
		ScrollLastY=window.pageYOffset;
		if(window.onscroll){
			window.onscroll();
		}
	}
	setTimeout('NSWatchScroll()',400)
}

var lift=40;
if(navigator.appName=="Netscape"&&parseFloat(navigator.appVersion)>=5){
	lift+=16;
}else if(navigator.appName=="Microsoft Internet Explorer"&&navigator.appVersion.toLowerCase().indexOf('mac')>=0){
	lift+=21;
}
var newTop=0;
var active=false;
if(!topKnopScrollMin){
	var topKnopScrollMin=0;
}

function initTopKnop(){
	newTop=getScrollY();
	if(!active&&((navigator.appName=="Microsoft Internet Explorer"&&navigator.appVersion.toLowerCase().indexOf('mac')>=0&&getScrollY()>lift)||(getPageHeight()-lift>getWindowHeight()))){
		if(document.layers){
			document.layers["MenuTop"].left=0;
			document.layers["MenuTop"].top=newTop;
		}else if(document.getElementById){
			document.getElementById("MenuTop").style.left=0;
			document.getElementById("MenuTop").style.top=newTop;
		}
		active=true;
		moveTopKnop();
	}
}

function moveTopKnop(){
	if(active){
		if(document.layers){
			var curTop=document.layers["MenuTop"].pageY;
		}else{
			var curTop=document.getElementById("MenuTop").offsetTop;
		}
		if(curTop!=newTop){
			var stepTop=Math.ceil((newTop-curTop)*0.3);
			var nextTop=(stepTop>0)?curTop+Math.max(1,stepTop):curTop+Math.min(-1,stepTop);
			if(document.layers){
				document.layers["MenuTop"].top=nextTop;
			}else if(document.getElementById){
				document.getElementById("MenuTop").style.top=nextTop;
			}
			setTimeout('moveTopKnop();',50);
		}else{
			setTimeout('moveTopKnop();',250);
		}
	}
}