jQuery.fn.accordion = function(options) {
	var SPEED = "medium";
	return this.each(function() {
        $(this).find('dd:not(.stay-open)').hide();
        $(this).find('dd.open').show();
        $(this).find('dt').click(function() {
			if( !$(this).find('+dd').is(":visible") ) {
				$(this.parentNode).find('dd:not(.stay-open)').slideUp(SPEED);
				$(this).find('+dd').slideDown(SPEED);
			}
		});
	});
};

// Auto-activate accordion menu(s)
$(document).ready( function() {
	var domainPattern = new RegExp("(http|ftp|https)://(.*?)/.*$");
	var accordionCookie = location.href.match(domainPattern)[2] + '_accordionMenu';
	$('.accordion-menu').accordion();
	$(".accordion-menu DT").css({ cursor: "pointer" }).click( function() {
		// Set cookie to preserve menu state for this domain
		$.cookie(accordionCookie, $(this).text(), {path: '/'});
	});
	$(".accordion-menu DT").each( function() {
		if( $(this).text() == $.cookie(accordionCookie) ) {
			$(this).parent().find('dd:not(.stay-open)').hide();
			$(this).find('+dd').show();
		}
	});
	
	// Force override for menus with the 'force' class
	$(".accordion-menu DT.force:last").trigger('click');
});
