var t, u, uparrow, downarrow, slider, toc, tocheight, viewport, viewportheight, scrollbarheight, sliderheight, sliderjumpincrement, sliderslideincrement, difference, multiple, initialpos, initialx, prevy;
var movement= new SmoothMovement(0, 0);
var movement2= new SmoothMovement(0, 0, 0);
var tocposition=0;
var sliderposition=0;
var tocjumpincrement = 50;
var tocslideincrement = 10;
var position = 0;
var holding = false;


window.onload = function (){
    uparrow = document.getElementById("uparrow");
    downarrow = document.getElementById("downarrow");
    slider = document.getElementById("slider");
    //document.onMouseMove = function() {drag();};
    //slider.onMouseDown = function (){alert("IT WORKS?");};//function () {holding=true; alert(holding);}
    //slider.onMouseUp = function () {holding=false; alert(holding);}
    document.onmouseup = release;
    uparrow.onmousedown = waitforit;
    uparrow.onmouseup = jumpup;
    downarrow.onmousedown = waitforit;
    downarrow.onmouseup = jumpdown;
    //sliderheight = slider.offsetHeight;
    scrollbarheight = document.getElementById("track").offsetHeight;
    toc = document.getElementById("toc");
    viewport = document.getElementById("articles");
    tocheight = toc.offsetHeight+15;
       //alert("sliderslideincrement: " + sliderslideincrement + " tocslideincrement: " + tocslideincrement + " sliderjumpincrement: "+ sliderjumpincrement + " scrollbarheight: "+ scrollbarheight + " sliderheight: " + sliderheight + " tocjumpincrement: " + tocjumpincrement + " tocheight: " + tocheight);
    viewportheight = viewport.offsetHeight;
    if(viewportheight >= tocheight){
        document.getElementById("scrollbar").style.visibility = "hidden";
    }//end if
    viewportheight = viewportheight - 15;//temporary fix to problem scrolling to the bottom of the toc
    difference = tocheight - viewportheight;
    sliderheight = (viewportheight * scrollbarheight)/tocheight;
    //alert(slider.style.height);
    slider.style.height = sliderheight+"px";
    sliderjumpincrement = ((scrollbarheight - sliderheight) * tocjumpincrement)/difference;
    sliderslideincrement = ((scrollbarheight - sliderheight) * tocslideincrement)/difference;
    multiple = (tocheight-viewportheight)/(scrollbarheight-sliderheight);
    window.setInterval(slide, 20);
    
    //if the browser is IE4+
    document.onselectstart=new Function ("return false")

    //if the browser is NS6
    if (window.sidebar){
        document.onmousedown=disabletext
        document.onclick=reEnable
    }//end if
    
    //document.onmousemove=drag(document.onmousemove);
    
}//end onload

function disabletext(e){
    return false
}

function reEnable(){
    return true
}

function waitforit(){//wait until it is clear that the user wishes to scroll rather than jump by increment
    if (this == uparrow){
        t=setTimeout("slideup();", 150);
    }//end if
    else if (this == downarrow)
        t=setTimeout("slidedown();", 150);
}//end function waitforit

function slidedown(){
    clearTimeout(t);
    if((position*-1)<(difference)){
        if ((tocposition-tocslideincrement)*-1 > difference){
            tocposition=(difference*-1);
        }//end if
        else
            tocposition=tocposition-tocslideincrement; 
        
        if (sliderposition < (scrollbarheight-sliderheight)){
            if((sliderposition + sliderslideincrement) > (scrollbarheight-sliderheight)){
                sliderposition = (scrollbarheight-sliderheight);
            }//if
            else
                sliderposition = sliderposition+sliderslideincrement;
            movement2.changeTarget(sliderposition);
        }//end slider if
        
        movement.changeTarget(tocposition);
        u=setTimeout("slidedown();", 10);
    }//end toc if
}//end function slidedown

function slideup(){
    clearTimeout(t);
    if(tocposition<0){
        if((tocposition+tocslideincrement)>0){
            tocposition=0;
        }//end if
        else
            tocposition=tocposition+tocslideincrement;
        if (sliderposition > 0){
            if((sliderposition - sliderslideincrement) < 0){
                sliderposition = 0;
            }//if
            else
                sliderposition = sliderposition-sliderslideincrement;
            movement2.changeTarget(sliderposition);
        }//end slider if
        movement.changeTarget(tocposition);
        u=setTimeout("slideup();", 10);
    }//end if
}//end function slideup

function jumpdown(){
//alert("tocheight: "+tocheight+" viewportheight: "+viewportheight+ " tocposition: "+tocposition);
    clearTimeout(t);
    clearTimeout(u);
    if((tocposition*-1)<(difference)){
        if ((tocposition-tocjumpincrement)*-1 > difference){
            tocposition=(difference*-1);
        }//end if
        else
            tocposition=tocposition-tocjumpincrement;
        
        if (sliderposition < (scrollbarheight-sliderheight)){
            if((sliderposition + sliderjumpincrement) > (scrollbarheight-sliderheight)){
                sliderposition = (scrollbarheight-sliderheight);
            }//if
            else
                sliderposition = sliderposition+sliderjumpincrement;
            movement2.changeTarget(sliderposition);
        }//end slider if
        
        movement.changeTarget(tocposition);
    }//end if
    else{
        clearTimeout(u);
    }//end else
}//end jumpdown

