/**
 * Expanding menu script. Supports a menuPrefix for expandable menues.
 * Use jQuery to make the correct selection of menues. Will add a default
 * css class "expandingMenu" to the supplied sections.
 *
 * Requires jQuery.
 * 
 * Example, add to all menues, using default options:
 * $("#menu ul").expandingMenu();
 *
 * Example, add to all menues, setting options:
 * $("#menu ul").expandingMenu({speed:300, menuPrefix:"&gt;", cssClass:"expandingMenu"});
 *
 * Example, add 2nd and 3rd menues (indexes are zero-based):
 * $("#menu ul").filter(":eq(1)").expandingMenu();
 * $("#menu ul").filter(":eq(2)").expandingMenu();
 *
 * @author Peter Bjorkman peter@josh.se
*/ 
(function($) {

	$.fn.expandingMenu = function(options) {
		
		var options = $.extend({},$.fn.expandingMenu.defaults,options);
	
		this.each(function()
		{
			$(this).addClass(options.cssClass);
			
			// hide all except active sections
			if ($(this).children(":gt(0)").filter(".active").length == 0)
			{
				$(this).children(":gt(0)").hide();
			}
			
			// Menu prefix if it exists, only for sections that have children (can be expanded)
			if (options.menuPrefix.length > 0 && $(this).children().length > 1)
			{
				$(this).children(":first").html(options.menuPrefix +$(this).children(":first").html());

			}
						
			$(this).children(":first").css({"cursor":"pointer"}).toggle(
				function () {
					$(this).siblings().show(options.speed);
				}, function () {
					$(this).siblings().hide(options.speed);
				}
			)
		})
	};
	
	// expandingMenu default options
	$.fn.expandingMenu.defaults = {
		speed: 300,
		menuPrefix: "",
		cssClass: "expandingMenu"
	};

})(jQuery);