/*

	javascript scrollbar
	2007/07/18 JMR:
		Added Firefox support

	ToDo:
		Clean-up

*/


var isDOM=document.getElementById?1:0;
var isIE=document.all?1:0;
var isNS4=navigator.appName=='Netscape'&&!isDOM?1:0;
var isIE4=isIE&&!isDOM?1:0;
var isOp=window.opera?1:0;
var isWin=navigator.platform.indexOf('Win')!=-1?1:0;
var isDyn=isDOM||isIE||isNS4;

// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}


function getRef(id, par)
{
	par=!par?document:(par.navigator?par.document:par);
	return isIE ? par.all[id] :
	(isDOM ? (par.getElementById?par:par.ownerDocument).getElementById(id) :
	(isNS4 ? par.layers[id] : null));
}

function getSty(id, par)
{
	var r=getRef(id, par);
	return r?(isNS4?r:r.style):null;
}

if (!window.LayerObj)
	var LayerObj = new Function('id', 'par','this.ref=getRef(id, par); this.sty=getSty(id, par); return this');

function getLyr(id, par)
{
	return new LayerObj(id, par)
}

function LyrFn(fn, fc)
{
 LayerObj.prototype[fn] = new Function('var a=arguments,p=a[0],px=isNS4||isOp?0:"px"; ' +
  'with (this) { '+fc+' }');
}
LyrFn('x','if (!isNaN(p)) sty.left=p+px; else return parseInt(sty.left)');
LyrFn('y','if (!isNaN(p)) sty.top=p+px; else return parseInt(sty.top)');
LyrFn('w','if (p) (isNS4?sty.clip:sty).width=p+px; ' +
 'else return (isNS4?ref.document.width:ref.offsetWidth)');
LyrFn('h','if (p) (isNS4?sty.clip:sty).height=p+px; ' +
 'else return (isNS4?ref.document.height:ref.offsetHeight)');
LyrFn('vis','sty.visibility=p');
LyrFn('clip','if (isNS4) with(sty.clip){left=a[0];top=a[1];right=a[2];bottom=a[3]} ' +
 'else sty.clip="rect("+a[1]+"px "+a[2]+"px "+a[3]+"px "+a[0]+"px)" ');


var CSSmode=document.compatMode;
CSSmode=(CSSmode&&CSSmode.indexOf('CSS')!=-1)||isDOM&&!isIE||isOp?1:0;

if (!window.page) var page = { win: window, minW: 0, minH: 0, MS: isIE&&!isOp,
 db: CSSmode?'documentElement':'body' }

page.winW=function()
 { with (this) return Math.max(minW, MS?win.document[db].clientWidth:win.innerWidth) }
page.winH=function()
 { with (this) return Math.max(minH, MS?win.document[db].clientHeight:win.innerHeight) }

page.scrollY=function()
 { with (this) return MS?win.document[db].scrollTop:win.pageYOffset }

function scrScrollTo(pos, isStick)
{
	with (this)
	{
		if (!isDyn || !loaded)
			return;
		cTop = pos;
		if (!isStick) stickTop = cTop;

		divHeight = div.h() + padTop + padBot;
		if (divHeight == 0) divHeight = 1;

		if (checkBounds)
		{
			if(cTop + cHeight > divHeight)
			{
				cTop = divHeight - cHeight;
			}
			if(cTop < 0)
			{
				cTop = 0;
			}
		}

		div.y(eval(divs[0].y) - cTop + padTop);
		div.clip(0, cTop - padTop, cWidth, cTop - padTop + cHeight);

		 thmHeight = Math.ceil(barHeight * (cHeight / divHeight));						//JB

		 if (thmHeight < minThmHeight) thmHeight = minThmHeight;							//JB
		 if (thmHeight > barHeight) thmHeight = barHeight;								//JB
		thm.h(thmHeight);

		if (onscroll) onscroll();
		if(cTop==0)																			//JB
		{
			getSty('imgUp').display='none';
			getSty('imgUpGrey').display='inline';
		}else{
			getSty('imgUp').display='inline';
			getSty('imgUpGrey').display='none';
		}
		if(cTop == (divHeight - cHeight))
		{
			getSty('imgDown').display='none';
			getSty('imgDownGrey').display='inline';
		}else{
			getSty('imgDown').display='inline';
			getSty('imgDownGrey').display='none';
		}
		if (activeScr || isStick) return;

		var fracDivDown = (cTop / (divHeight - cHeight));
		thm.y(bar.y() + fracDivDown * (barHeight - thmHeight));
	}
}

function scrScrollBy(amount) { with (this)
{
 scrollTo(cTop + amount);
}}

function scrSetScroll(newSpeed, steps) { with (this)
{
 if (!loaded) return;

 stepsLeft = steps;

 if (timer) clearInterval(timer);
 timer = setInterval('with (' + myName + ') { ' +
  'if (stepsLeft > 0) { ySpeed += ' + ((newSpeed-ySpeed)/steps) + '; stepsLeft-- } ' +
  'else if (parseInt(ySpeed)==0) {clearInterval(timer);timer=null} scrollBy(ySpeed) }', 50);
}}

