// Sticky Plugin
// =============
// Author: Anthony Garand
// Date: 2/14/2011
// Description: Makes an element on the page stick on the screen

(function($){
	$.fn.sticky = function(options) {
		var defaults = {
			topSpacing: 0,
			center: false,
			placeHolder: false
		};
		var options = $.extend(defaults, options);
		return this.each(function() {
			$(this).css("overflow", "visible");
		
			var topPadding = options.topSpacing,
			stickyElement = $(this),
			stickyElementHeight = stickyElement.innerHeight(),
			stickyElementWidth = stickyElement.outerWidth(),
			elementPosition = stickyElement.offset().top - $(window).scrollTop(),
			regPosition = stickyElement.position().top,
			stickyId = stickyElement.attr("id"),
			shadowElement = $(document.createElement('div'));
			
			if (options.placeHolder)	{
				if (($("#" + stickyElement.attr("id") + "_placeholder").length == 0)) { 
					stickyElement.before($("<div>").attr("id", stickyElement.attr("id") + "_placeholder").css("height", stickyElementHeight).hide()); 
				}
			}

			$(window).scroll(function(){
				var currentScrollTop = $(window).scrollTop();
				elementPosition = stickyElement.position().top - currentScrollTop;
				if (elementPosition <= topPadding) {
					stickyElement.css({"position": "fixed", "top": topPadding});
					$("#subnav_placeholder").show();
				}
				
				if (currentScrollTop <= regPosition - topPadding) {
					stickyElement.css({"position": "static"});
					$("#subnav_placeholder").hide();
				}
			});
		});
	};
})(jQuery);
