resetint = undefined;
var max_top = 0;
var max_bottom = 0;
gotinterval = 0;

function setRightadd () {
	var therightadd = document.getElementById('rightadd');
	if(therightadd && getDocHeight() > 800) {
		if(gotinterval == 1) {
			window.clearInterval(resetint);
		}
		var pox = findPosX(therightadd);
		var poy = findPosY(therightadd);
		max_top = poy;
		max_bottom = getDocHeight() - (therightadd.offsetHeight);
		therightadd.style.marginTop = poy + 'px';
		therightadd.style.zIndex = 2;
		eval('resetint = window.set' + 'Interval("reset_rig' + 'htadd()", 200)');
		gotinterval = 1;
	}
}

function reset_rightadd() {
	var therightadd = document.getElementById('rightadd');
	var theheader = document.getElementById('header');
	var winheight = window.innerHeight || document.documentElement.clientHeight;
	if(therightadd) {
		if(findScrollTop() < max_top) {
			therightadd.style.marginTop =  max_top + 'px';
		}
		else if( findScrollTop() > max_bottom) {
			therightadd.style.marginTop = (max_bottom - (60)) + 'px';
		}
		else {
			therightadd.style.marginTop = (findScrollTop() - (0)) + "px";
		}
	}
}

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	while(1) 
	{
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
			break;
		obj = obj.offsetParent;
	}
	else if(obj.x)
	curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	while(1)
	{
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
			break;
		obj = obj.offsetParent;
	}
	else if(obj.y)
	curtop += obj.y;
	return curtop;
}

function findScrollTop () {
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	var dsoctop=document.all? iebody.scrollTop : pageYOffset;
	return dsoctop;
}


window.onload = setRightadd;
window.onresize = setRightadd;

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}
