/*
		dw_glider.js - requires dw_lib.js
		glide to maintain window location on scroll
		version date: September 2003 

		This code is from Dynamic Web Coding 
    at http://www.dyn-web.com/
    See Terms of Use at http://www.dyn-web.com/bus/terms.html
    Permission granted to use this code 
    as long as this entire notice is included.	
    
    Resources: ypChaser by Aaron Boodman (www.youngpup.net)
    DHTML chaser tutorial at DHTML Lab - www.webreference.com/dhtml		
*/

// create an array called and call it Glider.holder
Glider.holder = [];

	// args: id, left, top, w, h, duration of glide to location onscroll, acceleration factor
  // acceleration factor should be -1 to 1. -1 is full deceleration

function Glider(id, x, y, w, h, d, ac) 
{
  // DEFAULTS
  // var = x || y; if x is not null, assign it, otherwise assign y
	this.glideDur = d || 1000; 
	this.origX = x; 
	this.origY = y; 
	this.ac = -ac || 0;
	this.baseObj = dynObj;
	this.baseObj(id, x, y, w, h);
	// Glider.holder.length is 0
	// Takes the Glider.holder element 0, and assigns to it the Glider object
  Glider.holder[Glider.holder.length] = this;
  
  if (!Glider.winHt) 
  {
  	Glider.winHt = getWinHeight();
  }
}

Glider.prototype = new dynObj;

/* add methods to Glider, in essence, extend class dynObj */

Glider.prototype.onGlideInit = function() 
{
	// nothing
}

Glider.prototype.checkGlider = function() 
{
	var destY = this.origY + getScrollY();

	// while glider is not at destination, keep moving it there
	// if glider is not yet in motion, set it in motion
	// continue glide
	if (destY != this.y) 
	{
		if (destY != this.dy) 
		{
		  //this.dy is the object's variable. assigned after it's been determined that abs of scrolled distance is not 0
			this.dy = destY;
			this.glideInit();
      this.onGlideInit();
		} 
		this.glide();
	}
}

Glider.prototype.glideInit = function() 
{
	this.gt = new Date().getTime();
	var distY = this.dy - this.y;
	
	// sets the distance the glider is to travel to reasonable amount, in the event of
	// a quick, long scroll
	// but what about downward scrolls? makes no diff. the difference in time it takes for the box
	// to reach the steady position is a little big greater when you scroll a whole screen down than when
	// you scroll just enough that the box is off the screen
	if (Math.abs(distY) > Glider.winHt) 
	{	
		this.gsy = (distY > 0) ? this.dy - Glider.winHt: this.dy + Glider.winHt;
	} 
	else 
	{
	  // just assign the glider's current y position
		this.gsy = this.y;
	}
  
  // what is this? acceleration profile?
  this.g_yc1 = this.gsy + ((1 + this.ac) * (this.dy - this.gsy) / 3);
	this.g_yc2 = this.gsy + ((2 + this.ac) * (this.dy - this.gsy) / 3);
}

Glider.prototype.glide = function() 
{
	var elapsed = new Date().getTime() - this.gt;
  
  // if there is still time remaining for the slide, do the bezier slide
  if (elapsed < this.glideDur) 
  {
    var y = dw_Bezier.getValue(elapsed/this.glideDur, this.gsy, this.dy, this.g_yc1, this.g_yc2);
    this.shiftTo(null, y);
  } 
  
  // otherwise move the glider to the final position immediately
  else 
  {
  	this.shiftTo(null, this.dy);
  }
}

Glider.control = function() 
{
  for (var i = 0; i < Glider.holder.length; i++) 
  {
    var curObj = Glider.holder[i];
    if (curObj)
    {
    	curObj.checkGlider();
    }
  }
}
//Glider.timer = setInterval("Glider.control()",20);
dw_Animation.add(Glider.control);

// returns height of window
function getWinHeight() 
{
	var winHt = 0;
	if (window.innerHeight) 
	{
		winHt = window.innerHeight-18;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ 
		winHt = document.documentElement.clientHeight;
	}
	else if (document.body && document.body.clientHeight) 
	{
		winHt = document.body.clientHeight;
	}
	return winHt;
}	

// returns amount of vertical scroll
function getScrollY() 
{
	// .documentElement returns the element that is a direct child of document, which in most cases is the HTML element.
	
	var sy = 0;
	
	if (document.documentElement && document.documentElement.scrollTop)
	{
		sy = document.documentElement.scrollTop;
	}
	else if (document.body && document.body.scrollTop) 
	{
		sy = document.body.scrollTop; 
	}
	else if (window.pageYOffset)
	{
		sy = window.pageYOffset;
	}
	else if (window.scrollY)
	{
		sy = window.scrollY;
	}
	return sy;
}

// onresize, get window height
if (window.addEventListener)
{
  window.addEventListener("resize", function() { Glider.winHt = getWinHeight(); }, "false");
}
else if (window.attachEvent)
{
  window.attachEvent("onresize", function() { Glider.winHt = getWinHeight(); } );
}