(function(pb, $)
{
	pb.shoutBox = function()
	{
		this.scrollObject = null;
		this.moveTimer = 0;
		this.overAllMoveTimer = false;
		this.moving = false;
		this.moveCounter = 1;
		this.itemHeight = 55;
		this.itemsInView = 3
		this.moveSpeed = 10;
		this.totalItems = 9
	}

	pb.shoutBox.prototype = 
	{
			setScrollObject : function( scrollObject )
			{
				if( typeof(scrollObject) != "object")
				{
					alert("ScrollObject is not an Object");
					return;
				}
				
				this.scrollObject = scrollObject.get(0);
			}
			
		,	setToTop : function()
			{
				this.scrollObject.scrollTop = (this.itemsInView*this.itemHeight);
			}
			
		,	setEvents : function(upObject, downObject)
			{
				var thisObject = this;
				
				if( typeof(upObject) != "object")
				{
					alert("UpObject is not an Object");
					return;
				}
				else if( typeof(downObject) != "object")
				{
					alert("DownObject is not an Object");
					return;
				}
				
				upObject.bind("mousedown", function(){ $(this).addClass("moveup-down"); return false; });
				upObject.bind("mouseup", function(){ $(this).removeClass("moveup-down"); return false; });
				upObject.bind("click", function()
				{
					if(thisObject.moving)
						return false;
						
					if(thisObject.moveTimer)
						clearInterval(thisObject.moveTimer);
						
					if(thisObject.moveCounter > 0)
						thisObject.moveCounter--;
						
					thisObject.moving = true;
					thisObject.animateMove(-1);
					thisObject.moveTimer = setInterval( function()
					{
						thisObject.animateMove(-1);
					}, thisObject.moveSpeed);
					
					return false;	
				});
			
				downObject.bind("mousedown", function(){ $(this).addClass("movedown-down"); return false; });
				downObject.bind("mouseup", function(){ $(this).removeClass("movedown-down"); return false; });
				downObject.bind("click", function()
				{
					if(thisObject.moving)
						return false;
						
					if(thisObject.moveTimer)
						clearInterval(thisObject.moveTimer);
						
					thisObject.moveCounter++;
					thisObject.moving = true;
					thisObject.animateMove(1);
					thisObject.moveTimer = setInterval( function()
					{
						thisObject.animateMove(1);
					}, thisObject.moveSpeed);
					
					return false;
				});
				
				delete thisObject;
			}
			
		,	animateMove : function( scrollSpeed )
			{
				var	thisObject = this;
				
				if(this.scrollObject.scrollTop < (this.moveCounter*(this.itemHeight*this.itemsInView)) && scrollSpeed > 0)
				{
					this.scrollObject.scrollTop += scrollSpeed;	
				}
				else if(this.scrollObject.scrollTop > (this.moveCounter*(this.itemHeight*this.itemsInView)) && scrollSpeed < 0)
				{
					this.scrollObject.scrollTop += scrollSpeed;	
				}
				else
				{
					if(this.scrollObject.scrollTop === (this.scrollObject.scrollHeight-(this.itemHeight*this.itemsInView)))
					{
						this.moveCounter = 1;
						this.scrollObject.scrollTop = (this.itemsInView*this.itemHeight);
					}
					if(this.scrollObject.scrollTop === 0)
					{
						this.moveCounter = (this.totalItems/this.itemsInView);
						this.scrollObject.scrollTop = (this.scrollObject.scrollHeight - (2*(this.itemsInView*this.itemHeight)));
					} 
					
					this.moving = false;
					
					clearInterval(this.moveTimer);
					
					if(this.overAllMoveTimer)
						this.moveTimer = setTimeout( function(){ thisObject.autoScroll( thisObject ); }, 5000);
					
					delete thisObject;
				}
			}
				
		,	autoScroll : function( thisObject )
			{	
				var	thisObject = thisObject || this;
			
				if(thisObject.moveTimer)
					clearInterval(thisObject.moveTimer);
			
				thisObject.moveCounter++;
				thisObject.moving = true;
				thisObject.animateMove(1);
				thisObject.moveTimer = setInterval( function()
				{
					thisObject.animateMove(1);
				}, thisObject.moveSpeed);
			
				delete thisObject;
			}
			
		,	setAnimateMove : function( thisObject )
			{
				var	thisObject = thisObject || this;
					thisObject.overAllMoveTimer = true;
					thisObject.moveTimer = setTimeout( function(){ thisObject.autoScroll( thisObject ); }, 5000);
			}
			
		,	clearAnimateMove : function( thisObject )
			{
				var	thisObject = thisObject || this;
					thisObject.overAllMoveTimer = false;

				if(thisObject.moveTimer && !thisObject.moving)
					clearInterval(thisObject.moveTimer);
			}
	}
	
})(Pluxbox, jQuery);
