// get and display current framerate
//----------------------------------

fps_old     = 0;
cnt         = 0;
filter_fac  = 0.4;

function getFrameRate()
{
  if ( cnt == 0 )
  {
    fps_new = filter_fac * fps_old + ( 1 - filter_fac) * document.CC3D.getCurrentFrameRate();
    fps_old = fps_new;

    document.getElementById( 'id_current_frame_rate').innerHTML = Math.round( fps_new ) + ' fps';

    cnt = 4;
  }
  
  --cnt;
}


// variables to hold current show/hide state of parts
//---------------------------------------------------

var o = 0;
var p = 0;

var n = 0;

var m = 0;
var l = 0;

var b0_b1_b2 = 0;
var b3	     = 0;

var e2_e5 = 0;
var e3_e4 = 0;
var e6_e1 = 0;

var k1_k2 = 0;

var a = 0;

var c = 0;
var d = 0;

var f = 0;
var g = 0;
var h = 0;
var i = 0;

var q = 0;



//----------------------------------------------
// code to show/hide/toggle a part
//
// parameter x: 2      = force show
//              3      = force hide
//              0 or 1 = toggle between show and hide
//
//----------------------------------------------
//
//                        the world                     object         field                show  or  hide    0 or 1     toggle between 1 and 0
function show_O        (x) { document.CC3D.setNodeEventIn( 'O',        'hide', o        = ((x==2) || (x==3)) ? (x-2) : ( o        == 0 ) ? 1 : 0 ); reset(); }
function show_P        (x) { document.CC3D.setNodeEventIn( 'P',        'hide', p        = ((x==2) || (x==3)) ? (x-2) : ( p        == 0 ) ? 1 : 0 ); reset(); }
function show_N        (x) { document.CC3D.setNodeEventIn( 'N',        'hide', n        = ((x==2) || (x==3)) ? (x-2) : ( n        == 0 ) ? 1 : 0 ); reset(); }
function show_M        (x) { document.CC3D.setNodeEventIn( 'M',        'hide', m        = ((x==2) || (x==3)) ? (x-2) : ( m        == 0 ) ? 1 : 0 ); reset(); }
function show_L		     (x) { document.CC3D.setNodeEventIn( 'L',        'hide', l        = ((x==2) || (x==3)) ? (x-2) : ( l        == 0 ) ? 1 : 0 ); reset(); }
function show_B0_B1_B2 (x) { document.CC3D.setNodeEventIn( 'B0_B1_B2', 'hide', b0_b1_b2 = ((x==2) || (x==3)) ? (x-2) : ( b0_b1_b2 == 0 ) ? 1 : 0 ); reset(); }
function show_B3       (x) { document.CC3D.setNodeEventIn( 'B3',       'hide', b3       = ((x==2) || (x==3)) ? (x-2) : ( b3       == 0 ) ? 1 : 0 ); reset(); }
function show_Q        (x) { document.CC3D.setNodeEventIn( 'Q',        'hide', q        = ((x==2) || (x==3)) ? (x-2) : ( q        == 0 ) ? 1 : 0 ); reset(); }
function show_E2_E5    (x) { document.CC3D.setNodeEventIn( 'E2_E5',    'hide', e2_e5    = ((x==2) || (x==3)) ? (x-2) : ( e2_e5    == 0 ) ? 1 : 0 ); reset(); }
function show_E3_E4    (x) { document.CC3D.setNodeEventIn( 'E3_E4',    'hide', e3_e4    = ((x==2) || (x==3)) ? (x-2) : ( e3_e4    == 0 ) ? 1 : 0 ); reset(); }
function show_E6_E1    (x) { document.CC3D.setNodeEventIn( 'E6_E1',    'hide', e6_e1    = ((x==2) || (x==3)) ? (x-2) : ( e6_e1    == 0 ) ? 1 : 0 ); reset(); }
function show_K1_K2    (x) { document.CC3D.setNodeEventIn( 'K1_K2',    'hide', k1_k2    = ((x==2) || (x==3)) ? (x-2) : ( k1_k2    == 0 ) ? 1 : 0 ); reset(); }
function show_A        (x) { /* alert( 'A..x: ' + x ); */ document.CC3D.setNodeEventIn( 'A',        'hide', a        = ((x==2) || (x==3)) ? (x-2) : ( a        == 0 ) ? 1 : 0 ); reset(); }
function show_C        (x) { document.CC3D.setNodeEventIn( 'C',        'hide', c        = ((x==2) || (x==3)) ? (x-2) : ( c        == 0 ) ? 1 : 0 ); reset(); }
function show_D        (x) { document.CC3D.setNodeEventIn( 'D',        'hide', d        = ((x==2) || (x==3)) ? (x-2) : ( d        == 0 ) ? 1 : 0 ); reset(); }
function show_F        (x) { document.CC3D.setNodeEventIn( 'F',        'hide', f        = ((x==2) || (x==3)) ? (x-2) : ( f        == 0 ) ? 1 : 0 ); reset(); }
function show_G        (x) { document.CC3D.setNodeEventIn( 'G',        'hide', g        = ((x==2) || (x==3)) ? (x-2) : ( g        == 0 ) ? 1 : 0 ); reset(); }
function show_H        (x) { document.CC3D.setNodeEventIn( 'H',        'hide', h        = ((x==2) || (x==3)) ? (x-2) : ( h        == 0 ) ? 1 : 0 ); reset(); }
function show_I        (x) { document.CC3D.setNodeEventIn( 'I',        'hide', i        = ((x==2) || (x==3)) ? (x-2) : ( i        == 0 ) ? 1 : 0 ); reset(); }


