
// Made by geeeet@ghtml.com
// Keep these two lines and you're free to use this code

// Known bugs :
// If ie4.5 mac, please press apple-t to remove sidebar, otherwise everything is pushed 20px to the right...

// Corrected bugs :
// 25.01.2001 - When the height of the span "content" was less than the height of the span "contentClip" a javascript error occured, function changed : move()
// 21.02.2001 - Scrolling text wasn't selectable in ie, function changed : move()
// 05.03.2001 - Ie x and y coordinates was wrong when page was scrolled, function changed : getMouse()

// 19.04.2001 - Finally able to remove browser-scrollbar if content is longer than the browser is high:
// Just put this in the style-tag right before the end head-tag:
// body {margin-left:0; margin-right:0; margin-top:0; margin-bottom:0; width:100%;height:100%;overflow:hidden}

// Touch me here :-)
var upH = 15; // Height of up-arrow
var upW = 22; // Width of up-arrow
var downH = 15; // Height of down-arrow
var downW = 22; // Width of down-arrow
var dragH = 15; // Height of scroller
var dragW = 10; // Width of scroller
var scrollH = 15; // Height of scrollbar
var scrollW = 729; // Width of scrollbar
var speed = 4; // 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 clickRight = false; // If click on up-arrow
var clickLeft = false; // If click on down-arrow
var clickDrag = false; // If click on scrollbar
var clickAbove = false; // If click above scrollbar
var clickBelow = false; // If click below scrollbar

var timer = setTimeout("",500); // Repeat variable
var upL; // Up-arrow X
var upT; // Up-arrow Y
var downL; // Down-arrow X
var downT; // Down-arrow Y
var dragL; // Scrollbar X
var dragT; // Scrollbar Y
var rulerL; // Ruler X
var rulerT; // Ruler Y
var contentT; // Content layer Y;
var contentH; // Content height
var contentClipH; // Content clip height
var contentClipW; // Content clip width
var scrollWidth; // Number of pixels scrollbar should move
var startX; // Keeps track of offset between mouse and span

// Mousedown
function down(e){
	if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
	getMouse(e);
	startX = (mouseX - dragL);
//	alert('x='+mouseX+' , y='+mouseY);
	// If click on right-arrow
	if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
		clickRight = true;
		return scrollRight();
	}	
	// Else if click on left-arrow
	else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
		clickLeft = true;
		return scrollLinks();
	}
	// Else if click on scrollbar
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
		clickDrag = true;
		return false;
	}
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
		// If click above drag
		if(mouseY < dragT){
			clickAbove = true;
			clickRight = true;
			return scrollRight();
		}
		// Else click below drag
		else{
			clickBelow = true;
			clickLeft = true;
			return scrollLinks();
		}
	}
	// If no scrolling is to take place
	else{
		return true;
	}
}

// Drag function
function move(e){
	if(clickDrag && contentT > contentClipW){
		getMouse(e);
		dragL = (mouseX - startX);
		
		if(dragL < (rulerL))
			dragL = rulerL;		
		if(dragL > (rulerL + scrollW - dragW))
			dragL = (rulerL + scrollW - dragW);
		
		contentH = ((dragL - rulerL)*(1/scrollWidth));
		contentH = eval('-' + contentH);

		moveTo();
		
		// So ie-pc doesn't select gifs
		if(ie4)
			return false;
	}
}

function up(){
	clearTimeout(timer);
	// Resetting variables
	clickRight = false;
	clickLeft = false;
	clickDrag = false;
	clickAbove = false;
	clickBelow = false;
	return true;
}

// Reads content layer top
function getT(){
	if(ie4)
		contentH = document.all.content.style.pixelLeft;
	else if(nn4)
		contentH = document.contentClip.document.content.left;
	else if(dom)
		contentH = parseInt(document.getElementById("content").style.left);
}

// Reads mouse X and Y coordinates
function getMouse(e){
	var weite=858;
	var hoehe=524;

	if(ie4){
		var t_width = document.body.offsetWidth;
		t_width = Math.round((t_width - weite) / 2);
		if (t_width < 0 || document.body.offsetWidth < weite) t_width = 0;
	
		var t_height = document.body.offsetHeight;
		t_height = Math.round((t_height - hoehe) / 2);
		if (t_height < 0 || document.body.offsetHeight < hoehe) t_height = 0;

		mouseY = event.clientY + document.body.scrollTop - t_height;
		mouseX = event.clientX + document.body.scrollLeft - t_width;
	}
	else if(nn4 || dom){
		var t_width = window.innerWidth;
		t_width = Math.round((t_width - weite) / 2);
		if (t_width < 0 || window.innerWidth < weite) t_width = 0;
	
		var t_height = window.innerHeight;
		t_height = Math.round((t_height - hoehe) / 2);
		if (t_height < 0 || window.innerHeight < hoehe) t_height = 0;

		mouseY = e.pageY - t_height;
		mouseX = e.pageX - t_width;
	}
}

