﻿
var x=600;
var level=1;
var trial=1;
var number_to_analyze=Math.floor(Math.random()*900+199);

var obj=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="";
var str1=" ";
var trialminus1=0;
var number_animation_time_counter=0;
var number_animation_time_counter_active="false";
var wrong_achadot="false";
var wrong_achadot_timer=0;
var wrong_asarot="false";
var wrong_asarot_timer=0;
var wrong_meot="false";
var wrong_meot_timer=0;
var lasttemprand=8;

var delay=15;

var pic=new Array(30);

var aySound = new Array();

	pic[0]= new Image(36,69); pic[0].src="./clickthedigit_images/0_clr_e0.gif"; 
	pic[1]= new Image(36,69); pic[1].src="./clickthedigit_images/1_clr_e0.gif"; 
	pic[2]= new Image(36,69); pic[2].src="./clickthedigit_images/2_clr_e0.gif"; 
	pic[3]= new Image(36,69); pic[3].src="./clickthedigit_images/3_clr_e0.gif"; 
	pic[4]= new Image(36,69); pic[4].src="./clickthedigit_images/4_clr_e0.gif"; 
	pic[5]= new Image(36,69); pic[5].src="./clickthedigit_images/5_clr_e0.gif"; 
	pic[6]= new Image(36,69); pic[6].src="./clickthedigit_images/6_clr_e0.gif"; 
	pic[7]= new Image(36,69); pic[7].src="./clickthedigit_images/7_clr_e0.gif"; 
	pic[8]= new Image(36,69); pic[8].src="./clickthedigit_images/8_clr_e0.gif"; 
	pic[9]= new Image(36,69); pic[9].src="./clickthedigit_images/9_clr_e0.gif"; 
							  
	pic[10]= new Image(36,69); pic[10].src="./clickthedigit_images/0_clr_e0_first.gif"; 
	pic[11]= new Image(36,69); pic[11].src="./clickthedigit_images/1_clr_e0_first.gif"; 
	pic[12]= new Image(36,69); pic[12].src="./clickthedigit_images/2_clr_e0_first.gif"; 
	pic[13]= new Image(36,69); pic[13].src="./clickthedigit_images/3_clr_e0_first.gif"; 
	pic[14]= new Image(36,69); pic[14].src="./clickthedigit_images/4_clr_e0_first.gif"; 
	pic[15]= new Image(36,69); pic[15].src="./clickthedigit_images/5_clr_e0_first.gif"; 
	pic[16]= new Image(36,69); pic[16].src="./clickthedigit_images/6_clr_e0_first.gif"; 
	pic[17]= new Image(36,69); pic[17].src="./clickthedigit_images/7_clr_e0_first.gif"; 
	pic[18]= new Image(36,69); pic[18].src="./clickthedigit_images/8_clr_e0_first.gif"; 
	pic[19]= new Image(36,69); pic[19].src="./clickthedigit_images/9_clr_e0_first.gif";


   	pic[22]= new Image(120,5); pic[22].src="./clickthedigit_images/duck_swimming_left1.gif";
	pic[23]= new Image(1022,620); pic[23].src="./clickthedigit_images/background-sea1.jpg"; 




function init_main(){




//	document.onmousemove=handlemousemove;
	document.onmousedown=handlemousedown;
	document.onmouseup=handlemouseup;

//	document.getElementById("div_exittomenu").style.visibility="visible";

//	document.getElementById("image_background").src=pic[23].src;
	document.body.style.background="url('"+pic[23].src+"') black top left no-repeat fixed "

	for (i=1; i<=10; i++){
		document.getElementById("image_smiley"+i).src="./clickthedigit_images/duck_swimming_left1.gif";
	}

//	document.getElementById("div_background").style.visibility="visible";

	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.getElementById("div_exittomenu").style.visibility="visible"; 






	for (i=1; i<19; i++) {
		obj[i]=new obj_prototype();	
	}

	
	document.getElementById("div_achadot1").style.visibility="visible";
	document.getElementById("div_asarot1").style.visibility="visible";
	document.getElementById("div_meot1").style.visibility="visible";
	document.getElementById("btnrunfirsttime").style.visibility="hidden";
	document.getElementById("div_btnrunfirsttime").style.visibility="hidden";
	document.getElementById("btnrestart").style.visibility="hidden";
	document.getElementById("btnbacktomenu").style.visibility="hidden";

	
	document.getElementById("div_sentence1").style.visibility="visible";
	document.getElementById("div_sentence2").style.visibility="hidden";



	init_this_task_variables();

	assign_divs_and_images_to_objects();	

	run_main_animation();

}

