///Slow & Fast Math Cats//////////////////////////////////////////////
///Copyright 2004: Wendy Petti of www.mathcats.com////////////////////
//Copyright this code: 2004 Maurici Carbó Jordi of www.nummolt.org////
///All rights reserved////////////////////////////////////////////////
///Any questions?: Ask to wpetti@mathcats.com/////////////////////////
///Made with Arachnophilia5.2  21-dic-2003 19:36:30 //////////////////
/////Dedicated to the Kansas kid//////////////////////////////////////

var True_sfObject= new Object();

function time_mice_Object(time,mice)
{
this.m_time=time;
this.m_mice=mice;
}

function fast_cat_item_speed_list(speed,time,mice)
{
this.m_speed=speed;
this.m_time_solution=time;
this.m_mice_solution=mice;
}

function sfObject(Drop1_Opt)
{

this.m_Drop1_Opt=Drop1_Opt;

this.m_Drop3_List=new Object(60);
this.m_index3=1;

this.m_Drop3_Opt=0;

this.m_timer=0;
this.m_ok=0;
this.m_resultandhint=0;

this.m_speed_slow=Drop1_Opt; ///in minutes///
this.m_time_after=0; ///in seconds////
this.m_speed_fast=0; ///in minutes////
this.m_experiment_timer=0; ///in seconds///
this.m_experiment_scale=10; ///in seconds///

this.m_kindhint=0;


this.p_newlist=pnl;
	function pnl(xltais)
		{
		this.m_index3=1;
		for(var x=this.m_Drop1_Opt+1;x<100;x++)
			{
			if((((x/60)*xltais)/(x/60-this.m_Drop1_Opt/60)-Math.floor(((x/60)*xltais)/(x/60-this.m_Drop1_Opt/60)))<=0.0001)
				{
				var y=Math.floor(((x/60)*xltais)/(x/60-this.m_Drop1_Opt/60));
				if (((y>0)&&(y<=720))&&(this.m_Drop1_Opt*y/60==Math.floor(this.m_Drop1_Opt*y/60)))
					{
///					alert("mice/Minute2= "+x+" Time: "+y+" Mice: "+this.m_Drop1_Opt*y/60);
					var z=Math.round(this.m_Drop1_Opt*y/60);
  					this.m_Drop3_List[this.m_index3]=new fast_cat_item_speed_list(x,y,z)
  					this.m_index3++;
					}
				
				}			
			}
		}
}

///////////////////////////INSTANCE//////////////////////////////////////
function init()
{
True_sfObject=new sfObject(document.FastSlow.slowcat_drop.selectedIndex);
document.FastSlow.timeafter_drop.selectedIndex=0;
True_sfObject.m_timer=setInterval('heart()',1000);
}
///////////////////////END INSTANCE//////////////////////////////////////

//////////////////////////HEART//////////////////////////////////////////

function heart()
{
if (True_sfObject.m_ok==1)
	{
	if(document.FastSlow.startbutton.value!="Start simulation")
		{
		document.FastSlow.startbutton.value="Start simulation";	
		}
	}
else
	{
	if (True_sfObject.m_ok==2)
		{
		if(document.FastSlow.startbutton.value!="Stop")
			{
			document.FastSlow.startbutton.value="Stop";
			}
		newstep();
		}
	else
		{
   	if (True_sfObject.m_ok==3)
   		{
   		if(document.FastSlow.startbutton.value!="Continue simulation")
   			{	
   			document.FastSlow.startbutton.value="Continue simulation";
   			}
   		}
   	else
   		{
			if(document.FastSlow.startbutton.value!="--------------")
				{
				document.FastSlow.startbutton.value="--------------";
   			}
   		}	
		}
	}
}

