// Constructor function for the chaser object
// Arguments:
//		iTopMargin:		the distance from top of the page (ie. the
//						menu bar) that the chaser should sit
//		iCallRate:		the maximum call rate in milliseconds
//		iCeiling:		the 'at rest' position of the chaser
//		iSlideTime:		the length of time of the animation (ms)
//		sChaserId:		the id of the chaser div
//

function chaser(iTopMargin, iCallRate, iCeiling, iSlideTime, sChaserId)
{
	if (sChaserId == 'NULL')
		return false;

	this.topMargin = iTopMargin;
	this.callRate = iCallRate;
	this.ceiling = iCeiling;
	this.slideTime = iSlideTime;
	this.isIE = document.all ? true : false;
	this.isNN4 = document.layers ? true : false;
	this.maxDifference = document.all ? document.body.clientHeight : window.innerHeight;
	this.chaserDiv = returnLayerFromId(sChaserId);

	/*this.currentY;
	this.scrollTop;
	this.targetY;
	this.A;
	this.B;
	this.C;
	this.D;
	*/

	return this;
}

new chaser(0, 0, 0, 0, 'NULL');
chaser.prototype.loop = chaserLoop;
chaser.prototype.initialise = chaserInit;
chaser.prototype.slide = chaserSlide;

function chaserLoop()
{
	var iNewTargetY;

	this.currentY = this.isNN4 ? this.chaserDiv.top : parseInt(this.chaserDiv.style.top);
	this.scrollTop = this.isIE ? document.body.scrollTop : window.pageYOffset;

	iNewTargetY = Math.max(this.scrollTop + this.topMargin, this.ceiling);

	if (iNewTargetY != this.currentY)
	{
		if (iNewTargetY != this.targetY)
		{
			this.targetY = iNewTargetY;
			this.initialise();
		}
		this.slide();
	}
}

function chaserInit()
{
	var dNow = new Date();

	this.A = this.targetY - this.currentY;
	this.B = Math.PI / (2 * this.slideTime);
	this.C = dNow.getTime();
	this.D = this.currentY;

	if (Math.abs(this.A) > this.maxDifference)
	{
		this.D = this.A > 0 ? this.targetY - this.maxDifference : this.targetY + this.maxDifference;
		this.A = this.A > 0 ? this.maxDifference : -this.maxDifference;
	}
	else
		this.D = this.currentY;
}

function chaserSlide()
{
	var dNow = new Date();
	var iNewY = this.A * Math.sin(this.B * (dNow.getTime() - this.C)) + this.D;

	iNewY = Math.round(iNewY);

	if ((this.A > 0 && iNewY > this.currentY) || (this.A < 0 && iNewY < this.currentY))
	{
		if (this.isNN4)
			this.chaserDiv.top = iNewY;
		else
			this.chaserDiv.style.top = iNewY;
	}
}

function returnLayerFromId(sLay)
{
	var oLay;

	if (parseInt(navigator.appVersion) < 4)
		return null;

	if (sLay.length < 1)
		return null;

	if (document.layers)
		oLay = document.layers[sLay];
	else
	{
		if (document.all)
			oLay = document.all(sLay);
		else
			oLay = document.getElementById(sLay);
	}

	return oLay;
}

