// scroll.js
//
// 2010 Wim Wepster


function init ()
{
	setNavigation();
	window.onresize = setNavigation;

}

function setNavigation()
{
	var e = document.getElementById('backtotop');
	
	var marginleft = Math.floor((getWindowSize().width-968)/2);
	var positiontop = Math.floor(getWindowSize().height-30);
	
	if (marginleft < 0) { marginleft = 0; }

	e.style.left =  marginleft + 'px';
	e.style.top = positiontop + 'px';
}


function getWindowSize()
{
    if(document.layers || (document.getElementById&&!document.all))
    {
        var width = window.innerWidth;
        var height = window.innerHeight;
    }
    else if(document.all)
    {
        var width = document.documentElement.clientWidth;
        var height = document.documentElement.clientHeight;
    }
        
    return {width:width, height:height}
}	
	
function getWindowCurrentPosition()
{

	if (window.innerHeight)
	{
		var y = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		var y = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		var y = document.body.scrollTop;
	}	

	return {y:y}
}


function initScroll(obj)
{
	// time
	t = {now:0, begin:0, end:0, old:0, relative:0, duration:1500};
	
	// place
	p = {now:{y:0}, begin:{y:0}, midway:{y:0}, end:{y:0}};	

	var d = new Date();
	t.now = d.getTime();
	t.begin = t.now;
	t.end = t.now + t.duration;

	p.begin = {y:getWindowCurrentPosition().y};
	p.end = {y:(getTop(obj).y - 100)};
	p.midway = {y:(p.end.y - p.begin.y)/2};
	
	theScroll = setInterval ('doScroll()', 80);
}

function doScroll()
{
	var d = new Date();

	t.old = t.now;
	t.now = d.getTime();

	t.relative = (((t.now - t.begin) / t.duration) - 0.5) * 20; // -10 -- 10

	if (t.now < t.end)
	{
		if (t.relative<0)
		{
			// fade in
			p.now.y = p.begin.y + ( p.midway.y * Math.pow(2,Math.abs(t.relative+10))) / 1024;
		}
		
		if (t.relative>0)
		{
			// fade uit
			p.now.y = p.end.y - ( p.midway.y * Math.pow (2,Math.abs(t.relative-10)) ) / 1024;
		}
		
		window.scroll(0, p.now.y);
		setNavigation();
	}
	else
	{
		window.clearInterval(theScroll);
	}
}


function getTop(obj)
{
	// gets the top of the referenced item obj

	var e = document.getElementById(obj);
	var y = 0;

	while (e)
	{
	    y += e.offsetTop;
	    e = e.offsetParent;
	}

	return {y:y};
}




