var name = "#floating_nav";
var menuYloc = null;
  
$(document).ready(function() {
	
	menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")));
	
	$(window).scroll(function () {
		
			navHeight = $(name).height();
			windowHeight = $(window).height();
			
			if(navHeight <= windowHeight) { 
				offset = menuYloc+$(document).scrollTop()+"px";
				$(name).animate({top:offset},{duration:1000,queue:false});
			} else {
				if($(window).scrollTop() + $(window).height() == $(document).height()) {
					offset = menuYloc+($(document).scrollTop() - (navHeight - windowHeight))+"px";
					$(name).animate({top:offset},{duration:1000,queue:false});
				} else {
					offset = menuYloc+$(document).scrollTop()+"px";
					$(name).animate({top:offset},{duration:1000,queue:false});
				}
			}
			
	});

});
