(function ($) {
    $.fn.mainmenu = function (settings) {
    	settings = jQuery.extend({
    		},
    		settings
    	);
    	var menu		= $(this);
    	var hideTimeout	= null;
    	var current		= null;

    	menu.find(' > li').each(function(i, item) {
    		var subitem		= $(item).find('ul');
    		var subHeight	= 0;
    		var iCount		= 0;
    		$(item).css('position', 'relative');
    		$(item).attr('mmKey', Math.random());
        	
    		if (subitem.length < 1) {
    			return;
    		}
    		subitem.css('top', $(item).outerHeight(true));
    		subitem.find('li').each(function() {
    			subHeight += parseInt($(this).height());
    			
    			/**
    			 * Fï¿½r IE6
    			 */
    			if (!$(this).hasClass('active')) {
	    			$(this).mouseover(function() {
	    				$(this).addClass('active');
	    			});
	    			$(this).mouseout(function() {
	    				$(this).removeClass('active');
	    			});
    			}
    			
    			//Fï¿½r Border
    			iCount++;
    		});
    		
    		
    		$(item).mouseover(function() {
				window.clearTimeout(hideTimeout);
    			if (current && current != $(item).attr('mmKey')) {
    				//Sofort schließen
    				menu.find('> li[mmKey="' + current + '"] ul').stop();
    				menu.find('> li[mmKey="' + current + '"] ul').height(0);
    			}
    			current = $(item).attr('mmKey');
    			subitem.stop();
    			subitem.animate({
	    				height: subHeight+iCount+1
	    			},
	    			200
	    		);
    		});
    		
    		$(item).mouseout(function() {
    			hideTimeout = window.setTimeout(function() {
	    			subitem.stop();
	    			subitem.animate({
		    				height: 0
		    			},
		    			200
		    		)},
		    		350
		    	);
    		});
    		
    		$(subitem).mouseover(function() {
    			window.clearTimeout(hideTimeout);
    		});
    	});
    };
}(jQuery));