// BeeHive - Scrollbar Object, August 14, 2002
// --------------------------------------------------------
// (c) 2002, Peter Nederlof, http://www.xs4all.nl/~peterned

// Layout vars, overwrite in html

var scrollerBG = "images/scrollbg.gif";
var upSRC      = "images/scrollup.gif";
var downSRC    = "images/scrolldn.gif";
var scrollSRC  = "images/scrollelm.gif";

var scrollerHeight = 28;
var leftOffset = 0;
var extraSpace = 20;
var scrollbarWidth = 17;
var scrollArrows = true;
var scrollCursor = 'hand';
scrollCursor = 'pointer';

// End of layout

var activeBar = null;
var scrollCount = 0;
var areaCount = 0;
var useBar = null;
var arrowActivator = "onmousedown"
document.bars = [];

function scrollbar(x, y, w, h, arrow_h, top, nest) {
	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
	// new property ah: height of up/down arrows
	// mg 20030618
	this.ah = arrow_h;
	this.eh = scrollerHeight;

/*	mg 20030618
	this.sPos = scrollArrows? this.w:0;
	this.sOff = scrollArrows? (2*this.w):0
*/
	this.sPos = scrollArrows? this.ah:0;
	this.sOff = scrollArrows? (2*this.ah):0

	this.currentLayer = null;
	this.currentLyrId = "";
	this.contentHeight = 0;
	this.availHeight = 0;
	this.scrolling = false;
	this.speed = 0;
	this.nested = nest;
	this.build();
}

scrollProto = scrollbar.prototype;
scrollProto.build = function() {
	this.id = 'bar'+scrollCount;
	this.cId = this.id + 'container';
	
	with(this.container = new dynObject(this.cId, this.nested)) {
		setProperties(this.x, this.y, this.w, this.h, 'visible', false, 1000);
		clipTo(0,this.w,this.h,0);
	}

	with(this.track = new dynObject(this.cId+'track', this.cId)) {
		setProperties(0,0, this.w, this.h, 'visible');
		setBackground(scrollerBG);
	}	this.setupElement(this.track, 0,0,this.w,this.h,'dynTrackScroll()');

	if(scrollArrows) {
		this.upArrow = new dynObject(this.id + 'upArrow', this.cId);
		this.downArrow = new dynObject(this.id + 'downArrow', this.cId);
/*		mg 20030618
		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.upArrow, 0, 0, this.w, this.ah, 'dynScrollBy(-1)', upSRC);
		this.setupElement(this.downArrow, 0, (this.h - this.ah), this.w, this.ah, 'dynScrollBy(1)', downSRC);
	}

	this.scroller  = new dynObject(this.id + 'scroller', this.cId);
	this.setupElement(this.scroller, 0, this.sPos, this.w, scrollerHeight, false, scrollSRC);

	this.availHeight = this.h - this.sOff - scrollerHeight;
	this.scroller.limitMovement(0, this.sPos, this.w, this.h - this.sPos);
	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(img) {
		if(is.NS4) elm.setBackground(img);
		else elm.createImage(img, 0, w, h);
	}

	if(mouseFunction) {
		elm.attachEvent(arrowActivator, 'dynGetScrollbar()');
		elm.attachEvent('onmouseout', 'dynStopScroll()')
		elm.attachEvent(arrowActivator, mouseFunction);
	}
}

scrollProto.setHeight = function(h) {
	this.h = h;
	with(this.container) {
		resizeTo(this.w, this.h);
		clipTo(0, this.w, this.h, 0);
	}

	this.downArrow.moveTo(0,(this.h-this.w));
	this.availHeight = this.h - this.sOff - scrollerHeight;
	this.setLayer(this.currentLayer.id)

	with(this.scroller) {
		moveTo(0,this.sPos);
		limitMovement(0, this.sPos, this.w, this.h - this.sPos);
	}
}

function dynScrollBy(dir, target) {
	if(useBar && useBar.needed) {
		var useDy = dir * useBar.speed;
		useBar.scroller.moveBy(0, useDy);
		dynHandleScroll();
		
//		alert(activeObj.clickY);
		
		if(activeObj.clickY>25 && activeObj.clickY<40) dir=1;
		if(activeObj.clickY>1 && activeObj.clickY<25) dir=-1;
		
		if(!target || Math.abs(useBar.scroller.y - target) > 7)
		setTimeout('dynScrollBy('+dir+','+(target || 0)+')', 40);
	}
}

function dynTrackScroll() {
	if(useBar && useBar.needed && arrowActivator != 'onmouseover') {
		var dy = activeObj.clickY - useBar.eh/2 - useBar.scroller.y
		dynScrollBy((dy < 0)? -2.5:2.5, activeObj.clickY - useBar.eh/2)
	}
}

	function dynWheelStep(dir) {
		useBar = activeBar || document.bars[0];
		if(useBar && useBar.needed) {
			useBar.scroller.moveBy(0, dir);
			dynHandleScroll();
		}
		dynStopScroll();
	}

function dynStopScroll() {
	useBar = false;
}

function dynGetScrollbar() {
	useBar = activeObj.scrollbar;
	activeBar = useBar;
}

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.pageHeight = this.availHeight/(h/this.h);
	}

	this.scroller.moveTo(0, this.sPos);
	this.contentHeight = this.currentLayer.h - this.h + extraSpace;
	this.currentLayer.setVisible(true);
	this.needed = (this.contentHeight <= 0)? false:true;
	this.container.setVisible(this.needed);
}

function dynHandleScroll() {
	if(useBar && useBar.currentLayer) {
		contentY = ((useBar.scroller.y - useBar.sPos)/
			useBar.availHeight) * useBar.contentHeight;
		useBar.currentLayer.moveTo(leftOffset, - contentY)
	}
}

document.attachEvent('onmousemove', 'dynHandleScroll()');
document.attachEvent('onmouseup', 'dynStopScroll()');
document.onkeydown = doKeyDown;

if(is.IE6) { document.onmousewheel = doWheel; }

function doWheel() {
	var delta = event.wheelDelta;
	if(delta) dynWheelStep(-delta/15);
}

var KU = is.NS4? 56:38;
var KD = is.NS4? 50:40;
var PU = is.NS4? 1000:33;
var PD = is.NS4? 1000:34;

function doKeyDown(e) {
	useBar = activeBar || document.bars[0];
	var key = is.IE? event.keyCode:e.which;
	if(key == KU) dynWheelStep(-2);
	if(key == KD) dynWheelStep(2);
	if(key == PU) dynWheelStep(-useBar.pageHeight);
	if(key == PD) dynWheelStep(useBar.pageHeight);
}
