(function ($) {

/** @brief  Handle a menu item that requires click to drop.
 *  @param  $ul     The jQuery ul element.
 */
function initClickMenu($ul)
{
    $ul.parent().click(function(e) {
                            var url = $(e.target).attr('href');

                            // If the url is '#', prevent the default action
                            if (url === '#')
                            {
                                e.preventDefault();
                                //e.stopPropagation();
                            }

                            if ($ul.is(':hidden'))
                            {
                                // show
                                $ul.slideDown('fast').show();
                            }
                            else
                            {
                                // hide
                                $ul.slideUp('slow');
                            }

                            if (url === '#')
                                return false;
                       })
                .hover(function() {},
                       function() {
                            // un-hover
                            $ul.slideUp('slow');
                       });
}

/** @brief  Handle a menu item that only requires hover to drop.
 *  @param  $ul     The jQuery ul element.
 */
function initHoverMenu($ul)
{
    $ul.parent().hover(function() {
                            // hover
							$("ul.subnav").stop(true,true);
                            $ul.slideDown('fast').show();
                       },
                       function() {
                            // un-hover
							$("ul.subnav").stop(true,true);
                            $ul.hide(); //$ul.slideUp('fast');
                       });
}

/** @brief  Initialize the navigation menu.
 *  @param  $ul     The top-level jQuery ul element (e.g. $('ul#nav li ul')).
 */
function initMenu($ul)
{
    $ul.each(function() {
        var $ul = $(this);

        $ul.addClass('subnav');
        initClickMenu($ul);

        // Now, for all sub-sub menus...
        $ul.find('li ul').each(function() {
            var $ul2    = $(this);

            $ul2.parent().find('a:first').addClass('subnav');
            initHoverMenu($ul2);
        });
    });
}



/** @brief  Attach a navMenu helper to jQuery
 *  @param  options     Options (none supported)
 *
 */
$.fn.navMenu = function(options) {

    /* To overcome the NASTY IE6 z-index bugs that, in this case don't seem to
     * be solvable via CSS alone, make sure the z-index of the top-level items
     * DECREASE left-to-right.  This way, any menu to the left that has
     * sub-menus that may overlap the menus to the right will appear ABOVE the
     * menus to the right.
     */
    //options = $.extend( { defaults... }, options);
    var zindex  = 0;

    this.each(function() {
        // ul#nav
        var $ul = $(this);

        $ul.hide();

        // ul#nav li (directly children only)
        $ul.find('> li').each(function() {
            var $el = $(this);
            $el.addClass('top');

            if (zindex < 1)
            {
                // Grab the z-index defined in the CSS (ul li.top)
                zindex = parseInt($el.css('z-index'));
            }

            $el.css('z-index', zindex--);
        });

        // ul#nav li ul (all descendants, all levels)
        initMenu( $ul.find('li ul') );

        $ul.show();
    });

    return this;
}


})(jQuery);



/*
$(document).ready(function(){

	$("ul.subnav").parent().append("<span></span>");

    // Only shows drop down trigger when js is enabled
    // (Adds empty span tag after ul.subnav*)

	$("ul.topnav li span").click(function() { //When trigger is clicked...

        // Following events are applied to the subnav itself
        // (moving subnav up and down)
		$(this).parent()
               .find("ul.subnav")
               .slideDown('fast')
               .show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent()
                   .find("ul.subnav")
                   .slideUp('slow'); // When the mouse hovers out of the
                                     // subnav, move it back up

		});

        // Following events are applied to the trigger
        // (Hover events for the trigger)
		}).hover(function() {
            // On hover over, add class "subhover"
			$(this).addClass("subhover");

		}, function(){	//On Hover Out
            // On hover out, remove class "subhover"
			$(this).removeClass("subhover");

	});

});
*/
