

jQuery.fn.Accordion = function() {
	
	return this.each(function() {
		
		// For each drawer make sure its contents do not overflow
		
		$('.drawer', this).css('overflow', 'hidden')
		
		// Wire up the close function to all open drawers
		
		.siblings('.open').bind('click', closeDrawer).end()
		
		// Wire up the open function to all closed drawers
		// and make sure all those classed as 'closed' actually are
		
		.siblings('.closed').bind("click", openDrawer)
		.each( function() {
			animateClosed( this, 0);
		});
	});

	function openDrawer(e) {
		e.preventDefault();
		var target = $(this);
		
		target.unbind("click", openDrawer );
		
		// Add up the height of all the children so we can open this drawer
		// to the correct height
		
		var height = 0;
		target.children().each( function() {
			height += $(this).height();
		});

		$(this).addClass("open");
					
		$(this).animate({ height:height+"px"},
				800,
				'easeInOutQuad',
				function() {
					
					var target = $(this);
					
					// Add the closed class
					target.removeClass("closed");
					//target.addClass("open");
					target.unbind( 'click', openDrawer );
					target.bind('click', closeDrawer);
					//target.css('overflow', 'visible');
				});

		// Closed all open drawers
		
		$openDrawers = $(this).siblings('.open').each ( function() { animateClosed(this); } );

		return false;
	}

	function closeDrawer(e) {
		var p = e.target;
		
		// We only want to do the close action if the item clicked is in fact
		// a button to close the list. Specifically, this will NOT close the drawer
		// if the clicked item is a child of a list that is not this list, i.e.
		// a nested list. Useful for navigation lists, such as for the Article page of
		// SLOPOD.
		
		var i = 0;
		while( p && p != $(this).parent()[0] && i < 100 ) {
			
			parent = $(p).parent();
			if((parent[0].localName).toUpperCase() == 'UL' ) {
				p = parent[0];
				break;
			}
			
			p = parent[0];
			i++;
		}
		
		p = $(p);	
		
		if(p.hasClass('accordian')) {
			var target = $(this);
			animateClosed(target);
			e.preventDefault();
			e.stopPropagation();
			return false;
		}
	}

	function animateClosed(target, speed) {
		if(speed == null) {
			speed = 800;
		}
		
		target = $(target);
		target.removeClass("open");
		target.addClass("closing");
		
		// The css for this site uses 0.9em as the height of a closed accordion.
		// However, using em's causes jquery to jump the window to the top every time
		// this animation runs, causing the page to scroll up if it is not already
		// at the top. This kinda negates the animation since it pulls the user away
		// so abruptly so we use pixels instead.
		target.animate({ height:"13px"},
				speed,
				'easeInOutQuad',
				function() {
					// Add the closed class
					
					var target = $(this);
					
					target.removeClass("open");
					target.removeClass("closing");
					target.addClass("closed");
					//target.children('a').unbind( 'click', closeDrawer );
					target.unbind( 'click', closeDrawer );
					target.bind("click", openDrawer );
					//target.css('overflow', 'hidden');
				});
	}
}

$(document).ready(function () {
	$('.accordian').Accordion();
});
