$.fn.flyouts = function(options) {
	
	options = $.extend({
		effect:'show', // show|slide|fade
		effectDuration:100,
		showDelay:100,
		hideDelay:100,
		markFirstLast:false
	}, options || {});
	
	this.each(function() {
	
		var zIndex = 1000;
		
		function getSubnav(ele) {
			if (ele.nodeName.toLowerCase() == 'li') {
				var subnav = $('> ul', ele);
				return subnav.length ? subnav[0] : null;
			} else {
				return ele;
			}
		}
		
		function hide() {
			var subnav = getSubnav(this);
			if (!subnav) { return; }
			
			$(subnav).data('cancelHide', false).data('cancelShow', true);
			setTimeout(function(){
				if ($.data(subnav, 'cancelHide')) { return; }
				switch(options.effect){
					case 'slide' :
						$(subnav).slideUp(options.effectDuration);
						break;
					case 'fade' :
						$(subnav).fadeOut(options.effectDuration);
						break;
					default : 
						$(subnav).hide();
				}
			}, options.hideDelay);
		}
		
		function show() {
			var subnav = getSubnav(this);
			if (!subnav) { return; }
			
			var px = $(subnav).parent().position().left;
			var py = $(subnav).parent().position().top;
			var ox = $(subnav).attr('foposition').match(/-{0,1}[0-9]{1,}\,-{0,1}[0-9]{1,}/) ? Math.round($(subnav).attr('foposition').split(',')[0]) : 0;
			var oy = $(subnav).attr('foposition').match(/-{0,1}[0-9]{1,}\,-{0,1}[0-9]{1,}/) ? Math.round($(subnav).attr('foposition').split(',')[1]) : 0;

			//$.data(subnav, );
			
			$(subnav)
				.data('cancelHide', true)
				.data('cancelShow', false)
				.css({ 'z-index':zIndex++, left:(px+ox), top:(py+oy) });
			
			setTimeout(function(){
				if ($.data(subnav, 'cancelShow')) { return; }
				switch(options.effect){
					case 'slide' :
						$(subnav).stop(true, true).slideDown(options.effectDuration);
						break;
					case 'fade' :
						$(subnav).stop(true, true).fadeIn(options.effectDuration);
						break;
					default : 
						$(subnav).show();
				}
			}, options.showDelay);
			
		}
		
		if (options.markFirstLast) {
			$('ul li:first-child', this).addClass('first');
			$('ul li:last-child', this).addClass('last');
		}
		
		$('ul', this).css({position:'absolute', 'z-index':zIndex});
		$('ul, li', this).hover(show, hide);
	
	});

};
