	(function( pb, $ )
	{
		pb.scrollBar = function ()
		{
			this.scrollBarObject = null;
			this.paneWidth = 0;	
			this.barWidth = 0;
			this.percentInView = 0;
			this.percentOutView = 0;
			this.startPosition = 0;
			this.dragging = false;
			this.scrollTimer = 0;
			this.scrollSpeed = 3;
			this.scrollMoveSpeed = 0;
			this.popupWindow = null;
		}
		
		pb.scrollBar.prototype = 
		{
				register : function( scrollBarObject, paneObject )
				{			
					if(typeof( scrollBarObject ) != "object")
						alert("ScrollBarObject is not a object!")
					else if(typeof( paneObject ) != "object")
						alert("PaneObject is not a object!")
					else
					{
						this.scrollBarObject = scrollBarObject;
						this.paneObject = paneObject;
						
						this.setPaneToPoint(0);
						this.setHandlerEvents();
						this.setButtonEvents();
					}
												
					delete scrollBarObject;
				}
				
			,	checkOfObjectIsSet : function()
				{
					if(!this.scrollBarObject || !this.scrollBarObject.get(0))
					{
						alert("There is no scrollbar registered!")
						return false;
					}
					else if(!this.paneObject || !this.paneObject.get(0))
					{
						alert("There is no pane registered!")
						return false;
					}
					
					return true;
				}
				
			,	update : function()
				{
					if(!this.checkOfObjectIsSet()) return;	
					this.paneWidth = this.paneObject.get(0).offsetWidth;
					this.paneInnerWidth = this.paneObject.get(0).scrollWidth;
					
					this.barWidth = this.scrollBarObject.find(".bar").get(0).offsetWidth;
					this.percentInView = this.paneWidth / this.paneInnerWidth;
					this.percentOutView = ( 1-this.percentInView );
					this.scrollMoveSpeed = (((this.barWidth*this.percentOutView) / 100)*this.scrollSpeed);
			
					this.setHandlerSize();
					this.setHandlerPosition();
				}
				
			,	setPopup : function( popupWindow )
				{
					if(typeof( popupWindow ) != "object")
					{
						alert("PopupWindow is not an object");
						return;
					}
					
					this.popupWindow = popupWindow;
					
					delete popupWindow;
					
					return;
				}
				
			,	setScrollSpeed : function( speed )
				{
					if(typeof( speed ) != "number")
						alert("Speed is not a number!")
					else
						this.scrollSpeed = speed;
					
					delete speed;
				}
				
			,	setHandlerSize : function()
				{
					//alert(( this.barWidth*this.percentInView ))
					
					this.scrollBarObject.find(".handler").css( { width:( this.barWidth*this.percentInView ) } );
				}
				
			,	setHandlerPosition : function()
				{				
					this.scrollBarObject.find(".handler").css({ left:((this.paneObject.get(0).scrollLeft / this.paneInnerWidth) * this.barWidth) });
				}
			
			,	setPaneToPoint : function( position )
				{
					var position = eval(position);
		
					this.paneObject.get(0).scrollLeft = position; //this.paneObject.get(0).scrollWidth;
				}
				
			,	setHandlerEvents : function()
				{
					if(!this.scrollBarObject.find(".handler").get(0)) return;
					
					var	object = this;
					var	handler = this.scrollBarObject.find(".handler");
					var	bar = object.getPosition(this.scrollBarObject.find(".bar").get(0));
							
						handler.unbind("mousedown").bind("mousedown", function( event )
						{
							if(object.popupWindow)
								object.popupWindow.hidePopup();			
							
							object.startPosition = (event.pageX-object.getPosition(handler.get(0)).x);
							object.dragging = true;
							
							$(document).bind("mouseup", function( event )
							{
								$(this).unbind("mouseup");
								object.dragging = false;
								
								return false;
							});
							
							return false;
						});
						
						$(document).unbind("mousemove").bind("mousemove", function( event )
						{
							if(!object.dragging)
								return;
					
							var currentPos = ((event.pageX)-(bar.x + object.startPosition)) ;
							
							if(currentPos < 0)
							{
								handler.css( { left:0  } );
								object.paneObject.get(0).scrollLeft = 0;
							}
							else if(currentPos > (object.barWidth*object.percentOutView) )
							{
								object.paneObject.get(0).scrollLeft = ((object.paneInnerWidth*object.percentOutView));
								handler.css( { left:(object.barWidth*object.percentOutView)  } );
							}
							else
							{
								object.paneObject.get(0).scrollLeft = ((currentPos / object.barWidth) * object.paneInnerWidth);
								handler.css( { left:currentPos  } );
							}
							
							return false;
						});
					
					delete object, handler, bar;
				}
				
			,	setButtonEvents : function()
				{
					if(!this.scrollBarObject.find(".scrollLeft").get(0) || !this.scrollBarObject.find(".scrollRight").get(0)) return;
					
					var	object = this;
					var	handler = object.scrollBarObject.find(".handler");
					var	bar = object.getPosition(this.scrollBarObject.find(".bar").get(0));
		
					this.scrollBarObject.find(".scrollLeft").unbind("mousedown").bind("mousedown", function( event )
					{
						if(object.popupWindow)
							object.popupWindow.hidePopup();		
					
						$(this).addClass("scrollLeftDown");
					
						object.scrolling = setInterval( function()
						{
							var	currentPos = ((object.getPosition(handler.get(0)).x-bar.x) - object.scrollMoveSpeed)
					
							if(currentPos < 0)
							{
								currentPos = 0;
								clearInterval(object.scrolling);
							}
							
							object.paneObject.get(0).scrollLeft = ((currentPos / object.barWidth) * object.paneInnerWidth);
							handler.css( { left:currentPos } );
							
							delete currentPos;	
						}, 10);
						
						object.addMouseUp();
						
						return false;
					});
					
					this.scrollBarObject.find(".scrollRight").unbind("mousedown").bind("mousedown", function( event )
					{
						if(object.popupWindow)
							object.popupWindow.hidePopup();		
								
						$(this).addClass("scrollRightDown");
					
						var msieBugFix = ($.browser.msie) ? 2 : 0;
					
						object.scrolling = setInterval( function()
						{
							var maxPos = (object.barWidth*object.percentOutView);						
							var	currentPos = ((object.getPosition(handler.get(0)).x-(bar.x-msieBugFix)) + object.scrollMoveSpeed)
					
							if(currentPos > maxPos)
							{
								currentPos = maxPos;
								clearInterval(object.scrolling);
							}
							
							object.paneObject.get(0).scrollLeft = ((currentPos / object.barWidth) * object.paneInnerWidth);
							handler.css( { left:currentPos } );
							
							delete maxPos, currentPos;
							
						}, 10);
	
						delete msieBugFix;
						
						object.addMouseUp();
	
						return false;
					});
					
					handler.bind("click", function(){ return false; } );
					this.scrollBarObject.find(".scrollRight").unbind("click").bind("click", function(){ return false; } );
					this.scrollBarObject.find(".scrollLeft").unbind("click").bind("click", function(){ return false; } );
					
					delete object, handler, bar;
				}
				
			,	addMouseUp : function()
				{
					var thisObject = this;
					
					$(document).bind("mouseup", function( event )
					{
						$(this).find(".scrollLeftDown").removeClass("scrollLeftDown");
						$(this).find(".scrollRightDown").removeClass("scrollRightDown");
					
						if(thisObject.scrolling)
							clearInterval(thisObject.scrolling);
						
						$(this).unbind("mouseup");
						
						return false;
					});
					
					delete thisObject;
				}
				
			,	getPosition : function ( obj )
				{
					var 	x=0, y=0;
					var 	elm = obj;
					
					while(elm)
					{
						x += elm.offsetLeft + ( !$.browser.opera ? ( parseInt($.css(elm, "borderLeftWidth") ) || 0) : 0);
						y += elm.offsetTop + ( !$.browser.opera ? ( parseInt($.css(elm, "borderTopWidth") ) || 0) : 0);
				
						elm = elm.offsetParent;			
					}
			
					return { x:x, y:y }
				}
		}
})( Pluxbox, jQuery );