var alertTimerId = 0;
var temp1,temp2,temp3,temp4;

var factor1=3, factor2=5;
var end_animation_now=false;

var feedback_timer=0; number_to_show=0;
var rank=4, finishrank=0, averagerank=0;
var timer;
var x=0;
var x_to_stop=-8000;
var level=1;
var trial=1;
var number_to_analyze=Math.floor(Math.random()*900+199);
var a=0, b=0;
var temprand;
var distance;
var competitor_speed_factor=1;
var during_end_of_stage_period=false;

var obj_boat=new Array(100);
var trial=1;
var mousex=0, mousey=0;
var ready_for_a_new_trial="false";
var number_to_extract_temp=100;
var sentence1="", sentence2="";
var str1=" ", str2=" ", str_end_of_stage=" ";
var number1, number2;
var stage=1;
var trialminus1=0;
var number_animation_time_counter=0;
var number_animation_time_counter_active="false";
var rand1,rand2,rand3,rand4,repeatthis,number1,number2,number3,number4;
var mishtaneezer;
var dontshow=false;
var score=0;
var distance=0;
var targetvalue=Math.floor(Math.random()*900+199);

var aySound = new Array();


	picbackground= new Image(1152,864); picbackground.src="./speedboat_images/panoramic-seattle1.jpg";

	pic1= new Image(100,100); pic1.src="./speedboat_images/raceboat.gif";
	pic2= new Image(100,100); pic2.src="./speedboat_images/raceboat-yellow.gif";
	pic3= new Image(100,100); pic3.src="./speedboat_images/raceboat-green.gif";
	pic4= new Image(100,100); pic4.src="./speedboat_images/raceboat-blue.gif";

	pic5= new Image(100,100); pic5.src="./speedboat_images/winning_flag.gif";


	pica1=new Image(87,32); pica1.src="./speedboat_images/1.jpg";
	pica1h=new Image(88,31); pica1h.src="./speedboat_images/1h.jpg";
	pica10=new Image(87,31); pica10.src="./speedboat_images/10.jpg";
	pica10h=new Image(88,31); pica10h.src="./speedboat_images/10h.jpg";
	pica100=new Image(87,31); pica100.src="./speedboat_images/100.jpg";
	pica100h=new Image(88,31); pica100h.src="./speedboat_images/100h.jpg";
	pica1000=new Image(87,31); pica1000.src="./speedboat_images/1000.jpg";
	pica1000h=new Image(88,32); pica1000h.src="./speedboat_images/1000h.jpg";
	picminus=new Image(35,32); picminus.src="./speedboat_images/minus.jpg";
	picminush=new Image(35,31); picminush.src="./speedboat_images/minush.jpg";




function init_main(temp){

level=temp;

//	document.onmousemove=handlemousemove;
//	document.onmousedown=handlemousedown;
//	document.onmouseup=handlemouseup;

	document.getElementById("div_instructions").style.visibility="hidden";
	document.getElementById("div_imgthumb").style.visibility="hidden";
	document.getElementById("div_linkbacktomenu").style.visibility="hidden";
	document.getElementById("div_linkbacktomathgames").style.visibility="hidden";

	document.body.style.background="black left no-repeat fixed " ; 




	init_this_task_variables();



}

