//Version 1.01 modified by Jeroen klein Gebbink - (c)2003 Montferland Media Studio
//t.b.v. website Lovink Fluormouldings b.v.

//Enige basis browser en os checks
var agent = navigator.userAgent.toLowerCase();
var os = navigator.platform.toLowerCase();
var win = (os.indexOf('win32')!=-1);
var mac = (os.indexOf('mac')!=-1);
var ie5 = (agent.indexOf('msie 5')!=-1);
var ie6 = (agent.indexOf('msie 6')!=-1);
var nn = (agent.indexOf('netscape')!=-1);
var mz = (agent.indexOf('gecko')!=-1 && !nn);
var op5 = (agent.indexOf('opera 5')!=-1);
var op6 = (agent.indexOf('opera 6')!=-1);
if (op5 || op6) {	ie5 = false; ie6 = false; nn = false;	}

//Dit te doen wanneer browsers dit script niet ondersteund; verwijder dit desgewenst
//if (!document.getElementById || op5) {    window.location = "http://www.doc-ep.nl/index2.html/"	}

//Initiatie variabelen
var scrollnum = 1;	// aantal scrollers op de pagina (eigenlijk enige te wijzigen variabele)
var interval = 25; // aantal miliseconden tussen de scrollupdates (lager = sneller)
var speed = 3;	// speed of movement pr. interval - normal scroll
var barspeed = 6; // speed of movment pr. interval - click on scrollbg
var wheelspeed = 20; //	speed of movment pr. interval  - mousewheel
var px = (op5 || op6) ? '' : 'px';
var scrollers = new Array();
var dobarscroll = false;
var orgslide = false;
var loadedscroll = false;
var dowheelscroll = false;
var thenum = null;
var thetimer = null;
var evy = null;

//Overige tijdbesparende gedeelde functies
function gety(el) {
	if (op5) {
		return getObjStyle(el).pixelTop; 
	} else {
		return getObj(el).offsetTop;
		}
	}

function geth(el) {
	if (op5) {
		return getObjStyle(el).pixelHeight;
	} else {
		return getObj(el).offsetHeight;
		}
	}

function sety(el,y) {
	getObjStyle(el).top = y+px;
	}

//Start event capturing
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
document.onmousedown = mouseDown;

//Setup alle benodigde scrollers
//window.onload = setUpScrollers;

//----------------------------------------------------

// functie om het target element te bepalen (t.b.v. event capturing)
function getTargetElement(evt) {
	var elem = (evt.target)	? evt.target : evt.srcElement;
	if (elem.nodeType == 3 || elem.tagName.toLowerCase()=='img') { 
		return elem.parentNode;//parent container dient event te ontvangen
	} else {
		return elem;
		}
	}

//----------------------------------------------------