function scrThumbDown(evt) { with (this)
{
 var evt = evt?evt:window.event;

 activeScr = this;

 dragOffset = (isNS4 ? evt.layerY : page.scrollY() + evt.clientY - thm.y());

 if (onthumbdown) onthumbdown();

 if (timer) clearInterval(timer);
 timer = null;

 return false;
}}

function scrThumbMove(evt)
{
 var evt = evt?evt:window.event;

 if (!activeScr) return true;
 else with (activeScr)
 {
  if ((cTop + cHeight > divHeight) || (thmHeight == barHeight)) return true;

  var thmTop = (isNS4 ? evt.pageY : page.scrollY() + evt.clientY) - dragOffset - bar.y();
 
  if (thmTop < 0) thmTop = 0;
  if (thmTop + thmHeight > barHeight) thmTop = barHeight - thmHeight;
  thm.y(bar.y() + thmTop);

  stickTop = (divHeight - cHeight) * (thmTop / (barHeight - thmHeight));
  
  if (stick == 1) scrollTo(stickTop);
  else if (!timer) timer = setInterval(myName + '.stickScroll()', 40);

  return false;
 }
}

function scrStickScroll() { with (this)
{
 if (Math.abs(cTop - stickTop) > 1)
 {
  cTop += (stickTop - cTop) * stick;
  scrollTo(cTop, true);
 }
 else if (cTop != stickTop)
 {
  cTop = stickTop;
  scrollTo(cTop, true);
 }
}}

function scrThumbUp(evt)
{
 if (activeScr) with (activeScr) if (onthumbup) onthumbup();

 activeScr = null;
}

function scrBarClick(evt) { with (this)
{
 var evt = evt?evt:window.event;

 clickPos = isNS4 ? evt.pageY : page.scrollY() + evt.clientY;

 if (clickPos < thm.y()) scrollBy(0 - cHeight);
 if (clickPos > (thm.y() + thmHeight)) scrollBy(cHeight);

 if (isNS4) return document.routeEvent(evt);
}}

function scrLayout() { with (this)
{
 if (!isDyn || !loaded) return;

 for (var i = 0; i < divs.length; i++) with (divs[i].lyr)
 {
  x(eval(divs[i].x)); w(Math.max(0,eval(divs[i].w)));
  if (i) { y(eval(divs[i].y)); h(Math.max(0,eval(divs[i].h))) }
 }

 barHeight = eval(divs[1].h);
 cWidth = eval(divs[0].w);
 cHeight = eval(divs[0].h);

 if (onlayout) onlayout();

 scrollBy(0);
}}

function scrSetup(defaultFile)
{
with (this)
{
	if(!isDyn)
		return;
	for(var i = 0; i < divs.length; i++)
		divs[i].lyr = getLyr(divs[i].id, eval(divs[i].par));

	div = divs[0].lyr;
	bar = divs[1].lyr;
	thm = divs[2].lyr;

	if (!isNS4) bufRef = eval('window.' + myName + 'Buf');

	if (isNS4)
	{
		bar.ref.captureEvents(Event.CLICK);
		thm.ref.captureEvents(Event.MOUSEDOWN);
	}

	bar.ref.onclick = new Function('evt', 'return ' + myName + '.barClick(evt)');
	thm.ref.onmousedown = new Function('evt', 'return ' + myName + '.thumbDown(evt)');

	var noSel = new Function('if (activeScr) return false');
	if (isIE) document.onselectstart = noSel;
	else if (isDOM) document.onselect = noSel;

	loaded = true;
	layout();
	if (onsetup) onsetup();
}}

function addProps(obj, data, names, addNull)
{
	for (var i = 0; i < names.length; i++)
		if(i < data.length || addNull)
			obj[names[i]] = data[i];
}

function DHTMLScroller()
{
 var names = ['myName', 'bufRef', 'div', 'bar', 'thm', 'loaded', 'timer',
 'divHeight', 'thmHeight', 'barHeight', 'cHeight', 'cWidth', 'dragOffset',
 'onbeforeload', 'onload', 'onscroll', 'onsetup', 'onlayout', 'onthumbdown', 'onthumbup'];
 addProps(this, arguments, names, true);

 this.cTop = 0;

 this.divs = new Array();
 
 this.minThmHeight = 20;
 this.padTop = 0;
 this.padBot = 0;
 this.checkBounds = true;

 this.ySpeed = this.stepsLeft = 0;

 this.stick = 1;
 this.stickTop = 0;

 this.scrollTo = scrScrollTo;
 this.scrollBy = scrScrollBy;
 this.setScroll = scrSetScroll;
 this.thumbDown = scrThumbDown;
 this.stickScroll = scrStickScroll;
 this.barClick = scrBarClick;
 this.setup = scrSetup;
 this.layout = scrLayout;
}