function init_this_task_variables(){


	finishrank=0;
	during_end_of_stage_period=false;


	document.getElementById("img_backgroundimage1").src=picbackground.src;
	document.getElementById("img_backgroundimage2").src=picbackground.src;

	document.getElementById("img_boat1").src=pic1.src;
	document.getElementById("img_boat2").src=pic2.src;
	document.getElementById("img_boat3").src=pic3.src;
	document.getElementById("img_boat4").src=pic4.src;

	document.getElementById("img_winning_flag").src=pic5.src;

	document.getElementById("txbx_first_number").src=pica1.src;
	document.getElementById("txbx_second_number").src=pica10.src;






	document.getElementById("div_btnrun").style.visibility="hidden";
		document.getElementById("div_btnrun1").style.visibility="hidden";
			document.getElementById("div_btnrun2").style.visibility="hidden";

	document.getElementById("div_play_easy").style.visibility="hidden";
		document.getElementById("div_play_medium").style.visibility="hidden";
			document.getElementById("div_play_hard").style.visibility="hidden";


	document.getElementById("div_exittomenu").style.visibility="visible";
//	document.getElementById("div_score1").style.visibility="visible";

	
	document.getElementById("div_backgroundimage1").style.visibility="visible";
	document.getElementById("div_backgroundimage2").style.visibility="visible";
	document.getElementById("div_boat1").style.visibility="visible";
	document.getElementById("div_boat2").style.visibility="visible";
	document.getElementById("div_boat3").style.visibility="visible";
	document.getElementById("div_boat4").style.visibility="visible";

	document.getElementById("div_winning_flag").style.visibility="visible";

	document.getElementById("div_first_number").style.visibility="visible";

	document.getElementById("div_second_number").style.visibility="visible";


	document.getElementById("div_speed").style.visibility="visible";
	document.getElementById("txbx_speed").value=targetvalue;
	document.getElementById("div_distance").style.visibility="visible";
	document.getElementById("div_rank").style.visibility="visible";
	document.getElementById("txbx_rank").value=rank;


	obj_background1=new obj_prototype;
	obj_background2=new obj_prototype;

	obj_background1.x=0;
	obj_background2.x=7990;

	obj_background1.movex=0;
	obj_background2.movex=0;


	for (i=1;i<5 ;i++ )	{
			obj_boat[i]=new obj_prototype();
			obj_boat[i].x=50;

	}

	obj_boat[1].y=440;
	obj_boat[2].y=470;
	obj_boat[3].y=500;
	obj_boat[4].y=530;



	obj_winning_flag=new obj_prototype;
	obj_winning_flag.x=10000;  //7080   15000-60;
	obj_winning_flag.y=430;


	create_numbers();

	run_main_animation();


}


function assign_divs_and_images_to_objects(){ 


			document.getElementById("div_backgroundimage1").style.left=obj_background1.x;

			document.getElementById("div_backgroundimage2").style.left=obj_background2.x;

	for (i=1;i<5 ;i++ )	{
				document.getElementById("div_boat"+i).style.left=obj_boat[i].x;
				document.getElementById("div_boat"+i).style.top=obj_boat[i].y;

	}

			document.getElementById("div_winning_flag").style.left=obj_winning_flag.x;
			document.getElementById("div_winning_flag").style.top=obj_winning_flag.y;


		document.getElementById("txbx_speed").value=" Current Speed: "+Math.floor(obj_background1.movex*factor2+0.999);	
		document.getElementById("txbx_distance").value=" Distance Left: "+Math.floor(distance/100);	
		if(finishrank==0) {document.getElementById("txbx_rank").value=" Current Rank: "+rank;}
			else {document.getElementById("txbx_rank").value=" Current Rank: "+finishrank;}

}



function obj_prototype(){

	this.x=Math.floor(Math.random()*650+100);
	this.y=Math.floor(Math.random()*400+10);
	this.xnumber=42;
	this.ynumber=46;
	this.numbervalue=4837;
	this.movex=1;
	this.movey=0;
	this.istarget=false;
	this.animationstage=0;
	this.timer1=100;
	this.moveacceleration=0;
	this.sizex=142;
	this.sizey=68;

}


function run_main_animation(){



	if(obj_background1.x<x_to_stop){obj_background1.x=7970}
	if(obj_background2.x<x_to_stop){obj_background2.x=7970}

	obj_background1.x=obj_background1.x-obj_background1.movex;
	obj_background2.x=obj_background2.x-obj_background2.movex;

	obj_winning_flag.x=obj_winning_flag.x-obj_background2.movex;

	distance=Math.floor(obj_winning_flag.x);
	if (distance<0){ distance=0;	}

	if (obj_background1.movex>0) {obj_background1.movex=obj_background1.movex*0.999} else {obj_background1.movex=0;};
	if (obj_background2.movex>0) {obj_background2.movex=obj_background2.movex*0.999} else {obj_background2.movex=0;};


	
//	if(Math.random()*20>19.5) {obj_raceboat1.y=obj_raceboat1.y-1}
//	if(Math.random()*20>19.5) {obj_raceboat1.y=obj_raceboat1.y+1}


	for (i=2;i<5 ;i++ )	{
			if (Math.random()*20>19.8)		{obj_boat[i].movex=obj_boat[i].movex+0.1*competitor_speed_factor}
			obj_boat[i].x=obj_boat[i].x+obj_boat[i].movex;
			obj_boat[i].x=obj_boat[i].x-obj_background1.movex;	
			
	}

	rank=1;
	if (obj_boat[2].x>50){rank=rank+1};
	if (obj_boat[3].x>50){rank=rank+1};
	if (obj_boat[4].x>50){rank=rank+1};





	if(feedback_timer>0){
				document.getElementById("div_feedback").style.visibility="visible";
				mousex=0; mousey=0;
				feedback_timer=feedback_timer-1;
	
				document.getElementById("div_feedback").style.filter = 'alpha(opacity=' + feedback_timer + ')';
				document.getElementById("div_feedback").style.filter = 'alpha(opacity=' + feedback_timer + ')';


} else {
	document.getElementById("div_feedback").style.visibility="hidden";

  }


if(feedback_timer==0&&during_end_of_stage_period==false){	document.getElementById("div_first_number").style.visibility="visible";		document.getElementById("div_second_number").style.visibility="visible";	}



assign_divs_and_images_to_objects()


if (obj_boat[1].x>=obj_winning_flag.x&&during_end_of_stage_period==false){
	if (finishrank==0)	{finishrank=rank	}
	end_of_stage();

	}




if(end_animation_now==false) {var timer=setTimeout('run_main_animation()',1);}
	else { clearTimeout(timer); end_animation_now=false;}

}


