// originally from Director ( using Lingo ), then converted to Flash AScript, then brought into here, JS;
// see bottom of this page for examples;


//alert("gvo_timeout");
var gvo_timeout         = new Object() ;
gvo_timeout.pm_initiate = function ( )
  { //gf_ri("gvo_timeout.pm_initiate");
    this.pvi_fps       = 12   ; //////////////////////////////// 24
    this.pvo_timeout   = null ; 
    // will not be set until something registers itself; 
	//  and will remain running from there on out;
	// setInterval's do not make their first call immediately, but wait the amount of time established ;
  
    this.pvl_advancers = { } ; // [ #name : [ path , function       ] ]
    this.pvl_pausers   = { } ; // [ #name : [ path , function , ftg ] ] -- frames to go

    //--------------------------------------------------------
	this.pm_register = function( args_which , argn_TO , argr_object , argn_method , argi_ms , arga_params )
      { 
//	    gf_ri("timeout.register : " + args_which  ) ;
//        gf_ri("timeout.register : " + argn_TO     ) ;
//        gf_ri("timeout.register : " + argr_object ) ;
//        gf_ri("timeout.register : " + argn_method ) ;
//        gf_ri("timeout.register : " + argi_ms     ) ;
//        gf_ri("timeout.register : " + arga_params ) ;
        //alert("timeout.register : " + argn_TO + " , " + arga_params ) ;
        if      ( args_which == "advancer" )
          { this.pvl_advancers[ argn_TO ] = [ argr_object , argn_method , arga_params           ] ; // , arga_params ] ;
		  }
        else if ( args_which == "pauser" ) 
          { //alert("timeout.register : " + argn_TO + " , " + argn_method + " , " + argi_ms + " , " + arga_params ) ;
		    var lvi_ftg = this.pm_calculate_number_of_frames ( argi_ms ) ;// frames to go
            if ( lvi_ftg == 0 ) 
			  { lvi_ftg = 1 ; // sometime a 0 second pause is wanted ( usually for protocol's sake )
			  } ;
			this.pvl_pausers  [ argn_TO ] = [ argr_object , argn_method , arga_params , lvi_ftg ] ;
    
		  } ; 
		
		if ( this.pvo_timeout == null )
		  { //gf_ri("timeout.pm_register , set the timeout");
		    var lvi_ms       = 1000 / this.pvi_fps                                         ;
			this.pvo_timeout = window.setInterval( "gvo_timeout.pm_routine()" ,  lvi_ms  ) ; // 83
		  };
	  };
    

    //--------------------------------------------------------
	this.pm_forget = function( argn_timout )
      { //alert(  "ps_timeout.forget , argn_timeout   : " + argn_timout )
        // put "ps_timeout.forget , pvl_pausers    : " & this.pvl_pausers 
        // put "ps_timeout.forget , pvl_advancers  : " & this.pvl_advancers
  
          if ( this.pvl_pausers[ argn_timout ] == null ) 
            { // don't do anything
			}
	      else
	        { //alert( "ps_timeout.forget , kill pauser   : " + argn_timout      )
              //gf_ri( "ps_timeout.forget , kill pauser A : " & this.pvl_pausers )
              delete this.pvl_pausers[ argn_timout ] ;
              //put "ps_timeout.forget , kill pauser B : " & this.pvl_pausers    
			} ;
  
          if ( this.pvl_advancers [ argn_timout ] == null ) 
	        { // don't do anything
			}
	      else
	        { //put "ps_timeout.forget , kill advancer   : " & argn_timout
              //put "ps_timeout.forget , kill advancer A : " & this.pvl_advancers
              delete this.pvl_advancers[ argn_timout ] ;
              //put "ps_timeout.forget , kill advancer B : " & this.pvl_advancers
    
			} ;  
	  } ;
	

    //--------------------------------------------------------
    //this.pvb_temp_for_testing = true ; // just for alert()
	//this.pvi_temp_for_testing_max = 10    ; // just for alert()
	//this.pvi_temp_for_testing_min = 0    ; // just for alert()
	
	this.pm_decrease_pausers  = function ( )  
      { //alert ( "ps_timeout.decrease_pausers , total pausers : " + this.pm_calculate_number_of_properties(this.pvl_pausers) ) ;
	    for ( var lvi_i in this.pvl_pausers )
		  { var lvn_TO   = lvi_i                      ;
            var lva      = this.pvl_pausers[ lvn_TO ] ;
            
            var lvi_ftg = lva[ 3 ] - 1                ;  // frames_to_go
            //put "ps_timeout.decrease_pausers , name : " & lvn_prop
            //alert ( "ps_timeout.decrease_pausers  : " + lvn_TO + " , " + lvi_ftg ) ;
    
            this.pvl_pausers[ lvn_TO ][ 3 ] = lvi_ftg ;
            //if ( lva[1] == "pm_turn_on_go_button" && this.pvb_temp_for_testing ) 
			//if ( lva[1] == "pm_turn_on_go_button" &&   (  lvi_ftg > this.pvi_temp_for_testing_min && lvi_ftg < this.pvi_temp_for_testing_max ) )
			//  { this.pvb_temp_for_testing = false ;
			//	//alert ( "ps_timeout.decrease_pausers  : " + lvn_TO + " , " + lva ) ;
			//  } ;
    
            if ( lvi_ftg == 0 )
              { //put "ps_timeout.decrease_pausers , transfer to advancers : " & lvn_prop
		        this.pm_callback( lva , lvn_TO ) ;
				//delete this.pvl_pausers[ lvn_TO ] ;
				this.pm_forget( lvn_TO ) ;
			  } ;    
		  } ;  
	  } ;
	
	//--------------------------------------------------------
    this.pm_calculate_number_of_frames = function ( argi_milliseconds )
      { //gf_ri( "ps_timeout.calculate , ms    : " + argi_milliseconds ) ;
        //put "ps_timeout.calculate , tempo : " & mcvo.pvi_fps
        // 
		//  actuator_scale_object.js uses this also, to calculate frames for animations
		
		var lvi_frames = Math.round( ( argi_milliseconds / 1000 ) * this.pvi_fps ) ;
		return lvi_frames ;      
	  } ;
    
	//--------------------------------------------------------
    this.pm_calculate_number_of_properties = function ( argr_object )
      { var lvi_length = 0 ;
	    for ( var lvi_i in argr_object )
	      { lvi_length++ ;
		  };
		return lvi_length ;
	  } ;
    
	//--------------------------------------------------------
	this.pm_routine = function ( ) 
      { //gf_ri( "timeout.routine , advancers : " + this.pvl_advancers ) ;
	    for ( var lvi_i   in   this.pvl_advancers )
	      { //trace("timeout.onenterframe , pvl_advancers : " + lvi_i );
		    var lvn_TO       = lvi_i                                    ;     
            var lva          = this.pvl_advancers[ lvn_TO ]             ;
            
			this.pm_callback( lva , lvn_TO ) ;
			
		  } ;  
        
        this.pm_decrease_pausers (  )  ; 
	  };
		
	//--------------------------------------------------------
    this.pm_callback = function( arga , argn_TO )
	  {
			var lvr_object   = arga[ 0 ]                                 ;
            var lvn_method   = arga[ 1 ]                                 ;        
            var lva_params   = arga[ 2 ]                               ;  
			//alert ( "ps_timeout.pm_callback : " + argn_TO + " , " + lvr_object + " , " + lvn_method + " , " + lva_params ) ;
			lvr_object[ lvn_method ].call( lvr_object , lva_params )  ;
			
	  } ; 
	
	
	//--------------------------------------------------------
  };  

