var agent			= window.navigator.userAgent; 
var i				= agent.indexOf("MSIE ")+5;
var isIE5plus		= (parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0);
var isMac			= ((agent.indexOf("Mac")!=-1)||(agent.indexOf("mac")!=-1)); 
var isIE6			= ( agent.indexOf("MSIE 6") != -1 );
var isMozilla		= agent.toLowerCase().indexOf("gecko")!= -1 && agent.toLowerCase().indexOf("firefox") == -1
var isFirefox		= agent.toLowerCase().indexOf("firefox") != -1

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

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

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

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

var leftOffsetSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1 ) ? 1 : 0;

// 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 getPageOffset() {
	if (document.body && document.body.scrollTop && document.body.scrollTop > 0) {
		return document.body.scrollTop;
	} else if (document.documentElement && document.documentElement.scrollTop && document.documentElement.scrollTop > 0) {
		return document.documentElement.scrollTop;
	} else if (window.scrollY > 0) {
		return window.scrollY;
	} else {
		return 0;
	}
}

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

function iconimage() {
	return (document.all) ? document.all.iconimage : document.getElementById("iconimage");
}
/**/
function initUpIcon() {
	// write the icon on screen
	document.write ('<div id="iconup" style="visibility: hidden; z-index: 10; position: absolute; width: 25px; left: 0px; top: 0px;">' +
					'	<a title="Terug naar boven" href="javascript:scrollToTop();">' +
					'		<img id="iconimage" name="iconimage" src="/images/buttons/btnTop.gif" border="0" style="-moz-opacity:10%; filter:alpha(opacity=10)" />' +
					'	</a>' +
					'</div>\n');				

	// set scroll & resize event
	if (document.all) {
		window.onscroll  = smoothIconScroll;
	} else {
		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
		var docwidth 	 = (window.innerWidth) ? window.innerWidth - mozPadding : document.body.clientWidth;
		var leftOffset 	 = Math.round( (docwidth-contentWidth > 0) ? (docwidth-contentWidth)/2 + iconPadding : iconPadding );
		iconup().style.left = leftOffset + "px";
		if (offset < scrollOffset) {
			moveIconTo(scrollOffset);
		} 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;
	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 = 100;
	else
		iconimage().style.MozOpacity = 100+"%";
}


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