// dhtml scrollbar by rod morelos - www20.brinkster.com/viewsrc, rod@dhtmlcentral.com
// keep these two lines and you're free to use this code

// variables to set:
var scrollstep=30; // the speed of the timeout between each scroll
var scrollables=1; // number of scrollable content
var scrollspeed=3; // scroll speed
var wheelspeed=12; // scroll speed when using the mouse wheel

// variables not to set:
var scrolltimer=0; // timer variable for vscroll function
var wheelscrolled=false; // wheel scroll

// checks if the mouse pointer is over the element
dhtmlobject.prototype.mover=function(x,y)
{
el=this.el;
while (el.parentNode.nodeName.toLowerCase()!='body') { el=el.parentNode; x-=el.offsetLeft; y-=el.offsetTop; }
if (x>=this.x&&(x<=(this.x+this.w))&&y>=this.y&&(y<=(this.y+this.h))) return true; else return false;
};

// mouse down
function mdown(e)
{
opage.mouse(e);
for (var i=1;i<=scrollables;i++)
{
if (obar[i].mover(opage.mouseX,opage.mouseY))
{
obar[i].grab=true;
clicky=(opage.mouseY-obar[i].y);
return false;
}
else if (obg[i].mover(opage.mouseX,opage.mouseY))
{
obg[i].grab=true;
if (opage.layerY<obar[i].y) scrollup(i,scrollspeed);
else if (opage.layerY>obar[i].y) scrolldown(i,scrollspeed);
return false;
}
else if (oup[i].mover(opage.mouseX,opage.mouseY))
{
oup[i].grab=true;
scrollup(i,scrollspeed);
return false;
}
else if (odown[i].mover(opage.mouseX,opage.mouseY))
{
odown[i].grab=true;
scrolldown(i,scrollspeed);
return false;
}
}
}

// mouse up
function mup(e)
{
for (var i=1;i<=scrollables;i++) { obar[i].grab=false; obg[i].grab=false; oup[i].grab=false; odown[i].grab=false; }
clearTimeout(scrolltimer);
}

// mouse move
function mmove(e)
{
opage.mouse(e);
for (var i=1;i<=scrollables;i++)
{
if (obar[i].grab&&ocontent[i].h>ocontainer[i].h)
{
dragy=(opage.mouseY-clicky);
if (dragy<0) dragy=0;
if (dragy>obar[i].endy) dragy=obar[i].endy;
contenty=dragy/(obg[i].h-obar[i].h);
contenty*=-(ocontent[i].h-ocontainer[i].h);
scrollit(i);
return false;
}
if (obg[i].grab||oup[i].grab||odown[i].grab) return false;
}
}

// scroll up
function scrollup(num,speed)
{
if (oup[num].grab||obg[num].grab||wheelscrolled)
{
if (ocontent[num].y<0)
{
if (obg[num].grab&&(obar[num].y<=(opage.layerY-(obar[num].h/2)))) return mup();
contenty=ocontent[num].y+speed;
if (contenty>0) contenty=0;
scrollit(num);
if (!wheelscrolled) scrolltimer=setTimeout('scrollup('+num+','+speed+')',scrollstep);
wheelscrolled=false;
}
}
}

// scroll down
function scrolldown(num,speed)
{
if (odown[num].grab||obg[num].grab||wheelscrolled)
{
if (obg[num].grab&&(obar[num].y>=(opage.layerY-(obar[num].h/2)))) return mup();
if (ocontent[num].y>-(ocontent[num].h-ocontainer[num].h))
{
contenty=ocontent[num].y-speed;
if (contenty<-(ocontent[num].h-ocontainer[num].h)) contenty=-(ocontent[num].h-ocontainer[num].h);
scrollit(num);
if (!wheelscrolled) scrolltimer=setTimeout('scrolldown('+num+','+speed+')',scrollstep);
wheelscrolled=false;
}
}
}

// function that scrolls the content
function scrollit(num)
{
ocontent[num].moveto(0,contenty);
dragy=ocontent[num].y/(ocontent[num].h-ocontainer[num].h);
dragy*=-(obg[num].h-obar[num].h);
obar[num].moveto(obar[num].x,dragy);
}

// function that scrolls the content using mouse wheel
function wheelscroll(num)
{
if (event.wheelDelta<=-120) { wheelscrolled=true; scrolldown(num,wheelspeed); }
else if (event.wheelDelta>=120) { wheelscrolled=true; scrollup(num,wheelspeed); }
}

// initialise function
function init()
{
obar=new Array();
odown=new Array();
oup=new Array();
obg=new Array();
ocontainer=new Array();
ocontent=new Array();
scrollh=new Array();
for (var i=1;i<=scrollables;i++)
{
ocontainer[i]=new dhtmlobject('divcontainer'+i);
ocontent[i]=new dhtmlobject('divcontent'+i);
obar[i]=new dhtmlobject('divbar'+i);
obar[i].grab=false;
odown[i]=new dhtmlobject('divdown'+i);
odown[i].grab=false;
oup[i]=new dhtmlobject('divup'+i);
oup[i].grab=false;
obg[i]=new dhtmlobject('divbg'+i);
obg[i].grab=false;
// obar[i].seth((ocontainer[i].h*obg[i].h)/ocontent[i].h);
scrollh[i]=((obg[i].h-obar[i].h)/(ocontent[i].h-ocontainer[i].h));
obar[i].endy=obg[i].h-obar[i].h;
if (bw.ns6) ocontent[i].css.position='relative'; // scrollbar hack for ns6
if (bw.ie6) ae(ocontainer[i].el,'mousewheel',new Function('wheelscroll('+i+'); return false;'),true);
}
ae(document,'mousemove',mmove,true);
ae(document,'mousedown',mdown,true);
ae(document,'mouseup',mup,true);
}

// initiate script on page load
onload=init;


