window.addEvent( 'domready', function()
{
    if ( /Mac/.test ( window.navigator.platform ) ) {
        $( 'wrapper' ).addClass( 'mac' );
    }

    $$('#nav ul.top-nav li.zip > a').addEvent( 'click', function( event ){
        this.getParent().addClass( 'focus' );
        new Event( event ).stop();
    });

    $$('#nav ul.top-nav li.zip form a.close').addEvent('click', function( event )
    {
        this.getParent('li.zip').removeClass( 'focus' );
        new Event( event ).stop();
    });

    document.addEvent( 'click', function( event )
    {
        if ( !$( event.target ).getParent( 'li.zip' ) )
            $$( '#nav ul.top-nav li.zip' ).removeClass( 'focus' );
    });

    $$('#nav ul.top-nav li.zip form a.button').addEvent( 'click', function( event )
    {
        this.getParent( 'form' ).submit();

        new Event( event ).stop();
    });

    $$( '#menu ul li' ).each( function( item )
    {
        if ( item.hasClass( 'animate' ) )
        {
            var link = item.getElement( 'a' );

            link.addEvent('mouseenter', function()
            {
                if ( 'ofx' in this && typeof this.ofx == 'object' )
                    this.ofx.cancel();

                this.ofx = new Fx.Tween( this.getElement( 'span.bar' ), { transition: Fx.Transitions.Expo.easeOut, duration: 400 } ).start( 'width', 92 );
            });

            if ( !item.hasClass( 'submenu' ) )
            {
                link.addEvent('mouseleave', function()
                {
                    this.ofx.cancel();
                    this.ofx = new Fx.Tween( this.getElement( 'span.bar' ), { transition: Fx.Transitions.Expo.easeOut, duration: 400 } ).start( 'width', 0 );
                });
            }
        }

        if ( item.hasClass( 'submenu' ) )
        {
            item.addEvents(
            {
                mouseenter: function()
                {
                    if ( 'tmr' in this )
                        clearTimeout( this.tmr );

                    this.addClass('hover');
                },
                mouseleave: function()
                {
                    var self = this;

                    self.tmr = setTimeout(function()
                    {
                        self.removeClass( 'hover' );

                        if ( typeof link == 'object' && typeof link.ofx == 'object' )
                        {
                            link.ofx.cancel();
                            link.ofx = new Fx.Tween( link.getElement( 'span.bar' ), { transition: Fx.Transitions.Expo.easeOut, duration: 400 } ).start( 'width', 0 );
                        }

                    }, 500 );
                }
            });
        }
    });
});