/* ------------------------------------
	Smooth TopButton Scroller
	(c) 2004 Netdesign
	2004/06/04 M. Sgaier
------------------------------------ */
//  edited for XHTML documents

var agent	   	   = window.navigator.userAgent; 
var isMac     	   = ((agent.indexOf("Mac")!=-1)||(agent.indexOf("mac")!=-1)); 
var isIE6 		   = ( agent.indexOf("MSIE 6") != -1 );

var iconsOffsetTop = 200; //6;
var scrollOffset   = 1; // 200 pixels the document has to scroll before our little icon pops up

var contentWidth   = 779; // this is the size of the table with all the content
var iconPadding    = 741; // this actually is the offset of the icon relative to the content's startoffset
var mozPadding 	   = 16;  // Mozilla correction on above's padding

var fader 	  	   = null;
var fadespeed 	   = 20;
var fadestep	   = 10;

var scroller	   = null;
var scrollstep	   = 40;
var scrollint	   = 30;
var scrollwait     = 500;

// init the to-top button
initUpIcon();

function scrollToTop () {
	hideIcon();
	smoothScroll();
}
function getPageOffset() {
	return (isIE6) ? document.documentElement.scrollTop : ((document.body) ? document.body.scrollTop : document.documentElement.scrollTop);
}

function smoothScroll() {
	docheight = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	pageoffset = getPageOffset();
	step = (pageoffset <= scrollstep) ? Math.round(pageoffset / 2) : ( ( pageoffset >  docheight) ?  docheight : scrollstep );	
	new_y = pageoffset - step; //(pageoffset < step) ? pageoffset - step : 0;
	if (new_y<0) new_y = 0;
	window.scrollTo(0, new_y);
	if (new_y>0) setTimeout("smoothScroll()", scrollint);
}

function smoothIconScroll() {
	clearTimeout(scroller);
	scroller = setTimeout("positionIcon()", scrollwait);
}

function iconup() {
	return (document.all) ? document.all.iconup : document.getElementById("iconup");
}

function iconimage() {
	return (document.all) ? document.all.iconimage : document.getElementById("iconimage");
}

function initUpIcon() {
	//preload
	upImg =  new Image(); upImg.src = "/img/icon_top.gif"; 
	upImgF = new Image(); upImgF.src = "/img/icon_top_f.gif";
	// write the icon on screen
	document.write ('<div id="iconup" style="visibility: hidden; z-index: 10; position: absolute; width: 31px; left: 0px; top: 0px;">' +
					'	<a title="Terug naar boven" href="javascript:scrollToTop();" onmouseover="document.iconimage.src=upImgF.src; window.status=\'Scroll naar het begin van de pagina\';return true" onmouseout="document.iconimage.src=upImg.src; window.status=\'\'; return true">' +
					'		<img id="iconimage" name="iconimage" src="/img/icon_top.gif" border="0" style="-moz-opacity:10%; filter:alpha(opacity=10)" />' +
					'	</a>' +
					'</div>\n');				

	// set scroll & resize event
	if (document.all) {
		window.onscroll  = smoothIconScroll;
		window.onresize  = positionIcon;
	}
	else { // Moz
		window.onmousemove = smoothIconScroll;
		window.onresize    = positionIcon;
	}
}

function positionIcon() {
	clearTimeout(scroller);
	pageoffset = getPageOffset();
	
	// check position
	if (document.all) {
		if (pageoffset<scrollOffset) {
			fadeOutIcon(); 
			return;
		}
		else showIcon();
	}
	else {
		if (window.scrollY<scrollOffset) {
			fadeOutIcon(); 
			return;
		}
		else showIcon();
	}

	// place icon
	if (isMac) {
		placeIcon(document.body.scrollTop);
	}
	else if (document.all) {
		placeIcon(pageoffset);
	}
	else {
		placeIcon(window.scrollY);
	}
}

function placeIcon(offset) {
	if (!iconup()) return;
	else {
		// calculate left offset
		docwidth 	 = (window.innerWidth) ? window.innerWidth - mozPadding : document.body.clientWidth;
		leftOffset 	 = Math.round( (docwidth-contentWidth>0) ? (docwidth-contentWidth)/2 + iconPadding : iconPadding );
		iconup().style.left = leftOffset+"px";
		if (iconup().style.visibility == "hidden") {
			fadeIcon(0);
			obj.style.top = (offset+iconsOffsetTop)+"px";
			showIcon();
			fadeInIcon();
			}
		else {
			moveIconTo(offset+iconsOffsetTop);
		}
	}
}

function moveIconTo(goal_y) {
	if (!iconup()) return;
	docheight = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	cur_y = parseInt( iconup().style.top.substring(0, iconup().style.top.length-2) );
	distance = Math.abs(cur_y - goal_y);
	step = (distance <= scrollstep) ? Math.round(distance / 2) : ( ( distance >  docheight) ?  docheight : scrollstep );
	new_y = (cur_y < goal_y) ? cur_y + step : cur_y - step;
	//window.status = cur_y +" -> "+ new_y +" -> "+ goal_y;
	//if (new_y < scrollOffset) new_y = scrollOffset;
	if ( ((cur_y > goal_y) && (new_y <= goal_y)) || ((cur_y < goal_y) && (new_y >= goal_y)) ) {
		iconup().style.top = goal_y+"px";
		// if (isIE6) window.status += " CHECK";
		fadeIcon(goal_y - scrollOffset);
	}		
	else {
		iconup().style.top = new_y+"px";
		fadeIcon(new_y - scrollOffset);
		if (goal_y != new_y) scroller = setTimeout("moveIconTo("+goal_y+")", scrollint);
		// window.status += " MOVE! ("+new_y+") "  +scroller;
	}
	
}

function hideIcon() {
	if (!iconup()) return;
	iconup().style.visibility = "hidden"; 
//	moveIconTo(scrollOffset);
}
function showIcon() {
	if (!iconup()) return;
	iconup().style.visibility = "visible"; 
}
function fadeIcon(perc) {
	showIcon();
	if (perc>100) perc = 100;
	if (perc<0) perc = 0;
	if (iconimage().filters) 
		iconimage().filters[0].opacity = perc;
	else
		iconimage().style.MozOpacity = perc+"%";
}
function fadeOutIcon(msg) {
	if (!iconimage().filters) {
		hideIcon();
		return;
	}
	perc = iconimage().filters[0].opacity - fadestep;
	if (perc < 0) perc = 0;
	iconimage().filters[0].opacity = perc;
	if (perc > 0) setTimeout("fadeOutIcon('')", fadespeed);
	else hideIcon();
}

function fadeInIcon() {
	if (!iconimage().filters) {
		showIcon();
		return;
	}
	perc = iconimage().filters[0].opacity + fadestep;
	if (perc > 100) perc = 100;
	iconimage().filters[0].opacity = perc;
	if (perc < 100) setTimeout("fadeInIcon()", fadespeed);
	showIcon();
}