function init_this_task_variables(){

	for (i=1; i<19; i++){

		// SET LOCATIONS FOR THE feedback ITEMS
		if (i<6) {obj[i].feedback_target_x=i*70;} else {obj[i].feedback_target_x=(i-5)*70;}

		if (i<6) {obj[i].feedback_target_y=430;} else {obj[i].feedback_target_y=460;}



		//EXTRACT THE NUMBERS INTO DIGITS - ACHADOT, ASAROT, MEOT
		number_to_extract_temp=obj[i].number_to_click_on;

		obj[i].achadot=number_to_extract_temp%10;
		number_to_extract_temp=number_to_extract_temp-obj[i].achadot;
		number_to_extract_temp=number_to_extract_temp/10;

		obj[i].asarot=number_to_extract_temp%10;
		number_to_extract_temp=number_to_extract_temp-obj[i].asarot;
		number_to_extract_temp=number_to_extract_temp/10;

		obj[i].meot=number_to_extract_temp%10;
		number_to_extract_temp=number_to_extract_temp-obj[i].asarot;
		number_to_extract_temp=number_to_extract_temp/10;



	}




	
}


function assign_divs_and_images_to_objects(){   //THIS IS RELEVANT TO A SPECIFIC TRIAL


		if(obj[trial].sifra_lilchots=="achadot") {str1="אחדות"}
		if(obj[trial].sifra_lilchots=="asarot") {str1="עשרות"}
		if(obj[trial].sifra_lilchots=="meot") {str1="מאות"}

		sentence2=str1;
		sentence1="נא ללחוץ על ספרת ה"+str1;	
		document.getElementById("txbx_sentence1").value=sentence1;	
		document.getElementById("txbx_sentence1").style.textAlign='left';	
		document.getElementById("txbx_sentence1").style.fontSize=38;	
		document.getElementById("txbx_sentence1").style.height=50;	
		document.getElementById("txbx_sentence1").style.color='#0000aa';	

		document.getElementById("txbx_sentence2").value=sentence2;	
		document.getElementById("txbx_sentence2").style.textAlign='center';	
		document.getElementById("txbx_sentence2").style.fontSize=30;	
		document.getElementById("txbx_sentence2").style.height=40;	
		document.getElementById("txbx_sentence2").style.color='#8888cc';
		document.getElementById("txbx_sentence2").style.background='#000022';


		
			document.getElementById("div_achadot1").style.visibility="visible";
			document.getElementById("div_achadot1").style.left=obj[trial].achadot_x;
			document.getElementById("div_achadot1").style.top=obj[trial].achadot_y; 

			for (i=0; i<10; i++){
				if (obj[trial].achadot==i) {	document.getElementById("image_achadot1").src=pic[i+10].src;} 
			}



			document.getElementById("div_asarot1").style.left=obj[trial].asarot_x;
			document.getElementById("div_asarot1").style.top=obj[trial].asarot_y; 

			for (i=0; i<10; i++){
				if (obj[trial].asarot==i) {	document.getElementById("image_asarot1").src=pic[i+10].src;} 
			}



			document.getElementById("div_meot1").style.left=obj[trial].meot_x;
			document.getElementById("div_meot1").style.top=obj[trial].meot_y; 


			for (i=0; i<10; i++){
				if (obj[trial].meot==i) {	document.getElementById("image_meot1").src=pic[i+10].src;} 
			}


		


}





function obj_prototype(){
	temprand=Math.floor(Math.random()*900+100);

	this.number_to_click_on=temprand;
	this.number_to_extract_temp=temprand;

	this.timer1=0;
	
	do{
		temprand=Math.floor(Math.random()*3);
	}
	while (temprand==lasttemprand);
	lasttemprand=temprand;



	if (temprand==0) {this.sifra_lilchots="achadot";}
	if (temprand==1) {this.sifra_lilchots="asarot";}
	if (temprand==2) {this.sifra_lilchots="meot";}

	temprandx=Math.floor(Math.random()*600+120);
	temprandy=Math.floor(Math.random()*120+140);


	this.achadot=0;
	this.achadot_x=temprandx;
	this.achadot_y=temprandy;

	this.asarot=0;
	this.asarot_x=temprandx-30;
	this.asarot_y=temprandy;

	this.meot=0;
	this.meot_x=temprandx-60;
	this.meot_y=temprandy;

	this.alafim=0;
	this.alafim_x=50;
	this.alafim_y=100;

	this.visibility="false";


	this.feedback_visibility="false";

	this.feedback_x=600;			
	this.feedback_y=600;			

	this.feedback_is_on_its_way="false";

	this.feedback_movespeed1=0;
	this.feedback_origin_x=900;
	this.feedback_origin_y=400;
	this.feedback_target_x=100;
	this.feedback_target_y=100;



}



