/*  backtotop.js  -  Implementeert het 'terug-naar-begin-pagina' pijltje.
 *  (C) Copyright 2003  Kars Meyboom <kars@kde.nl>
 *
 *  LICENSE:
 * 
 *  This code is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This code is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

// --
// -- GLOBALS
// --

var arrowWidth = 37;
var arrowHeight = 42;

// --
// -- FUNCTIONS
// --

// writes out the necessary HTML for the arrow.
function create_arrow () {
	if (isIE) {
		var position = 'absolute';
	} else {
		var position = 'fixed';
	}

	document.write('<div id="to_top"');
	document.write(' style="position: '+position+'; left: 0px; top: 0px; visibility: hidden">');
	document.write('<a href="#topbar">');
	document.write('<img src="'+baseUrl+'images/newup.gif" ');
	document.write('alt="Terug naar het begin van de pagina" ');
	document.write('title="Terug naar het begin van de pagina" />');
	document.write('</a></div>');
}

// Determines if there is enough content to warrant the appearance
// of the arrow, and if so, places it and makes it visible.
function init_arrow () {
	var clientHeight = document.body.clientHeight;
	var scrollHeight = document.body.scrollHeight;
	var div = document.getElementById('to_top');

	if (scrollHeight <= (1.2 * clientHeight)) {
		// less than two screen-fulls of content
		div.style.visibility = 'hidden';
		div.style.left = '0px';
		div.style.top = '0px';
		return;
	}

	var x = document.body.scrollWidth - arrowWidth - 5;
	var y = document.body.scrollHeight - arrowHeight - 5;

	img.style.left = x+'px';
	img.style.top = y+'px';

	if (isIE) {
		setTimeout('update_arrow()', 500);
	}
}

// Repositions the arrow on window resize events.
// With IE, this function has to be called periodically too,
// as IE doesn't handle the 'position: fixed' style attribute
// correctly.
function update_arrow () {
	if (document.getElementById('to_top') == null) {
		return;
	}

	var div = document.getElementById('to_top');

	var scrollLeft = document.body.scrollLeft;
	var scrollTop = document.body.scrollTop;
	var scrollHeight = document.body.scrollHeight;
	var clientWidth = document.body.clientWidth;
	var clientHeight = document.body.clientHeight;

	if (scrollHeight <= (1.2 * clientHeight)) {
		// less than two screen-fulls of content
		div.style.visibility = 'hidden';
		div.style.left = '0px';
		div.style.top = '0px';
		return;
	}

	div.style.left = (scrollLeft + clientWidth - arrowWidth - 5) + 'px';
	div.style.top = (scrollTop + clientHeight - arrowHeight - 5) + 'px';
	div.style.visibility = 'visible';

	if (isIE) {
		setTimeout('update_arrow()', 500);
	}
}

// --
// -- INITIALISATION CODE
// --

at_preload('create_arrow()');
at_init('update_arrow()');
at_update('update_arrow()');


