
function initAccordionMenu(){
	//on page load, hide the ajax loading image
	$('.ajaxLoader').hide();
	// re-usable accordion code... gets called below.
	//   "link" is a jQuery "a.accordionLink" tag object
	//   "currentTocTree" is a jQuery "div.tocTree" object
	function doAccordionThing(link, currentTocTree, no_slide){
		// hide the existing accordion item
		if(no_slide){
			$('.accordionized').show().removeClass('accordionized');
		}else{
		           //commenting out the next line allows more than one accordion facet to be open at once
			//$('.accordionized').slideUp('normal').removeClass('accordionized');
		}
		// append the new one to the links' parent
		link.parent().append(currentTocTree);
		// make a new variable, referencing the newly appended toc (Safari needs this)
		var newTocTree = $('.tocTree', link.parent());
		// hide it
		newTocTree.hide(); // needed in Safari for smooth transition
		// now slide it down
		if(no_slide){
			newTocTree.show();
		}else{
			newTocTree.slideDown(500);
		}
		
		// add the class so that other accordion menu calls
		// will know that there is a previously activated one (they'll close it up)
		newTocTree.addClass('accordionized');
		
		// handle the sub expand/collapse using hackety magic
		$('a.tocExpandCollapseLink', newTocTree).click(function(){
			// need to pass in fragment_id=toc - otherwise loading takes too long for some collections
			$.get($(this).attr('href') + '&fragment_id=toc', function(data){
				var expanded_content = $(data);
				//var found = $('.toc-line a[href*="toc.id=;"]', expanded_content);
				var subToc = $('.tocTree', expanded_content);
				$('.tocTree', link.parent()).remove();
				link.parent().append(subToc);
				doAccordionThing(link, subToc, true);
			});
			return false;
		});
	}
	
	// find all accordion links - a.accordionLink
	$('a.accordionLink').click(function(){
		// make jQueryified reference
		var link=$(this);
		// grab the div.tocTree from the links' parent
		var tocTree=$('.tocTree', link.parent());
		// if the .tocTree is already an active accordion item... (has a class of "accordionized")
		if( tocTree.hasClass('accordionized') ){
			// slide it up
			tocTree.slideToggle();
			// remove the class
			tocTree.removeClass('accordionized');
			// return false to prevent link from being followed
			return false;
		}
		// here we are... this link does not have an active/accordionized .tocTree
		// shortcut for href value
		var href = link.attr('href');
		
		// if there is a .tocTree in the DOM for this link (but not the active "accordionized" item)
		if( tocTree.length==1 ){
			// load from "cache" - do the accordion thing...
			doAccordionThing(link, tocTree);
		}else{
			// make an ajax request - turn the loading image on...
			$(link).parent().append( $('.ajaxLoader').show() );
			// pass in fragment_id=toc here as extra param.
			// the teiDocFormatter.xsl stylesheet will
			// then only build the tocTree.
			// - reduces the processing for the remote call
			// - reduces the amount of data being returned here
			$.get(href, {'fragment_id':'toc'}, function(data){
				// hide the associated loading image
				$('.ajaxLoader', link.parent()).hide();
				// make a jQuery object out of the loaded .tocTree element
				var newTocTree = $('div.submenu li div.tocTree', data);
				// do the accordion thing
				doAccordionThing(link, newTocTree);
			})
		}
		return false;
	})
	
	
}

