/**
 * jQuery.collapsible - create collapsible list
 * Date: 2010/08/03
 *
 * @author Michaël van Oosten
 * @version 1.0
 *
 **/

(function($) {
	$.fn.collapsible = function(settings) {

		var config = {
			'showfirst' : true,		// show first item by default
			'wrp' : '.moreinfo',	// classname of toggle area
			'act' : 'act', 			// classname of active <li>
			'speed' : 300			// animation speed
		};
		if (settings) $.extend(config, settings);
		
		this.each(function() {
			
			var busy_animation = false;
			var act_elem;
			
			$(this).find(config.wrp).hide();
			
			
			if(config.showfirst) {
				$(this).find('li:eq(0)').addClass(config.act);
				$(this).find('li:eq(0) ' + config.wrp).show();
			}
			
			$(this).find('li').hover(
				function() {
					act_elem = $(this).index();					

					if( !$(this).hasClass(config.act) && !busy_animation) {
						busy_animation = true;
						var parentUl = $(this).parents('ul:first');
						parentUl.find('li').removeClass(config.act);
						parentUl.find(config.wrp).slideUp('fast');
						$(this).addClass(config.act).find(config.wrp).slideDown(
							config.speed, function() { 
								busy_animation=false;								
								var listItem = $(this).parent();
								if( act_elem != listItem.parent().find('li').index( listItem ) ) {
									listItem.parent().find('li:eq(' + act_elem + ')').mouseover();
								}
							}
						);
					}
				},
				function() {}
			);

		});
		
		return this;
 
	};
 
 })(jQuery);
