// BeeHive - Scrollbar Object, August 14, 2002
// --------------------------------------------------------
// (c) 2002, Peter Nederlof, http://www.xs4all.nl/~peterned

// Layout vars, overwrite in html

var scrollerBG = "images/scrollbg.jpg";
var upSRC      = "images/scrollup.jpg";
var downSRC    = "images/scrolldn.jpg";
var scrollSRC  = "images/scrollelm.jpg";

var scrollerHeight = 50;
var leftOffset = 7;
var extraSpace = 50;
var scrollCursor = 'hand';

// 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 = function() {
	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 ++;
}

scrollProto.setupElement = function(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 = scrollCursor;
	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;
}

scrollProto.setLayer = function(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()');