function create_numbers(){

	

	if (level==1){

	do	{
			temprand=Math.floor(Math.random()*7);

			do	{
				a=Math.floor(Math.random()*9)+1;
				b=Math.floor(Math.random()*9)+1;
			}  while (a==b);

			if (temprand==0){str1=a+"+"+b;		number1=a+b;}

			if (temprand==1){str1=a+"-"+b;		number1=a-b;}
			if (temprand==2){str1=a+"-"+b;		number1=a-b;}
			if (temprand==3){str1="-"+a+"+"+b;		number1=-a+b;}
			if (temprand==4){str1="-"+a+"+"+b;		number1=-a+b;}
			if (temprand==5){str1="-"+a+"-"+b;		number1=-a-b;}
			if (temprand==6){str1="-"+a+"-"+b;		number1=-a-b;}



			temprand=Math.floor(Math.random()*7);

			do{
				a=Math.floor(Math.random()*9)+1;
				b=Math.floor(Math.random()*9)+1;
			}
			while (a==b);

			if (temprand==0){str2=a+"+"+b;		number2=a+b;}
			if (temprand==1){str2=a+"-"+b;		number2=a-b;}
			if (temprand==2){str2=a+"-"+b;		number2=a-b;}
			if (temprand==3){str2="-"+a+"+"+b;		number2=-a+b;}
			if (temprand==4){str2="-"+a+"+"+b;		number2=-a+b;}
			if (temprand==5){str2="-"+a+"-"+b;		number2=-a-b;}
			if (temprand==6){str2="-"+a+"-"+b;		number2=-a-b;}

		}
		while (number1>0&&number2>0||number1<0&&number2<0);


	}

	if (level==2)	{
		do	{
			temprand=Math.floor(Math.random()*8);

			do	{
				a=Math.floor(Math.random()*9)+1;
				b=Math.floor(Math.random()*9)+1;
			}  while (a==b);

			
			if (temprand==0){str1="(-"+a+")+"+b;		number1=-a+b;}
			if (temprand==1){str1="(-"+a+")-"+b;		number1=-a-b;}
			if (temprand==2){str1="-(-"+a+")+"+b;		number1=a+b;}
			if (temprand==3){str1="-(-"+a+")-"+b;		number1=a-b;}
			if (temprand==4){str1=a+"+(-"+b+")";		number1=a-b;}
			if (temprand==5){str1=a+"-(-"+b+")";		number1=a+b;}
			if (temprand==6){str1="-"+a+"+(-"+b+")";		number1=-a-b;}
			if (temprand==7){str1="-"+a+"-(-"+b+")";		number1=-a+b;}

			temprand=Math.floor(Math.random()*8);

			do	{
				a=Math.floor(Math.random()*9)+1;
				b=Math.floor(Math.random()*9)+1;
			}  while (a==b);

			
			if (temprand==0){str2="(-"+a+")+"+b;		number2=-a+b;}
			if (temprand==1){str2="(-"+a+")-"+b;		number2=-a-b;}
			if (temprand==2){str2="-(-"+a+")+"+b;		number2=a+b;}
			if (temprand==3){str2="-(-"+a+")-"+b;		number2=a-b;}
			if (temprand==4){str2=a+"+(-"+b+")";		number2=a-b;}
			if (temprand==5){str2=a+"-(-"+b+")";		number2=a+b;}
			if (temprand==6){str2="-"+a+"+(-"+b+")";		number2=-a-b;}
			if (temprand==7){str2="-"+a+"-(-"+b+")";		number2=-a+b;}


		}
		while (number1>0&&number2>0||number1<0&&number2<0);
	}


		if (level==3)	{
		do	{
			temprand=Math.floor(Math.random()*12);

			do	{
				a=Math.floor(Math.random()*9)+1;
				b=Math.floor(Math.random()*9)+1;
			}  while (a==b);

			
			if (temprand==0){str1="(-"+a+")+"+b;		number1=-a+b;}
			if (temprand==1){str1="(-"+a+")-"+b;		number1=-a-b;}
			if (temprand==2){str1="-(-"+a+")+"+b;		number1=a+b;}
			if (temprand==3){str1="-(-"+a+")-"+b;		number1=a-b;}
			if (temprand==4){str1=a+"+(-"+b+")";		number1=a-b;}
			if (temprand==5){str1=a+"-(-"+b+")";		number1=a+b;}
			if (temprand==6){str1="-"+a+"+(-"+b+")";		number1=-a-b;}
			if (temprand==7){str1="-"+a+"-(-"+b+")";		number1=-a+b;}

			if (temprand==8){str1="(-"+a+")+(-"+b+")";		number1=-a-b;}
			if (temprand==9){str1="(-"+a+")-(-"+b+")";		number1=-a+b;}
			if (temprand==10){str1="-(-"+a+")+(-"+b+")";		number1=a-b;}
			if (temprand==11){str1="-(-"+a+")-(-"+b+")";		number1=a+b;}

			temprand=Math.floor(Math.random()*12);

			do	{
				a=Math.floor(Math.random()*9)+1;
				b=Math.floor(Math.random()*9)+1;
			}  while (a==b);

			
			if (temprand==0){str2="(-"+a+")+"+b;		number2=-a+b;}
			if (temprand==1){str2="(-"+a+")-"+b;		number2=-a-b;}
			if (temprand==2){str2="-(-"+a+")+"+b;		number2=a+b;}
			if (temprand==3){str2="-(-"+a+")-"+b;		number2=a-b;}
			if (temprand==4){str2=a+"+(-"+b+")";		number2=a-b;}
			if (temprand==5){str2=a+"-(-"+b+")";		number2=a+b;}
			if (temprand==6){str2="-"+a+"+(-"+b+")";		number2=-a-b;}
			if (temprand==7){str2="-"+a+"-(-"+b+")";		number2=-a+b;}

			if (temprand==8){str2="(-"+a+")+(-"+b+")";		number2=-a-b;}
			if (temprand==9){str2="(-"+a+")-(-"+b+")";		number2=-a+b;}
			if (temprand==10){str2="-(-"+a+")+(-"+b+")";		number2=a-b;}
			if (temprand==11){str2="-(-"+a+")-(-"+b+")";		number2=a+b;}



		}
		while (number1>0&&number2>0||number1<0&&number2<0);
	}


	document.getElementById("txbx_first_number").value=""+str1;
	document.getElementById("txbx_second_number").value=""+str2;


	document.getElementById("txbx_distance").value=number2;	

}




