var xScrollContent; // name of scroll contentvar xScrollContentTop; // content top
var xScrollContentLeft; // content left
var xScrollContentTop; // content top

var xScrollSpeed = 4; // speed of scroller
var xScrolling; // scrolling at the moment?
function initScroller(contentID)
{
	if (xScrollContent != contentID)
	{
		xScrollContent = contentID;
		xContent = document.getElementById(contentID).getElementsByTagName('div')[0];

		// calculate position of div
		xContent.style.position = 'absolute';
		xScrollContentTop = xContent.offsetTop;
		xScrollContentLeft = xContent.offsetLeft;
		xScrollContentHeight = xContent.offsetHeight;
		xContent.style.position = '';

		if (!document.getElementById('scrollDiv'+contentID) && xContent.scrollHeight > xContent.offsetHeight)
		{
			xContent.style.cssFloat = 'left';
			xContent.style.styleFloat = 'left';
			document.onmousemove = getmouseposition;
			document.onmouseup = stopScroll;
			document.onblur = stopScroll;
			//if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false); // for firefox
			//else window.onmousewheel = document.onmousewheel = wheel; // for internet explorer

			xContent.style.overflow = 'hidden';
			drawScroller(contentID, xScrollContentLeft+xContent.offsetWidth, xScrollContentTop, xScrollContentHeight);
			moveScroll(contentID, '', 1); // set to correct position
		}
	}
}

// update scroller and scroll to certain point
function updateScroller(contentID, xScrollTo)
{
	xContent = document.getElementById(contentID).getElementsByTagName('div')[0];
	if (xContent.scrollHeight > xContent.offsetHeight)
	{
		document.getElementById('scrollDiv'+contentID).style.display = '';
		moveScroll(contentID, 'set', xScrollTo);
	}
	// hide scroller from view
	else document.getElementById('scrollDiv'+contentID).style.display = 'none';
}


// draw scrollbars
function drawScroller(contentID, posLeft, posTop, divHeight)
{
	newScroller = '<div style="float: left; height: '+divHeight+'px; position: relative;" id="scrollDiv'+contentID+'">';
	newScroller += '	<table height="100%" cellspacing="0" cellpadding="0" border="0">';
	newScroller += '	<tr><td height="0%" valign="top"><a href="javascript: //"><img src="images/scrollup.gif" width="25" height="12" alt="" border="0" id="scrollUp'+contentID+'" onmousedown="moveScroll(\''+contentID+'\', \'up\', 1);" onmouseup="stopScroll();"></a></td></tr>';
	newScroller += '	<tr><td height="100%" valign="top">';
	newScroller += '		<a href="javascript: //"><img src="images/scrollball.gif" width="25" height="11" border="0" style="position: absolute;" id="scrollBar'+contentID+'" onmousedown="moveScroll(\''+contentID+'\', \'move\', 1);" onmouseup="stopScroll();"></a>';
	newScroller += '		<span style="width: 25px; position: absolute; cursor: pointer;" id="scrollTrack'+contentID+'" onmouseup="stopScroll();" onmousedown="moveScroll(\''+contentID+'\', \'move\', 1);"></span>';
	newScroller += '	</td></tr>';
	newScroller += '	<tr><td height="0%" valign="bottom"><a href="javascript: //"><img src="images/scrolldown.gif" width="25" height="12" alt="" border="0" id="scrollDown'+contentID+'" onmousedown="moveScroll(\''+contentID+'\', \'down\', 1);" onmouseup="stopScroll();"></a></td></tr>';
	newScroller += '</table></div>';
	document.getElementById(contentID).innerHTML += newScroller; // draw new scroller
	// set up track height

	document.getElementById('scrollTrack'+contentID).style.height = divHeight-document.getElementById('scrollUp'+contentID).offsetHeight-document.getElementById('scrollDown'+contentID).offsetHeight;
}

