var nScrollBy;
var nScrollDelay;
nScrollDelay = 25;
var nScrollableClientHeight; //visibled/clipped content height
var nScrollHeight; //content height
var nScrollbarHeight , nScrollbarTmbHeight;
var nStopScrolling;
var nY0;
var nDrag;
nDrag = false;

nStopScrolling = false;

function scrollContentBy(nScrollContentBy)
{
	nStopScrolling = true;
	startScrolling(nScrollContentBy);
}
function scrollContentLineUp()
{
	nStopScrolling = false;
	startScrolling(14);
}
function scrollContentLineDown()
{
	nStopScrolling = false;
	startScrolling(-14);
}
function scrollContentPageUp(nPageHeight)
{
    nStopScrolling = true;
    startScrolling(-nPageHeight);
}
function scrollContentPageDown(nPageHeight)
{
    nStopScrolling = true;
    startScrolling(nPageHeight);
}
function scrollContent()
{ 
	var oContentContainer, oScrollbarThumb;
	var nTop, nHeight;
	var dy = nScrollBy;
	oContentContainer = document.getElementById("contentContainer");
	
	nTop = parseInt(oContentContainer.style.top);
	if(isNaN(nTop))
	    nTop = 0;
	nTop += dy;
	if(nTop<0)
	{
		if(Math.abs(nTop) + nScrollableClientHeight > nScrollHeight )
		{
			nTop = nScrollableClientHeight - nScrollHeight;
			stopScrolling();	
		}
	}
	else
		if(nTop>0)
		{
			nTop = 0;
			stopScrolling();
		}
	
	oContentContainer.style.top = (nTop)+"px";
	
	setThumbPos(Math.ceil(Math.abs(nTop * nScrollbarHeight / nScrollHeight)));
	
	if(nScrollBy!=0 && !nStopScrolling)
		setTimeout("scrollContent()", nScrollDelay);
	
}
function startScrolling(dy)
{
	nScrollBy = dy;
	setTimeout("scrollContent()", nScrollDelay);
}
function stopScrolling()
{
	nScrollBy = 0;
}
function initScrollbars()
{
	var oContentContainer;
	var oScrollbar, oScrollbarArrowDown, oScrollbarThumb;
	
	oContentContainer = document.getElementById("scrollableContentContainer");
	if(oContentContainer)
	{
		nScrollableClientHeight = parseInt(oContentContainer.clientHeight);
		nScrollHeight = oContentContainer.scrollHeight;
		if(nScrollHeight>nScrollableClientHeight)
		{
			oScrollbar = document.getElementById("v_scrollbar");
			oScrollbar.style.top = oContentContainer.style.top;

			oScrollbar.style.left = (parseInt(oContentContainer.style.left)+
				parseInt(oContentContainer.style.width)+20)+"px";

			oScrollbar.style.display = "block";
			nScrollbarHeight = nScrollableClientHeight-24;
			oScrollbar.style.height = nScrollbarHeight+"px";
			oScrollbar.style.top = (parseInt(oContentContainer.style.top) + 14 ) +"px";

			//scrollbar events
			oScrollbar.onclick = onClickScrollbar;
			oScrollbar.onmousedown = onMouseDownScrollbar;
			oScrollbar.onmouseup = onMouseUpScrollbar;
			
			oScrollbarArrowDown = document.getElementById("scrollbarArrowDown");
			oScrollbarArrowDown.style.top = (nScrollableClientHeight-22)+"px";
			oScrollbarArrowDown.onmousedown = onMouseDownArrowDown;
			oScrollbarArrowDown.onmouseup = onMouseUpArrowDown;
			oScrollbarArrowDown.onclick = onClickArrowDown;
			
			var oScrollbarArrowUp = document.getElementById("scrollbarArrowUp");
			
			oScrollbarArrowUp.style.top = "-14px";
			oScrollbarArrowUp.onmousedown = onMouseDownArrowUp;
			oScrollbarArrowUp.onmouseup = onMouseUpArrowUp;
			oScrollbarArrowUp.onclick = onClickArrowUp;
			
			oScrollbarThumb = document.getElementById("scrollbarThumb");
			oScrollbarThumb.style.top = "0px";
			nScrollbarTmbHeight = nScrollableClientHeight * nScrollbarHeight / nScrollHeight;
			oScrollbarThumb.style.height = nScrollbarTmbHeight + "px";
			//thumb events
			oScrollbarThumb.onmousedown = onMouseDownScrollbarThmb;
			oScrollbarThumb.onmouseup = onMouseUpScrollbarThmb;
			oScrollbarThumb.onmousemove = onMouseMoveScrollbarThmb;
			oScrollbarThumb.onclick = onClickScrollbarThmb;
		}
	}
}
function setThumbPos(nPos)
{
	oScrollbarThumb = document.getElementById("scrollbarThumb");
	oScrollbarThumb.style.top = nPos + "px"
}
function onClickScrollbar(e)
{
    var oScrollbarThumb = document.getElementById("scrollbarThumb");
    var nY, nT, nB, nH;
    //alert(oScrollbarThumb.style.top);
    nT = parseInt(oScrollbarThumb.style.top);
    nB = parseInt(oScrollbarThumb.style.top)+parseInt(oScrollbarThumb.style.height);
    nH = parseInt(oScrollbarThumb.style.height);
    
    nY = getMousePosY(e);
    //alert(nY+">"+nB);
    if(nY>nB)
	scrollContentPageUp(nScrollableClientHeight);
    else
	if(nY<nT)
	    scrollContentPageDown(nScrollableClientHeight);
    stopEventPropagation(e);
    return false;
}
//arrows ev. handlers
function onClickArrowUp(e)
{
    stopEventPropagation(e);
	return false;
}
function onMouseDownArrowUp(e)
{
    scrollContentLineUp(); 
    stopEventPropagation(e);
	return false;
}
function onMouseUpArrowUp(e)
{
    stopScrolling(); 
    stopEventPropagation(e);
	return false;
}
function onClickArrowDown(e)
{
    stopEventPropagation(e);
	return false;
}
function onMouseDownArrowDown(e)
{
    scrollContentLineDown(); 
    stopEventPropagation(e);
	return false;
}
function onMouseUpArrowDown(e)
{
    stopScrolling(); 
    stopEventPropagation(e);
	return false;
}
//scrollbar ev. handlers
function onMouseDownScrollbar(e)
{
    getMousePosY(e);
    stopEventPropagation(e);
	return false;
}
function onMouseUpScrollbar(e)
{
    getMousePosY(e);
	return false;
}
//scrollbar thmb. ev. handlers
function onMouseDownScrollbarThmb(e)
{
    var oScrollbarThumb = document.getElementById("scrollbarThumb");
    nStopScrolling = true;

    if(oScrollbarThumb.setCapture)
		oScrollbarThumb.setCapture();
    else
		if(document.addEventListener)
		{
		    document.addEventListener("mousemove", onMouseMoveScrollbarThmb, false);
		    document.addEventListener("mouseup", onMouseUpScrollbarThmb, false);		
		}

    nY0 = getMousePosY(e, true);
    nDrag = true;
    stopEventPropagation(e);
	return false;
}
function onMouseUpScrollbarThmb(e)
{
    //nScrollDelay = 25;
    var oScrollbarThumb = document.getElementById("scrollbarThumb");
    nStopScrolling = true;

    if(oScrollbarThumb.releaseCapture)
		oScrollbarThumb.releaseCapture();
    else
		if(document.removeEventListener)
		{
		    document.removeEventListener("mousemove", onMouseMoveScrollbarThmb, false);
		    document.removeEventListener("mouseup", onMouseUpScrollbarThmb, false);		
		}

    //getMousePosY(e);
    nDrag = false;
    stopScrolling();
    stopEventPropagation(e);
	return false;
}
function onMouseMoveScrollbarThmb(e)
{
    if(!nDrag)
	return;
    var nY = getMousePosY(e, true);
    
    var dy = ((nY0-nY)/nScrollableClientHeight)*nScrollHeight; 
    
    nScrollBy = dy;//> 0 ? 15 : -15;//dy;
    //dy = 10;
    scrollContent();
    nY0 = nY;
    stopEventPropagation(e);

	return false;
}
function onClickScrollbarThmb(e)
{
    stopEventPropagation(e);
	return false;
}
function getMousePosY(e, bAbsoulte)
{
    var nX;
    var oScrollbar = document.getElementById("v_scrollbar");
    
    if(bAbsoulte)
    {
	nY = window.event ? window.event.clientY + document.body.scrollTop : e.pageY;
    
	oScrollbar = document.getElementById("v_scrollbar");
    
	nY -= parseInt(oScrollbar.style.top);
    }
    else
	nY = window.event ? window.event.offsetY : e.layerY;
    
    return nY;
}
function stopEventPropagation(e)
{
    if(window.event)
        window.event.cancelBubble = true;
    else
        if(e.stopPropagation)
            e.stopPropagation();
	if(e)
		if(e.preventDefault)
			e.preventDefault();
}