(function($) {
	$.extend($.fn, {
		stickyfloat : function(options, lockBottom) 
		{
			// save this in var $obj
			var $obj 				= this;
			// get top padding of parent element
			var parentPaddingTop = parseInt($obj.parent().css('padding-top'));
			// calculate y start value
			var startY = parseInt($obj.css('top').substr(0,$obj.css('top').indexOf('px')));
			
			// extend given options object
			var opts = jQuery.extend(
			{ 
				startOffset: startOffset, 
				offsetY: parentPaddingTop, 
				duration: 200, 
				lockBottom:true 
			}, 
			options);
			
			// set start offset by options object or by parent offset top
			var startOffset = (typeof opts.startOffset != 'undefined')?
				opts.startOffset : $obj.parent().offset().top;
			
			
			// if scroll element given and has a height
			if(typeof opts.scrollEl != 'undefined' 
				&& opts.scrollEl.outerHeight())
			{
				// set bottom to scroll elements height
				opts.bottom = opts.scrollEl.outerHeight();
				
				// add resize event to scroll element!
				opts.scrollEl.resize(function()
				{
					// update bottom to current scroll elements height
					opts.bottom = opts.scrollEl.outerHeight();
				});
			}
			
			// on window scroll event
			$(window).scroll(function () 
			{
				// if datepicker div is visible
				if(jQuery('#ui-datepicker-div').length > 0 
					&& jQuery('#ui-datepicker-div').css('display') != 'none')
				{
					// dont scroll!
					return;
				}
				
				// resize event is not working with ie so refresh bottom on
				// each scroll event. this is not really performant, but it works!
				if(jQuery.browser.msie != 'undefined' && jQuery.browser.msie)
				{
					// update bottom to current scroll elements height
					opts.bottom = opts.scrollEl.outerHeight();
				}
				
				// if bottom position given
				if(typeof opts.bottom != 'undefined' 
					&& ! isNaN(parseInt(opts.bottom)))
				{
					// if height of page smaller or equal to obj height
					if(parseInt(opts.bottom) <= $obj.height())
					{
						// dont scroll
						return;
					}
					
					// calculate new bottom position!
					var bottomPos = parseInt(opts.bottom) 
						- $obj.height() + parentPaddingTop;
					
					// reset bottom pos to 0 if smaller than 0
					if( bottomPos < 0 ) bottomPos = 0;
				}
				
				// stop all curent animations
				$obj.stop();
				
				// check if the window was scrolled down more than 
				// the start offset declared
				var pastStartOffset = $(document).scrollTop() > opts.startOffset;
				
				// check if the object is at it's top position (starting point)
				var objFartherThanTopPos = $obj.offset().top > startOffset;	
	
				// if window was scrolled up or down
				if(pastStartOffset || objFartherThanTopPos)
				{
					// calculate new position
					var newpos = (jQuery(document).scrollTop() 
								- startOffset + opts.offsetY);
					
					// if top position smaller than start offset
					if(jQuery(document).scrollTop() 
						< opts.startOffset )
					{
						// reset position
						newpos = parentPaddingTop;
					}
					
					// if new position is smaller than start y position
					if(newpos < startY )
					{
						// reset position to start y position
						newpos = startY;
					}
					
					// if new position is greater than bottom position
					if (newpos > bottomPos)
					{
						// set position to bottom position
						newpos = bottomPos;
					}
					
					// animate object to new position
					$obj.animate({ top: newpos}, opts.duration);
				}
			});
		}
	});
})(jQuery);
