// scroller3 by rod morelos (rod@dhtmlcentral.com) www20.brinkster.com/viewsrc 
// 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 = 7; // number of scrollable content
var scrollspeed = 5; // scroll speed
var wheelspeed = 12; // scroll speed when using the mouse wheel
var active = 7; // set default page/layer visible onload

// variables not to set:
var scrolltimer = 0; // timer variable for vscroll function
var wheelscrolled = false; // wheel scroll
var contenty = 0; // content y
var dragy = 0; // scrollbar y
var clicky = 0; // offset between mouse and the scrollbar

// mouse down
function mdown(e)
{
	wheelscrolled = false;
	if (obar.onmouseover(e))
	{
		opage.preventdefault(e);
		obar.grab = true;
		clicky = opage.mousey - obar.y;
	}
	else if (obg.onmouseover(e))
	{
		obg.grab = true;
		if (opage.layery < obar.y) scrollup(active,scrollspeed);
		else if (opage.layery > obar.y) scrolldown(active,scrollspeed);
	}
	else if (oup.onmouseover(e))
	{
		oup.grab = true;
		scrollup(active,scrollspeed);
	}
	else if (odown.onmouseover(e))
	{
		odown.grab = true;
		scrolldown(active,scrollspeed);
	}
}

// mouse up
function mup(e)
{
	obar.grab = false;
	obg.grab = false;
	oup.grab = false;
	odown.grab = false;
	clearTimeout(scrolltimer);
}

// mouse move
function mmove(e)
{
	opage.mouse(e);
	if (obar.grab && ocontent[active].h > ocontainer.h)
	{
		dragy = opage.mousey - clicky;
		if (dragy < 0) dragy = 0;
		if (dragy > obg.h - obar.h) dragy = obg.h - obar.h;
		contenty = 0 - (dragy * (ocontent[active].h - ocontainer.h) / Math.round(obg.h - obar.h));
		ocontent[active].moveto(0,contenty);
		obar.moveto(obar.x,dragy);
		return false;
	}
	if (obg.grab || oup.grab || odown.grab) return false;
}

// scroll up
function scrollup(num,speed)
{
	if (oup.grab || obg.grab || wheelscrolled)
	{
		if (ocontent[num].y < 0)
		{
			if (obg.grab && (obar.y <= opage.layery - (obar.h / 2))) return mup();
			contenty = ocontent[num].y + speed;
			if (contenty > 0) contenty = 0;
			ocontent[num].moveto(0,contenty);
			dragy = 0 - (ocontent[num].y * Math.round(obg.h - obar.h) / (ocontent[num].h - ocontainer.h));
			obar.moveto(obar.x,dragy);
			if (!wheelscrolled) scrolltimer = setTimeout('scrollup('+num+','+speed+')',scrollstep);
		}
	}
}

// scroll down
function scrolldown(num,speed)
{
	if (odown.grab || obg.grab || wheelscrolled)
	{
		if (obg.grab && (obar.y >= opage.layery - (obar.h / 2))) return mup();
		if (ocontent[num].y > -(ocontent[num].h - ocontainer.h))
		{
			contenty = ocontent[num].y - speed;
			if (contenty < -(ocontent[num].h - ocontainer.h)) contenty = -(ocontent[num].h - ocontainer.h);
			ocontent[num].moveto(0,contenty);
			dragy = 0 - (ocontent[num].y * Math.round(obg.h - obar.h) / (ocontent[num].h - ocontainer.h));
			obar.moveto(obar.x,dragy);
			if (!wheelscrolled) scrolltimer = setTimeout('scrolldown('+num+','+speed+')',scrollstep);
		}
	}
}

// function that scrolls the content using mouse wheel
function wheelscroll(num)
{
	if (event.wheelDelta <= -120)
	{
		wheelscrolled = true;
		scrolldown(active,wheelspeed);
	}
	else if (event.wheelDelta >= 120)
	{
		wheelscrolled = true;
		scrollup(active,wheelspeed);
	}
}

// load layer
function swapto(num)
{
	ocontent[active].visibility(0);
	active = num;
	obar.moveto(obar.x,0);
	ocontent[active].moveto(0,0);
	ocontent[active].visibility(1);
}

// initialise function
function init()
{
	ocontent = new Array();
	ocontainer = new dhtmlobject('divcontainer');
	for (var i=1;i<=scrollables;i++)
	{
		ocontent[i] = new dhtmlobject('divcontent'+i);
	}
	if (bw.ie6) addevent(ocontainer.el,'mousewheel',new Function('wheelscroll(); return false;'),true);
	obar = new dhtmlobject('divbar');
	obar.grab = false;
	odown = new dhtmlobject('divdown');
	odown.grab = false;
	oup = new dhtmlobject('divup');
	oup.grab = false;
	obg = new dhtmlobject('divbg');
	obg.grab = false;
	swapto(active);
	addevent(document,'mousemove',mmove,true);
	addevent(document,'mousedown',mdown,true);
	addevent(document,'mouseup',mup,true);
}

// initiate script on page load
onload = init;