// Moves the layer
function moveTo(){
	if(ie4){
		document.all.content.style.left = contentH;
		document.all.ruler.style.left = dragL;
		document.all.drag.style.left = dragL;
	}
	else if(nn4){
		document.contentClip.document.content.left = contentH;
		document.ruler.left = dragL;
		document.drag.left = dragL;
	}
	else if(dom){
		document.getElementById("content").style.left = contentH + "px";
		document.getElementById("drag").style.left = dragL + "px";
		document.getElementById("ruler").style.left = dragL + "px";
	}
}


// Scrolls right
function scrollRight(){
	getT();
	
	if(clickAbove){
		if(dragT <= (mouseX-(dragW/2)))
			return right();
	}
	
	if(clickRight){
		if(contentH < 0){		
			dragL = dragL - (speed*scrollWidth);
			
			if(dragL < (rulerL))
				dragL = rulerL;
				
			contentH = contentH + speed;
			if(contentH > 0)
				contentH = 0;
			
			moveTo();
			timer = setTimeout("scrollRight()",25);
		}
	}
	return false;
}

// Scrolls left
function scrollLinks(){
	getT();
	
	if(clickBelow){
		if(dragT >= (mouseX-(dragW/2)))
			return up();
	}

	if(clickLeft){
		if(contentH > -(contentT - contentClipW)){			
			dragL = dragL + (speed*scrollWidth);
			if(dragL > (rulerL + scrollW - dragW))
				dragL = (rulerL + scrollW - dragW);
			
			contentH = contentH - speed;
			if(contentH < -(contentT - contentClipW))
				contentH = -(contentT - contentClipW);
			
			moveTo();
			timer = setTimeout("scrollLinks()",25);
		}
	}
	return false;
}

// reloads page to position the layers again
function reloadPage(){
	location.reload();
}

// Preload
function eventLoader(){
	if(ie4){
		// Up-arrow X and Y variables
		upL = document.all.up.style.pixelLeft;
		upT = document.all.up.style.pixelTop;		
		// Down-arrow X and Y variables
		downL = document.all.down.style.pixelLeft;
		downT = document.all.down.style.pixelTop;
		// Scrollbar X and Y variables
		dragL = document.all.drag.style.pixelLeft;
		dragT = document.all.drag.style.pixelTop;		
		// Ruler Y variable
		rulerT = document.all.ruler.style.pixelTop;		
		rulerL = document.all.ruler.style.pixelLeft;		
		// Height of content layer and clip layer
		contentH = parseInt(document.all.content.scrollHeight);
		contentT = parseInt(document.all.content.scrollWidth);
		contentClipH = parseInt(document.all.contentClip.style.height);
		contentClipW = parseInt(document.all.contentClip.style.width);
	}
	else if(nn4){
		// 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 = document.drag.left;
		dragT = document.drag.top;		
		// Ruler Y variable
		rulerT = document.ruler.top;
		rulerL = document.ruler.left;
		// Height of content layer and clip layer
		contentH = document.contentClip.document.content.clip.bottom;
		contentT = document.contentClip.document.content.clip.right;
		contentClipH = document.contentClip.clip.bottom;
		contentClipW = document.contentClip.clip.right;
	}
	else if(dom){
		// Up-arrow X and Y variables
		upL = parseInt(document.getElementById("up").style.left);
		upT = parseInt(document.getElementById("up").style.top);
		// Down-arrow X and Y variables
		downL = parseInt(document.getElementById("down").style.left);
		downT = parseInt(document.getElementById("down").style.top);
		// Scrollbar X and Y variables
		dragL = parseInt(document.getElementById("drag").style.left);
		dragT = parseInt(document.getElementById("drag").style.top);
		// Ruler Y variable
		rulerT = parseInt(document.getElementById("ruler").style.top);
		rulerL = parseInt(document.getElementById("ruler").style.left);
		// Height of content layer and clip layer
		contentH = parseInt(document.getElementById("content").offsetHeight);
		if (navigator.platform == "MacPPC")
			contentT = parseInt(document.getElementById("content").offsetWidth);
		else
			contentT = zusatzfaktor+2*parseInt(document.getElementById("content").offsetWidth);
		contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
		contentClipW = parseInt(document.getElementById("contentClip").offsetWidth);
		document.getElementById("content").style.top = 0 + "px";
		document.getElementById("content").style.left = 0 + "px";
	}
	// Number of pixels scrollbar should move
	scrollWidth = ((scrollW-dragW)/(contentT-contentClipW));
	// Initializes event capturing
	if(nn4){
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
		window.onresize = reloadPage;
	}
	document.onmousedown = down;
	document.onmousemove = move;
	document.onmouseup = up;
}
