

(function($) {

	$.fn.jmenu = function(options) {
		
		var defaults = {
			timer : 500,
			xOffset: 0,
			yOffset: 0
		};
		
		var options = $.extend( defaults, options );
		
		var timer = 0;
		
		var hideOtherBranche = function(ele) {
			$(ele).parent().find('ul').css('visibility', 'hidden');
			$($(ele).find('ul').get(0)).css('visibility', 'visible');
		};
		
		return this.each(function() {
			$t = $(this);
			
			$t.find('ul').css('position', 'absolute');
			$t.find('li').css('position', 'relative');
			
			$t.find('ul ul').each( function() {
				var liElement = $(this).parent().get(0);
				var w = $(liElement).innerWidth();
				$(this).css('left', w + options.xOffset);
				$(this).css('top', 0 + options.yOffset);
				$(this).css('margin-top', 0);
				$(this).css('padding-top', 0);
			});
			
			$t.find('ul').css('visibility', 'hidden');
			
			$t.find('li').each( function() {
				$(this).mouseenter(function(event) {
					event.stopPropagation();
					hideOtherBranche(this);
					window.clearTimeout(timer);
				});
			});
			
			$t.mouseleave( function() {
				timer = window.setTimeout( function() {
					$t.find('ul').css('visibility', 'hidden');
				}, options.timer );
			});
			
			$t.mouseenter( function() {
				window.clearTimeout(timer);
			});
			
			$t.find('ul').each( function() {
				$(this).mouseenter(function(event) {
					event.stopPropagation();
					$(this).css('visibility', 'visible');
					window.clearTimeout(timer);
				});
			});
		});
		
	}

})(jQuery);

