// <![CDATA[

( function( $ )
{
    
    $.fn.nastyDropDown = function( options )
    {
        var options = $.extend(
            {},
            $.fn.nastyDropDown.defaultOptions,
            options
        ),
        waiting     = false,
        $menu       = this;
        
        $menu.children( 'li' ).each(
            function()
            {
                var $this = $( this );
                
                if( !$this.children( 'ul' ).length )
                {
                    return;
                }
                
                $this.hover(
                    showSubMenu,
                    hideSubMenu
                );
            }
        );
        
        function showSubMenu()
        {
            var self  = this,
                $this = $( this );
            
            $this.siblings( 'li' ).each( hideSubMenu );
            
            if( options.animated )
            {
                if( $menu.find( 'ul:animated' ).length )
                {
                    if( waiting )
                    {
                        return;
                    }

                    waiting = true;

                    window.setTimeout(
                        function()
                        {
                            waiting = false;

                            showSubMenu.call( self );
                        },
                        100
                    );

                    return;
                }

                waiting = false;
                
                $this.children( 'ul' ).slideDown( 'fast' );
            }
            else
            {
                $this.children( 'ul' ).show();
            }
            
            $this.addClass( 'open' );
        }

        function hideSubMenu()
        {
            var $this = $( this );

            $this.removeClass( 'open' );
            
            if( options.animated )
            {
                $this.children( 'ul' ).hide().slideUp(
                    'fast',
                    function()
                    {
                        $this.removeClass( 'open' );
                    }
                );
            }
            else
            {
                $this.children( 'ul' ).hide();
            }
        }
    };
    
    $.fn.nastyDropDown.defaultOptions = {
        showSpeed : 'fast',
        hideSpeed : 'fast',
        animated  : false
    };
    
    function init()
    {
        moveMenu( '#tx-krzblog-menu', 1 );
        moveMenu( '#tx-krzcal-menu', 4 );
        moveMenu( '#tx-team-menu', 3 );
        moveMenu( '#tx-ideas-menu', 2 );
        moveMenu( '#tx-publications-menu', 6 );
        
        $( '#level1 > ul' ).nastyDropDown();
    }
    
    function moveMenu( list, position )
    {
        var $list = $( list + ' ul' ),
            $item = $( $( '#level1 > ul > li' )[ position ] );
        
        if( !$list.length || !$item.length || !$list.find( 'li' ).length )
        {
            return;
        }
        
        $list.remove();
        $list.attr( 'id', list.substr( 1, list.length - 1 ) + '-list' );
        $list.appendTo( $item );
        
        var $spacers = $list.find( 'hr' );
        
        if( $spacers.length )
        {
            $spacers.last().remove();
        }
    }
    
    $( document ).ready( init );
    
} )( jQuery );

// ]]>