// JQuery slide scroll by Ben Beckford
// The container's line-height MUST be a factor of the wrapper height else
// half lines could be cut in the middle
function slideScroll(params){
	
	if(params.divID != null)
	{
		var container = "#" + params.divID;
	}
	else
	{
		alert("Error, wrapper ID not specified");
	}
	
	
	var buttonAutoHide = true;
	
	var currentY = 0;	
	var minY = 0;
	var upArrowOn = false;
	var downArrowOn = true;
	var excludeButtonHeight = false;
	var scrollJumpProportion = 1;
	
	var upArrow = null;
	var downArrow = null;
	
	if(params.scrollJumpProportion != null)
	{
		scrollJumpProportion = params.scrollJumpProportion;
	}
	
	if(params.excludeButtonHeight != null)
	{
		excludeButtonHeight = params.excludeButtonHeight;
	}
	
	if(params.upArrow != null)
	{
		upArrow = params.upArrow;
	}
	
	if(params.downArrow != null)
	{
		downArrow = params.downArrow;
	}
	
	if ( $(container + ' .UpBtn').length )
	{
		$(container + ' .UpBtn').remove();
	}
	
	if ( $(container + ' .DownBtn').length )
	{
		$(container + ' .DownBtn').remove();
	}
	
	if($(container + ' .slideScrollMover').length)
	{
		var tempHTML = $(container + ' .slideScrollMover').html();
		$(container + ' .slideScrollMover').remove();
		$(container).append(tempHTML);
	}
	
	var contentHTML = $(container).html();
	
	var beforeHTML = '<div class="slideScrollBtnUp" id="'+ params.divID +'UpBtn"></div>';
	var appendHTML = '';
	appendHTML += '<div class="slideScrollMover">';
	appendHTML += contentHTML;
	appendHTML += '</div>';
	var afterHTML = '<div class="slideScrollBtnDown" id="'+ params.divID +'DownBtn"></div>';
	
	
	if(upArrow == null)
	{
		$(container).before(beforeHTML);
		upArrow = $('#' + params.divID +'UpBtn');
	}
	else
	{
		upArrow = $('#' + upArrow);
	}
	$(container).empty();
	$(container).append(appendHTML);
	if(downArrow == null)
	{
		$(container).after(afterHTML);
		downArrow = $('#' + params.divID +'DownBtn');
	}
	else
	{
		downArrow = $('#' + downArrow);
	}
	
	var buttonHeight = 0;
	var realButtonHeight = upArrow.height();
	
	if(excludeButtonHeight == false)
	{
		buttonHeight = upArrow.height();
	}
	
	var wrapperHeight = $(container).height();
	var contentHeight = wrapperHeight - (buttonHeight * 2);
	$(container).height(contentHeight);
	
	var moverHeight = $(container + ' .slideScrollMover').height();
	
	if(buttonAutoHide)
	{
		upArrow.stop().animate({
					height: 0
					}, 0, "swing");
		$(container).height(contentHeight+buttonHeight);
	}
	
	upArrow.click(function(event){
		event.preventDefault();
		scrollContentUp();
	});
	
	downArrow.click(function(event){
		event.preventDefault();
		scrollContentDown();
	});
	
	var scrollContentDown = function()
	{
		if(moverHeight + currentY > contentHeight * scrollJumpProportion)
		{
			if(currentY - (contentHeight * scrollJumpProportion) < minY)
			{
				currentY -= (contentHeight * scrollJumpProportion);
				
				$(container + " .slideScrollMover").stop().animate({
				marginTop: currentY
				}, 500, "easeOutExpo");
			}
		}
		
		updateArrows();
	}
	
	var scrollContentUp = function()
	{
		if (currentY < 0)
		{
			currentY += (contentHeight * scrollJumpProportion);
			
			$(container + " .slideScrollMover").stop().animate({
				marginTop: currentY
				}, 500, "easeOutExpo");
		}
		
		updateArrows();

	}
	
	var updateArrows = function()
	{
		if(buttonAutoHide)
		{
			if(moverHeight + currentY - 1 < contentHeight)
			{
				downArrow.stop().animate({
					height: 0
					}, 300, "swing");
				downArrowOn = false;
				
			}
			else
			{
				downArrow.stop().animate({
					height: realButtonHeight
					}, 300, "swing");
				downArrowOn = true;
			}
			
			if(currentY < 0)
			{
				upArrow.stop().animate({
					height: realButtonHeight
					}, 300, "swing");
				upArrowOn = true;
				
			}
			else
			{
				upArrow.stop().animate({
					height: 0
					}, 300, "swing");
				upArrowOn = false;
			}
			
			if(upArrowOn == true && downArrowOn == true)
			{
				$(container).stop().animate({
					height: contentHeight
					}, 300, "swing");
			}
			else if(upArrowOn != downArrowOn)
			{
				$(container).stop().animate({
					height: contentHeight+buttonHeight
					}, 300, "swing");
			}
			else
			{
				$(container).stop().animate({
					height: contentHeight+(buttonHeight*2)
					}, 300, "swing");
			}
		}
	}
}