function newstep()
{
True_sfObject.m_experiment_timer+=True_sfObject.m_experiment_scale;
var ournewrow=True_sfObject.m_experiment_timer+" sec. ";
var msb=True_sfObject.m_experiment_timer*True_sfObject.m_speed_slow/60;
var mfb=(True_sfObject.m_experiment_timer-True_sfObject.m_time_after)*True_sfObject.m_speed_fast/60;
var miceslowbasket=Math.floor(msb);
var micefastbasket=Math.floor(mfb);
if ((miceslowbasket==micefastbasket)&&(msb!=mfb))
	{
	miceslowbasket=Math.floor(msb*10)/10;
	micefastbasket=Math.floor(mfb*10)/10;
	if ((miceslowbasket==micefastbasket)&&(msb!=mfb))
		{
		miceslowbasket=Math.floor(msb*100)/100;
		micefastbasket=Math.floor(mfb*100)/100;
		}
	}
if(micefastbasket<0){micefastbasket=0;}
ournewrow+=miceslowbasket+" mice ";
if(msb>mfb){ournewrow+=" > ";}
if(msb==mfb){ournewrow+=" = ";}
if(msb<mfb)
	{
	ournewrow+=" < ";
	if(True_sfObject.m_resultandhint==0)
		{
		document.FastSlow.equation.value="Solution:\n";
		document.FastSlow.equation.value+="Slow cat time= "+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution+" seconds\n";
		document.FastSlow.equation.value+="Fast cat time= "+(True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution-True_sfObject.m_time_after)+" seconds\n";
		document.FastSlow.equation.value+="At each basket: "+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_mice_solution+" mice\n";
		if(True_sfObject.m_kindhint>0)
			{
			document.FastSlow.equation.value+="Your hint: \n";  ///////////////////////////aqui m'he fet un lio/////////
			if(document.FastSlow.my_hint.value==""){document.FastSlow.my_hint.value="0";}
			var hintvalue=parseInt(document.FastSlow.my_hint.value);
			if(True_sfObject.m_kindhint==1)
				{
				document.FastSlow.equation.value+="Slow cat seconds: ";
				document.FastSlow.equation.value+=hintvalue+"\n";
				document.FastSlow.equation.value+="Simulation results: "+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution;
				document.FastSlow.equation.value+=" seconds\n";
				if(hintvalue==True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution){congratulations();}else{line();}
				}
			if(True_sfObject.m_kindhint==2)
				{
				document.FastSlow.equation.value+="Mice at each basket: ";
				document.FastSlow.equation.value+=hintvalue+"\n";
				document.FastSlow.equation.value+="Simulation results: "+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_mice_solution;
				document.FastSlow.equation.value+=" mice\n";
				if(hintvalue==True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_mice_solution){congratulations();}else{line();}
				}
			if(True_sfObject.m_kindhint==3)
				{
				document.FastSlow.equation.value+="Fast cat seconds: ";
				document.FastSlow.equation.value+=hintvalue+"\n";
				document.FastSlow.equation.value+="Simulation results: "+(True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution-True_sfObject.m_time_after);
				document.FastSlow.equation.value+=" seconds\n";
				if(hintvalue==(True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution-True_sfObject.m_time_after)){congratulations();}else{line();}
				}
			}
		True_sfObject.m_resultandhint=1;
		}
	}
ournewrow+=micefastbasket+" mice ";
document.FastSlow.experiment.value=ournewrow+"\n"+document.FastSlow.experiment.value;
if(True_sfObject.m_experiment_timer>720){True_sfObject.m_ok=1;True_sfObject.m_experiment_timer=0;}

var general_minute=Math.floor(True_sfObject.m_experiment_timer/60);
var general_second=True_sfObject.m_experiment_timer%60;
var magenta_minute=Math.floor((True_sfObject.m_experiment_timer-True_sfObject.m_time_after)/60);
var magenta_second=(True_sfObject.m_experiment_timer-True_sfObject.m_time_after)%60;
if(magenta_minute<0){magenta_minute=0;magenta_second=0;}

document.fs.IGC(general_minute,general_second);
document.fs.ICC(general_minute,general_second);
document.fs.IMC(magenta_minute,magenta_second);
document.fs.MC(miceslowbasket,micefastbasket);
document.fs.nstnt(True_sfObject.m_experiment_timer);
document.fs.rfr();
}

