function SBUp(e) { SB.up(); }
function SBKey(e) { SB.key(e); }
function SBDown(e) { SB.down(); }
function SBDrag(e) { SB.drag(e); }
function SBWheel(e) { SB.wheel(e); }
function SBEndDrag(e) { SB.endDrag(); }
function SBStartDrag(e) { SB.startDrag(e); }
function SBInit() { window.setTimeout("SB.init()", 1000); }

var SB = {

	arrowT : null,
	arrowB : null,
	arrowM : null,
	
	elmScrollBar 		: null,
	elmContentInner 	: null,
	elmContent	 		: null,
	
	cH : 0,
	sH : 0,
	procC : 0,
	procS : 0,
	
	scrollBarTopPos : 0,
	
	tmpMouseY : 0,
	
	init : function()
	{
		SB.arrowT = $('ScrollBarTop');
		SB.arrowB = $('ScrollBarBottom');
		SB.arrowM = $('Scroller');
		
		SB.elmScrollBar 		= $('ScrollBar');
		SB.elmContentWrapper = $('ContentWrapper');
		SB.elmContent 			= $('Content');

		SB.elmContent.style.marginLeft = '25px';
		SB.elmScrollBar.style.display = 'block';
		
		SB.sH = U.getHeight(SB.elmScrollBar);
		SB.cH = U.getHeight(SB.elmContent) - SB.sH;
		
		SB.elmContentWrapper.style.overflow = 'hidden';

		SB.sH -= 50;
		
		SB.procC = SB.cH / 100;
		SB.procS = SB.sH / 100;
		
		SB.set(U.getInt(SB.elmContentWrapper.scrollTop));
		
		SB.scrollBarTopPos = SB.elmScrollBar.offsetTop;
		sbParent = SB.elmScrollBar.offsetParent;
		while(sbParent.tagName.toLowerCase() != 'body')
		{
		   SB.scrollBarTopPos += sbParent.offsetTop;
		   if(sbParent.offsetParent)
		   	sbParent = sbParent.offsetParent;
			else
			   break;
		}
		
		SB.scrollBarTopPos -= SB.elmContentWrapper.scrollTop;
		
		U.setEvent(SB.arrowB, 'onclick', SBDown);
		U.setEvent(SB.arrowT, 'onclick', SBUp);
		U.setEvent(SB.arrowM, 'onmousedown', SBStartDrag);

		if(isNS && !isSafari)
		{
			document.body.addEventListener('DOMMouseScroll', SBWheel, false);
			U.setEvent(document, 'onkeydown', SBKey);
		}
		else
		{
			U.setEvent(document.body, 'onmousewheel', SBWheel);
			U.setEvent(/*SB.elmContentWrapper*/document, 'onkeydown', SBKey);
		}
	},
	
	down : function(inc)
	{
	   var pos = U.getInt(SB.elmContentWrapper.scrollTop)
	   inc = !inc ? 20 : inc;
	   
	   if(pos == SB.cH)
	      return;
	   
	   pos += inc;
	   pos = pos > SB.cH ? SB.cH : pos;
	   SB.set(pos);
	},
	
	up : function(inc)
	{
	   var pos = U.getInt(SB.elmContentWrapper.scrollTop);
	   inc = !inc ? 20 : inc;

	   if(pos == 0)
	      return;

	   pos -= inc;
	   pos = pos < 0 ? 0 : pos;
	   SB.set(pos);
	},
	
	wheel : function(e)
	{
		e = e || window.event;
	
	   var delta = e.wheelDelta ? e.wheelDelta : e.detail;
    	delta = (isNS && !isSafari)  ? -1 * delta : delta;
    	
    	if(delta < 0)
    	   SB.down();
	  	else
	  	   SB.up();
	},
	
	key : function(e)
	{
		e = e || window.event;
		
	   var key = U.getKey(e);
	   
		switch(key)
		{
		   case KEY_UP: 		SB.up(); 			break;
			case KEY_DOWN:    SB.down(); 	 		break;
			case KEY_HOME:    SB.set(0); 	 		break;
			case KEY_END:     SB.set(SB.cH);		break;
			case KEY_PGUP:    SB.up(SB.sH); 	 	break;
			case KEY_PGDOWN:  SB.down(SB.sH);   break;
		}
	},
	
	startDrag : function(e)
	{
	   e = e || window.event;
	   SB.tmpMouseY = e.offsetY || (e.pageY - SB.scrollBarTopPos - U.getInt(SB.arrowM.style.top));
	   
	   U.setEvent(document.body, 'onmouseup', SBEndDrag);
	   U.setEvent(document.body, 'onmousemove', SBDrag);
	},
	
	endDrag : function()
	{
	   U.resetEvent(document.body, 'onmousemove', SBDrag);
	   U.resetEvent(document.body, 'onmouseup', SBEndDrag);
		U.emptySel();
	},
	
	drag : function(e)
	{
		e = e || window.event;

		y = e.y || (e.pageY - SB.scrollBarTopPos);
		y -= isSafari ? SB.scrollBarTopPos : 0;

		if(y < 0 || y > (SB.sH + 50) || e.clientY < (SB.scrollBarTopPos + 20))
 		   return;
		
		y -= SB.tmpMouseY;
		y = Number(y);
		
		if(y < 12 || y - 13 > SB.sH)
		   return;

		var proc = (y - 12) / SB.procS;
		var cPos = Math.round(SB.procC * proc);
		
		SB.arrowM.style.top = y + 'px';
		SB.elmScrollBar.style.top = cPos + 'px';
	   SB.elmContentWrapper.scrollTop = cPos;
	   
		U.emptySel();
	},
	
	set : function(pos)
	{
		var proc = pos / SB.procC;
		
	   var arrPos = Math.round(proc * SB.procS);
	   arrPos += 12;

	   arrPos = arrPos < 12 ? 12 : (arrPos - 15 > SB.sH ? SB.sH + 15 : arrPos);
	   
	   SB.arrowM.style.top = arrPos + 'px';
	   SB.elmScrollBar.style.top = (pos < 0 ? 0 : pos)+ 'px';
	   SB.elmContentWrapper.scrollTop = pos;
	}
}

U.setEvent(window, 'onload', SBInit);