// get mouse position
var mousex = 0; // mouse position x
var mousey = 0; // mouse position y
function getmouseposition(e)
{
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		mousex = e.pageX;
		mousey = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		mousex = e.clientX + document.body.scrollLeft;
		mousey = e.clientY + document.body.scrollTop;
	}
}

// wheel event
function wheel(event)
{
	 var delta = 0;

	 if (!event) event = window.event; // capture internet explorer event
	 if (event.wheelDelta) delta = event.wheelDelta; // ie case
	 else if (event.detail) delta = -event.detail; // firefox and ie deltas differ in sign
	 if (delta) delta = delta<0?-1:+1;
	 if (delta)
	 {
		if (delta > 0) haltScroll = moveScroll(xScrollContent, 'wheelup', 1); // wheel going up
		else haltScroll = moveScroll(xScrollContent, 'wheeldown', 1); // wheel going down
	 }
	 if (haltScroll) return false;
}

function moveScroll(contentID, direction, moving)
{
	if (xScrollContent != contentID) initScroller(contentID);

	xContent = document.getElementById(contentID).getElementsByTagName('div')[0];
	xScrollbar = document.getElementById('scrollBar'+contentID); // scroll bar image
	var xScrollup = document.getElementById('scrollUp'+contentID); // scroll up image
	var xScrolldown = document.getElementById('scrollDown'+contentID); // scroll down image
	var xInsideContent = false; // is mouse inside content div

	// calculate how much to remove from total scrolling area
	scrollArrows = xScrolldown.offsetHeight+xScrollup.offsetHeight+xScrollbar.offsetHeight;
	scrollActual = xContent.scrollHeight-xContent.offsetHeight; // actual scroll positon (without variable scrollbar height)

	// check if mouse is inside div window for using wheel
	/*if (mousex > xScrollContentLeft && mousex < (xScrollContentLeft+xContent.offsetWidth+xScrollbar.offsetWidth)
		&& mousey > xScrollContentTop && mousey < (xScrollContentTop+xContent.offsetHeight)) xInsideContent = true;*/

	if (moving)
	{
		if (xScrolling) clearInterval(xScrolling);
		// scrollup button
		if (direction == 'up')
		{
			xContent.scrollTop = xContent.scrollTop-xScrollSpeed;
			xScrolling = setInterval("moveScroll(xScrollContent, 'up', 1)", 1);
		}
		// on scrolldown button
		else if (direction == 'down')
		{
			xContent.scrollTop = xContent.scrollTop+xScrollSpeed;
			xScrolling = setInterval("moveScroll(xScrollContent, 'down', 1)", 1);
		}
		// mouse down on scrollbar
		else if (direction == 'move')
		{
			xContent.scrollTop = ((mousey-xScrollup.offsetHeight-xScrollContentTop-(xScrollbar.offsetHeight/2))*scrollActual)/(xContent.offsetHeight-scrollArrows);
			xScrolling = setInterval("moveScroll(xScrollContent, 'move', 1)", 1);
		}
		// using mousewheel (not working at the moment, dont know how to find if mouse is over correct div window to scroll)
		/*else if (direction == 'wheelup')
		{
			if (xInsideContent) xContent.scrollTop = xContent.scrollTop-10;
			return true;
		}
		// using mousewheel down
		else if (direction == 'wheeldown')
		{
			if (xInsideContent) xContent.scrollTop = xContent.scrollTop+10;
			return true;
		}*/
		// set position of scroller with code
		else if (direction == 'set')
		{
			xContent.scrollTop = moving;
		}

		forScrollbar = (xContent.scrollTop/scrollActual)*(xContent.offsetHeight-scrollArrows);
		xScrollbar.style.top = xScrollup.offsetTop+xScrollup.offsetHeight+forScrollbar+'px'; // set position of scrollbar image
	}
	else clearInterval(xScrolling);
	return false;
}
// halt scrolling
function stopScroll()
{
	if (xScrollContent) moveScroll(xScrollContent, '', 0);
}