var SeiteLeft = 150;

var upH = 12; // Height of up-arrow
var upW = 12; // Width of up-arrow
var downH = 12; // Height of down-arrow
var downW = 12; // Width of down-arrow
var dragH = 16; // Height of scrollbar
var dragW = 10; // Width of scrollbar
var scrollH = 170; // Height of scrollbar
var speed_scroll = 10; // Scroll speed

// 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 clickUp = false; // If click on up-arrow
var clickDown = 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 WheelDown = false;
var WheelUp = false;

var dragT_zwischen=130;
var dragT_zwischen_older=130;
var contentT_zwischen=0;
var  contentT_zwischen_older=0;
var WheelUp

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 scrollLength; // Number of pixels scrollbar should move
var startY; // Keeps track of offset between mouse and span

var aktuelleSeite = "left";//Sucht die Seite des Scrollers aus

function wheel(e){
if (event.wheelDelta >= 120){
        WheelUp=true;
		scrollUp();
		}
    else if (event.wheelDelta <= -120){
        WheelDown=true;
		scrollDown();
		}

}

// Mousedown
function down(e){

	if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true; // Enables the right mousebutton
	getMouse(e);
	startY = (mouseY - dragT);
	// If click on up-arrow
	if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
		//clickUp = true;
		//return scrollUp();
	}	
	// Else if click on down-arrow
	else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
		//clickDown = true;
		//return scrollDown();
	}
	// Else if click on scrollbar
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT+20 && (mouseY <= (dragT+20 + dragH))){
		clickDrag = true;
		return false;
	}
	else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
		// If click above drag
		
		if(mouseY < dragT+20){
			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;
	}
}

