 
var name = "#sidebar";
var menuYloc = null;
var menuHeight = null;
var footerHeight = 75;
var headerHeight = 239;
var minPosition = null;

$(document).ready(function(){ 
	menuHeight = $(name).outerHeight();
	menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
	minPosition = $(document).height() - menuHeight - headerHeight - footerHeight
	$(window).scroll(function () {
		var offset = ((menuYloc - 239 + $(document).scrollTop()) < 0) ? 0 : (menuYloc - headerHeight + $(document).scrollTop()) + "px";
		if(($(document).height() - $(document).scrollTop() - footerHeight) < menuHeight) offset = minPosition;
		//console.log($(document).height() - $(document).scrollTop() - footerHeight + " - " + menuHeight + " = " + minPosition);
		$(name).animate({top:offset},{duration:500,queue:false});
	});
}); 
