
var upH = 17; // Height of up-arrow
var upW = 13; // Width of up-arrow
var downH = 17; // Height of down-arrow
var downW = 13; // Width of down-arrow
var dragH = 30; // Height of scrollbar
var dragW = 13; // Width of scrollbar
var scrollH = 0; // Height of scrollbar
var speed = 4; // Scroll speed

var ScrollTopPos;
var ScrollLeftPos;
var ScrollHeight;
var ScrollWidth;

var dom = document.getElementById ? true:false;
var ie4 = document.all ? true:false;
var nn4 = document.layers ? 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 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

// 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 && (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;
	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(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;
}
else if(nn4){
document.contentClip.document.content.top = contentT;
document.ruler.top = dragT;
document.drag.top = dragT;
}
else if(dom){
document.getElementById("content").style.top = contentT + "px";
document.getElementById("drag").style.top = dragT + "px";
document.getElementById("ruler").style.top = dragT + "px";
}
}

// Scrolls up
function scrollUp(){
  getT();
  
  if(clickAbove){
    if(dragT <= (mouseY-(dragH/2)))
    	return up();
  }
  
  if(clickUp){
  if(contentT < 0){		
  	dragT = dragT - (speed*scrollLength);
  	
  	if(dragT < (rulerT))
  		dragT = rulerT;
  		
  	contentT = contentT + speed;
  	if(contentT > 0)
  		contentT = 0;
  	
  	moveTo();
  	timer = setTimeout("scrollUp()",25);
  }
  }
  return false;
}

// Scrolls down
function scrollDown(){
getT();
   //alert(contentT);
if(clickBelow){
if(dragT >= (mouseY-(dragH/2)))
	return up();
}

if(clickDown){
if(contentT > -(contentH - contentClipH)){			
	dragT = dragT + (speed*scrollLength);
	if(dragT > (rulerT + scrollH - dragH))
		dragT = (rulerT + scrollH - dragH);
	
	contentT = contentT - speed;
	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(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;		
								// Height of content layer and clip layer
								contentH = parseInt(document.all.content.scrollHeight);
								contentClipH = parseInt(document.all.contentClip.style.height);
				}
				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;
								// 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
								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);
								// Height of content layer and clip layer
								contentH = parseInt(document.getElementById("content").offsetHeight);
								contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
								document.getElementById("content").style.top = "0px";
				}
				// Number of pixels scrollbar should move
				
				//alert(contentH + ' -> ' + contentClipH);

				if (contentH >= contentClipH)
				{
								dragH = parseInt((scrollH*contentClipH)/contentH);
								/*
								alert("scrollH = "+scrollH);
								alert("contentClipH = "+contentClipH);
								alert("contentH = "+contentH);
								alert("dragH = "+dragH);
								*/
								
								if(ie4){
												// Scrollbar X and Y variables
												document.all.drag.style.height = dragH+"px";	
								}
								else if(nn4){
												// Scrollbar X and Y variables
												document.drag.height = dragH;
								}
								else if(dom){
												// Scrollbar X and Y variables
												document.getElementById("drag").style.height = dragH+"px";	
								}
				
								
								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;
				}
				else
				{
								if(ie4){
												// Scrollbar X and Y variables
												document.all.up.style.visibility = "hidden";	
												document.all.down.style.visibility = "hidden";	
												document.all.drag.style.visibility = "hidden";	
												document.all.ruler.style.visibility = "hidden";	
								}
								else if(nn4){
												// Scrollbar X and Y variables
												document.up.visibility = "hidden";
												document.down.visibility = "hidden";
												document.drag.visibility = "hidden";
												document.ruler.visibility = "hidden";
								}
								else if(dom){
												// Scrollbar X and Y variables
												document.getElementById("up").style.visibility = "hidden";
												document.getElementById("down").style.visibility = "hidden";
												document.getElementById("drag").style.visibility = "hidden";	
												document.getElementById("ruler").style.visibility = "hidden";
								}
				}
}

