/***************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  Copyright 2004 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.php
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
****************************************************************************/

/*
  dw_scroll_aux.js    version date: Feb 20, 2004  
  Adds scroll bar functionality to scrolling layers code
  (i.e., dw_scroll.js or dw_glidescroll.js, version date Feb 2004 or later)
  Also requires dw_slidebar.js, which requires dw_event.js
*/

// Size dragBar according to layer size?  
dw_scrollLayer.prototype.bSizeDragBar = false;

dw_scrollLayer.prototype.setUpScrollbar = function(id, trkId, axis, offx, offy) {
  if (!document.getElementById) return;
  var bar = document.getElementById(id);
  var trk = document.getElementById(trkId);
  dw_slidebar.init(bar, trk, axis, offx, offy);
  // connect dw_slidebar with dw_scrollLayer
  bar.wn = dw_scrollLayers[this.id]; // scroll area object this bar connected to
  if (axis == "v") this.vBarId = id; else this.hBarId = id;
  // also called on_load from loadLayer, but in case h and v scrollbars, need to call here too
  if (this.bSizeDragBar) this.setBarSize();
  bar.on_drag_start = bar.on_slide_start = dw_scrollLayer.getWndoLyrRef;
  bar.on_drag_end =   bar.on_slide_end =   dw_scrollLayer.tossWndoLyrRef;
  bar.on_drag =       bar.on_slide =       dw_scrollLayer.UpdateWndoLyrPos;
}

// for these 3 functions (assigned to bar.on_drag/slide...) "this" refers to bar
// get/discard ref to layer visible in scroll area
dw_scrollLayer.getWndoLyrRef = function()  { this.wnLyr = document.getElementById(this.wn.lyrId); }
dw_scrollLayer.tossWndoLyrRef = function() { this.wnLyr = null; }
// keep position of scrolling layer in synch with slide/drag of bar
dw_scrollLayer.UpdateWndoLyrPos = function(x, y) {
  var nx, ny;
  if (this.axis == "v") {
    nx = this.wn.x; // floating point values for loaded layer's position held in shiftTo method
    ny = -(y - this.minY) * ( this.wn.maxY / (this.maxY - this.minY) );
  } else {
    ny = this.wn.y;
    nx = -(x - this.minX) * ( this.wn.maxX / (this.maxX - this.minX) );
  }
  this.wn.shiftTo(this.wnLyr, nx, ny);
}

// Keep position of dragBar in sync with position of layer onscroll
dw_scrollLayer.prototype.updateScrollbar = function(x, y) {
  var nx, ny;
  if ( ( this.xdir == 0 || this.distX == 0 ) && this.vBarId ) {
    if (!this.maxY) return;
    ny = -( y * ( (this.bar.maxY - this.bar.minY) / this.maxY ) - this.bar.minY );
    ny = Math.min( Math.max(ny, this.bar.minY), this.bar.maxY);  
    nx = parseInt(this.bar.style.left);
  } else if ( ( this.ydir == 0 || this.distY == 0 ) && this.hBarId ) {
    if (!this.maxX) return;
    nx = -( x * ( (this.bar.maxX - this.bar.minX) / this.maxX ) - this.bar.minX );
    nx = Math.min( Math.max(nx, this.bar.minX), this.bar.maxX);
    ny = parseInt(this.bar.style.top);
  } 
  this.bar.style.left = nx + "px"; this.bar.style.top = ny + "px";
}

// Restore dragBar to start position when loading new layer
dw_scrollLayer.prototype.restoreScrollbars = function() {
  var bar;
  if (this.vBarId) {
    bar = document.getElementById(this.vBarId);
    bar.style.left = bar.minX + "px"; bar.style.top = bar.minY + "px";
  }
  if (this.hBarId) {
    bar = document.getElementById(this.hBarId);
    bar.style.left = bar.minX + "px"; bar.style.top = bar.minY + "px";
  }
}
  
// Size dragBar in proportion to size of content in layer
// called "on_load" of loadLayer method if bSizeDragBar prop true
dw_scrollLayer.prototype.setBarSize = function() {
  var lyr, wn, bar;
  lyr = document.getElementById(this.lyrId);
  wn = document.getElementById(this.id);
  if (this.vBarId) {
    bar = document.getElementById(this.vBarId);
    bar.style.height = (lyr.offsetHeight > wn.offsetHeight)? bar.trkHt / ( lyr.offsetHeight / wn.offsetHeight ) + "px": bar.trkHt - 2*bar.minY + "px";
    bar.maxY = bar.trkHt - bar.offsetHeight - bar.minY; 
  }
  if (this.hBarId) {
    bar = document.getElementById(this.hBarId);
    bar.style.width = (this.wd > wn.offsetWidth)? bar.trkWd / ( this.wd / wn.offsetWidth ) + "px": bar.trkWd - 2*bar.minX + "px";
    bar.maxX = bar.trkWd - bar.offsetWidth - bar.minX; 
  }
}

// called from loadLayer
dw_scrollLayer.prototype.on_load = function() { 
  this.restoreScrollbars();
  if (this.bSizeDragBar) this.setBarSize();
}

dw_scrollLayer.prototype.on_scroll = dw_scrollLayer.prototype.on_slide = function(x,y) { this.updateScrollbar(x,y); }

// obtain and discard references to relevant dragBar
dw_scrollLayer.prototype.on_scroll_start = dw_scrollLayer.prototype.on_slide_start = function() {
  if ( ( this.xdir == 0 || this.distX == 0 ) && this.vBarId ) this.bar = document.getElementById(this.vBarId);
  else if ( ( this.ydir == 0 || this.distY == 0 ) && this.hBarId ) this.bar = document.getElementById(this.hBarId);
}

dw_scrollLayer.prototype.on_scroll_end = dw_scrollLayer.prototype.on_slide_end = function(x, y) { 
  this.updateScrollbar(x,y);
  this.lyr = null; this.bar = null; 
}

