// Stick sidebar to top
function sticky_relocate() {
	var window_top = $(window).scrollTop();
	var div_top = $('#sidebar-anchor').offset().top;
	if (window_top + 5 > div_top) // Extra 5px accounts for the distance to anchor the div from the top of screen
		$('#sidebar').addClass('stick')
	else
		$('#sidebar').removeClass('stick');
}
// Show all of selected ul and hide others
function expanddiv($id) {
	$transitionspeed = 550;
	// Need code to show both selected div (if fully hidden) or its individual li's (if only partially hidden)
	$('#sidebar #'+$id+' ul:hidden').show($transitionspeed);
	$('#sidebar #'+$id+' li:hidden').show($transitionspeed);
	$('#sidebar #'+$id).removeClass('collapsed');
	// Hide the ul's in non-selected divs
	$('#sidebar div.relatedlinks').not('#'+$id).find('ul').hide($transitionspeed);
	$('#sidebar div.relatedlinks').not('#'+$id).addClass('collapsed');
}
// Prep on page load
$(document).ready(function() {
	// if the new forums stylesheet has been chosen (check the headers' padding-left property to find out), run the code to make the sidebar collapsible and stickable
	if ($('#sidecontents h2').css('padding-left') == '3px') { // FIND A BETTER WAY TO CHECK FOR STYLESHEET?
		$letstick = true;
		$letcollapse = false;
		// create the anchor div for the sticky function, right where the sidebar starts
		$('#sidebar').before('<div id="sidebar-anchor"></div>');
		// check if each sidebar div is small enough to fit on page when expanded, allowing space for each div's header
		$remainingheight = $(window).height();
		$('#sidebar h2').each(function(index) {
			$remainingheight = $remainingheight - $(this).height() -15; // Extra 15 is to allow room for "show more links" link for each div
		});
		$('#sidebar ul').each(function(index) {
			if ($(this).height() > $remainingheight) {
				$letstick = false;
			}
		});
		// if enough vertical space to stick divs, get to work on it
		if ($letstick) {
			// add the 'show more links' links (will determine later whether to show them)
			$('#sidebar div.relatedlinks').append('<div class="showmore"><a href="#">Show more</a></div>');
			// assign the expanddiv function to 'show more links' links, assigning them the name of the parent div's id
			$('#sidebar .showmore a').click(function() {
				expanddiv($(this).parent().parent().attr('id'));
				return false;
			});
			// collapse each sidebar div as much as needed for them all to fit on single screen
			$maxlistitems = 5; // max number of links output by php script
			$reversecounter = $maxlistitems;
			while ($('#sidebar').height() + 15 > $(window).height() && $reversecounter > 1) { // extra 15px to allow space for footer.  last condition thrown in to safeguard against infinite loop
				$('#sidebar div.relatedlinks').each(function(index) {
					// element arrays start from 0 so subtract 1 from counter
					$itemtocheck = $reversecounter - 1;
					// if album (pic) link, counter is one less than others because one less total list items allowed
					if ($(this).attr('id') == 'relatedalbums') {
						$itemtocheck = $itemtocheck--;
					}
					// hide the latest item and designate its div as collapsed
					if ($(this).find('li:eq('+$itemtocheck+')').not('.viewall').length) { // can skip final "viewall" links b/c they are hidden by css anyway
						$(this).find('li:eq('+$itemtocheck+')').hide()
						$(this).addClass('collapsed');
					}
				});
				$reversecounter--;
			}
			// apply the stick
			$(window).scroll(sticky_relocate);
			sticky_relocate();
		}
	}
});