
/* Javascript - animator_once.js

Originally based on the Ulven script. Modified February 2008 by P.G. van de Veen. Abreviated in August 2008 by LFS.

animator_once.js = function toggle() does a single animation from t0=0 to t0=1.

Definitions/description of the functions: 
1.	  toggle()    Starts and Stops Animation.   button: <input type="button" value="Start/Stop"  onclick="toggle();"> 
2.    Startover() Stops counter and send t0=0.0 to the .ggb    button: <input type="button" value="Reset"  onclick="Startover();">

** The Restriction Javascript of Mathmagic was added on 28 March 2008

Increase value of Delay = Slower animation (Delay=20 means 100 animations takes 3 seconds)
Decrease value of Step = More animation steps (Step=0.01 means 100 animations)
*/

//Globals
	Ttimer=null; T=0; tt=1; Trunning=false; 	
	Delay=20; Step=0.01; 

window.onload=Activation

//-----------------
function toggle() {
//----------------- Called by Start/Stop button
	var applet=document.Animated;
	T=applet.getValue("t0");
	if(Trunning){
		     Trunning=false;
		     setTimeout("clearInterval(Ttimer)",0);			//Stop Timer in 0 ms, that is now
		    }else{
		     Trunning=true;
		     Ttimer=setInterval("Singlestep()",Delay);		//Timer calls onestep() every delay ms
	}//if
}//toggle()

//---------------------
function Singlestep() {
//--------------------- Animates one step. Not directly called by HTML
	var applet=document.Animated;
	if(T<1.0) {
 		     T=T+Step;
		     applet.evalCommand("t0="+T);						
		    }else{
		    // T=0.0;	
	}//if
}//Singlestep()

//----------------
function Startover() {															
//----------------  Called by Reset button
	var applet=document.Animated;
	setTimeout("clearInterval(Ttimer)",0);			//Stop Timer immediately
	Trunning=false;	T=0;
	applet.evalCommand("t0=0");
	applet.refreshViews();
}//Startover()


//-----------------------
function Activation(){	
//----------------------- Activates the Listener and calls Restrict
	var applet=document.Animated;
tt=1;
applet.registerObjectUpdateListener("A_{flag}", "Restrict");
applet.registerObjectUpdateListener("B_{flag}", "Restrict");
applet.registerObjectUpdateListener("C_{flag}", "Restrict");	
applet.registerObjectUpdateListener("D_{flag}", "Restrict");
applet.registerObjectUpdateListener("E_{flag}", "Restrict");
}//Activation()	

//-----------------------
function Restrict(N_flag)  {
//-----------------------
	var applet=document.Animated;
	N_flag=""+N_flag; //This is important for Okay in Firefox!
	N_point=N_flag.substring(0,N_flag.length-7);
	flag=applet.getValue(N_flag);
    xnew=applet.getXcoord(N_point);		ynew=applet.getYcoord(N_point);
	xold=applet.getValue("x"+N_point);	yold=applet.getValue("y"+N_point);
	
	if (xnew!=xold || ynew!=yold)  	{
		if (flag>0){
						tt=1;
						applet.evalCommand("x"+N_point+"="+xnew);
						applet.evalCommand("y"+N_point+"="+ynew);
					}
		else		{
						tt=0.5*tt;
				    	if (tt>0.0005){
				    				xtemp=(1-tt)*xold+tt*xnew;
				    				ytemp=(1-tt)*yold+tt*ynew;
				    				applet.evalCommand(""+N_point+"=("+xtemp+","+ytemp+")");
				    			}
				    	else   {
				    			tt=1;
				    			applet.evalCommand(""+N_point+"=("+xold+","+yold+")");
				    			}
								}
				}		
}//Restrict()

