﻿
/********************************************************************
* You may use this code for free on any web page provided that     *
* these comment lines and the following credit remain in the code. *
* Keep In View (c) JavaScript-FX. (www.javascript-fx.com)          *
********************************************************************/
JSFX_KeepInView = function(id) {
	var getPageY = function(el) {
		return (el == null) ? 0 : el.offsetTop + getPageY(el.offsetParent);
	};
	var getScrollTop = function() {
		return document.body.scrollTop || document.documentElement.scrollTop
	};
	var el = document.getElementById(id);
	if (el == null)
		return;
	if (el.style.position == "absolute") {
		el.startPageTop = -el.offsetTop;
		el.currentX = el.offsetLeft;
		el.currentY = el.offsetTop;
	} else {
		el.style.position = "relative";
		el.startPageTop = getPageY(el);
		el.currentX = el.currentY = 0;
	};
	el.floatInView = function() {
		var targetY = (getScrollTop() > this.startPageTop) ? getScrollTop() - this.startPageTop : 0;

		// Set fixed ------------
		if (targetY > 0) {
			this.style.position = 'fixed';
			this.style.top = '8px';
		} else {
			this.style.position = '';
			this.style.top = '';
		}
		// ----------------------

		// Float in view --------
		//	this.currentY += (targetY - this.currentY) / 4;
		//	this.style.top = (this.currentY + 8) + "px";
		// ----------------------
	};
	setInterval('document.getElementById("' + id + '").floatInView()', 60);
};