var floatingBox = ".sideBar";
var containBox = "#contentBlock";
var marginY = 15;
var offsetY;

$(document).ready(function(){
	var startPosY =  $(floatingBox).position().top - marginY;
	var startOffsetY = $(floatingBox).offset().top;
	
	$(window).scroll(function(){ 
		offsetY = Math.min( Math.max( startPosY, $(document).scrollTop() - startOffsetY ), $(containBox).height() - $(floatingBox).height() );
		if( offsetY < 1 ) offsetY -= marginY;
		$(floatingBox).stop().animate({top:offsetY+marginY},{duration:400,queue:false});
	});
});
