if ( !__DISABLE_JS ) {
    document.observe( "dom:loaded", menuFirstInit );
    Event.observe( window, 'load', menuSecondInit );
}

var menuAnimation;
var isOverMenu = false;
var mouseDownInFlash = false;
var isIE7 = ( Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7 );

function menuFirstInit( event ) 
{
    var menu = $( "menu" );
    menu.insert( { before: new Element( 'div', { id: 'menuBackground' } ) } );
    
    menu.removeClassName( 'precollapsed' );

    menuAnimation = new Animator().addSubject(
        new CSSStyleSubject( menu, "", "collapsed" ) 
    );
    menuAnimation.jumpTo( 1 );
}

function menuSecondInit( event )
{
    var menu = $( "menu" );
    menu.observe( "mouseover", onMouseOver_menu );

    document.observe( "mouseup", onMouseUp_document );
}

function onComplete_menuAnimation( animation ) 
{
    if ( isIE7 ) {
        // IE 7! force restyling so that the active column background reaches all the way down
        $( 'holder' ).removeClassName( 'dynamic' );
        $( 'holder' ).addClassName( 'dynamic' );
    }
}

function onMouseOver_document( event )
{
    var target = event.element();
    var menu = $( "menu" );

    if ( !target.descendantOf( menu ) && menu != target ) {
        menuAnimation.setOptions( { transition: Animator.makeEaseOut( 3 ) } );
        menuAnimation.seekTo( 1 );
        isOverMenu = false;
        
        document.stopObserving( "mouseover", onMouseOver_document );
        menuSecondInit();
    }
}

function onMouseOver_menu( event )
{
    if ( !isOverMenu && !mouseDownInFlash ) {

        menuAnimation.setOptions( { transition: Animator.makeEaseIn( 3 ), onComplete: ( isIE7 ? onComplete_menuAnimation : function() {} ) } );
        menuAnimation.seekTo( 0 );
        isOverMenu = true;

        var menu = $( "menu" );
        menu.stopObserving( "mouseover", onMouseOver_menu );
        document.observe( "mouseover", onMouseOver_document );
    }
}

function onMouseUp_document( event )
{
    mouseDownInFlash = false;

    var target = event.element();
    var flash = $( "flashContent" );

    if ( flash && !target.descendantOf( flash ) && flash != target ) {
        if ( flash.setMouseDownState ) {
            flash.setMouseDownState( false );
        }
    }
}

function setMouseDownState( isDown )
{
    mouseDownInFlash = isDown;
}

