var bkg_color_rgb = null;


// function received events from the VRML world via a VBScript function
//---------------------------------------------------------------------

function OnEvent(par1, par2, time) 
{ 
  // reflect state of part on web-page buttons: green or white
  //----------------------------------------------------------

  if ( par1 == 'O'        ){ document.ass_O.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  if ( par1 == 'P'        ){ document.ass_P.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  if ( par1 == 'N'        ){ document.ass_N.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  if ( par1 == 'M'        ){ document.ass_M.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  if ( par1 == 'L'        ){ document.ass_L.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 

  if ( par1 == 'B0_B1_B2' ){ document.ass_B0_B1_B2.src = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  if ( par1 == 'B3'       ){ document.ass_B3.src       = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  
  if ( par1 == 'E2_E5'    ){ document.ass_E2_E5.src    = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  if ( par1 == 'E3_E4'    ){ document.ass_E3_E4.src    = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 
  if ( par1 == 'E6_E1'    ){ document.ass_E6_E1.src    = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 

  if ( par1 == 'K1_K2'    ){ document.ass_K1_K2.src    = ( par2 == 0 ) ? './img/green.png' : './img/white.png'; } 

  if ( par1 == 'A'        ){ document.ass_A.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 

  if ( par1 == 'C'        ){ document.ass_C.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 
  if ( par1 == 'D'        ){ document.ass_D.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 

  if ( par1 == 'F'        ){ document.ass_F.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 
  if ( par1 == 'G'        ){ document.ass_G.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 
  if ( par1 == 'H'        ){ document.ass_H.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 
  if ( par1 == 'I'        ){ document.ass_I.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 

  if ( par1 == 'Q'        ){ document.ass_Q.src        = ( par2 == 0 ) ? './img/green.png' : './img/white.png';  } 


  //----------------------------------------------------------------------------------------
  // Update the local variable. failing to so results in the following error:
  // After building up a gear train, clicking on a shaft will not result in hiding the shaft
  // Only the second click hides the shaft.
  //----------------------------------------------------------------------------------------

  if ( par1 == 'O'        ){ if ( par2 == 0 ) { o        = 0; } } 
  if ( par1 == 'P'        ){ if ( par2 == 0 ) { p        = 0; } } 
  if ( par1 == 'N'        ){ if ( par2 == 0 ) { n        = 0; } } 
  if ( par1 == 'M'        ){ if ( par2 == 0 ) { m        = 0; } } 
  if ( par1 == 'L'        ){ if ( par2 == 0 ) { l        = 0; } } 

  if ( par1 == 'B0_B1_B2' ){ if ( par2 == 0 ) { b0_b1_b2 = 0; } } 
  if ( par1 == 'B3'       ){ if ( par2 == 0 ) { b3       = 0; } } 
  
  if ( par1 == 'E2_E5'    ){ if ( par2 == 0 ) { e2_e5    = 0; } } 
  if ( par1 == 'E3_E4'    ){ if ( par2 == 0 ) { e3_e4    = 0; } } 
  if ( par1 == 'E6_E1'    ){ if ( par2 == 0 ) { e6_e1    = 0; } } 

  if ( par1 == 'K1_K2'    ){ if ( par2 == 0 ) { k1_k2    = 0; } } 

  if ( par1 == 'A'        ){ if ( par2 == 0 ) { a        = 0; } } 

  if ( par1 == 'C'        ){ if ( par2 == 0 ) { c        = 0; } } 
  if ( par1 == 'D'        ){ if ( par2 == 0 ) { d        = 0; } } 

  if ( par1 == 'F'        ){ if ( par2 == 0 ) { f        = 0; } } 
  if ( par1 == 'G'        ){ if ( par2 == 0 ) { g        = 0; } } 
  if ( par1 == 'H'        ){ if ( par2 == 0 ) { h        = 0; } } 
  if ( par1 == 'I'        ){ if ( par2 == 0 ) { i        = 0; } } 

  if ( par1 == 'Q'        ){ if ( par2 == 0 ) { q        = 0; } } 


  // other callbacks
  //----------------

  if ( par1 == 'speed_ind' )
  {          
    document.getElementById( 'id_speed_ind' ).innerHTML = par2;
  } 

  if ( par1 == 'delta_ang' )
  {          
    document.getElementById( 'id_delta_ang' ).innerHTML = Math.abs( Math.round(  par2 * 100000.0 ));
  } 

  if ( par1 == 'button_in_pause' )
  {          
    document.motion_pause.src = ( par2 == true ) ? './img/green.png' : './img/white.png';
  } 

  if ( par1 == 'button_in_stop' )
  {          
    document.motion_stop.src = ( par2 == true ) ? './img/green.png' : './img/white.png';
  } 

  if ( par1 == 'button_in_zero' )
  {          
    document.motion_zero.src = ( par2 == true ) ? './img/green.png' : './img/white.png';
  } 

  if ( par1 == 'button_in_rev' )
  {          
    document.motion_rev.src = ( par2 == true ) ? './img/green.png' : './img/white.png';
  } 

  if ( par1 == 'button_in_fwd' )
  {          
    document.motion_fwd.src = ( par2 == true ) ? './img/green.png' : './img/white.png';
  } 


  // create a cookie to remember the sky color
  //------------------------------------------

  if ( par1 == 'skyColorChanged' )
  {
    createCookie( "bkg_rgb", "" + par2, 100 );    
  }


  // send the background from cookie to the VRML world
  //--------------------------------------------------

  if ( par1 == 'get_bkg' )
  {          
//    if ( par2 )
//    {
//      // send the background to the VRML world if cookie was read
//      //---------------------------------------------------------

//      if ( bkg_rgb != null )
//      {
//        document.CC3D.setNodeEventIn( 'SCR_INIT_BKG', 'sky_rgb', bkg_rgb );
//      }                    
//    }
  }
 
 
  // request to get the user background (read from cookie): send to vrml world
  //--------------------------------------------------------------------------

  if ( par1 == 'getUserBKG' )
  {          
    // send the background to the VRML world if cookie was read
    //---------------------------------------------------------

    if ( bkg_rgb != null )
    {
      document.CC3D.setNodeEventIn( 'SCR_INIT_USER_BKG', 'sky_rgb', bkg_rgb );
    }                    
  }
}