// left right anpassung
function scrollerAnTextAnpassen(){
if(ie4){
document.all.drag.style.pixelTop = dragT_zwischen_older;
dragT=document.all.drag.style.pixelTop;
}else if(nn4){
document.ruler.style.top = dragT_zwischen_older;
document.drag.style.pixelTop = dragT_zwischen_older;
dragT=document.drag.style.pixelTop;
}else if(dom){
document.getElementById("ruler").style.top = dragT_zwischen_older;
document.getElementById("drag").style.top = dragT_zwischen_older;
dragT=document.getElementById("drag").style.top = dragT_zwischen_older;
}


}
// Drag function
function move(e){
if(document.all){
  posx = document.all ? window.event.clientX : ereignis.pageX;
  posy = document.all ? window.event.clientY : ereignis.pageY;
  }else{
  posx = e.clientX;
  }
  if(posx<354+SeiteLeft && aktuelleSeite!="left"){
  dragT_zwischen_older=dragT_zwischen;
  dragT_zwischen=dragT;
  if(ie4){
  document.all.upBild.src="img/lightblue.jpg";
  document.all.downBild.src="img/lightblue.jpg";
  document.all.dragBild.src="img/blue.jpg";
  document.all.scrollBild.src="img/lightblue.jpg";
  document.all.content.id="zwischen";
  document.all.contentClip.id="zwischenClip";
  document.all.contentwait.id="content";
  document.all.contentClipwait.id="contentClip";
  document.all.zwischen.id="contentwait";
  document.all.zwischenClip.id="contentClipwait";
  } else if(nn4){
  document.upBild.src="img/lightblue.jpg";
  document.downBild.src="img/lightblue.jpg";
  document.dragBild.src="img/blue.jpg";
  document.scrollBild.src="img/lightblue.jpg";
  document.content.id="zwischen";
  document.contentClip.id="zwischenClip";
  document.contentwait.id="content";
  document.contentClipwait.id="contentClip";
  document.zwischen.id="contentwait";
  document.zwischenClip.id="contentClipwait";
  } else if(dom){
  document.getElementById("upBild").src="img/lightblue.jpg";
  document.getElementById("downBild").src="img/lightblue.jpg";
  document.getElementById("dragBild").src="img/blue.jpg";
  document.getElementById("scrollBild").src="img/lightblue.jpg";
  document.getElementById("content").id="zwischen";
  document.getElementById("contentClip").id="zwischenClip";
  document.getElementById("contentwait").id="content";
  document.getElementById("contentClipwait").id="contentClip";
  document.getElementById("zwischen").id="contentwait";
  document.getElementById("zwischenClip").id="contentClipwait";
  } else {
  alert("Scrollerror");
  }
  aktuelleSeite="left";
  eventLoader();
  scrollerAnTextAnpassen();
  }else if(posx>368+SeiteLeft && aktuelleSeite!="right") {
  dragT_zwischen_older=dragT_zwischen;
  dragT_zwischen=dragT;
  if(ie4){
  document.all.upBild.src="img/lightorange.jpg";
  document.all.downBild.src="img/lightorange.jpg";
  document.all.dragBild.src="img/orange.jpg";
  document.all.scrollBild.src="img/lightorange.jpg";
  document.all.content.id="zwischen";
  document.all.contentClip.id="zwischenClip";
  document.all.contentwait.id="content";
  document.all.contentClipwait.id="contentClip";
  document.all.zwischen.id="contentwait";
  document.all.zwischenClip.id="contentClipwait";
  } else if(nn4){
  document.upBild.src="img/lightorange.jpg";
  document.downBild.src="img/lightorange.jpg";
  document.dragBild.src="img/orange.jpg";
  document.scrollBild.src="img/lightorange.jpg";
  document.content.id="zwischen";
  document.contentClip.id="zwischenClip";
  document.contentwait.id="content";
  document.contentClipwait.id="contentClip";
  document.zwischen.id="contentwait";
  document.zwischenClip.id="contentClipwait";
  } else if(dom){
  document.getElementById("upBild").src="img/lightorange.jpg";
  document.getElementById("downBild").src="img/lightorange.jpg";
  document.getElementById("dragBild").src="img/orange.jpg";
  document.getElementById("scrollBild").src="img/lightorange.jpg";
  document.getElementById("content").id="zwischen";
  document.getElementById("contentClip").id="zwischenClip";
  document.getElementById("contentwait").id="content";
  document.getElementById("contentClipwait").id="contentClip";
  document.getElementById("zwischen").id="contentwait";
  document.getElementById("zwischenClip").id="contentClipwait";
  }

  aktuelleSeite="right";
  eventLoader();
  scrollerAnTextAnpassen();
  }
	if(clickDrag && contentH > contentClipH){
		getMouse(e);
		dragT = (mouseY - startY);
		
		if(dragT < (rulerT))
			dragT = rulerT;		
		if(dragT > (rulerT + scrollH - dragH))
			dragT = (rulerT + scrollH - dragH);
		
		contentT = ((dragT - rulerT)*(1/scrollLength));
		contentT = eval('-' + contentT);

		moveTo();
		
		// So ie-pc doesn't select gifs
		if(ie4)
			return false;
	}
}

function up(){
	clearTimeout(timer);
	// Resetting variables
	clickUp = false;
	clickDown = false;
	clickDrag = false;
	clickAbove = false;
	clickBelow = false;
	return true;
}

// Reads content layer top
function getT(){
	if(ie4)
		contentT = document.all.content.style.pixelTop;
	else if(nn4)
		contentT = document.contentClip.document.content.top;
	else if(dom)
		contentT = parseInt(document.getElementById("content").style.top);
}

// 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;
	}
}

// Moves the layer
function moveTo(){
	if(ie4){
		document.all.content.style.top = contentT;
		document.all.ruler.style.top = dragT;
		document.all.drag.style.top = dragT;
		WheelDown = false;
		WheelUp = false;
	}
	else if(nn4){
		document.contentClip.document.content.top = contentT;
		document.ruler.top = dragT;
		document.drag.top = dragT;
		WheelDown = false;
		WheelUp = false;
	}
	else if(dom){
		document.getElementById("content").style.top = contentT + "px";
		document.getElementById("drag").style.top = dragT + "px";
		document.getElementById("ruler").style.top = dragT + "px";
		WheelDown = false;
		WheelUp = false;
	}
}

