/**
 * Kegamenu jquery plug-in.
 * 
 * @param options array with options.
 * 
 * Available options:
 * - activeClass: name of class which indicates active element. default: current
 * - speed: speed of slide animations. default: normal
 * 
 */
jQuery.fn.kegamenu = function(options) {
	// set overwrite options
	settings = jQuery.extend({
		activeClass: 'current',
		speed: 'normal'
	}, options);
	
	var makeMenu = function(menu) {
		
		jQuery('ul', menu).hide();						// hide sub menu's
    	jQuery('.' + settings['activeClass'], menu).parents('ul').show();	// show active item
        
    	// add click handler to links
    	jQuery('li a', menu).click(function() {
        	var checkElement = jQuery(this).next();
        	
        	// if a submenu is available and is hidden
            if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {

            	// close visible ul's of same level
            	var parent = checkElement.parents('ul')[0];
            	
            	jQuery('li ul:visible', parent).slideUp(settings['speed']);
            	
            	// show active ul
                checkElement.slideDown(settings['speed']);
                return false;
            }

            // close element if it's already open.
            if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
                checkElement.slideUp(settings['speed']);
                return false;
            }
        });
	};
	
	return this.each(function(){
		makeMenu(this);
	});
};
