/**
* Modify block level elements to have custom scrolling behaviour
* @param	ID					string	The ID attribute of the sarea we want to scroll
* @param	w					int		Width of scrolling area
* @param	h					int		Height of scrolling area
* @param	scrollBar			bool	(Optional) (Default: false) Use a scrollbar
* @param	scrollerBarHeight	int		(Optional) (Default: h) 	Height of scrollbar
* @notes	Where variables are used instead of class properties (ie: var propertyName instead of
*	 		this.propertyName)
*			It's because the property needs to be used withing a jQuery object and therefore
*			the "this" keyword looses context.
*/
function PickledEggScroller(ID,w,h,scrollBar,scrollerBarHeight) {		

	// Config
	var scrollerOffsetX	= 13;
	var scrollerOffsetY	= 30;

	// Construct
	this.holder 			= $("#"+ID);		


	this.usescrollbar		= scrollBar == undefined || scrollBar == false ? false : true;
	this.scrollerBarHeight	= scrollerBarHeight == undefined ? h : scrollerBarHeight;	
	this.scrollerHeight		= h;

	// Add CSS and HTML enhancements					
	this.holder.css({height:h+"px",width:w+"px"});		
	$(this.holder).html('<div class="mask"><div class="txt">'+$(this.holder).html()+'</div></div>');
	$(".mask",this.holder).css({height:h+"px",width:w+"px",overflow:"hidden"});	
				
	// Dimensional information
	var contentWrapper	= $(".txt", this.holder);	
	this.contentHeight 	= contentWrapper.height();	
	this.maxScroll		= parseInt(-this.contentHeight+h);
	
	// Do scrollbar?
	if(this.usescrollbar && this.contentHeight > h) {
		
		var mouseDown = false;
		
		this.holder.prepend('<div class="scrollBarHolder"><a class="scrollButton"><span><img src="/site_media/images/s.gif" /></span></a></div>');
		
		this.scrollBarHolder = $(".scrollBarHolder",this.holder);
		this.scrollBarHolder.css({position:"absolute",top:scrollerOffsetY+"px",left:(w+scrollerOffsetX)+"px",height:this.scrollerBarHeight+'px'});
		
		var scrollButton 		= $(".scrollButton",this.scrollBarHolder);
		var scrollButtonHeight 	= Math.round((this.scrollerHeight/this.contentHeight)*this.scrollerBarHeight);
		scrollButton.css({height:scrollButtonHeight+"px"})
		
		var offsetY = this.scrollBarHolder.offset().top;
 
		var lowerBound = this.scrollerBarHeight - scrollButtonHeight;
		var sRatio = (this.contentHeight-this.scrollerHeight) / lowerBound;		
		var attachPoint;
		
		// Scroll Bar Events
		scrollButton.mousedown(function(e) {	
			mouseDown 	= true;		
			attachPoint = e.clientY - $(this).offset().top;
     
			$("body").css("cursor","pointer");	
		});
		$().mouseup(function() {		
			mouseDown = false;		
			$("body").css("cursor","default")				
		});
		$().mousemove(function(e) {		
			y = e.clientY - offsetY - attachPoint;
			
			if(mouseDown){	
				if(y<0) {					
					scrollButton.css("top","0px");
					contentY = 0;
				} else if(y > lowerBound) {
 $("#testing").html('Y is set to ' + y);
					scrollButton.css("top",lowerBound+"px");
					contentY = lowerBound*sRatio;
				} else {
					scrollButton.css("top",y+"px")
$("#testing").html(y);
					contentY = y*sRatio;
				}				
				contentWrapper.css("top",-Math.round(contentY)+"px");	
				if (document.selection) {
			    	document.selection.empty();
			    } else {
			        window.getSelection().removeAllRanges();
			    }					
			}	
		})			
	}

	// Button Scrolling 
	this.Scroll = Scroll;
	function Scroll(direction) {
		this.curY 		= parseInt(contentWrapper.css("top").split("px")[0]);
		this.nextY 		= direction == "up" ? this.curY+this.scrollerHeight : this.curY-this.scrollerHeight;							
		if (this.nextY > 0) { 
			this.nextY = 0;	
		}
			
		if (this.nextY < this.maxScroll) { 
			this.nextY = this.maxScroll;	
		}
		
		if(this.usescrollbar && this.contentHeight > this.scrollerBarHeight) {					
			scrollButton.animate({top:((this.scrollerBarHeight / this.contentHeight)* -this.nextY)+"px"})
		}
					
		if(this.nextY != this.currY) {
			contentWrapper.animate({top: this.nextY + "px"});	
		}			
	}
	
	/**	
	 * Draw a button to the screen
	 */
	this.AddButton = function(instanceName,direction,src,id) {
		if(this.contentHeight > h){
			document.write('<img id="'+id+'" src="'+src+'" alt="'+direction+'" onclick="'+instanceName+'.Scroll(\''+direction+'\');" />')
		}
	}
	
	
}