var whichScroller = 'vertical';

// Horizontal scroller

var leftH = 90; // Height of left-arrow
var leftW = 25; // Width of left-arrow
var rightH = 90; // Height of right-arrow
var rightW = 25; // Width of right-arrow
var dragH = 20; // Height of slidebar
var dragW = 50; // Width of slidebar
var scrollW = 910; // Width of scrollbar
var speed = 25; // Scroll speed

// And now... go to the bottom of the page...

// Browser detection
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;

var mouseY; // Mouse Y position onclick
var mouseX; // Mouse X position onclick

var clickLeft = false; // If click on left-arrow
var clickRight = false; // If click on right-arrow
var clickDrag = false; // If click on drag
var clickLeftDrag = false; // If click left from drag
var clickRightDrag = false; // If click right from drag

var timer = setTimeout("",500); // Repeat variable
var leftL; // Left-arrow X
var leftT; // Left-arrow Y
var rightL; // Right-arrow X
var rightT; // Right-arrow Y
var dragL; // Scrollbar X
var dragT; // Scrollbar Y
var rulerL; // Ruler X
var rulerT; // Ruler Y
var contentL; // Content layer X;
var contentW; // Content width
var contentClipW; // Content clip width
var scrollLength; // Number of pixels scrollbar should move
var startX; // Keeps track of offset between mouse and span

// Vertical scroller

var upH_V = 10; // Height of up-arrow
var upW_V = 30; // Width of up-arrow
var downH_V = 10; // Height of down-arrow
var downW_V = 30; // Width of down-arrow
var downW_V = 30; // Height of scrollbar
var dragH_V = 30; // Width of scrollbar
var dragW_V = 273; // Height of scrollbar
var speed_V = 25; // Scroll speed_V

// And now... go to the bottom of the page...

var clickUp = false; // If click on up-arrow
var clickDown = false; // If click on down-arrow
var clickDrag_V = false; // If click on scrollbar
var clickAbove = false; // If click above scrollbar
var clickBelow = false; // If click below scrollbar

var upL; // Up-arrow X
var upT; // Up-arrow Y
var downL; // Down-arrow X
var downT; // Down-arrow Y
var dragL_V; // Scrollbar X
var dragT_V; // Scrollbar Y
var rulerL_V_V; // Ruler X
var rulerT; // Ruler Y
var contentT; // Content layer Y;
var contentH; // Content height
var contentClipH; // Content clip height
var scrollLength_V; // Number of pixels scrollbar should move
var startY; // Keeps track of offset between mouse and span

