$(function() {
	var floater   = $("#leftNav"),
		container = floater.parent(),
		scroller  = null;
	
	$(window).scroll(scroller = (function() {
		var threshold = floater.offset().top,
		orgPos    = floater.css("position") || "static",
		orgTop    = parseInt(floater.css("top")) || 0;
		
		// Browsers that don't have position:fixed 
		if ( !window.XMLHttpRequest ) {
			threshold = parseInt(floater.css("top")) || threshold;
			var difference = 336 - threshold;
				
			return function() {
	    		if ( window.pageYOffset - difference > threshold ) {
	    			floater.css({
			        	position: "absolute",
			        	top:      Math.min(
		        			orgTop + container.height() - floater.height() - 5, 
		        			Math.max(window.pageYOffset - difference, threshold)
			        	) + "px"
			        });
	    		} else {
			    	floater.css({
			        	position: orgPos,
			        	top:      orgTop + "px"
			        });
	    		}
			};
		} 
		
		// Modern browsers with position:fixed
		return function() {
		    if ( window.pageYOffset >= threshold ) {
		    	// Floater has descended past its parent container's bottom
		    	if ( window.pageYOffset + floater.height() > threshold + container.height() ) {
		    		floater.css({
			        	position: "absolute",
//				        top:      (orgTop + container.height() - floater.height() + threshold - 35) + "px",
			        	top:      "auto",
			        	bottom:   0
			        });
		    	} else {
			    	floater.css({
			        	position: "fixed",
			        	top:      0
			        });
		    	}
		    } else {
		    	floater.css({
		        	position: orgPos,
		        	top:      orgTop + "px"
		        });
		    }
		};
	})());
	
	scroller();
});