function jumpup(){
    clearTimeout(t);
    clearTimeout(u);
    if(tocposition<0){
        if((tocposition+tocjumpincrement)>0){
            tocposition=0;
        }
        else
            tocposition=tocposition+tocjumpincrement;
            
        if (sliderposition > 0){
            if((sliderposition - sliderjumpincrement) < 0){
                sliderposition = 0;
            }//if
            else
                sliderposition = sliderposition-sliderjumpincrement;
            movement2.changeTarget(sliderposition);
        }//end slider if
            
        movement.changeTarget(tocposition);
    }
    else{
        clearTimeout(u);
    }
}//end function jumpup

function slide(){
    toc.style.marginTop = movement.updatePosition() + "px";
    slider.style.marginTop = movement2.updatePosition() + "px";
}//end function slide


function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }//end if
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }//end else
    return cursor;
}//end function getPosition


function capture(event){
    holding = true;
    prevy = getPosition(event).y;
    slider.style.backgroundColor = "#135573";
    //alert(prevy);
}//end function capture

function release(){
    holding = false;
    slider.style.backgroundColor = "#fff";
}//end function release

function drag(event){
    var nextyval = getPosition(event).y;
    if(getPosition(event).x > 418 || getPosition(event).x < 370 || getPosition(event).y > 530 || getPosition(event).y < 80){
        holding = false;
        slider.style.backgroundColor = "#fff";
    }//end if
    if (holding){
        if(prevy > nextyval){
            if(sliderposition - (prevy - nextyval) >= 0){
                sliderposition = sliderposition - (prevy - nextyval);
            }//end if
            else
                sliderposition = 0;
            if(tocposition < 0)
                tocposition = tocposition + (multiple * (prevy - nextyval));
            else
                tocposition = 0;
        }//end if moving up
        else if (prevy < nextyval){
            if(sliderposition + (nextyval - prevy) + sliderheight < scrollbarheight){
                sliderposition = sliderposition + (nextyval - prevy);
            }//end if
            else
                sliderposition = scrollbarheight - sliderheight;
            if((tocposition*-1)<(difference))
                tocposition = tocposition - (multiple * (nextyval - prevy));
            else
                tocposition = difference*-1;
        }//end else if moving down
        movement2.changeTarget(sliderposition);
        movement.changeTarget(tocposition);
        prevy = nextyval;
    }//end if
}//end function drag


function SmoothMovement(position, target, velocity){

  // initialise the position, target, and velocity
  position = Math.round(position);
  target   = Math.round(target);
  velocity = (velocity ? Math.round(velocity) : 0);

  /* Updates the position and velocity of this SmoothMovement and returns the
   * new position. The position is updated before the new velocity is
   * calculated, so the position is adjusted by the initial velocity the first
   * time this function is called. If this SmoothMovement was created with an
   * initial velocity of 0, or without the initial velocity being specified, the
   * position will not change the first time this function is called.
   *
   * The velocity changes by at most a single unit. The velocity decreases if
   * this SmoothMovement would otherwise overshoot the target position (which
   * may still occur if the initial velocity was too great). The velocity
   * increases if doing so will not lead to this SmoothMovement overshooting the
   * target position. In all other cases, the velocity does not change.
   */

  this.updatePosition = function(){
    position += velocity;
    if (velocity < 0){
      if (position - velocity * (velocity  - 1) / 2 < target){
        velocity++;
      }else if (position - (velocity - 1) * (velocity - 2) / 2 >= target){
        velocity--;
      }
    }else{
      if (position + velocity * (velocity + 1) / 2 > target){
        velocity--;
      }else if (position + (velocity + 1) * (velocity + 2) / 2 <= target){
        velocity++;
      }
    }
    return position;
  }
  
  /* Changes the target position of this SmoothMovement will maintaining its
   * current position and velocity. The parameter is:
   *
   * newTarget - the new target position, which is rounded to the nearest
   *             integer
   */
  this.changeTarget = function(newTarget){
    target = Math.round(newTarget);
  }
  
  /* Returns the current position of this SmoothMovement.
   */
  this.getPosition = function(){
    return position;
  }

  /* Returns the current velocity of this SmoothMovement.
   */
  this.getVelocity = function(){
    return velocity;
  }
  
  /* Returns true if this SmoothMovement has stopped, and false otherwise. This
   * SmoothMovement has stopped if its position equals its target position and
   * its velocity is 0.
   */
  this.hasStopped = function(){
    return (position == target && velocity == 0);
  }

}//end SmoothMovement