gvo_timeout.pm_initiate();

//--------------------------------------------------------------------------------
// EXAMPLES OF REGISTERING
/*
	var lvs_which   = "advancer"   ;
    var lvn_booger_TO      = "the_booger"  ;
    var lvr_object  = this         ;
    var lvn_method  = "pm_booger" ;
    var lvi_ms      = -1           ;
    var lva_params  = [ "a" , "b" , "c" ]          ;
    gvo_timeout.pm_register( lvs_which , lvn_booger_TO , lvr_object , lvn_method , lvi_ms , lva_params ) ;
	
	var lvi_boogers = 0 ;
	function pm_booger()
	  { document.getElementById("display" ).innerHTML += "pm_booger<br/>" ;
	    lvi_boogers++;
		if ( lvi_boogers == 5 )
		  {  gvo_timeout.pm_forget( lvn_booger_TO );
		  }
	  }
	
	
	var lvs_which   = "advancer"   ;
    var lvn_fatboy_TO      = "the_fatboy"  ;
    var lvr_object  = this         ;
    var lvn_method  = "pm_fatboy" ;
    var lvi_ms      = -1           ;
    var lva_params  = [ "a" , "b" , "c" ]          ;
    gvo_timeout.pm_register( lvs_which , lvn_fatboy_TO , lvr_object , lvn_method , lvi_ms , lva_params ) ;
	
	var lvi_fatboys = 0 ;
	function pm_fatboy()
	  { document.getElementById("display" ).innerHTML += "pm_fatboy<br/>" ;
	    lvi_fatboys++;
		if ( lvi_fatboys == 10 )
		  {  gvo_timeout.pm_forget( lvn_fatboy_TO );
		  }
	  }
	
	
	
	var lvs_which   = "pauser"   ;
    var lvn_mouse_TO      = "the_mouse"  ;
    var lvr_object  = this         ;
    var lvn_method  = "pm_mouse" ;
    var lvi_ms      = 5000          ;
    var lva_params  = [ "a" , "b" , "c" ]          ;
    gvo_timeout.pm_register( lvs_which , lvn_mouse_TO , lvr_object , lvn_method , lvi_ms , lva_params ) ;
	
	var lvi_mouses = 0 ;
	function pm_mouse()
	  { document.getElementById("display" ).innerHTML += "pm_mouse<br/>" ;
	    lvi_mouses++;
		if ( lvi_mouses == 3 )
		  {  gvo_timeout.pm_forget( lvn_mouse_TO );
		  }
	  }

*/ 
  
  
  
  
  
  
  
  
  
  
  
  
  