function rePosObjects()
{
				if(ie4){
								//alert(document.all.container.style.height);

                ScrollTopPos = getPageTop(document.all.container);
								ScrollLeftPos = getPageLeft(document.all.container);

								//alert(ScrollTopPos+' x '+ScrollLeftPos);

								ScrollHeight = parseInt(document.all.container.style.height);
								ScrollWidth = parseInt(document.all.container.style.width);

								//alert(ScrollWidth+' x '+ScrollHeight);
								
								// Up-arrow X and Y variables
								document.all.up.style.pixelLeft = ScrollLeftPos+ScrollWidth-upW;
								document.all.up.style.pixelTop = ScrollTopPos;
								
								// Down-arrow X and Y variables
								document.all.down.style.pixelLeft = ScrollLeftPos+ScrollWidth-downW;
								document.all.down.style.pixelTop = ScrollTopPos+ScrollHeight-downH;

								// Scrollbar X and Y variables
								document.all.drag.style.pixelLeft = ScrollLeftPos+ScrollWidth-upW;
								document.all.drag.style.pixelTop = ScrollTopPos+upH;
								
								// Ruler Y variable
								document.all.ruler.style.pixelLeft = ScrollLeftPos+ScrollWidth-upW;
								document.all.ruler.style.pixelTop = ScrollTopPos+upH;
								
								// Height of content layer and clip layer
								document.all.contentClip.style.position = "absolute";
								document.all.contentClip.style.pixelLeft = ScrollLeftPos;
								document.all.contentClip.style.pixelTop = ScrollTopPos;
								document.all.contentClip.style.width = (ScrollWidth-upW)+"px";
								document.all.contentClip.style.height = (ScrollHeight)+"px";
								document.all.content.style.position = "absolute";
								document.all.content.style.width = (ScrollWidth-upW)+"px";
				}
				else if(nn4){
								ScrollTopPos = getPageTop(document.container);
								ScrollLeftPos = getPageLeft(document.container);
								
								//alert(ScrollTopPos+' x '+ScrollLeftPos);
								
								ScrollHeight = parseInt(document.container.height);
								ScrollWidth = parseInt(document.container.width);

								// Up-arrow X and Y variables
								document.up.left = ScrollLeftPos+ScrollWidth-upW;
								document.up.top = ScrollTopPos;

								// Down-arrow X and Y variables
								document.down.left = ScrollLeftPos+ScrollWidth-downW;
								document.down.top = ScrollTopPos+ScrollHeight-downH;
								
								// Scrollbar X and Y variables
								document.drag.left = ScrollLeftPos+ScrollWidth-upW;
								document.drag.top = ScrollTopPos+upH;

								// Ruler Y variable
								document.ruler.left = ScrollLeftPos+ScrollWidth-upW;
								document.ruler.top = ScrollTopPos+upH;
								
								// Height of content layer and clip layer
								document.contentClip.position = "absolute";
								document.contentClip.left = ScrollLeftPos;
								document.contentClip.top = ScrollTopPos;
								document.contentClip.width = (ScrollWidth-upW)+"px";
								document.contentClip.height = (ScrollHeight)+"px";
								document.content.position = "absolute";
								document.content.width = (ScrollWidth-upW)+"px";
								
				}
				else if(dom){
								ScrollTopPos = getPageTop(document.getElementById("container"));
								ScrollLeftPos = getPageLeft(document.getElementById("container"));
								
								//alert(ScrollTopPos+' x '+ScrollLeftPos);

								ScrollHeight = parseInt(document.getElementById("container").style.height);
								ScrollWidth = parseInt(document.getElementById("container").style.width);

        				// Up-arrow X and Y variables
								document.getElementById("up").style.left = (ScrollLeftPos+ScrollWidth-upW)+"px";
								document.getElementById("up").style.top = ScrollTopPos+"px";
								
								// Down-arrow X and Y variables
								document.getElementById("down").style.left = (ScrollLeftPos+ScrollWidth-downW)+"px";
								document.getElementById("down").style.top = (ScrollTopPos+ScrollHeight-downH)+"px";
								
								// Scrollbar X and Y variables
								document.getElementById("drag").style.left = (ScrollLeftPos+ScrollWidth-upW)+"px";
								document.getElementById("drag").style.top = (ScrollTopPos+upH)+"px";
								
								// Ruler Y variable
								document.getElementById("ruler").style.left = (ScrollLeftPos+ScrollWidth-upW)+"px";
								document.getElementById("ruler").style.top = (ScrollTopPos+upH)+"px";
								
								// Height of content layer and clip layer
								document.getElementById("contentClip").style.position = "absolute";
								document.getElementById("contentClip").style.left = ScrollLeftPos+"px";
								document.getElementById("contentClip").style.top = ScrollTopPos+"px";
								document.getElementById("contentClip").style.width = (ScrollWidth-upW)+"px";
								document.getElementById("contentClip").style.height = (ScrollHeight)+"px";
								document.getElementById("content").style.position = "absolute";
								document.getElementById("content").style.width = (ScrollWidth-upW)+"px";
				}
				
				// Height of scrollbar
				scrollH = ScrollHeight-(upH+downH);
				return true;
}	
