var nlbdrop_i = 0;
jQuery.fn.nlbdrop = function(options) {
	// default options
	var defaults = {
		parentContainer: 'body',
		fade: 0.85,
		backgroundColor: '#FFFFFF',
		textTransform: 'uppercase',
		activeLink: '',
		activeLinkCSS: { 'font-weight' : 'bold', 'border' : '1px solid #000000' }
	};
	
	// merge options
	var options = jQuery.extend( defaults, options );

	return this.each( function() {
		var root = jQuery(this);
		var rootPos = root.offset( );
		var rootHeight = root.outerHeight( );
		// create a div underneath the root element where the dropdowns will go
		var container = addContainer( root, true );
	});

	function show( )
	{
		var cNumber = jQuery(this).data( 'nlbdrop_container' );
		var container = jQuery('div#'+cNumber);
		if( container.hasClass( 'nlbdrop_notroot' ) )
			repositionDiv( jQuery(this), container );
		container.fadeIn( 'fast' );
	}

	function hide( )
	{
		var cNumber = jQuery(this).data( 'nlbdrop_container' );
		setTimeout( function() {
			var container = jQuery('div#'+cNumber);
			if( !container.data( 'cancelHide' ) )
				container.fadeOut( 'fast' );
		}, 500 );
	}

	function showC( )
	{
		jQuery(this).data( 'cancelHide', true );
	}

	function hideC( )
	{
		jQuery(this).data( 'cancelHide', false );
		var cont = this;
		setTimeout( function() {
			var container = jQuery('div#'+cont.id);
			if( !container.data( 'cancelHide' ) )
				container.fadeOut( 'fast' );
		}, 500 );
	}

	function repositionDiv( elem, container )
	{
		container.css(
			{
				'position' : 'absolute',
				'top' : elem.position( ).top-8+'px',
				'left' : elem.position( ).left+elem.outerWidth( )+12+'px'
			}
		);
	}

	function addContainer( elem, isRoot )
	{
		var isRoot = isRoot || false;
		var elemPos = (isRoot?elem.offset( ):elem.offset( ));
		var elemHeight = (isRoot?elem.outerHeight( ):elem.parent( ).outerHeight( ));
		var elemWidth = (isRoot?elem.outerWidth( ):elem.parent( ).outerWidth( ));
		var container = jQuery( '<div id="nlbdropc_'+(nlbdrop_i++)+'" class="nlbdrop_container'+(!isRoot?' nlbdrop_notroot':'')+'"></div>' ).css(
			{
				'position' : 'absolute',
				'top' : (elemPos.top+(isRoot?elemHeight+2:0))+'px',
				'left' : elemPos.left+(isRoot?0:elemWidth+2)+'px',
				'display' : 'none'
			}).fadeTo( 0, options.fade );
		if( isRoot )
		{
			container.appendTo(options.parentContainer);
			elem.data( 'nlbdrop_container', container.attr( 'id' ) );
			elem.hover( show, hide );
		}
		else
		{
			container.insertAfter( elem );
			elem.parent( ).data( 'nlbdrop_container', container.attr( 'id' ) );
			elem.parent( ).hover( show, hide );
		}
		if( isRoot )
			elem.children('ul').show( ).appendTo( container );
		else
			elem.show( ).appendTo( container );

		var containerLists = container.children('ul').css(
			{
				'display' : 'block',
				'padding' : '8px 10px',
				'list-style' : 'none',
				'margin' : '0px',
				'background-color' : options.backgroundColor,
				'text-transform' : options.textTransform
			}).find('li > ul').show( );
		if( containerLists.length > 0 )
		{
			containerLists.each( function() {
				addContainer( jQuery(this), false );
			});
		}
		if( isRoot )
		{
			container.data( 'elem', elem[0] );
			container.hover( showC, hideC );
		}
		else
		{
			elem.parent( ).data( 'elem', elem[0] );
			elem.parent( ).hover( showC, hideC );
		}
		return container;
	}
};
