var upH = 12; // Height of up-arrow
var upW = 14; // Width of up-arrow
var downH = 12; // Height of down-arrow
var downW = 14; // Width of down-arrow
var dragH = 85; // Height of scrollbar
var dragW = 14; // Width of scrollbar
var scrollH = 273; // Height of scrollbar
var speed = 6; // 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 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);

  var dragTopCheck = dragT + dragTopAbsPos;
  if (dragTopRelPos == dragT)
    dragTopCheck = dragTopAbsPos;
    
  var rulerTopCheck = dragT + rulerTopAbsPos;
  if (rulerTopRelPos == dragT)
    rulerTopCheck = rulerTopAbsPos;
    
  // 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 >= dragTopCheck) && (mouseY <= (dragTopCheck + dragH))){
    clickDrag = true;
    return false;
  }
  else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && (mouseY >= rulerTopCheck) || mouseX >= dragL && (mouseX <= (dragL + dragW)) && (mouseY <= (rulerTopCheck + scrollH))){
    // If click above drag
    if(mouseY < (rulerTopCheck)){
        clickUp = true;
        return scrollUp();
    }
    // Else click below drag
    else{
        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 || dom)
  {
     contentT = parseInt(document.getElementById("contentClipArea").style.top);
//    contentT = document.all.contentClipArea.style.pixelTop;
  }
  else if(nn4)
  {
    contentT = document.contentClip.document.contentClipArea.top;
  }
  else if(dom)
  {
    contentT = parseInt(document.getElementById("contentClipArea").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 || dom){
//    document.all.contentClipArea.style.top = contentT;
//    document.all.ruler.style.top = dragT;
//    document.all.drag.style.top = dragT;
//test();
    document.getElementById("contentClipArea").style.top = contentT + "px";
   //alert('drag top: ' + document.getElementById("drag").style.top);
    document.getElementById("drag").style.top = dragT + "px";
    document.getElementById("ruler").style.top = dragT + "px";
  }
  else if(nn4){
    document.contentClip.document.contentClipArea.top = contentT;
    document.ruler.top = dragT;
    document.drag.top = dragT;
  }
  else if(dom){
    document.getElementById("contentClipArea").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();
  
  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();
}

function findPos(obj) {
	var curleft = curtop = 0;

    // check current offset first
//    curleft = obj.offsetLeft;
//    curtop = obj.offsetTop;
    
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

// Preload
function eventLoader(){

  if(ie4){
    // Up-arrow X and Y variables
    var xy=findPos(document.getElementById("up"));
    upL = parseInt(xy[0]);
    upT = parseInt(xy[1]);   

    // Down-arrow X and Y variables
    var xy=findPos(document.getElementById("down"));
    downL = parseInt(xy[0]);
    downT = parseInt(xy[1]);   

    // Scrollbar X and Y variables
    var xy=findPos(document.getElementById("drag"));
    dragL = parseInt(xy[0]);
    dragTopAbsPos = parseInt(xy[1]);
    dragTopRelPos = document.getElementById("drag").style.pixelTop; 
    dragT = document.getElementById("drag").style.pixelTop;   

    // Ruler Y variable
    var xy=findPos(document.getElementById("ruler"));
    rulerTopAbsPos = parseInt(xy[1]);
    rulerTopRelPos = document.getElementById("ruler").style.pixelTop; 
    rulerT = document.getElementById("ruler").style.pixelTop; 
    
    // Height of content layer and clip layer
    contentH = parseInt(document.getElementById("contentClipArea").offsetHeight);
    contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
    document.getElementById("contentClipArea").style.top = 0 + "px";
    }
  else if(nn4){
    // Up-arrow X and Y variables
    var xy=findPos(document.getElementById("up"));
    upL = parseInt(xy[0]);
    upT = parseInt(xy[1]);   

    // Down-arrow X and Y variables
    var xy=findPos(document.getElementById("down"));
    downL = parseInt(xy[0]);
    downT = parseInt(xy[1]);   

    // Scrollbar X and Y variables
    var xy=findPos(document.getElementById("drag"));
    dragL = parseInt(xy[0]);
    dragTopAbsPos = parseInt(xy[1]);
    dragTopRelPos = document.getElementById("drag").top; 
    dragT = document.getElementById("drag").top;   

    // Ruler Y variable
    var xy=findPos(document.getElementById("ruler"));
    rulerTopAbsPos = parseInt(xy[1]);
    rulerTopRelPos = document.getElementById("ruler").top; 
    rulerT = document.getElementById("ruler").top; 
    
    // Height of content layer and clip layer
    contentH = document.getElementById("contentClipArea").clip.bottom;
    contentClipH = document.getElementById("contentClip").clip.bottom;
    document.getElementById("contentClipArea").top = 0 + "px";
  }
  else if(dom){
    // Up-arrow X and Y variables
    var xy=findPos(document.getElementById("up"));
    upL = parseInt(xy[0]);
    upT = parseInt(xy[1]);   

    // Down-arrow X and Y variables
    var xy=findPos(document.getElementById("down"));
    downL = parseInt(xy[0]);
    downT = parseInt(xy[1]);   

    // Scrollbar X and Y variables
    var xy=findPos(document.getElementById("drag"));
    dragL = parseInt(xy[0]);
    dragTopAbsPos = parseInt(xy[1]);
    dragTopRelPos = parseInt(document.getElementById("drag").style.top); 
    dragT = parseInt(document.getElementById("drag").style.top);   

    // Ruler Y variable
    var xy=findPos(document.getElementById("ruler"));
    rulerTopAbsPos = parseInt(xy[1]);
    rulerTopRelPos = parseInt(document.getElementById("ruler").style.top); 
    rulerT = parseInt(document.getElementById("ruler").style.top); 
    
    // Height of content layer and clip layer
    contentH = parseInt(document.getElementById("contentClipArea").offsetHeight);
    contentClipH = parseInt(document.getElementById("contentClip").offsetHeight);
    document.getElementById("contentClipArea").style.top = 0 + "px";
  }
  
  // Number of pixels scrollbar should move
  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;
}