$( document ).ready( function( ) {
  $( '#close' ).mousedown( function( e, param ) {
    // NAB 08.06.2011; added e.button==0 && param=leftButtonPressed
    if ( ( e.button == 1 ) || ( e.button == 0 ) || ( param == 'leftButtonPressed' ) ) {
      $( '#minimize-hover' ).stop( false, true ).hide( );
      $( '#close-hover' ).stop( false, true ).show( );
      $( '#minimize-pressed' ).stop( false, true ).hide( );
      $( '#close-pressed' ).stop( false, true ).show( );

      if ( param != 'leftButtonPressed' ) {
        mouseDownPressed = getClientRect( this );
      }
    }
  } );
  // NAB 08.06.2011; changed from click(), due to troubles with mousedown(e.button == 0)
  $( '#close' ).mouseup( function( e ) {
    // NAB 08.06.2011; added e.button==0
    if ( ( e.button == 1 ) || ( e.button == 0 ) || $.keyboardNavigation ) {
      $( this ).mouseenter( );
      // NAB; "if" added
      if ( !mouseDownPressed || ( mouseDownPressed && isMouseInRect( e ) ) ) {
        tuDispatcher.trigger( 'close' );
      }
      mouseDownPressed = false;

    }
  } );
  // needed for keyboard navigation (s. setTimeout in integrator.tuningindex.js)
  $( '#close' ).click( function( e ) {
    $( this ).mouseup( );
  } );

  $( '#close' ).mouseenter( function( event ) {
    if ( mouseDownPressed && !isMouseInRect( event ) ) {
      return;
    }
    $( '#minimize-hover' ).stop( false, true ).fadeOut( 150 );
    $( '#close-hover' ).stop( false, true ).fadeIn( 150 );
    $( '#minimize-pressed' ).stop( false, true ).hide( );
    $( '#close-pressed' ).stop( false, true ).hide( );
    // NAB 08.06.2011: if mousebutton still pressed, show the pressed state
    if ( mouseDownPressed && isMouseInRect( event ) ) {
      $( this ).trigger( 'mousedown', 'leftButtonPressed' );
    }
  } );

  $( '#close' ).mouseleave( function( ) {
    $( '#minimize-hover' ).stop( false, true ).fadeOut( 150 );
    if ( mouseDownPressed ) {
      // NAB 08.06.2011: changed from fadeOut(150) to prevent flickering
      $( '#close-hover' ).stop( false, true ).hide( );
    } else {
      $( '#close-hover' ).stop( false, true ).fadeOut( 150 );
    }
    $( '#minimize-pressed' ).stop( false, true ).hide( );
    $( '#close-pressed' ).stop( false, true ).hide( );
  } );

  $( '#minimize' ).mousedown( function( e, param ) {
    if ( ( e.button == 1 ) || ( e.button == 0 ) || ( param == 'leftButtonPressed' ) ) {
      $( '#minimize-hover' ).stop( false, true ).show( );
      $( '#close-hover' ).stop( false, true ).hide( );
      $( '#minimize-pressed' ).stop( false, true ).show( );
      $( '#close-pressed' ).stop( false, true ).hide( );

      // get new clientRect only if mouse is not pressed
      if ( param != 'leftButtonPressed' ) {
        mouseDownPressed = getClientRect( this );
      }
    }
  } );

  $( '#minimize' ).mouseup( function( e ) {
    if ( ( e.button == 1 ) || ( e.button == 0 ) || $.keyboardNavigation ) {
      if ( !mouseDownPressed || ( mouseDownPressed && isMouseInRect( e ) ) ) {
        tuDispatcher.trigger( 'minimize' );
      }
      mouseDownPressed = false;
    }
  } );
  // click() is needed for keyboard navigation (s. setTimeout in integrator.tuningindex.js)
  $( '#minimize' ).click( function( e ) {
    $( this ).mouseup( );
  } );

  $( '#minimize' ).mouseenter( function( event ) {
    if ( mouseDownPressed && !isMouseInRect( event ) ) {
      return;
    }
    $( '#minimize-hover' ).stop( false, true ).fadeIn( 150 );
    $( '#close-hover' ).stop( false, true ).fadeOut( 150 );
    $( '#minimize-pressed' ).stop( false, true ).hide( );
    $( '#close-pressed' ).stop( false, true ).hide( );
    // NAB 08.06.2011: if mousebutton still pressed, show the pressed state
    if ( mouseDownPressed && isMouseInRect( event ) ) {
      $( this ).trigger( 'mousedown', 'leftButtonPressed' );
    }
  } );

  $( '#minimize' ).mouseleave( function( ) {
    if ( mouseDownPressed ) {
      // NAB 08.06.2011: changed from fadeOut(150) to prevent flickering
      $( '#minimize-hover' ).stop( false, true ).hide( );
    } else {
      $( '#minimize-hover' ).stop( false, true ).fadeOut( 150 );
    }

    $( '#close-hover' ).stop( false, true ).fadeOut( 150 );
    $( '#minimize-pressed' ).stop( false, true ).hide( );
    $( '#close-pressed' ).stop( false, true ).hide( );
  } );
} );