//---------------------------------------------------------------------------------------
// show/hide/toggle visibility of parts
//---------------------------------------------------------------------------------------


// reset toggle buttons
//---------------------

function reset() 
{
  document.drive_sp.src = './img/white.png';
  document.drive_ep.src = './img/white.png';
  document.drive_cp.src = './img/white.png';
  document.drive_mp.src = './img/white.png';
}


// show, hide or toggle all wheel assemblies
//------------------------------------------

function show_or_hide_or_toggle_all ( x ) 
{ 
  show_O ( x ); 
  show_P ( x ); 
  show_N ( x ); 
  show_M ( x ); 
  show_L ( x ); 

  show_A ( x ); 

  show_B0_B1_B2 ( x ); 
  show_B3 ( x ); 
  show_Q ( x ); 

  show_E2_E5 ( x ); 
  show_E3_E4 ( x ); 
  show_E6_E1 ( x ); 

  show_K1_K2 ( x ); 


  show_C ( x ); 
  show_D ( x ); 

  show_F ( x ); 
  show_G ( x ); 
  show_H ( x ); 
  show_I ( x ); 

  reset();
}


//---------------------------------------------------------------------------------------
// show drive trains
//---------------------------------------------------------------------------------------

function show_drive_sp()
{
  show_or_hide_or_toggle_all( 3 );            // hide all

	document.CC3D.setNodeEventIn( 'SEQ_SP',
                                'start', 
                                 0        );  // start the sequencer

  document.drive_sp.src = './img/green.png';  // show on web page
}


function show_drive_ep()
{
  show_or_hide_or_toggle_all( 3 );

	document.CC3D.setNodeEventIn( 'SEQ_EP',
                                'start', 
                                 0        );  // start the sequencer
  document.drive_ep.src = './img/green.png';
}


function show_drive_cp()
{
  show_or_hide_or_toggle_all( 3 );            // force hide all

	document.CC3D.setNodeEventIn( 'SEQ_CA',
                                'start', 
                                 0        );  // start the sequencer

  document.drive_cp.src = './img/green.png';  // done: green light
}

function show_drive_mp()
{
  show_or_hide_or_toggle_all( 3 );

	document.CC3D.setNodeEventIn( 'SEQ_MP',
                                'start', 
                                 0        );  // start the sequencer

  document.drive_mp.src = './img/green.png';
}


//----------------------------------------------
// functions to 'inject keystrokes' in the model
//----------------------------------------------

function keyboard_1_key( k1 )
{
  document.CC3D.setNodeEventIn( 'CONTROL_BOX',
                                'fromPage', 
                                 k1         );
}

function keyboard_2_keys( k1, k2 )
{
	document.CC3D.setNodeEventIn( 'CONTROL_BOX',
                                'fromPage', 
                                 k1         );

	document.CC3D.setNodeEventIn( 'CONTROL_BOX',
                                'fromPage', 
                                 k2         );
}


// called once on page load
//-------------------------

function onPageLoad()
{
  readCookies();
  count();
}


// called once on page load then on timer expiry
//----------------------------------------------

function count ()
{
  getFrameRate();             // update current framerate in HTML page

  timer = 1;
  clearTimeout ( timer );
  timer = setTimeout ( "count() ", 250 );


  // try to read the viewpoints
  //---------------------------

  if ( !count_vp_done )
  {        
    count_vp();
  }
}