// Scrolls up
function scrollUp(){
	getT();
	
	if(clickAbove){
		if(dragT+20 <= (mouseY-(dragH/2)))
			return up();
	}
	
	if(WheelUp){
		if(contentT < 0){		
			dragT = dragT - (speed_scroll*scrollLength);
			
			if(dragT < (rulerT))
				dragT = rulerT;
				
			contentT = contentT + speed_scroll;
			if(contentT > 0)
				contentT = 0;
			
			moveTo();
		}
	}
	
	if(clickUp){
		if(contentT < 0){		
			dragT = dragT - (speed_scroll*scrollLength);
			
			if(dragT < (rulerT))
				dragT = rulerT;
				
			contentT = contentT + speed_scroll;
			if(contentT > 0)
				contentT = 0;
			
			moveTo();
			timer = setTimeout("scrollUp()",25);
		}
	}
	return false;
}

// Scrolls down
function scrollDown(){
	getT();
	
	if(clickBelow){
		if(dragT+20 >= (mouseY-(dragH/2)))
			return up();
	}

	if(WheelDown){
		if(contentT > -(contentH - contentClipH)){			
			dragT = dragT + (speed_scroll*scrollLength);
			if(dragT > (rulerT + scrollH - dragH))
				dragT = (rulerT + scrollH - dragH);
			
			contentT = contentT - speed_scroll;
			if(contentT < -(contentH - contentClipH))
				contentT = -(contentH - contentClipH);
			moveTo();
		}
	}
	
	if(clickDown){
		if(contentT > -(contentH - contentClipH)){			
			dragT = dragT + (speed_scroll*scrollLength);
			if(dragT > (rulerT + scrollH - dragH))
				dragT = (rulerT + scrollH - dragH);
			
			contentT = contentT - speed_scroll;
			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();
}

// Preload
function eventLoader(){
//if( document.all){
//if( document.body.offsetWidth < 1000 ){
//var SeiteLeft = 10;
//}
//else{
//var SeiteLeft = 150;
//}
//}

	if(ie4){
		//document.all.hauptfeld.style.pixelLeft = SeiteLeft;
		// Up-arrow X and Y variables
		upL = document.all.up.style.pixelLeft+SeiteLeft;
		upT = document.all.up.style.pixelTop;		
		// Down-arrow X and Y variables
		downL = document.all.down.style.pixelLeft+SeiteLeft;
		downT = document.all.down.style.pixelTop;
		// Scrollbar X and Y variables
		dragL = document.all.drag.style.pixelLeft+SeiteLeft;
		dragT = document.all.drag.style.pixelTop;		
		// Ruler Y variable
		rulerT = document.all.up.style.pixelTop;		
		// Height of content layer and clip layer
		contentH = parseInt(document.all.content.scrollHeight);
		contentClipH = parseInt(document.all.contentClip.style.height);
	}
	else if(nn4){
		//document.hauptfeld.left = SeiteLeft;
		// Up-arrow X and Y variables
		upL = document.up.left+SeiteLeft;
		upT = document.up.top;		
		// Down-arrow X and Y variables
		downL = document.down.left+SeiteLeft;
		downT = document.down.top;		
		// Scrollbar X and Y variables
		dragL = document.drag.left+SeiteLeft;
		dragT = document.drag.top;		
		// Ruler Y variable
		rulerT = document.ruler.top;
		// Height of content layer and clip layer
		contentH = document.contentClip.document.content.clip.bottom;
		contentClipH = document.contentClip.clip.bottom;
	}
	else if(dom){
		// Up-arrow X and Y variables
		//parseInt(document.getElementById("hauptfeld").style.left) = SeiteLeft;
		upL = parseInt(document.getElementById("up").style.left)+SeiteLeft;
		upT = parseInt(document.getElementById("up").style.top);
		// Down-arrow X and Y variables
		downL = parseInt(document.getElementById("down").style.left)+SeiteLeft;
		downT = parseInt(document.getElementById("down").style.top);
		// Scrollbar X and Y variables
		dragL = parseInt(document.getElementById("drag").style.left)+SeiteLeft;
		dragT = parseInt(document.getElementById("drag").style.top);
		// Ruler Y variable
		rulerT = parseInt(document.getElementById("ruler").style.top);
		// Height of content layer and clip layer
		contentH = parseInt(document.getElementById("content").offsetHeight);
		contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
		document.getElementById("content").style.top = 0 + "px";
		
	}
	// Number of pixels scrollbar should move
	scrollH = downT-upT+downH+4;
	scrollLength = ((scrollH-dragH)/(contentH-contentClipH));
	// 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;
	document.onmousewheel = wheel;
}