//Functie die de mousedown events afwikkelt
function mouseDown(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "");
    if (evt) {
        var elem = getTargetElement(evt);//ontvangende element
		var theid = elem.id; //layer id van het event ontvangende element
		// scroll functies
		if (loadedscroll) {
			//kijken of het event wordt ontvangen door een scroller 'h' of 'v'
			if (theid.slice(0,6) == 'scroll') {
				thenum = theid.charAt(theid.length-1); //welke scroller ontvangt event (nummer)
				var thefunc = theid.slice(0,theid.length-1); //thefunc extraheert de naam van de layer (scrollv, scrollbar, etc.)
			}
			// als het event ontvangend element is 'scrolldown' of 'scrollup'
			if (thefunc=='scrollvup' || thefunc=='scrollvdown') {	
				if (thefunc=='scrollvup') {
					scrollers[thenum].currspeed = speed;
				} else {
					scrollers[thenum].currspeed = -speed;
				}
				doScroll();	
				//return;
			}
			// als het event ontvangend element is 'scrollbg'
			if (thefunc=='scrollvbg') {
				evy = (ie5 || ie6 || op6) ? evt.offsetY : evt.layerY;
				var ratio = scrollers[thenum].main_endy / scrollers[thenum].scroll_height;
				evy = evy - (scrollers[thenum].bar_height / 2);
				if (evy < gety(scrollers[thenum].bar_v_id)) {
					scrollers[thenum].currspeed = barspeed;
					scrollers[thenum].main_starty = Math.round(evy*ratio);
					if (scrollers[thenum].main_starty > scrollers[thenum].main_starty_org) scrollers[thenum].main_starty = scrollers[thenum].main_starty_org;
				}
				if (evy > gety(scrollers[thenum].bar_v_id)) {
					scrollers[thenum].currspeed = -barspeed;
					if (evy > scrollers[thenum].scroll_height_org) evy = scrollers[thenum].scroll_height_org;
					scrollers[thenum].main_endy = Math.round(evy * ratio);
					scrollers[thenum].scroll_height = evy;
				}
				doScroll();
				//return;
			}
			// als het event ontvangend element is 'scrollbar'
			if(thefunc=='scrollvbar')	{	
				dobarscroll = true;	
				evy = (ie5 || ie6 || op6) ? evt.offsetY : evt.layerY;	
				//return;
			}
		} else {
		//andere functies
		}
	}
}

//Functie die de mouseup events afwikkeld
function mouseUp(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (evt) {
		var elem = getTargetElement(evt);
		// scroll functies
		if (thetimer) clearTimeout(thetimer);
		if (dobarscroll) dobarscroll = false;
		if (thenum)	{
			scrollers[thenum].main_starty = scrollers[thenum].main_starty_org;
			scrollers[thenum].main_endy = scrollers[thenum].main_endy_org;
			scrollers[thenum].scroll_height = scrollers[thenum].scroll_height_org;
		}
	}
}

//Functie die de mousemove events afwikkelt
function mouseMove(evt) {
	evt = (evt) ? evt : ((window.event) ? window.event : "");
    if (evt) {
        var elem = getTargetElement(evt);
		// scroll functies
		if (dobarscroll && thenum) {
			var my = evt.clientY;
			var y = (my - evy) - scrollers[thenum].scroll_top;
			if (y < scrollers[thenum].bar_starty) y = scrollers[thenum].bar_starty;	
			if (y > scrollers[thenum].bar_endy) y = scrollers[thenum].bar_endy;
			sety(scrollers[thenum].bar_v_id,y);	
			var barslide = Math.round((gety(scrollers[thenum].bar_v_id) - (scrollers[thenum].bar_starty)) * (1 / scrollers[thenum].slide_v_length));
			sety(scrollers[thenum].main_id,-barslide);
			if (barslide==0 && gety(scrollers[thenum].main_id)!=scrollers[thenum].main_starty) sety(scrollers[thenum].main_id,scrollers[thenum].main_starty);
			return false;
		}
	}
}

//----------------------------------------------------

// wheelscroll functie
function wheelScroll() {
	if (!dowheelscroll)  {
		var el = event.srcElement;
		if (el.id.slice(0,el.id.length-1)!='main') {
		do {
			el = el.parentNode;
		} while (el.tagName.toLowerCase()!='div' || el.id.slice(0,el.id.length-1)!='main')
	}
	thenum = el.id.charAt(el.id.length-1);
	if (thenum) oldnum = thenum;
	if (!thenum) thenum = oldnum;
	if (event.wheelDelta >= 120) scrollers[thenum].currspeed = wheelspeed;
	else if (event.wheelDelta <= -120) scrollers[thenum].currspeed = -wheelspeed;
	dowheelscroll = true;
	doScroll();
	return;
	}
}