// Mousedown
function down(e){
	
	if(whichScroller == 'horizontal') {
		if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
		getMouse(e);
		startX = (mouseX - dragL);

		// If click on left-arrow
		if(mouseX >= leftL && (mouseX <= (leftL + leftW)) && mouseY >= leftT && (mouseY <= (leftT + leftH))){
			clickLeft = true;
			return scrollLeft();
		}	
		// Else if click on right-arrow
		else if(mouseX >= rightL && (mouseX <= (rightL + rightW)) && mouseY >= rightT && (mouseY <= (rightT + rightH))){
			clickRight = true;
			return scrollRight();
		}
		// Else if click on drag
		else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
			clickDrag = true;
			return false;
		}
		// Else if click on scrollbar
		else if(mouseX >= rulerL && (mouseX <= (rulerL + scrollW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
			// If click above drag
			if(mouseX < dragL){
				clickLeftDrag = true;
				clickLeft = true;
				return scrollLeft();
			}
			// Else click below drag
			else{
				clickRightDrag = true;
				clickRight = true;
				return scrollRight();
			}
		}
		// If no scrolling is to take place
		else{
			return true;
		}
	}
	else {
		if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
		getMouse(e);
		startY = (mouseY - dragT_V);

		// If click on up-arrow
		if(mouseX >= upL && (mouseX <= (upL + upW_V)) && mouseY >= upT && (mouseY <= (upT + upH_V))){
			clickUp = true;
			return scrollUp();
		}	
		// Else if click on down-arrow
		else if(mouseX >= downL && (mouseX <= (downL + downW_V)) && mouseY >= downT && (mouseY <= (downT + downH_V))){
			clickDown = true;
			return scrollDown();
		}
		// Else if click on scrollbar
		else if(mouseX >= dragL_V && (mouseX <= (dragL_V + dragH_V)) && mouseY >= dragT_V && (mouseY <= (dragT_V + downW_V))){
			clickDrag_V = true;
			return false;
		}
		else if(mouseX >= dragL_V && (mouseX <= (dragL_V + dragH_V)) && mouseY >= rulerT && (mouseY <= (rulerT + dragW_V))){
			// If click above drag
			if(mouseY < dragT_V){
				clickAbove = true;
				clickUp = true;
				return scrollUp();
			}
			// Else click below drag
			else{
				clickBelow = true;
				clickDown = true;
				return scrollDown();
			}
		}
		// If no scrolling is to take place
		else{
			return true;
		}
	}
}

// Drag function
function move(e){
	if(whichScroller == 'horizontal') {
		if(clickDrag && contentW > contentClipW){
			getMouse(e);
			dragL = (mouseX - startX);
			
			if(dragL < (rulerL))
				dragL = rulerL;		
			if(dragL > (rulerL + scrollW - dragW))
				dragL = (rulerL + scrollW - dragW);
			
			contentL = ((dragL - rulerL)*(1/scrollLength));
			contentL = eval('-' + contentL);
	
			moveTo();
			
			// So ie-pc doesn't select gifs
			if(ie4)
				return false;
		}
	}
	else {
		if(clickDrag_V && contentH > contentClipH){
			getMouse(e);
			dragT_V = (mouseY - startY);
			
			if(dragT_V < (rulerT))
				dragT_V = rulerT;		
			if(dragT_V > (rulerT + dragW_V - downW_V))
				dragT_V = (rulerT + dragW_V - downW_V);
			
			contentT = ((dragT_V - rulerT)*(1/scrollLength_V));
			contentT = eval('-' + contentT);
	
			moveTo();
			
			// So ie-pc doesn't select gifs
			if(ie4)
				return false;
		}
	}
}

function up(){
	if(whichScroller == 'horizontal') {
		clearTimeout(timer);
		// Resetting variables
		clickLeft = false;
		clickRight = false;
		clickDrag = false;
		clickLeftDrag = false;
		clickRightDrag = false;
		return true;
	}
	else {
		clearTimeout(timer);
		// Resetting variables
		clickUp = false;
		clickDown = false;
		clickDrag_V = false;
		clickAbove = false;
		clickBelow = false;
		return true;
	}
}

// Reads content layer left
function getL(){
	if(ie4)
		contentL = document.all.content.style.pixelLeft;
	else if(nn4)
		contentL = document.contentClip.document.content.left;
	else if(dom) {
		var leftstring = document.getElementById("content").style.left;
		leftstring = leftstring.slice(0,leftstring.length-2);
		contentL = parseInt(leftstring);
	}
	
}

// Reads content layer top
function getT(){
	if(ie4) {
		contentT = document.all.contentV.style.pixelTop;	
	}
	else if(nn4)
		contentT = document.contentClipV.document.contentV.top;
	else if(dom) {
		var topstring = document.getElementById("contentV").style.top;
		topstring = topstring.slice(0,topstring.length-2);
		contentT = parseInt(topstring);
	}
}

// Reads mouse X and Y coordinates
function getMouse(e){

	if(ie4){
		mouseY = event.clientY + document.body.scrollTop;
		mouseX = event.clientX + document.body.scrollLeft;
	}
	else if(nn4 || dom){
		mouseY = e.pageY;
		mouseX = e.pageX;
	}
	if(whichScroller == 'horizontal') {
		if(ie4){
		mouseY = mouseY - document.all.scrollbar.offsetTop - document.all.container.offsetTop - document.all.footer.offsetTop;
		mouseX = mouseX - document.all.scrollbar.offsetLeft - document.all.container.offsetLeft - document.all.footer.offsetLeft;
		}
		else if(nn4){
		mouseY = mouseY - document.scrollbar.offsetTop - document.container.offsetTop - document.footer.offsetTop;
		mouseX = mouseX - document.scrollbar.offsetLeft - document.container.offsetLeft - document.footer.offsetLeft;
		}
		else if(dom){
		mouseY = mouseY - document.getElementById("scrollbar").offsetTop - document.getElementById("container").offsetTop - document.getElementById("footer").offsetTop;
		mouseX = mouseX - document.getElementById("scrollbar").offsetLeft - document.getElementById("container").offsetLeft - document.getElementById("footer").offsetLeft;
		}	
	} else {
		if(ie4){
		mouseY = mouseY - document.all.scrollbarV.offsetTop - document.all.container.offsetTop;
		mouseX = mouseX - document.all.scrollbarV.offsetLeft - document.all.container.offsetLeft;
		}
		else if(nn4){
		mouseY = mouseY - document.scrollbarV.offsetTop - document.container.offsetTop;
		mouseX = mouseX - document.scrollbarV.offsetLeft - document.container.offsetLeft;
		}
		else if(dom){
		mouseY = mouseY - document.getElementById("scrollbarV").offsetTop - document.getElementById("container").offsetTop;
		mouseX = mouseX - document.getElementById("scrollbarV").offsetLeft - document.getElementById("container").offsetLeft;
		}	
	}
}

// Moves the layer
function moveTo(){
	if(whichScroller == 'horizontal') {
		if(ie4){
			document.all.content.style.left = contentL
			document.all.ruler.style.left = dragL;
			document.all.drag.style.left = dragL;
		}
		else if(nn4){
			document.contentClip.document.content.left = contentL;
			document.ruler.left = dragL;
			document.drag.left = dragL;
		}
		else if(dom){
			document.getElementById("content").style.left = contentL + "px";
			document.getElementById("drag").style.left = dragL + "px";
			document.getElementById("ruler").style.left = dragL + "px";
		}
	}
	else {
		if(ie4){
			document.all.contentV.style.top = contentT;
			//document.all.rulerV.style.top = dragT_V;
			document.all.dragV.style.top = dragT_V;
		}
		else if(nn4){
			document.contentClipV.document.contentV.top = contentT;
			//document.rulerV.top = dragT_V;
			document.dragV.top = dragT_V;
		}
		else if(dom){
			document.getElementById("contentV").style.top = contentT + "px";
			document.getElementById("dragV").style.top = dragT_V + "px";
			//document.getElementById("rulerV").style.top = dragT_V + "px";
		}
	}

}

// Hides the scrollbar
function hideScrollbar(){
	if(whichScroller == 'horizontal') {
		if(ie4){
			document.all.scrollbar.style.visibility = "hidden";
		}
		else if(nn4){
			document.scrollbar.style.visibility = "hidden";
		}
		else if(dom){
			document.getElementById("scrollbar").style.visibility = "hidden";
		}
	}
	else {
		if(ie4){
			document.all.scrollbarV.style.visibility = "hidden";
		}
		else if(nn4){
			document.scrollbarV.style.visibility = "hidden";
		}
		else if(dom){
			document.getElementById("scrollbarV").style.visibility = "hidden";
		}
	}

}

// Shows the scrollbar
function showScrollbar(){
	if(whichScroller == 'horizontal') {
		if(ie4){
			document.all.scrollbar.style.visibility = "visible";
		}
		else if(nn4){
			document.scrollbar.style.visibility = "visible";
		}
		else if(dom){
			document.getElementById("scrollbar").style.visibility = "visible";
		}
	}
	else {
		if(ie4){
			document.all.scrollbarV.style.visibility = "visible";
		}
		else if(nn4){
			document.scrollbarV.style.visibility = "visible";
		}
		else if(dom){
			document.getElementById("scrollbarV").style.visibility = "visible";
		}
	}

}


// Scrolls left
function scrollLeft(){
	getL();
	if(clickLeftDrag){
		if(dragL <= (mouseX-(dragW/2)))
			return up();
	}

	if(clickLeft){
		if(contentL < 0){		
			dragL = dragL - (speed*scrollLength);
			
			if(dragL < (rulerL))
				dragL = rulerL;
				
			contentL = contentL + speed;
			if(contentL > 0)
				contentL = 0;
			
			moveTo();
			timer = setTimeout("scrollLeft()",25);
		}
	}
	return false;
}

// Scrolls right
function scrollRight(){

	getL();
	
	if(clickRightDrag){
		if(dragL >= (mouseX-(dragW/2)))
			return up();
	}

	if(clickRight){
		if(contentL > -(contentW - contentClipW)){			
			dragL = dragL + (speed*scrollLength);
			if(dragL > (rulerL + scrollW - dragW))
				dragL = (rulerL + scrollW - dragW);
			
			contentL = contentL - speed;
			if(contentL < -(contentW - contentClipW))
				contentL = -(contentW - contentClipW);
			
			moveTo();
			timer = setTimeout("scrollRight()",25);
		}
	}
	return false;
}

// Scrolls up
function scrollUp(){
	getT();
	
	if(clickAbove){
		if(dragT_V <= (mouseY-(downW_V/2)))
			return up();
	}
	
	if(clickUp){
		if(contentT < 0){		
			dragT_V = dragT_V - (speed_V*scrollLength_V);
			
			if(dragT_V < (rulerT))
				dragT_V = rulerT;
				
			contentT = contentT + speed_V;
			if(contentT > 0)
				contentT = 0;
			
			moveTo();
			timer = setTimeout("scrollUp()",25);
		}
	}
	return false;
}

// Scrolls down
function scrollDown(){
	getT();
	
	if(clickBelow){
		if(dragT_V >= (mouseY-(downW_V/2)))
			return up();
	}

	if(clickDown){
		if(contentT > -(contentH - contentClipH)){			
			dragT_V = dragT_V + (speed_V*scrollLength_V);
			if(dragT_V > (rulerT + dragW_V - downW_V))
				dragT_V = (rulerT + dragW_V - downW_V);
			
			contentT = contentT - speed_V;
			if(contentT < -(contentH - contentClipH))
				contentT = -(contentH - contentClipH);
			
			moveTo();
			timer = setTimeout("scrollDown()",25);
		}
	}
	return false;
}

// reloads page to position the layers again
function reloadPage(){
	location.reload();
}
function wheel(event){
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta) {
		delta = event.wheelDelta/120;
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	//alert(delta);
	if (delta > 0) {clickUp = true; scrollUp();clickUp = false;};
	if (delta < 0) {clickDown = true; scrollDown();clickDown = false;};
	return false;
}

function leereventLoader(){

}

// Preload
function eventLoader(){

	if(ie4){
		
		// Left-arrow X and Y variables
		leftL = document.all.left.style.pixelLeft;
		leftT = document.all.left.style.pixelTop;		
		// Right-arrow X and Y variables
		rightL = document.all.right.style.pixelLeft;
		rightT = document.all.right.style.pixelTop;
		// Scrollbar X and Y variables
		dragL = document.all.drag.style.pixelLeft;
		dragT = document.all.drag.style.pixelTop;		
		// Ruler X variable
		rulerL = document.all.ruler.style.pixelLeft;		
		// Height of content layer and clip layer
		contentW = parseInt(document.all.content.scrollWidth);
		//alert(document.all.content.scrollWidth + "::" + document.all.content.scrollHeight);
		contentClipW = parseInt(document.all.contentClip.style.width);
		
		// Up-arrow X and Y variables
		upL = document.all.upV.style.pixelLeft;
		upT = document.all.upV.style.pixelTop;		
		// Down-arrow X and Y variables
		downL = document.all.downV.style.pixelLeft;
		downT = document.all.downV.style.pixelTop;
		// Scrollbar X and Y variables
		dragL_V = document.all.dragV.style.pixelLeft;
		dragT_V = document.all.dragV.style.pixelTop;		
		// Ruler Y variable
		rulerT = document.all.rulerV.style.pixelTop;		
		// Height of content layer and clip layer
		contentH = parseInt(document.all.contentV.scrollHeight);
		contentClipH = parseInt(document.all.contentClipV.style.height);
		
	}
	else if(nn4){
		// Left-arrow X and Y variables
		leftL = document.left.left;
		leftT = document.left.top;		
		// Right-arrow X and Y variables
		rightL = document.right.left;
		rightT = document.right.top;		
		// Scrollbar X and Y variables
		dragL = document.drag.left;
		dragT = document.drag.top;		
		// Ruler X variable
		rulerL = document.ruler.left;
		// Height of content layer and clip layer
		contentW = document.contentClip.document.content.clip.right;
		contentClipW = document.contentClip.clip.right;
		
		// Up-arrow X and Y variables
		upL = document.up.left;
		upT = document.up.top;		
		// Down-arrow X and Y variables
		downL = document.down.left;
		downT = document.down.top;		
		// Scrollbar X and Y variables
		dragL_V = document.drag.left;
		dragT_V = document.drag.top;		
		// Ruler Y variable
		rulerT = document.ruler.top;
		// Height of content layer and clip layer
		contentH = document.contentClip.document.contentV.clip.bottom;
		contentClipH = document.contentClip.clip.bottom;
	}
	else if(dom){
		
		// Up-arrow X and Y variables
		upL = parseInt(document.getElementById("upV").style.left);
		upT = parseInt(document.getElementById("upV").style.top);
		// Down-arrow X and Y variables
		downL = parseInt(document.getElementById("downV").style.left);
		downT = parseInt(document.getElementById("downV").style.top);
		// Scrollbar X and Y variables
		dragL_V = parseInt(document.getElementById("dragV").style.left);
		dragT_V = parseInt(document.getElementById("dragV").style.top);
		// Ruler Y variable
		rulerT = parseInt(document.getElementById("rulerV").style.top);
		// Height of content layer and clip layer
		contentH = parseInt(document.getElementById("contentV").offsetHeight);
		contentClipH = parseInt(document.getElementById("contentClipV").offsetHeight);
		document.getElementById("contentV").style.top = 0 + "px";
		
		// Left-arrow X and Y variables
		leftL = parseInt(document.getElementById("left").style.left);
		leftT = parseInt(document.getElementById("left").style.top);
		// Right-arrow X and Y variables
		rightL = parseInt(document.getElementById("right").style.left);
		rightT = parseInt(document.getElementById("right").style.top);
		// Scrollbar X and Y variables
		dragL = parseInt(document.getElementById("drag").style.left);
		dragT = parseInt(document.getElementById("drag").style.top);
		// Ruler X variable
		rulerL = parseInt(document.getElementById("ruler").style.left);
		// Height of content layer and clip layer
		contentW = parseInt(document.getElementById("content").offsetWidth);
		contentClipW = parseInt(document.getElementById("contentClip").offsetWidth);
		//document.getElementById("content").style.left = 0 + "px";
		
	}
	// Number of pixels scrollbar should move
	scrollLength = ((scrollW-dragW)/(contentW-contentClipW));
	scrollLength_V = ((dragW_V-downW_V)/(contentH-contentClipH));
	
	whichScroller = 'vertical';
	if (scrollLength_V == Number.POSITIVE_INFINITY || scrollLength_V<0) {
	  hideScrollbar();
	} else {
	  showScrollbar();
	}
	whichScroller = 'horizontal';
	if (scrollLength<0) {
	  hideScrollbar();
	} else {
	  showScrollbar();
	}

	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
	if (window.addEventListener)
		window.addEventListener('DOMMouseScroll', wheel, false);

	window.onmousewheel = document.onmousewheel = wheel;
}