function run_main_animation(){

	// IF THERE IS A CORRECT CLICK

	if ((checkforcontact(obj[trial].sifra_lilchots)=="true") && (ready_for_a_new_trial=="true")) {

		mousex=0; mousey=0;

		document.getElementById("div_feedback"+trial).style.visibility="visible";
		obj[trial].feedback_x=obj[trial].feedback_origin_x;
		obj[trial].feedback_y=obj[trial].feedback_origin_y;


		ready_for_a_new_trial="false";

		playSound(0);		
	
		obj[trial].feedback_is_on_its_way="true";

		animate_the_numebrs();

		// preparing the new trial
		trial=trial+1;

		
		
		obj[trial].timer1=160;
		
	}



// animating the wrong feedback	

if(wrong_achadot_timer>0){
	document.getElementById("div_answerfeedback").style.visibility="visible";
	mousex=0; mousey=0;
	wrong_achadot_timer=wrong_achadot_timer-1;

	document.getElementById("txtarea_answerfeedback").style.width="270";
	document.getElementById("txtarea_answerfeedback").value="לחצת על ספרת האחדות.\nנסה שוב.";		

		document.getElementById("div_answerfeedback").style.filter = 'alpha(opacity=' + wrong_achadot_timer + ')';
		document.getElementById("div_answerfeedback").style.filter = 'alpha(opacity=' + wrong_achadot_timer + ')';


} else {
	document.getElementById("div_answerfeedback").style.visibility="hidden";

  }


  if(wrong_asarot_timer>0){
	  
	document.getElementById("div_answerfeedback").style.visibility="visible";
	mousex=0; mousey=0;
	wrong_asarot_timer=wrong_asarot_timer-1;

	document.getElementById("txtarea_answerfeedback").style.width="270";
	document.getElementById("txtarea_answerfeedback").value="לחצת על ספרת העשרות.\nנסה שוב.";		

		document.getElementById("div_answerfeedback").style.filter = 'alpha(opacity=' + wrong_asarot_timer + ')';
		document.getElementById("div_answerfeedback").style.filter = 'alpha(opacity=' + wrong_asarot_timer + ')';

  }


if(wrong_meot_timer>0){
	
	document.getElementById("div_answerfeedback").style.visibility="visible";
	mousex=0; mousey=0;
	wrong_meot_timer=wrong_meot_timer-1;

	document.getElementById("txtarea_answerfeedback").style.width="300";
	document.getElementById("txtarea_answerfeedback").value="לחצת על ספרת המאות.\nנסה שוב.";		

		document.getElementById("div_answerfeedback").style.filter = 'alpha(opacity=' + wrong_meot_timer + ')';
		document.getElementById("div_answerfeedback").style.filter = 'alpha(opacity=' + wrong_meot_timer + ')';

	}






		// animate the elements (digits and feedbacks)

	for (i4=1; i4<18; i4++){

		if (obj[i4].timer1>1) {obj[i4].timer1=obj[i4].timer1-1;}

		if (obj[i4].timer1==1) {
			assign_divs_and_images_to_objects();			
			obj[i4].timer1=0;

			// if collected all 10 ducks - end of game

			if (i4==11) {

				document.getElementById("div_sentence1").style.visibility="hidden"; 
				document.getElementById("div_achadot1").style.visibility="hidden"; 
				document.getElementById("div_asarot1").style.visibility="hidden"; 
				document.getElementById("div_meot1").style.visibility="hidden"; 
				document.getElementById("txbx_sentence2").value="כל הכבוד! אספת בהצלחה את כל 10 הברווזונים!";	
				document.getElementById("div_sentence2").style.visibility="visible"; 

				document.getElementById("div_btnrestart").style.visibility="visible"; 
				document.getElementById("btnrestart").style.visibility="visible"; 
				document.getElementById("div_backtomenu").style.visibility="visible"; 
				document.getElementById("div_exittomenu").style.visibility="hidden"; 
				document.getElementById("btnbacktomenu").style.visibility="visible"; 
				document.getElementById("div_btnrestart").style.left=760; 
				document.getElementById("div_backtomenu").style.left=760; 
				document.getElementById("btnrestart").style.width=160; 
				document.getElementById("btnbacktomenu").style.width=160; 
				document.getElementById("btnrestart").style.fontSize=20; 
				document.getElementById("btnbacktomenu").style.fontSize=20; 
				document.getElementById("btnrestart").value="שחק שוב"; 
				





			} else {ready_for_a_new_trial=="true";}
		}
			

		if (obj[i4].feedback_is_on_its_way=="true") {


//			document.getElementById("txbx_sentence1").value=i4;	

				//move the feedback on the x axis towards its target.
			if (obj[i4].feedback_x>obj[i4].feedback_target_x) { obj[i4].feedback_x=obj[i4].feedback_x-5;}
				else
					{if (obj[i4].feedback_x<obj[i4].feedback_target_x) { obj[i4].feedback_x=obj[i4].feedback_x+5;}}

				//move the feedback on the y axis towards its target.


			if (obj[i4].feedback_y>obj[i4].feedback_target_y) { obj[i4].feedback_y=obj[i4].feedback_y-5; 	}
				else {		if (obj[i4].feedback_y<obj[i4].feedback_target_y) { obj[i4].feedback_y=obj[i4].feedback_y+5;}}



			document.getElementById("div_feedback"+i4).style.left=obj[i4].feedback_x;
			document.getElementById("div_feedback"+i4).style.top=obj[i4].feedback_y;
		

		}
	}




	setTimeout('run_main_animation()',delay);
}




         
function animate_the_numebrs(){


	for (i3=0; i3<10; i3++){

		if (obj[trial].sifra_lilchots=="achadot"){
			if (obj[trial].achadot==i3) {	document.getElementById("image_achadot1").src=pic[i3].src;} 
		}

		if (obj[trial].sifra_lilchots=="asarot"){
			if (obj[trial].asarot==i3) {	document.getElementById("image_asarot1").src=pic[i3].src;} 
		}

		if (obj[trial].sifra_lilchots=="meot"){
			if (obj[trial].meot==i3) {	document.getElementById("image_meot1").src=pic[i3].src;} 
		}
	} 


}





