
// Revised version of http://www.fiendish.demon.co.uk/html/javascript/layerfloat.html
// Removed horizontal movement and tweaked to work in XHTML rather than just HTML.

/////////////////////////////////////////////////////////////////////

var FloatLayers       = new Array();
var FloatLayersByName = new Array();

function addFloatLayer(n,offY,spd){new FloatLayer(n,offY,spd);}
function getFloatLayer(n){return FloatLayersByName[n];}
function alignFloatLayers() {

	for(var i=0;i<FloatLayers.length;i++)FloatLayers[i].align();
}

/////////////////////////////////////////////////////////////////////

FloatLayer.prototype.setFloatToTop=setTopFloater;
FloatLayer.prototype.setFloatToBottom=setBottomFloater;
FloatLayer.prototype.initialize=defineFloater;
FloatLayer.prototype.adjust=adjustFloater;
FloatLayer.prototype.align=alignFloater;

function FloatLayer(n, offY, spd) {
	this.index=FloatLayers.length;

	FloatLayers.push(this);
	FloatLayersByName[n] = this;

	this.name    = n;
	this.floatY  = 0;
	this.tm      = null;
	this.steps   = spd;
	this.alignVertical  =(offY>=0) ? topFloater : bottomFloater;
	this.ifloatY = Math.abs(offY);
}

/////////////////////////////////////////////////////////////////////

function defineFloater(){
	this.layer  = document.getElementById(this.name);
	this.height = this.layer.offsetHeight;
	this.prevY  = this.layer.offsetTop;
}

function adjustFloater() {
	this.tm=null;
	if(this.layer.style.position!='absolute')return;

	var dy = Math.abs(this.floatY-this.prevY);

	if (dy < this.steps/2)
		cy = (dy>=1) ? 1 : 0;
	else
		cy = Math.round(dy/this.steps);

	if (this.floatY > this.prevY)
		this.prevY += cy;
	else if (this.floatY < this.prevY)
		this.prevY -= cy;

	this.layer.style.top  = this.prevY + 'px';

	if (cy!=0){
		if(this.tm==null)this.tm=setTimeout('FloatLayers['+this.index+'].adjust()',50);
	}else
		alignFloatLayers();
}

function setTopFloater(){this.alignVertical=topFloater;}
function setBottomFloater(){this.alignVertical=bottomFloater;}

function topFloater() {
	if ( document.documentElement && 
		 document.documentElement.scrollTop > 0 ) {
		this.floatY = document.documentElement.scrollTop + this.ifloatY;
	}
	else {
		// Safari needs to use this one but recognises documentElement...
		// The workaround is the scrollTop > 0 above, as Safari will always fall into here.
		this.floatY = document.body.scrollTop + this.ifloatY;
	}
}

function bottomFloater() {
	this.floatY = document.documentElement.scrollTop + document.body.clientHeight - this.ifloatY - this.height;
}

function alignFloater(){
	if(this.layer==null)this.initialize();
	this.alignVertical();
	if(this.prevY!=this.floatY){
		if(this.tm==null)this.tm=setTimeout('FloatLayers['+this.index+'].adjust()',50);
	}
}
