/**
 * @author Paul Jones
 */
 
 var sideBarState = 'regScroll';
 var prevScrollPos = 0;

$(document).ready(function($){
	
	var _winHt = $(window).height(); 	// get window height
	var _siteContainerHt = $('#siteContainer').height();	// get html height
	var _sidePanelHt = $('#sidebar').outerHeight(); // get sidebar height
	if(_winHt >= _sidePanelHt)
	{
		//window is bigger than sidebar, the default fixed position is good
		//$(window).scroll(slideSidebar);
		//$(window).resize(slideSidebar);
	}else{
		
		//sidebar is bigger than window, sidebar will need to scroll
		var footFillerSize = _sidePanelHt - _siteContainerHt;
		if(footFillerSize >= 0) {
			//We had to expand the content area just to fit the sidebar, we don't need any complex scrolling
			$('#footerFiller').height(footFillerSize + 10); //Make sure the black site container is at least as big as the sidebar
			$('#sidebar').css({'position':'relative'});
		}else{
			//The content area is larger than the sidebar and the sidebar is bigger than the window, we need complex scrolling
			$('#sidebar').css({'position':'relative'});
			prevScrollPos = $(this).scrollTop();
			$(window).scroll(scrollSidebar);
		}
	}
	
});

function scrollSidebar() {
	//States: regScroll, bottomFixed, topFixed
	var _winHt = $(window).height(); 	// get window height
	var _scrollPos = $(this).scrollTop();	// get scrollpos
	var _sidePanelHt = $('#sidebar').outerHeight(); // get sidebar height
	var _sidePanelTop = parseInt($('#sidebar').css('top')); //Get the "top" position of the scrollbar
	var _sidePosition = $('#sidebar').css('position');
	
	//alert('ScrollPos: '+_scrollPos+'\nsidePanelTop: '+_sidePanelTop+'\nwindowHt: '+_winHt+'\nsidePanelHt: '+_sidePanelHt+'\ncss: '+$('#sidebar').css('position'));
	if(_sidePosition == 'fixed')
	{
		if(sideBarState=='topFixed' && _scrollPos > prevScrollPos)
		{
			//They scrolled down when the menu was fixed at the top
			var topVal = '' + (prevScrollPos) + 'px';
			$('#sidebar').css({'top':topVal});
			$('#sidebar').css({'position':'relative'});
			sideBarState = 'regScroll';
		}
		else if(sideBarState=='bottomFixed' && _scrollPos < prevScrollPos)
		{
			//They scrolled up when the menu was fixed at the bottom
			var topVal = '' + (prevScrollPos - (_sidePanelHt - _winHt)) + 'px';
			$('#sidebar').css({'top':topVal});
			$('#sidebar').css({'position':'relative'});
			sideBarState = 'regScroll';
		}
	}
	else
	{
		//The menu's position is set to relative
		if(_scrollPos < _sidePanelTop && _sidePosition!='fixed')
		{
			//The user scrolled above the menu, set it to fixed if not already
			$('#sidebar').css({'position':'fixed'});
			$('#sidebar').css({'top':'0px'});
			sideBarState = 'topFixed';
		}
		else if((_scrollPos + _winHt) > (_sidePanelTop + _sidePanelHt) && _sidePosition!='fixed')
		{
			//alert('bottomfixed');
			//The user scrolled below the bottom of the menu, set it to fixed if not already set
			var topVal = '-' + (_sidePanelHt - _winHt) + 'px';
			$('#sidebar').css({'position':'fixed'});
			$('#sidebar').css({'top':topVal});
			sideBarState = 'bottomFixed';
		}
	}
	prevScrollPos = _scrollPos;
}

