var scroll_to = false;
var last_scroll = false;
var scroll_sign = false;

function pageScroll(id) 
{
	scroll_sign = 1;
	
	var clientHeight = document.documentElement.clientHeight;
	var documentHeight = getDocumentHeight();
	
	var current = getScrollTop();
		
	if (id == 'top') {
		scroll_sign = -1;
		scroll_to = current;
	} else {
		var item = document.getElementById('a-' + id);
		item.style.position = "absolute";

		scroll_to = item.offsetTop - 15;
		
		if (scroll_to > documentHeight - clientHeight)
			scroll_to = documentHeight - clientHeight + 10;
	
		item.style.position = "";		
	}
	
	startScroll(id, 4);
}

function startScroll(id, step) 
{
	var current = getScrollTop();
	
	if (scroll_sign == -1) {
		current = scroll_to - current - 10;
	}
	
	if (current + step > scroll_to)
		step = scroll_to - current;

	window.scrollBy(0, step * scroll_sign);
	
	if (current < scroll_to - 10 && current + step != last_scroll) {
		var next_step = (current > scroll_to / 2) ? (scroll_to - current) / 10 : step + 4;
		
		last_scroll = current + step;
		
		setTimeout("startScroll('" + id + "', " + next_step + ")", 20);
	}
	
}

function getScrollTop()
{
	var ScrollTop = document.body.scrollTop;

	if (ScrollTop == 0)
	{
		 if (window.pageYOffset)
			  ScrollTop = window.pageYOffset;
		 else
			  ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}

	return ScrollTop;
}

function getDocumentHeight()
{
	 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)
    );
}


