// JavaScript Document

var frametop = 0;
var bartop = 0;
var speed = 10;

var scrollobj;
var scrollerobj;
var scrollupto;
var scrolldownto;
var frameheight;
var barheight;
var scrollareaheight = 460;

function scrollinit() {
	scrollobj = document.getElementById("bioframe");
	scrollerobj = document.getElementById("scroller");
	containerobj = document.getElementById("biolayout");
	
	if (scrollobj.scrollHeight > containerobj.offsetHeight) {
		frameheight = scrollobj.scrollHeight - containerobj.offsetHeight;
		
		barheight = Math.round(containerobj.offsetHeight * scrollareaheight / scrollobj.scrollHeight);
		if(barheight < 25) barheight = 25;
		scrollerobj.style.height = barheight + "px";
		scrollerobj.style.display = '';
		scrollerobj.onmousedown = startmove;
	}else {
		scrollerobj.style.display = 'none';	
	}
	return true;
}

function scrollup(barspeed) {
	if(scrolldownto) { 
	  clearTimeout(scrolldownto);
	}

	if(!barspeed) {
    barspeed = speed;
    scrollupto=setTimeout("scrollup()",20);
  }
  
  scrollspeed = Math.round(barspeed * frameheight / (scrollareaheight - barheight));

	if(frameheight + frametop > 0) {
		frametop = frametop - scrollspeed;
	} else {
	  frametop = -frameheight;
		clearTimeout(scrollupto);
	}
	
	if(bartop + barheight <= scrollareaheight - barspeed) {
	  bartop = bartop + barspeed;
	} else {
		bartop = scrollareaheight - barheight;
		clearTimeout(scrollupto);
	}

	scrollobj.style.top = frametop + "px";
	scrollerobj.style.top = bartop + "px";
	
	return true;
}

function scrolldown(barspeed) {
  if(scrollupto) { 
	  clearTimeout(scrollupto);
	}

	if(!barspeed) {
    barspeed = speed;
    scrolldownto=setTimeout("scrolldown()",20)
  }
  
  scrollspeed = Math.round(barspeed * frameheight / (scrollareaheight - barheight));

  if(frametop < 0) {
		frametop = frametop + scrollspeed;
	} else {
	  frametop = 0;
		clearTimeout(scrolldownto);
	}
	
	if(bartop - barspeed >= 0) {
	  bartop = bartop - barspeed;
	} else {
	  bartop = 0;
		clearTimeout(scrolldownto);
	}

	scrollobj.style.top = frametop + "px";
	scrollerobj.style.top = bartop + "px";
	
	return true;
}

function scrollstop() {
	if(scrollupto) clearTimeout(scrollupto);
	if(scrolldownto) clearTimeout(scrolldownto);

	return true;
}

function startmove(event) {
  if(event) {
  	scrollerobj.ymouse = event.clientY;
  } else {
	scrollerobj.ymouse = window.event.clientY;
  }
  scrollerobj.ybar = bartop;

  document.onmousemove = keepmove;
  document.onmouseup = stopmove;
}

function keepmove(event) {
  if(event) {
  delta = event.clientY - scrollerobj.ymouse - (bartop - scrollerobj.ybar);
  } else {
  delta = window.event.clientY - scrollerobj.ymouse - (bartop - scrollerobj.ybar);
  }
  if(delta > 0) {
    scrollup(delta);
  } else {
    scrolldown(-delta);
  }
}

function stopmove(event) {
  document.onmousemove=null;
  document.onmouseup=null;
  scrollstop();
}

