(function($) {
  $.fn.menu = function(opt) {
    opt = $.extend({
      showDelay: 0,
    	switchDelay: 0,
    	hideDelay: 0,
    	menuSel: 'ul',
    	itemSel: 'li',
    	show: function() {
    	  //$(this).show();
				this.style.display = 'block';
    	},
    	hide: function() {
    	  //$(this).hide();
				this.style.display = 'none';
    	}
    }, opt);
  	setTo = function(action, time) {
  		var o = this;
  		$(o).attr('pending', action);
  		window.setTimeout(function() {
  			if($(o).attr('pending') == action) {
  				if(action == 'show') {
  					$(o).parent().addClass('active');
  					opt.show.call(o);
  				} else {
  					$(o).parent().removeClass('active');
  					opt.hide.call(o);
  				}
  			}
  		}, time);
  	};
  	$(this).children(opt.itemSel).each(function(i) {
  		if($(this).children(opt.menuSel).length) $(this).addClass('parent').hover(function() {
  			var o = this;
  			$(this).parent().children('.active').each(function() {
  				if(this != o) setTo.call($(this).children(opt.menuSel).get(0), 'hide', opt.switchDelay);
  			});
  			setTo.call($(this).children(opt.menuSel).get(0), 'show', $(this).parent().children('.active').length ? opt.switchDelay : opt.showDelay);
  		}, function() {
  			setTo.call($(this).children(opt.menuSel).get(0), 'hide', opt.hideDelay);
  		});
  		$(this).children(opt.menuSel).each(function() {
  		  $(this).menu(opt);
  		});
  	});
  	return $(this);
  }
})(jQuery);


$(document).ready(function(){
	$('#menu').menu({showDelay: 200,switchDelay: 300,hideDelay: 600});
});

