jQuery.fn.jlsHMenu = function(options) {
	
	var defaults = {
      'container': '#container',
	  'duration': 100,
	  'delay': 500
	};
	
	if ( options ) { $j.extend( defaults, options ); }

	var c = $j(this);
	var container = $j(defaults.container);
	var duration = defaults.duration;
	var delay = defaults.delay;
	var interval;
	var thisOne;
	
	
	function checkOffset(c){
		if($j(c).find('>ul').length != 0){
			var popX = $j(c).position().left;
			var popY = $j(c).position().top;
			
			var popW = $j(c).find('>ul').css('width');
			popW = Math.floor(popW.substring(0, popW.length-2));
			
			var contW = $j(container).css('width');
			contW = Math.floor(contW.substring(0, contW.length-2));
			
			var bodyW = $j(window).width();
			var offset = popX+popW;
			
			if(offset > bodyW) { $j(c).find('>ul').css({ 'right': '0', 'left': 'auto'}) }
			else {  $j(c).find('>ul').css({ 'left': '0', 'right': 'auto'})  }
		}
	}
	
	function showPopup(e){
		$j(e).addClass('active');
		$j(e).find('>ul').stop(true, true).fadeIn(duration);
	}
	
	function hidePopup(e){
		$j(e).removeClass('active');
		$j(e).find('>ul').stop(true, true).fadeOut(duration);
		clearInterval(interval);
	}
	
	function hideAll(){
		$j(c).find('>ul>li').each(function(){ $j(this).find('>ul').hide(); $j(this).removeClass('active');});
	}
	
	
	
	$j(c).find('> ul >li').hover(function(){
		checkOffset(this);
		hideAll();
		showPopup(this);
		clearInterval(interval);
		
	}, function(){
		thisOne = this;
		interval = setInterval(function() {hidePopup(thisOne)}, delay);
	});

};
