$.fn.extend({
	fixed : function(container) {
		return this.each(function() {
			var startup_position = $(this).position().top;
			var el = this;
			
			/*
			var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();  
			if ($(this).height() > viewportHeight) return false; // workaround
			*/
			if ($(this).height() > $(container).height()) {
				return false;
			}
			
			$(this).css('position', 'absolute');
			
			getViewportScrollY = function() {
				var scrollY = 0;
				if( document.documentElement && document.documentElement.scrollTop ) {
				scrollY = document.documentElement.scrollTop;
				}
				else if( document.body && document.body.scrollTop ) {
				scrollY = document.body.scrollTop;
				}
				else if( window.pageYOffset ) {
				scrollY = window.pageYOffset;
				}
				else if( window.scrollY ) {
				scrollY = window.scrollY;
				}
				return scrollY;
			}
			
			function check() {
				scrollY = getViewportScrollY();
				var top = 0;
				if (scrollY > startup_position) {
					top = scrollY;
				} else {
					top = startup_position;
				}
				
				if (top + $(el).height() > $(container).position().top + $(container).height()) {
					top = $(container).position().top + $(container).height() - start_position;
				}
				
					
				$(el).css('top', top);
			}
			
			$(window).scroll(check);
			$.ready(check);
		});
	}
});