var scrollerBG = "scrollbg.jpg";
var upSRC      = "up.jpg";
var downSRC    = "down.jpg";
var scrollSRC  = "bar.gif";

var scrollerHeight = 25;
var leftOffset = 0;
var extraSpace = 0;

// End of layout --------------------------------

var activeScroller = null;
var scrollCount = 0;
var useBar = null;
document.bars = [];

// Scrollbar
// --------------------------------------------------------

function scrollbar(x, y, w, h, nest) {
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;

	this.currentLayer = null;
	this.currentLyrId = "";
	this.contentHeight = 0;
	this.availHeight = 0;
	this.scrolling = false;
	this.nested = nest;
	this.speed = 0;
	this.build();
}

scrollProto = scrollbar.prototype;
scrollProto.build = dynBuildScrollbar;
scrollProto.setLayer = dynScrollSetLayer;
scrollProto.setupElement = dynScrollSetupElm;

	function dynBuildScrollbar() {
		this.id = 'bar'+scrollCount;
		this.cId = this.id + 'container';

		this.container = new dynObject(this.cId, this.nested);
		this.container.setProperties(this.x, this.y, this.w, this.h, 'visible', false, 20);		
		this.container.setBackground(scrollerBG);
		
		this.upArrow   = new dynObject(this.id + 'upArrow', this.cId);
		this.downArrow = new dynObject(this.id + 'downArrow', this.cId);
		this.scroller  = new dynObject(this.id + 'scroller', this.cId);
		this.setupElement(this.upArrow, 0, 0, this.w, this.w, 'dynScrollBy(-1)', upSRC);
		this.setupElement(this.downArrow, 0, (this.h - this.w), this.w, this.w, 'dynScrollBy(1)', downSRC);
		this.setupElement(this.scroller, 0, this.w, this.w, scrollerHeight, false, scrollSRC);
		
		this.availHeight = this.h - (2 * this.w) - scrollerHeight;
		this.scroller.limitMovement(0, this.w, this.w, this.h - this.w);
		this.scroller.enableDragDrop('vertical');

		document.bars[scrollCount] = this;
		scrollCount ++;
	}

	function dynScrollSetupElm(elm, x, y, w, h, mouseFunction, img) {
		elm.setProperties(x, y, w, h, 'visible');
		elm.clipTo(0,w,h,0);
		elm.css.visibility = "inherit";
		elm.scrollbar = this;
		elm.css.cursor = "hand";
		elm.attachEvent('onmousedown', 'dynGetScrollbar()');
		elm.attachEvent('onmouseup', 'dynStopScroll()');
		if(is.NS4) elm.setBackground(img); else elm.createImage(img, 0, w, h);
		if(mouseFunction) elm.attachEvent('onmousedown', mouseFunction);
	}

	function dynScrollBy(dir) {
		if(useBar && useBar.needed) {			
			var useDy = dir * useBar.speed;
			useBar.scroller.moveBy(0, useDy);
			dynHandleScroll();			
			setTimeout('dynScrollBy('+dir+')', 40);
		}
	}	

	function dynStopScroll() {
		useBar = false;		
	}

	function dynGetScrollbar() {
		useBar = activeObj.scrollbar;		
	}

	function dynScrollSetLayer(to) {
		if(!document.dyn[to]) { new dynObject(to, null, true); }
		if(this.currentLayer) {	this.currentLayer.setVisible(false); }	
		
		this.currentLayer = document.dyn[to];
		this.currentLyrId = to;

		with(this.currentLayer) {
			moveTo(leftOffset, 0);
			getDimensions();
			this.speed = (this.h/h)*7;
		}

		this.scroller.moveTo(0, this.w);
		this.contentHeight = this.currentLayer.h - this.h + extraSpace;
		this.currentLayer.setVisible(true);
		this.needed = (this.contentHeight <= 0)? false:true;		
		this.scroller.setVisible(this.needed);
	}

function dynHandleScroll() {
	if(useBar && useBar.needed) {
		contentY = ((useBar.scroller.y - useBar.w)/
			useBar.availHeight) * useBar.contentHeight;
		useBar.currentLayer.moveTo(leftOffset, - contentY)
	}
}

document.attachEvent('onmousemove', 'dynHandleScroll()');
document.attachEvent('onmouseup', 'dynStopScroll()');