/* chaser.js
 * by Aaron Boodman v1.0 000919
 * Copyright (c) 2000 Aaron Boodman. All Rights Reserved.
 * Created for GreatEqualizer.com (http://www.greatequalizer.com/) and
 * documented at DHTML Lab (http://www.webreference.com/dhtml/)
 * License to use is granted if and only if this entire
 * copyright notice is included.
 * This program 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.
*/

// The chaser object. Since we don't anticipate having more
// than one on a page, we don't bother making this into
// a class definition. All necessary properties are set as
// properties of this object to avoid global variables.

function ChaserObj() {

	this.topMargin = 230;
	this.callRate = 10;
	this.slideTime = 1200;
	this.maxDiff = document.all ? document.body.clientHeight : window.innerHeight;
	this.isIE = document.all ? true : false;
	
	this.chaserDiv = buildObjRef("chaserLyr");
	
	this.main = main;
	this.slideInit = slideInit;
	this.slide = slide;
}

// Arrange to have the main loop called as often as possible, 
// but not more often than every 35 milliseconds.
// Even though some OS's can acheive better than that, there's no real reason to
// overload them if its not going to improve the animation quality very much.
// I tested, and better than 35ms doesn't do very much visually.

function initChaser() {
	oChaser = new ChaserObj();
	window.setInterval("oChaser.main()", oChaser.callRate);
}

// Main loop. Updates targetY, and decides whether to start
// the animation over again, continue an existing animation,
// or do nothing at all.
function main()
{

	if (BR.brand=="NS")
		lTopSyntax = "top"
	else
		lTopSyntax = "posTop"

	this.currentY = eval('this.chaserDiv.'+lTopSyntax);
	if (BR.name=="NS6") {
		// ** NS6 - a layer's left coord is returned with the "px" ** //
		// custom logic to remove px and add newY
		this.currentY = parseInt(this.currentY);
	}
	
	this.scrollTop = (BR.brand=="IE") ? document.body.scrollTop : window.pageYOffset;

	var newTargetY	= this.scrollTop + this.topMargin
	
	if ( this.currentY != newTargetY ) {

		if ( newTargetY != this.targetY ) {

			this.targetY = newTargetY
			this.slideInit( )
	
		}

		this.slide( )
		
	}
}

// .slideInit( ). Initializes the slide animation. Sets properties
// of the oChaser object that will represent the various paramaters
// for the sine wave function.
function slideInit()
{
	var now	= new Date( )
	
	this.A		= this.targetY - this.currentY
	this.B		= Math.PI / ( 2 * this.slideTime )
	this.C		= now.getTime( )

	if (Math.abs(this.A) > this.maxDiff) {
		this.D = this.A > 0 ? this.targetY - this.maxDiff : this.targetY + this.maxDiff
		this.A = this.A > 0 ? this.maxDiff : -this.maxDiff
	} else {
		this.D = this.currentY
	}
}



// .slide( ). Moves the oChaser one frame. Its rate decreases and
// is defined by a sine wave.
function slide()
{
	var now	= new Date( )
	var newY	= this.A * Math.sin( this.B * ( now.getTime( ) - this.C ) ) + this.D
	newY		= Math.round( newY )
	
	if (( this.A > 0 && newY > this.currentY ) ||
		( this.A < 0 && newY < this.currentY )) {
			
			if (BR.brand=="NS")
				lTopSyntax = "top"
			else
				lTopSyntax = "posTop"

			if (BR.name=="NS6") {
				// ** NS6 - layer coords are set with the "px" ** //
				eval('this.chaserDiv.'+ lTopSyntax +' = newY + "px";');
			}
			else {
				eval('this.chaserDiv.'+ lTopSyntax +' = newY;');
			}
	}
}