// hoofd scroll functie
function doScroll()	{
	if (!thenum) return;
	if (gety(scrollers[thenum].main_id)+scrollers[thenum].currspeed>scrollers[thenum].main_starty) scrollers[thenum].currspeed = scrollers[thenum].main_starty - gety(scrollers[thenum].main_id);
	if (gety(scrollers[thenum].main_id)+scrollers[thenum].currspeed<scrollers[thenum].main_endy) scrollers[thenum].currspeed = scrollers[thenum].main_endy - gety(scrollers[thenum].main_id);	 
	sety(scrollers[thenum].main_id, gety(scrollers[thenum].main_id)+scrollers[thenum].currspeed);
	var mainslide = Math.round(gety(scrollers[thenum].main_id)/(scrollers[thenum].main_endy/scrollers[thenum].scroll_height));
	if (mainslide<0) mainslide = 0;
	if (mainslide>scrollers[thenum].scroll_height) mainslide = scrollers[thenum].scroll_height;
	sety(scrollers[thenum].bar_v_id, scrollers[thenum].bar_starty+mainslide);
	if (!dowheelscroll)	thetimer = setTimeout('doScroll()', interval);
	dowheelscroll = false;
	return false;
}

//Functie om bepaalde scrollbar te tonen
function showVertScrollBars(numv) {
	showLayer(scrollers[numv].scroller_v_id);
	showLayer(scrollers[numv].up_id);
	showLayer(scrollers[numv].down_id);
	showLayer(scrollers[numv].scrollerbg_v_id);
	showLayer(scrollers[numv].bar_v_id);
}

//Functie om bepaalde scrollbar te verbergen
function hideVertScrollBars(numv) {
	hideLayer(scrollers[numv].scroller_v_id);
	hideLayer(scrollers[numv].up_id);
	hideLayer(scrollers[numv].down_id);
	hideLayer(scrollers[numv].scrollerbg_v_id);
	hideLayer(scrollers[numv].bar_v_id);
}

//Functie om een srollbar te resetten in zijn oorspronkelijk toestand
function resetVertScrollBars() {
	for (var i = 0; i < scrollnum; i++) {
		sety(scrollers[i].bar_v_id, scrollers[i].bar_starty_org);
		sety(scrollers[i].main_id, scrollers[i].main_starty_org);
		}
	loadedscroll = false;
	}

//Daadwerkelijke Setup functie voor de scrollbars
function setUpScrollers() {
	if (loadedscroll == true) {
		resetVertScrollBars();
	}
	for (var i = 0; i < scrollnum; i++) {
		if (loadedscroll != true) {
			scrollers[i] = {
				scroller_v_id		: 'scrollv'+i,
				up_id				: 'scrollvup'+i,
				down_id				: 'scrollvdown'+i,
				scrollerbg_v_id		: 'scrollvbg'+i,
				bar_starty			: gety('scrollvup'+i)+geth('scrollvup'+i),
				bar_starty_org		: gety('scrollvup'+i)+geth('scrollvup'+i),
				bar_height			: geth('scrollvbar'+i),
				bar_endy			: gety('scrollvdown'+i)-geth('scrollvbar'+i),
				bar_v_id			: 'scrollvbar'+i,
				scroll_height		: geth('scrollvbg'+i)-geth('scrollvbar'+i),
				scroll_height_org	: geth('scrollvbg'+i)-geth('scrollvbar'+i),
				scroll_top			: gety('scrollv'+i),
				main_starty			: gety('main'+i),
				main_starty_org	 	: gety('main'+i),
				main_endy			: ((geth('main'+i)*-1)+geth('cont'+i))-5,
				main_endy_org		: ((geth('main'+i)*-1)+geth('cont'+i))-5,
				main_id				: 'main'+i,
				currspeed			: -speed,
				slide_v_length		: (geth('scrollvbg'+i)-geth('scrollvbar'+i))/(geth('main'+i)+5-geth('cont'+i))
			}
			if (ie6 && !mac) {
				getObj(scrollers[i].main_id).onmousewheel = function() {
					wheelScroll();
				}	
			}
		}
		loadedscroll = true;
		if (geth('cont'+i) < geth('main'+i)) {
			showVertScrollBars(i);
			} else {
				hideVertScrollBars(i);
			}
		}
	}