function ScrDiv()
{
	addProps(this, arguments, ['id','x','y','w','h','par','lyr'], true);
}

var activeScr = null;

var mainDiv = new DHTMLScroller('mainDiv');

with (mainDiv)
{
	//divs[0] = new ScrDiv('mainDivContent', '0', '0', '390', '375');
	divs[0] = new ScrDiv('mainDivContent', '0', '0', '614', '568');
	//divs[1] = new ScrDiv('mainDivBar', '0', '0', '13', '340');
	//divs[1] = new ScrDiv('mainDivBar', '0', '0', '12', '124');
	divs[1] = new ScrDiv('mainDivBar', '0', '0', '12', '546');
	divs[1] = new ScrDiv('mainDivBar', '0', '0', '12', '522');
	//divs[2] = new ScrDiv('mainDivThumb', '0', '0', '13', '');
	divs[2] = new ScrDiv('mainDivThumb', '0', '0', '12', ''); // de '' gaat automatisch...

	//divs[3] = new ScrDiv('mainDivUpArrows', '0', '4', '13', '16');
	divs[3] = new ScrDiv('mainDivUpArrows', '0', '0', '12', '7');
	//divs[4] = new ScrDiv('mainDivDownArrows', '0', '0', '13', '16');
	divs[4] = new ScrDiv('mainDivDownArrows', '0', '0', '12', '7');

	//page.minW = 400;
	page.minW = 614;
	//page.minH = 300;
	page.minH = 568;

	onload = loadFunction; 

	onlayout = thumbVis;
}

function loadFunction()
{
	with (this)
	{
		getSty('loadMessage').visibility = 'hidden';
		for (var i = 0; i < divs.length; i++)
			if (i != 2)
				divs[i].lyr.vis('visible');
		onlayout();
	}
}

function thumbVis()
{
	with (this)
	{ 
		divs[2].lyr.vis(div.h() > cHeight ? 'visible' : 'hidden');
		if(div.h() > cHeight)
		{
			getSty('tdScroll').visibility='visible';
			getSty('imgDown').display='inline';
			getSty('imgDownGrey').display='none';
		}else{
			getSty('tdScroll').visibility='hidden';
		}
	}
}

function pageLoad()
{
	mainDiv.setup("");
}

window.onresize = function()
{
	mainDiv.layout();
}

if (isNS4)
	document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);

document.onmousemove = function(evt)
{
	var ret = scrThumbMove(evt);
	return (ret ? (isNS4?document.routeEvent(evt):true) : false);
}

document.onmouseup = function(evt)
{
	scrThumbUp(evt);
	if(isNS4)
		return document.routeEvent(evt);
}

var nsWinW = window.innerWidth, nsWinH = window.innerHeight;

function scrKeyDown(evt)
{
	with (mainDiv)
	{
		if (!loaded) return;

		var evt = evt?evt:window.event;
		var key = evt.keyCode?evt.keyCode:(evt.charCode?evt.charCode:evt.which);

		if (key==36) scrollTo(0);         // 'Home'
		if (key==33) scrollBy(0-cHeight); // 'PgUp'
		if (key==38) scrollBy(-10);       // 'Up'
		if (key==40) scrollBy(10);        // 'Down'
		if (key==34) scrollBy(cHeight);   // 'PgDn'
		if (key==35) scrollTo(divHeight); // 'End'
	}
}

if (isIE)
	document.onkeydown = scrKeyDown;
else
{
	if (isNS4)
		document.captureEvents(Event.KEYPRESS);
	document.onkeypress = scrKeyDown;
}

if (isIE) document.onmousewheel = function()
{
	wheel(event);
}

var brwsr = whichBrs();

if (brwsr == 'Mozilla' ||
	brwsr == 'Firefox') {
	if (window.addEventListener)
	{
		window.addEventListener('DOMMouseScroll', wheel, false);
	}
}

if (brwsr == 'Opera')
{
	window.onmousewheel = document.onmousewheel = wheel;
}

function wheel(event) {

	var delta = 0;
	if (!event) /* For IE. */
		event = window.event;
	if (event.wheelDelta) { /* IE/Opera. */
		delta = event.wheelDelta/120;
		/** In Opera 9, delta differs in sign as compared to IE.
		 */
		if (window.opera)
			delta = -delta;
	} else if (event.detail) { /** Mozilla case. */
		/** In Mozilla, sign of delta is different than in IE.
		 * Also, delta is multiple of 3.
		 */
		delta = -event.detail/3;
	}

	/** If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */
	if (delta) {
		//handle(delta);

		//mainDiv.scrollBy(event.wheelDelta / -5);
		mainDiv.scrollBy(-10 * delta);
		//return false;

	}
	/** Prevent default actions caused by mouse wheel.
	 * That might be ugly, but we handle scrolls somehow
	 * anyway, so don't bother here..
	 */
	if (event.preventDefault)
		event.preventDefault();
	event.returnValue = false;

}

