/**
* @author    Kyle Hengst <kyle@cyberdesignworks.com.au>
*/
(function($)
	{

		$.fn.followScroll = function(options)
			{

				options = jQuery.extend({
					container:'',
					delay:0
				}, options);

				this.each(function()
				{
					var scope = $(this);
					var container = $(options.container);
					var offsetTop = scope.offset().top;
					var offsetLeft = scope.offset().left;
					var within = container.height() - scope.height() - 30;
					scope.width(scope.width());
					
					if(scope.height()>container.height())
						container.css({minHeight:(scope.height()+30)+'px'});
					else
						container.css({minHeight:container.height()+'px'});
					
					function ani()
					{
						
						if(scope.height()+30>=container.height())
							return;
						
						// A bunch of values we need to determine where to animate to
						var viewportHeight = parseInt( $( window ).height() );	
						var pageScroll =  parseInt( $( document ).scrollTop() );
						var margin = pageScroll - offsetTop;				
						
						if(pageScroll>offsetTop)
						{
							if(margin>within)
								scope.css({position:'absolute',left:'0px',top:within+'px'})
							else
								scope.css({position:'fixed',left:offsetLeft+'px',top:'0px'})
						}
						else
							scope.css({position:'relative',left:'0px',top:'0px'})
						
					}
					
					// Animate the box when the page is scrolled
					$( window ).scroll( function ()
						{
							ani();
						}
					);

					// Animate the box when the page is resized
					$( window ).resize( function ()
						{
							
						}
					);

					ani();


				})

			} // close slidingPanels

	})(jQuery);