function handlemousedown(evt) {

	var e = (window.event) ? window.event : evt; 

	

	if (e.pageX)  {  				//firefox

		mousex=e.pageX; //event.clientX;
		mousey=e.pageY; //event.clientY;
	} else {  					//IE
		mousex=event.clientX;
		mousey=event.clientY;

	}

	

//	if (checkforcontact(obj[trial].sifra_lilchots)=="false"&&e.target!="[object HTMLInputElement]")	{ playSound(1) }

	return true;
}

function handlemouseup(evt) {

	ready_for_a_new_trial="true";


	return true;
}


function checkforcontact(what_to_check){

var rtemp="false";

	if (what_to_check=="achadot"){
		x1=obj[trial].achadot_x; x2=obj[trial].achadot_x+32;
		y1=obj[trial].achadot_y; y2=obj[trial].achadot_y+68;
	}

	if (what_to_check=="asarot"){
		x1=obj[trial].asarot_x; x2=obj[trial].asarot_x+32;
		y1=obj[trial].asarot_y; y2=obj[trial].asarot_y+68;
	}

	if (what_to_check=="meot"){
		x1=obj[trial].meot_x; x2=obj[trial].meot_x+32;
		y1=obj[trial].meot_y; y2=obj[trial].meot_y+68;
	}

		x3=mousex; 	x4=mousex+2;
		y3=mousey; 	y4=mousey+2;
	
		if(x3>x1&&x3<x2&&y3>y1&&y3<y2) rtemp="true";
		if(x4>x1&&x4<x2&&y3>y1&&y3<y2) rtemp="true";
		if(x3>x1&&x3<x2&&y4>y1&&y4<y2) rtemp="true";
		if(x4>x1&&x4<x2&&y4>y1&&y4<y2) rtemp="true";
		
		if(x2>x3&&x2<x4&&y2>y3&&y2<y4) rtemp="true";
		if(x1>x3&&x1<x4&&y2>y3&&y2<y4) rtemp="true";
		if(x2>x3&&x2<x4&&y1>y3&&y1<y4) rtemp="true";
		if(x1>x3&&x1<x4&&y1>y3&&y1<y4) rtemp="true";	



	if (wrong_achadot_timer==0&&(what_to_check=="asarot"||what_to_check=="meot")){
		wrong_achadot="false";
	
		x5=obj[trial].achadot_x; x6=obj[trial].achadot_x+32;
		y5=obj[trial].achadot_y; y6=obj[trial].achadot_y+68;

		if(x3>x5&&x3<x6&&y3>y5&&y3<y6) wrong_achadot="true";
		if(x4>x5&&x4<x6&&y3>y5&&y3<y6) wrong_achadot="true";
		if(x3>x5&&x3<x6&&y4>y5&&y4<y6) wrong_achadot="true";
		if(x4>x5&&x4<x6&&y4>y5&&y4<y6) wrong_achadot="true";
		
		if(x6>x3&&x6<x4&&y6>y3&&y6<y4) wrong_achadot="true";
		if(x5>x3&&x5<x4&&y6>y3&&y6<y4) wrong_achadot="true";
		if(x6>x3&&x6<x4&&y5>y3&&y5<y4) wrong_achadot="true";
		if(x5>x3&&x5<x4&&y5>y3&&y5<y4) wrong_achadot="true";	

		if (wrong_achadot=="true"){  if (navigator.appName=="Netscape")	{wrong_achadot_timer=300; playSound(1)} else {wrong_achadot_timer=230; playSound(1)}
		}

	}

	if (wrong_asarot_timer==0&&(what_to_check=="achadot"||what_to_check=="meot")){
		wrong_asarot="false";
	
		x5=obj[trial].asarot_x; x6=obj[trial].asarot_x+32;
		y5=obj[trial].asarot_y; y6=obj[trial].asarot_y+68;

		if(x3>x5&&x3<x6&&y3>y5&&y3<y6) wrong_asarot="true";
		if(x4>x5&&x4<x6&&y3>y5&&y3<y6) wrong_asarot="true";
		if(x3>x5&&x3<x6&&y4>y5&&y4<y6) wrong_asarot="true";
		if(x4>x5&&x4<x6&&y4>y5&&y4<y6) wrong_asarot="true";
		
		if(x6>x3&&x6<x4&&y6>y3&&y6<y4) wrong_asarot="true";
		if(x5>x3&&x5<x4&&y6>y3&&y6<y4) wrong_asarot="true";
		if(x6>x3&&x6<x4&&y5>y3&&y5<y4) wrong_asarot="true";
		if(x5>x3&&x5<x4&&y5>y3&&y5<y4) wrong_asarot="true";	

		if (wrong_asarot=="true"){  if (navigator.appName=="Netscape")	{wrong_asarot_timer=300; playSound(1)} else {wrong_asarot_timer=230; playSound(1)}
		}

	}

	if (wrong_meot_timer==0&&(what_to_check=="achadot"||what_to_check=="asarot")){
		wrong_meot="false";
	
		x5=obj[trial].meot_x; x6=obj[trial].meot_x+32;
		y5=obj[trial].meot_y; y6=obj[trial].meot_y+68;

		if(x3>x5&&x3<x6&&y3>y5&&y3<y6) wrong_meot="true";
		if(x4>x5&&x4<x6&&y3>y5&&y3<y6) wrong_meot="true";
		if(x3>x5&&x3<x6&&y4>y5&&y4<y6) wrong_meot="true";
		if(x4>x5&&x4<x6&&y4>y5&&y4<y6) wrong_meot="true";
		
		if(x6>x3&&x6<x4&&y6>y3&&y6<y4) wrong_meot="true";
		if(x5>x3&&x5<x4&&y6>y3&&y6<y4) wrong_meot="true";
		if(x6>x3&&x6<x4&&y5>y3&&y5<y4) wrong_meot="true";
		if(x5>x3&&x5<x4&&y5>y3&&y5<y4) wrong_meot="true";	

		if (wrong_meot=="true"){  if (navigator.appName=="Netscape")	{wrong_meot_timer=300; playSound(1)} else {wrong_meot_timer=230; playSound(1)}
		}

	}
	
	
	return rtemp;
}	




					// Sound Script


						var aySound = new Array();
						// Below: source for sound files to be preloaded
						aySound[0] = "../sounds/DingLing.wav";
						aySound[1] = "../sounds/Buzz01.wav";

						// 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); }