function congratulations()
{
document.FastSlow.equation.value+="!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
document.FastSlow.equation.value+=" Congratulations! Right result!! \n";
document.FastSlow.equation.value+="!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}
function line()
{
document.FastSlow.equation.value+="*****************************\n";
document.FastSlow.equation.value+="Calculations:\n";
document.FastSlow.equation.value+="("+True_sfObject.m_Drop1_Opt+"/60)*"+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution;
document.FastSlow.equation.value+="seconds="+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_mice_solution+"mice\n";
document.FastSlow.equation.value+="("+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_speed+"/60)*"+(True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_time_solution-True_sfObject.m_time_after);
document.FastSlow.equation.value+="seconds="+True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_mice_solution+" mice";

}

////////////////////////END HEART////////////////////////////////////////

///////////////////////////INTERFACE/////////////////////////////////////

function slowcat_DropDown()
{
if(document.FastSlow.slowcat_drop.selectedIndex!=0)
	{
	True_sfObject=new sfObject(document.FastSlow.slowcat_drop.selectedIndex);
   document.FastSlow.fastcat_drop.selectedIndex=0;
   document.FastSlow.timeafter_drop.selectedIndex=0;
	document.FastSlow.rate[3].checked=1;
	True_sfObject.m_experiment_scale=10;

	document.FastSlow.hints_drop.selectedIndex=0;
	hints();
	True_sfObject.m_ok=0;

	elect=document.FastSlow.slowcat_drop.selectedIndex;
	document.fs.scs(elect);
   document.fs.scf(0);
   document.fs.tca(0);
	document.fs.rfr();
	}
} 

function timeafter_DropDown()
{
if(document.FastSlow.timeafter_drop.selectedIndex!=0)
	{
	var ltais=document.FastSlow.timeafter_drop.selectedIndex*10;
	document.FastSlow.fastcat_drop.selectedIndex=0;
	document.FastSlow.rate[3].checked=1;
	True_sfObject.m_experiment_scale=10;
	
	document.FastSlow.hints_drop.selectedIndex=0;
	hints();
	True_sfObject.m_ok=0;
			
	document.fs.scf(0);
	document.fs.tca(ltais);
	True_sfObject.m_time_after=ltais;
	
	True_sfObject.p_newlist(ltais);
	var elect=document.FastSlow.slowcat_drop.selectedIndex;
	document.fs.scs(elect);
	document.fs.rfr();
	for (var n=1;n<60;n++)
		{
		if(n<True_sfObject.m_index3)
			{
			document.FastSlow.fastcat_drop.options[n].text=True_sfObject.m_Drop3_List[n].m_speed+" mice/minute";
			}
		else
			{
			document.FastSlow.fastcat_drop.options[n].text="------------";
			}
		}
	}
}

function fastcat_DropDown()
{
if(True_sfObject.m_index3>1)
	{
	if((document.FastSlow.fastcat_drop.selectedIndex!=0)&&(document.FastSlow.fastcat_drop.selectedIndex<True_sfObject.m_index3))
		{	
		True_sfObject.m_Drop3_Opt=document.FastSlow.fastcat_drop.selectedIndex;
		True_sfObject.m_speed_fast=True_sfObject.m_Drop3_List[True_sfObject.m_Drop3_Opt].m_speed;
		document.fs.scf(True_sfObject.m_speed_fast);
		document.fs.rfr();
		True_sfObject.m_ok=1;
		True_sfObject.m_experiment_timer=0; ///////////last line added
		}
	else
		{
		alert("This is not a valid option");
		document.FastSlow.fastcat_drop.selectedIndex=0;
		True_sfObject.m_ok=0;

		}
	}
else
	{
	alert("No options: Select *time after* before");
	document.FastSlow.fastcat_drop.selectedIndex=0;
	True_sfObject.m_ok=0;
	}

document.FastSlow.rate[3].checked=1;
True_sfObject.m_experiment_scale=10;

document.FastSlow.hints_drop.selectedIndex=0;
hints();
} 

function hints()
{
if (document.FastSlow.hints_drop.selectedIndex!=0)
	{
   if(document.FastSlow.hints_drop.selectedIndex==1){document.FastSlow.result_unit.value="seconds";}
   if(document.FastSlow.hints_drop.selectedIndex==2){document.FastSlow.result_unit.value="mice";}
   if(document.FastSlow.hints_drop.selectedIndex==3){document.FastSlow.result_unit.value="seconds";}
   document.FastSlow.my_hint.value="";

   }
else
   {
   document.FastSlow.result_unit.value="------";
   document.FastSlow.my_hint.value="No hint";
   True_sfObject.m_kindhint=0;
   }
True_sfObject.m_kindhint=document.FastSlow.hints_drop.selectedIndex;
}

function startapp()
{
if (True_sfObject.m_ok==1)
	{
	/////start simulation/////
	True_sfObject.m_ok=2;
	document.FastSlow.experiment.value="";
	document.FastSlow.equation.value="";
	True_sfObject.m_resultandhint=0;
	}
else
	{
   if (True_sfObject.m_ok==2)
   	{
   	/////stop simulation////
   	True_sfObject.m_ok=3;
   	}
	else
		{
      if (True_sfObject.m_ok==3)
      	{
      	///re-start simulation////
      	True_sfObject.m_ok=2;
      	}
		}
	}
}

function radio_sim(numero)
{
True_sfObject.m_experiment_scale=numero;
}

//////////////////////////END INTERFACE//////////////////////////////////////