function button_pressed(x){




	if (x==1)	{
			obj_background1.movex=obj_background1.movex+number1/factor2;
			obj_background2.movex=obj_background2.movex+number1/factor2;

			number_to_show=number1;

			feedback_timer=100;
			document.getElementById("div_feedback").style.top=90;		
			if(number_to_show>0) {document.getElementById("txbx_feedback").value="+"+number_to_show;}
				else {document.getElementById("txbx_feedback").value=+number_to_show;}

	}

	if (x==2)	{
			obj_background1.movex=obj_background1.movex+number2/factor2;
			obj_background2.movex=obj_background2.movex+number2/factor2;

			number_to_show=number2;
			
			feedback_timer=100;
			document.getElementById("div_feedback").style.top=130;		
			if(number_to_show>0) {document.getElementById("txbx_feedback").value="+"+number_to_show;}
				else {document.getElementById("txbx_feedback").value=+number_to_show;}
	}

	document.getElementById("div_first_number").style.visibility="hidden";		
	document.getElementById("div_second_number").style.visibility="hidden";


	playSound(0);

	create_numbers();
		
}




function end_of_stage(){
	playSound(1);
	stage=stage+1;
	averagerank=averagerank+finishrank; 
	
			during_end_of_stage_period=true;

			document.getElementById("div_first_number").style.visibility="hidden";
			document.getElementById("div_second_number").style.visibility="hidden";

			
			if (finishrank==1)	{	str_end_of_stage=" You finished this stage first place!";  if(stage!=4)  str_end_of_stage+=" Now let's try it faster.";	}
			if (finishrank==2)	{	str_end_of_stage=" You finished this stage second place."; if(stage!=4)  str_end_of_stage+=" Now let's try it faster.";	}
			if (finishrank==3)	{	str_end_of_stage=" You finished this stage third place.";  if(stage!=4)  str_end_of_stage+=" Now let's try it faster.";	}
			if (finishrank==4)	{	str_end_of_stage=" You finished this stage last place.";}

			document.getElementById("txbx_end_of_stage").value=str_end_of_stage;
			document.getElementById("div_end_of_stage").style.visibility="visible";
			if(stage!=4) {document.getElementById("div_btncontinue_to_next_stage").style.visibility="visible";}
		
	if (stage==4||stage==7||stage==10){ endgame();  }
	
}



