/*   dw_scroll.js    version date: Feb 2004    */



/*************************************************************************

  This code is from Dynamic Web Coding at http://www.dyn-web.com/

  Copyright 2001-4 by Sharon Paine 

  See Terms of Use at http://www.dyn-web.com/bus/terms.html

  regarding conditions under which you may use this code.

  This notice must be retained in the code as is!

*************************************************************************/



dw_scrollLayers = {};

dw_scrollLayer.speed = 70; // default speed for mouseover scrolling

//  constructor arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 

//	id of table or other element that scrolling content is nested in. 

//	ns6+/moz need that extra container to get width for horizontal scrolling.

//	(not needed for vertical scrolling)

function dw_scrollLayer(wnId, lyrId, cntId) {

  this.id = wnId; dw_scrollLayers[this.id] = this;

  this.animString = "dw_scrollLayers." + this.id;

  this.loadLayer(lyrId, cntId);

}



dw_scrollLayer.prototype.loadLayer = function(lyrId, cntId) {

  if (!document.getElementById) return;

  var wndo, lyr;

  if (this.lyrId) {

    lyr = document.getElementById(this.lyrId);

    lyr.style.visibility = "hidden";

  }

  lyr = document.getElementById(lyrId);

  wndo = document.getElementById(this.id);

  lyr.style.top = this.y = 0; lyr.style.left = this.x = 0;

  this.maxY = (lyr.offsetHeight - wndo.offsetHeight > 0)? lyr.offsetHeight - wndo.offsetHeight: 0;

  this.wd = cntId? document.getElementById(cntId).offsetWidth: lyr.offsetWidth;

  this.maxX = (this.wd - wndo.offsetWidth > 0)? this.wd - wndo.offsetWidth: 0;

  this.lyrId = lyrId; // hold id of currently visible layer

  lyr.style.visibility = "visible";

  this.on_load(); this.ready = true;

}



dw_scrollLayer.prototype.on_load = function() {}



dw_scrollLayer.prototype.shiftTo = function(lyr, x, y) {

  lyr.style.left = (this.x = x) + "px"; 

  lyr.style.top = (this.y = y) + "px";

}



// functions for onmouseover scrolling 

// algorithm for time-based scrolling from youngpup.net



// called onmouseover. arguments: direction (possible values: 'up', 'down', 'left', 'right')

// speed (optional) to override default speed (set in dw_scrollLayer.speed)

dw_scrollLayer.prototype.startScroll = function(dir, speed) {

  if (!this.ready) return; if (this.timerId) clearInterval(this.timerId);

  this.speed = speed || dw_scrollLayer.speed;

  this.lyr = document.getElementById(this.lyrId);

  if (dir == "up" || dir == "down") {

    this.xdir = 0; this.endX = parseInt(this.lyr.style.left);

    this.ydir = (dir == "down")? -1: 1; this.endY = (dir == "down")? -this.maxY: 0;

  } else if (dir == "right" || dir == "left") {

    this.ydir = 0; this.endY = parseInt(this.lyr.style.top);

    this.xdir = (dir == "right")? -1: 1; this.endX = (dir == "right")? -this.maxX: 0;

  } else {

    alert("Up, down, left, and right are the only scroll directions currently supported.");

    return; 

  }

  this.lastTime = ( new Date() ).getTime();

  this.on_scroll_start();  

  this.timerId = setInterval(this.animString + ".scroll()", 10); 

}



dw_scrollLayer.prototype.scroll = function() {

  var now = ( new Date() ).getTime();

  var x = this.x + this.xdir * (now - this.lastTime)/1000 * this.speed;

  var y = this.y + this.ydir * (now - this.lastTime)/1000 * this.speed;

  if ( ( this.xdir == -1 && x > -this.maxX ) || ( this.xdir == 1 && x < 0 ) || 

    ( this.ydir == -1 && y > -this.maxY ) || ( this.ydir == 1 && y < 0 ) ) {

    this.lastTime = now;

    this.shiftTo(this.lyr, x, y);

    this.on_scroll(x, y);

  } else {

    clearInterval(this.timerId); this.timerId = 0;

    this.shiftTo(this.lyr, this.endX, this.endY);

    this.on_scroll_end(this.endX, this.endY);

  }

}



dw_scrollLayer.prototype.stopScroll = function() {

  if (!this.ready) return;

  if (this.timerId) clearInterval(this.timerId);

  this.timerId = 0;  this.lyr = null;

}

  

dw_scrollLayer.prototype.on_scroll = function() {}

dw_scrollLayer.prototype.on_scroll_start = function() {}

dw_scrollLayer.prototype.on_scroll_end = function() {}

  

// remove layers from table for ns6+/mozilla (needed for scrolling inside tables)

function GeckoTableBugFix() {

  var i, wndo, holderId, holder, x, y;

	if ( navigator.userAgent.indexOf("Gecko") > -1 ) {

    dw_scrollLayer.hold = []; // holds id's of wndo and its container

    for (i=0; arguments[i]; i++) {

      if ( dw_scrollLayers[ arguments[i] ] ) {

        wndo = document.getElementById( arguments[i] );

        holderId = wndo.parentNode.id;

        holder = document.getElementById(holderId);

        document.body.appendChild( holder.removeChild(wndo) );

        wndo.style.zIndex = 1000;

        x = holder.offsetLeft; y = holder.offsetTop;

        wndo.style.left = x + "px"; wndo.style.top = y + "px";

        dw_scrollLayer.hold[i] = [ arguments[i], holderId ];

      }

    }

   window.addEventListener("resize", rePositionGecko, true);

  }

}



// ns6+/mozilla need to reposition layers onresize when scrolling inside tables.

function rePositionGecko() {

  var i, wndo, holder, x, y;

  if (dw_scrollLayer.hold) {

    for (i=0; dw_scrollLayer.hold[i]; i++) {

      wndo = document.getElementById( dw_scrollLayer.hold[i][0] );

      holder = document.getElementById( dw_scrollLayer.hold[i][1] );

      x = holder.offsetLeft; y = holder.offsetTop;

      wndo.style.left = x + "px"; wndo.style.top = y + "px";

    }

  }

}


