$(document).ready(function(){
	//display A upon init						
  	$('#glossary > div').not('#glossaryA-B').not('.glossaryHeader').hide();

  	//event listeners for index links
	$('#glossaryIndex > a').each(function() {
		var current = this;
	  	var currentItem = $(current).attr('custom');
	  
	  	$(this).bind('click', function(event) {
		 	if($('#' + $(current).attr('custom')).is(':hidden')) //index links can ONLY turn on glossary entries
				toggleGlossaryItem($(current).attr('custom'));

		  	$.scrollTo('#' + $(current).attr('custom'), 800, {offset:{top:-20}});
		});
  	});
		  
 	//event listeners for glossary grey headers
  	$('#glossary .glossaryHeader').each(function() {
		var current = this;
	  
	  	$(this).bind('click', function(event) {
			toggleGlossaryItem($(current).attr('custom'));
		});
  	});
  
	//anchor tags
	$('#glossary .glossaryItem a').each(function () {
		var current = this;
		$(this).bind('click', function(event) {
			event.preventDefault();
			var glossarySection = $(current).attr('custom'); //what section should be opened?
			
			if($('#' + glossarySection).is(':hidden')) //section closed?
				toggleGlossaryItem($(current).attr('custom'));
			
			var href = $(current).attr('href'); //for the target we aren't interested in the hashmark
			var anchor = new Array();
			anchor = href.split('#');
	
			$.scrollTo("a[name = '" + anchor[1] + "']", 800, {offset:{top:-10}});
		});
	});
			  
}); //end document.ready

function toggleGlossaryItem(itemToDisplay) {
	var glossaryItem = '#' + itemToDisplay;

	if($(glossaryItem).is(':hidden')) {
		$(glossaryItem).fadeIn('slow', function(){toggleCloseImage(itemToDisplay)});
		return false;
	} else {
		$(glossaryItem).fadeOut('fast', function(){toggleCloseImage(itemToDisplay)});
		return false;
	}
}

function toggleCloseImage(glossaryItemReference) {
	//get the reference to the image of the glossary item that was clicked.
	var itemReference = $("#glossary div[custom = '" + glossaryItemReference + "'] img");

	if($('#' + glossaryItemReference).is(':hidden')) {
		$(itemReference).attr("src","/global/images/secondary-nav/plus-box-bullet.png");
		return false;
	} else {
		$(itemReference).attr("src","/global/images/secondary-nav/minus-box-bullet.png");
		return false;
	}
}

//overwrite core functions to fix IE7 cleartype font glitch
jQuery.fn.fadeIn = function(speed, callback) { 
	return this.animate({opacity: 'show'}, speed, function() { 
		if (jQuery.browser.msie)  
			this.style.removeAttribute('filter');  
		if (jQuery.isFunction(callback)) 
			callback();  
	}); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
	return this.animate({opacity: 'hide'}, speed, function() { 
		if (jQuery.browser.msie)  
			this.style.removeAttribute('filter');  
		if (jQuery.isFunction(callback)) 
			callback();  
	}); 
};
