//INIT VARIABLES...MESS
var scrollnum = 1;	// number of scrollers on this page..THIS IS THE ONLY VARIABLE THAT MUST BE SET!
var interval = 25; // number of miliseconds between each scrollupdate..recommended to 25... lower = faster
var speed = 6;	// speed of movement pr. interval - normal scroll
var barspeed = 6; // speed of movment pr. interval - click on scrollbg
var wheelspeed = 20; //	speed of movment pr. interval  - mousewheel
// DON'T MESS BELOW THIS LINE...(UNLESS YOU INTEND TO MODIFY AND KNOW WHAT YOU ARE DOING)
// some basic browser/os checks
var agent = navigator.userAgent.toLowerCase();
var os = navigator.platform.toLowerCase();
var win = (os.indexOf('win32') != -1);
var mac = (os.indexOf('mac') != -1);
var ie5 = (agent.indexOf('msie 5') != -1);
var ie6 = (agent.indexOf('msie 6') != -1) || (agent.indexOf('msie 7') != -1);
var nn = (agent.indexOf('netscape') != -1);
var mz = (agent.indexOf('gecko') != -1 && !nn);
var op5  = (agent.indexOf('opera 5') != -1);
var op6  = (agent.indexOf('opera 6') != -1);
if (op5||op6)	{	ie5 = false; ie6 = false;	nn = false;	 }
// this is what to do with browsers that don't support the scroller. delete if wantet.
var d = document;
var px = (op5||op6)?'':'px';
// some timesaving functions
function getel(el)	{	return d.getElementById(el);	}
function gety(el)		{	if(op5) return getel(el).style.pixelTop; else return getel(el).offsetTop;	}
function geth(el)		{	if(op5) return getel(el).style.pixelHeight; else return getel(el).offsetHeight;	}
function sety(el,y)	{	getel(el).style.top=y+px;	}
var scrollers = new Array();
var dobarscroll,orgslide,loadedscroll,dowheelscroll = false;
var thenum,thetimer,evy;
d.onmouseup=mup; 
d.onmousemove=mmove; 
d.onmousedown=mdown;	
window.onload = setup;
function mdown(e)	{
	if(!loadedscroll) return;
		var ev=(ie5||ie6||op6)?event:e;	
		var el = (ie5||ie6) ? ev.srcElement : ev.target;
		
		if(el.nodeType==3||el.tagName.toLowerCase() == 'img')	el = el.parentNode;
		var theid=el.id;
		var thefunc = theid.slice(0,theid.length-1);
		thenum = theid.charAt(theid.length-1);
		//if the function is scrolldown or scrollup
		if(thefunc=='scrollup'||thefunc=='scrolldown')	 {	
			if(thefunc=='scrollup') scrollers[thenum].currspeed= speed;
			else	scrollers[thenum].currspeed= -speed;
			doscroll();	
			return;
		}
	
		// if the function is  scrollbg
		if(thefunc=='scrollbg')	 {
			evy=(ie5||ie6||op6)?ev.offsetY:ev.layerY;
			var ratio = scrollers[thenum].main_endy / scrollers[thenum].scroll_height;
			evy= evy - (scrollers[thenum].bar_height/2);
				if(evy<gety(scrollers[thenum].bar_id))	 {
					scrollers[thenum].currspeed = barspeed;
					scrollers[thenum].main_starty = Math.round(evy*ratio);
					if(scrollers[thenum].main_starty>scrollers[thenum].main_starty_org) scrollers[thenum].main_starty = scrollers[thenum].main_starty_org;
				}
				if(evy>gety(scrollers[thenum].bar_id))	 {
					scrollers[thenum].currspeed = -barspeed;
					if(evy>scrollers[thenum].scroll_height_org) evy = scrollers[thenum].scroll_height_org;
					scrollers[thenum].main_endy = Math.round(evy*ratio);
					scrollers[thenum].scroll_height = evy;
				}
			doscroll();
			return;
		}
	
		// if the function is scrollbar
		if(thefunc=='scrollbar')	{	
			dobarscroll=true;	
			evy=(ie5||ie6||op6)?ev.offsetY:ev.layerY;	
			return;
		}
}
function mup()	{
	if(thetimer)	clearTimeout(thetimer);
	if(dobarscroll)	dobarscroll = false;
		if(thenum)	{
		}
}
function mmove(e)	{
	if(!dobarscroll||!thenum)	return;
	
	var ev=(ie5||ie6||op6)?event:e;	
	var my = ev.clientY;	
	var y = (my-evy) - scrollers[thenum].scroll_top;
	if(y<scrollers[thenum].bar_starty)	y = scrollers[thenum].bar_starty;	
	if(y>scrollers[thenum].bar_endy)	y = scrollers[thenum].bar_endy;
	sety(scrollers[thenum].bar_id, y);	
	var barslide = Math.round((gety(scrollers[thenum].bar_id)-(scrollers[thenum].bar_starty))*(1/scrollers[thenum].slidelength));
	sety(scrollers[thenum].main_id, -barslide);
	if(barslide == 0 && gety(scrollers[thenum].main_id)!=scrollers[thenum].main_starty) sety(scrollers[thenum].main_id, scrollers[thenum].main_starty);
	
	return false
}
// wheelscroll function
function wheelscroll() {
 if(!dowheelscroll)  {
  var el = event.srcElement;
 
 if(el.id.slice(0,el.id.length-1)!='main') {
  do {
 
   el = el.parentNode;
 
  } while(el.tagName.toLowerCase()!='div'||el.id.slice(0,el.id.length-1)!='main')  
 }
  thenum = el.id.charAt(el.id.length-1);
  if(thenum)  oldnum = thenum;
  if(!thenum) thenum = oldnum;
     if (event.wheelDelta >= 120) scrollers[thenum].currspeed = wheelspeed;
  else if (event.wheelDelta <= -120) scrollers[thenum].currspeed = -wheelspeed;
      dowheelscroll = true;
  doscroll();
  return;
 }
}
// main scrolling function
function doscroll()	{
	if(!thenum) return;
	if(gety(scrollers[thenum].main_id)+scrollers[thenum].currspeed>scrollers[thenum].main_starty) scrollers[thenum].currspeed = scrollers[thenum].main_starty - gety(scrollers[thenum].main_id);
	if(gety(scrollers[thenum].main_id)+scrollers[thenum].currspeed<scrollers[thenum].main_endy) scrollers[thenum].currspeed = scrollers[thenum].main_endy - gety(scrollers[thenum].main_id);	 
	
	sety(scrollers[thenum].main_id, gety(scrollers[thenum].main_id)+scrollers[thenum].currspeed);
	var mainslide = Math.round(gety(scrollers[thenum].main_id)/(scrollers[thenum].main_endy/scrollers[thenum].scroll_height));
	if(mainslide<0) mainslide = 0;
	if(mainslide>scrollers[thenum].scroll_height) mainslide = scrollers[thenum].scroll_height;
	sety(scrollers[thenum].bar_id, scrollers[thenum].bar_starty+mainslide);
	if(!dowheelscroll)	thetimer = setTimeout('doscroll()', interval);
	dowheelscroll = false;
	return false
}
// setting up some values
function setup()	{
	for(var i = 0; i<scrollnum;i++)	{
		scrollers[i] =	{
		
		bar_starty				: gety('scrollup'+i)+geth('scrollup'+i),
		bar_height				: geth('scrollbar'+i),
		bar_endy				: gety('scrolldown'+i)-geth('scrollbar'+i),
		bar_id					: 'scrollbar'+i,
		scroll_height			: geth('scrollbg'+i)-geth('scrollbar'+i),
		scroll_height_org	: geth('scrollbg'+i)-geth('scrollbar'+i),
		scroll_top				: gety('scroll'+i),
		main_starty			: gety('main'+i),
		main_starty_org	 	: gety('main'+i),
		main_endy				: ((geth('main'+i)*-1)+geth('cont'+i))-5,
		main_endy_org		: ((geth('main'+i)*-1)+geth('cont'+i))-5,
		main_id					: 'main'+i,
		currspeed				: -speed,
		slidelength				: ((geth('scrollbg'+i)-geth('scrollbar'+i))/((geth('main'+i)+5)-geth('cont'+i)))
		}
	if (ie6&&!mac)	{	getel(scrollers[i].main_id).onmousewheel = function(){wheelscroll();};	}
	}
loadedscroll = true;
}