function prepare_next_stage(){


end_animation_now=true;
during_end_of_stage_period=false;

		

	if (finishrank!=4)	{
		if(level==1)	{competitor_speed_factor=competitor_speed_factor*1.25; 	}
		if(level==2)	{competitor_speed_factor=competitor_speed_factor*1.15; 	}
		if(level==2)	{competitor_speed_factor=competitor_speed_factor*1.10; 	}
	}
	

	if(stage!=4) {document.getElementById("div_end_of_stage").style.visibility="hidden";}
	document.getElementById("div_btncontinue_to_next_stage").style.visibility="hidden";

	init_this_task_variables();



}

function endgame(){

	document.getElementById("div_first_number").style.visibility="hidden";
	document.getElementById("div_second_number").style.visibility="hidden";

	document.getElementById("div_speed").style.visibility="hidden";
	document.getElementById("div_distance").style.visibility="hidden";
	document.getElementById("div_rank").style.visibility="hidden";


	document.getElementById("div_btncontinue").style.visibility="visible";	
//	document.getElementById("div_btnrestart").style.visibility="visible";	
	document.getElementById("div_btnbacktomenu").style.visibility="visible";
	document.getElementById("div_exittomenu").style.visibility="hidden";

	document.getElementById("div_end_of_stage").style.visibility="visible";
	document.getElementById("div_sentence1").style.visibility="visible";
	document.getElementById("div_sentence2").style.visibility="visible";
	document.getElementById("txbx_sentence1").value=" You successfuly completed all "+(stage-1)+" stages!";
	temprand=Math.round((averagerank)/(stage-1)*10)/10;
	document.getElementById("txbx_sentence2").value=" Your average rank across all stages was "+temprand+".";

}


function continuegame(){

	document.getElementById("div_first_number").style.visibility="visible";
	document.getElementById("div_second_number").style.visibility="visible";
	document.getElementById("div_end_of_stage").style.visibility="visible";



	document.getElementById("div_btncontinue").style.visibility="hidden";	
	document.getElementById("div_btnrestart").style.visibility="hidden";	
	document.getElementById("div_btnbacktomenu").style.visibility="hidden";
	document.getElementById("div_sentence1").style.visibility="hidden";
	document.getElementById("div_sentence2").style.visibility="hidden";

	prepare_next_stage();

}



					// Sound Script


						var aySound = new Array();
						// Below: source for sound files to be preloaded
						aySound[0] = "./createnumber_images/Button.mp3";
						aySound[1] = "./createnumber_images/Fireworks2.mp3";

						// DO NOT edit below this line
						document.write('<BGSOUND ID="auIEContainer">')
						IE = (navigator.appVersion.indexOf("MSIE")!=-1 && document.all)? 1:0;
						NS = (navigator.appName=="Netscape" && navigator.plugins["LiveAudio"])? 1:0;
						ver4 = IE||NS? 1:0;
						onload=auPreload;

						function auPreload() {
						if (!ver4) return;
						if (NS) auEmb = new Layer(0,window);
						else {
						Str = "<DIV ID='auEmb' STYLE='position:absolute;'></DIV>";
						document.body.insertAdjacentHTML("BeforeEnd",Str);
						}
						var Str = '';
						for (i=0;i<aySound.length;i++)
						Str += "<EMBED SRC='"+aySound[i]+"' AUTOSTART='FALSE' HIDDEN='TRUE'>"
						if (IE) auEmb.innerHTML = Str;
						else {
						auEmb.document.open();
						auEmb.document.write(Str);
						auEmb.document.close();
						}
						auCon = IE? document.all.auIEContainer:auEmb;
						auCon.control = auCtrl;
						}
						function auCtrl(whSound,play) {
						if (IE) this.src = play? aySound[whSound]:'';
						else eval("this.document.embeds[whSound]." + (play? "play()":"stop()"))
						}
						function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); }
						function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); }




