/*!
 * Copyright (c) 2011 Brad Cooper | http://willworkforart.net
 * Licensed under GNU General Public License v3
 * Date: 03/28/2011
 * @author: Brad Cooper
 * @version: 0.2011.6
 *
 * http://willworkforart.net/
 */

(function (w, d) {
	"use strict";
	var wwfa = {
		stickySidebar : function () {
			var theSidebar, theRelated, justAboveRelated, originalSidebarPosition;
			theSidebar = d.getElementById("social");
			theRelated = d.getElementById("related");
	
			if (!theSidebar && !theRelated) { 
				return;
			}
			
			originalSidebarPosition = theSidebar.offsetTop;
			justAboveRelated = (theRelated.offsetTop - theSidebar.offsetHeight);
			
			function adjustTheSidebar() {

				if (w.pageYOffset <= originalSidebarPosition) {
					theSidebar.style.position = "inherit";
				} else if ((w.pageYOffset > originalSidebarPosition) && (w.pageYOffset < justAboveRelated)) {
					theSidebar.style.position = "fixed";
					theSidebar.style.top = "0";
				} else if (w.pageYOffset >= justAboveRelated) {
					theSidebar.style.position = "absolute";
					theSidebar.style.top = justAboveRelated + "px";
				}
			}
			w.addEventListener('scroll', adjustTheSidebar, false);
		},
		relatedLinksHiding : ""
	};
	w.onload = function () {
		wwfa.stickySidebar();
	};
	
})(this, document);
