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

var timer;
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 rand1,rand2,rand3,rand4,repeatthis,number1,number2,number3,number4;
var mishtaneezer;
var dontshow=false;
var score=0;
var myguess=0;
var targetvalue=Math.floor(Math.random()*900+199);


	pic1= new Image(1152,864); pic1.src="./createnumber_images/chicago.jpg"; //"./createnumber_images/chicago.jpg"; 

	pic_black_background= new Image(1022,620); pic_black_background.src="./../images/black_background.gif";

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

	pic2= new Image(100,100); pic2.src="./createnumber_images/fireworks-lig39.gif"; 
	pic3= new Image(100,100); pic3.src="./createnumber_images/Fireworks-01-June.gif"; 
	pic4= new Image(135,99); pic4.src="./createnumber_images/0008.gif"; 
	pic5= new Image(91,79); pic5.src="./createnumber_images/Fireworks-02-june.gif"; 
	pic6= new Image(72,54); pic6.src="./createnumber_images/0005.gif"; 
	pic7= new Image(135,99); pic7.src="./createnumber_images/0010.gif"; 
	pic8= new Image(160,120); pic8.src="./createnumber_images/0021.gif"; 
	pic9= new Image(128,128); pic9.src="./createnumber_images/0024.gif"; 
	pic10= new Image(128,128); pic10.src="./createnumber_images/0025.gif"; 
	pic11= new Image(6,8); pic11.src="./createnumber_images/0030.gif"; 
							  
	pic12= new Image(10,10); pic12.src="./createnumber_images/0033.gif"; 
	pic13= new Image(100,100); pic13.src="./createnumber_images/feuer-0111.gif"; 
	pic14= new Image(128,128); pic14.src="./createnumber_images/feuerwerk-0018.gif"; 
	pic15= new Image(128,128); pic15.src="./createnumber_images/fireworks.gif"; 
	pic16= new Image(93,92); pic16.src="./createnumber_images/fireworks_29.gif"; 
	pic17= new Image(1,1); pic17.src="./createnumber_images/fireworks_42.gif"; 

	

	var aySound_ie = new Array();
	// Below: source for sound files to be preloaded
	aySound_ie[0] = "./../sounds/Button.wav";
	aySound_ie[1] = "./../sounds/fireworks2.wav";

	var aySound_other = new Array();
		aySound_other[0]=new Image(); aySound_other[0].src = "./../sounds/Button.mp3";
		aySound_other[1]=new Image(); aySound_other[1].src = "./../sounds/fireworks2.mp3";



function set_width_of_screen(){

		//Get Window Width

		if (parseInt(navigator.appVersion)>3) {
		 if (navigator.appName=="Netscape") {
		  winW = window.innerWidth;
		  winH = window.innerHeight;
		 }
		 if (navigator.appName.indexOf("Microsoft")!=-1) {
		  winW = document.body.offsetWidth;
		  winH = document.body.offsetHeight;
		 }
		}

		var ad_left_limit=864;
		var ad_right_limit=1152;

		var ads_width=160;
		var str_ad="";
		winW-=5;

		if(winW<=1200){	str_ad="div_ad_vertical"; ads_width=160;} else {str_ad="div_ad_big"; ads_width=336;}

			if(winW<ad_left_limit+ads_width){document.getElementById(str_ad).style.left=ad_left_limit; document.getElementById("div_black_background").style.left=ad_left_limit;}  //setting the left limit of the ad location
			else {document.getElementById(str_ad).style.left=winW-ads_width; document.getElementById("div_black_background").style.left=winW-ads_width;}  

			if(winW>ad_right_limit+ads_width) {document.getElementById(str_ad).style.left=ad_right_limit; document.getElementById("div_black_background").style.left=ad_right_limit;} //setting the right limit of the ad location
		

		winW-=ads_width;

		document.getElementById(str_ad).style.visibility="visible";
		document.getElementById(str_ad).style.display="";


}

var wait_timer=100;
var wait_timer_kivun=-1;

function wait_for_images_to_load(){

	if(pic1.complete&&picminush.complete){
		document.getElementById("div_pleasewait").style.visibility="hidden";
		init_main();
	}	else {	
			document.getElementById("div_pleasewait").style.visibility="visible";
			document.getElementById("div_btnrun").style.visibility="hidden";
			document.getElementById("div_pleasewait").style.filter = 'alpha(opacity=' + wait_timer + ')';
			document.getElementById("div_pleasewait").style.opacity=wait_timer/100;
		
			wait_timer+=wait_timer_kivun;
			if(wait_timer<=1){wait_timer_kivun=1}
			if(wait_timer>=100){wait_timer_kivun=-1}
			setTimeout('wait_for_images_to_load()',15); 
		}
		
}


function init_main(){


//	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="url('"+pic1.src+"') black left no-repeat fixed " ; 
	document.getElementById("img_black_background").src=pic_black_background.src;
	document.getElementById("div_black_background").style.visibility="visible";

	score=0;


	init_this_task_variables();



}

function init_this_task_variables(){



	document.getElementById("div_btnrun").style.visibility="hidden";
	document.getElementById("div_exittomenu").style.visibility="visible";

	document.getElementById("div_score1").style.visibility="visible";
	
	document.getElementById("div_btnone").style.visibility="visible";
	document.getElementById("div_minus1").style.visibility="visible";
	document.getElementById("div_btnten").style.visibility="visible";
	document.getElementById("div_minus10").style.visibility="visible";
	document.getElementById("div_btnhundred").style.visibility="visible";
	document.getElementById("div_minus100").style.visibility="visible";
	document.getElementById("div_btnthousand").style.visibility="visible";
	document.getElementById("div_minus1000").style.visibility="visible";
	document.getElementById("div_target").style.visibility="visible";
	document.getElementById("txbx_target").value=targetvalue;
	document.getElementById("div_myguess").style.visibility="visible";

	document.getElementById("imgone").src=pica1.src;
	document.getElementById("imgten").src=pica10.src;
	document.getElementById("imghundred").src=pica100.src;
	document.getElementById("imgthousand").src=pica1000.src;
	document.getElementById("imgminusone").src=picminus.src;
	document.getElementById("imgminusten").src=picminus.src;
	document.getElementById("imgminushundred").src=picminus.src;
	document.getElementById("imgminusthousand").src=picminus.src;



//	for (i=1; i<=20 ;i++ ){
	
//		document.getElementById("div_fireworks"+i).style.visibility="visible";	

//	}


	
}

function button_pressed(x){

	if(x==1){document.getElementById("imgone").src=pica1h.src;}
		if(x==10){document.getElementById("imgten").src=pica10h.src;}
			if(x==100){document.getElementById("imghundred").src=pica100h.src;}
				if(x==1000){document.getElementById("imgthousand").src=pica1000h.src;}

	if(x==-1){document.getElementById("imgminusone").src=picminush.src;}
		if(x==-10){document.getElementById("imgminusten").src=picminush.src;}
			if(x==-100){document.getElementById("imgminushundred").src=picminush.src;}
				if(x==-1000){document.getElementById("imgminusthousand").src=picminush.src;}
	myguess=myguess+x;
	document.getElementById("txbx_myguess").value=myguess;	
	if(is_msie==true){playsound_ie(0);} else {playsound_other(0)};
	if (myguess==targetvalue)
	{success();
	}

	setTimeout ( "buttonimage_backtonormal("+x+")", 100 );
		
}

function buttonimage_backtonormal(x){

	if(x==1){document.getElementById("imgone").src=pica1.src;}
		if(x==10){document.getElementById("imgten").src=pica10.src;}
			if(x==100){document.getElementById("imghundred").src=pica100.src;}
				if(x==1000){document.getElementById("imgthousand").src=pica1000.src;}

	if(x==-1){document.getElementById("imgminusone").src=picminus.src;}
		if(x==-10){document.getElementById("imgminusten").src=picminus.src;}
			if(x==-100){document.getElementById("imgminushundred").src=picminus.src;}
				if(x==-1000){document.getElementById("imgminusthousand").src=picminus.src;}
}


function success(){
	if(is_msie){playsound_ie(1);} else {playsound_other(1);}
	score=score+1;
	document.getElementById("txbx_score1").value=score;	




	for (i=1;i<=score ;i++ ){
		document.getElementById("div_fireworks"+i).style.left=Math.floor(Math.random()*(winW-160)+30);

	}

	document.getElementById("div_btnone").style.visibility="hidden";
	document.getElementById("div_btnten").style.visibility="hidden";
	document.getElementById("div_btnhundred").style.visibility="hidden";
	document.getElementById("div_btnthousand").style.visibility="hidden";
	document.getElementById("div_minus1").style.visibility="hidden";
	document.getElementById("div_minus10").style.visibility="hidden";
	document.getElementById("div_minus100").style.visibility="hidden";
	document.getElementById("div_minus1000").style.visibility="hidden";

	document.getElementById("div_target").style.top="393";	
	document.getElementById("div_target").style.left="298";	
	document.getElementById("txbx_target").style.border="5px inset #ffaa44";	
	
	document.getElementById("div_myguess").style.top="438";	
	document.getElementById("div_myguess").style.left="298";	
	document.getElementById("txbx_myguess").style.border="5px inset #ffaa44";	



	animatefireworks(6);

}


function animatefireworks(timer){

	if (timer>0)
	{
		for (i=1;i<=score ;i++ ){

		document.getElementById("div_fireworks"+i).style.visibility="visible";	
		}
		timer=timer-1;
		alertTimerId = setTimeout ( "animatefireworks("+timer+")", 950 );  //1150
	} else {
				for (i=1;i<=score ;i++ )
				{	document.getElementById("div_fireworks"+i).style.visibility="hidden";	
				}
				if(is_msie){stopsound_ie(1);} else {stopsound_other(1);}
				clearTimeout ( alertTimerId );
				if (score%20==0)
				{endgame();} else {prepare_next_level();}
		   }
}

function prepare_next_level(){

		myguess=0;
		document.getElementById("txbx_myguess").value=myguess;

		targetvalue=Math.floor(Math.random()*9900+99);
		document.getElementById("txbx_target").value=targetvalue;

		do
		{temp1=Math.floor(Math.random()*4);
		temp2=Math.floor(Math.random()*4);
		temp3=Math.floor(Math.random()*4);
		temp4=Math.floor(Math.random()*4);
		}
		while (temp1==temp2||temp1==temp3||temp1==temp4||temp2==temp3||temp2==temp4||temp3==temp4);


		document.getElementById("div_btnone").style.top=390+40*temp1;
		document.getElementById("div_minus1").style.top=390+40*temp1;
		document.getElementById("div_btnten").style.top=390+40*temp2;
		document.getElementById("div_minus10").style.top=390+40*temp2;
		document.getElementById("div_btnhundred").style.top=390+40*temp3;
		document.getElementById("div_minus100").style.top=390+40*temp3;
		document.getElementById("div_btnthousand").style.top=390+40*temp4;
		document.getElementById("div_minus1000").style.top=390+40*temp4;
		
		document.getElementById("div_btnone").style.visibility="visible";
		document.getElementById("div_btnten").style.visibility="visible";
		document.getElementById("div_btnhundred").style.visibility="visible";
		document.getElementById("div_btnthousand").style.visibility="visible";
		document.getElementById("div_minus1").style.visibility="visible";
		document.getElementById("div_minus10").style.visibility="visible";
		document.getElementById("div_minus100").style.visibility="visible";
		document.getElementById("div_minus1000").style.visibility="visible";

	document.getElementById("div_target").style.top="395";	
	document.getElementById("div_target").style.left="300";
	document.getElementById("txbx_target").style.border="3px ridge #ffffff";

	document.getElementById("div_myguess").style.top="440";	
	document.getElementById("div_myguess").style.left="300";
	document.getElementById("txbx_myguess").style.border="3px ridge #ffffff";	



}

function endgame(){

	document.getElementById("div_btnone").style.visibility="hidden";
	document.getElementById("div_btnten").style.visibility="hidden";
	document.getElementById("div_btnhundred").style.visibility="hidden";
	document.getElementById("div_btnthousand").style.visibility="hidden";

	document.getElementById("div_minus1").style.visibility="hidden";
	document.getElementById("div_minus10").style.visibility="hidden";
	document.getElementById("div_minus100").style.visibility="hidden";
	document.getElementById("div_minus1000").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_sentence1").style.visibility="visible";
	document.getElementById("txbx_sentence1").value="Great job! You successfully completed "+score+" trials!";

}


function continuegame(){

	document.getElementById("div_btnone").style.visibility="visible";
	document.getElementById("div_btnten").style.visibility="visible";
	document.getElementById("div_btnhundred").style.visibility="visible";
	document.getElementById("div_btnthousand").style.visibility="visible";

	document.getElementById("div_minus1").style.visibility="visible";
	document.getElementById("div_minus10").style.visibility="visible";
	document.getElementById("div_minus100").style.visibility="visible";
	document.getElementById("div_minus1000").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";

	prepare_next_level();

}



					// Sound Script




						// 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_ie.length;i++)
						Str += "<EMBED SRC='"+aySound_ie[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_ie[whSound]:'';
						else eval("this.document.embeds[whSound]." + (play? "play()":"stop()"))
						}
						function playsound_ie(whSound) { if (window.auCon) auCon.control(whSound,true); }
						function stopsound_ie(whSound) { if (window.auCon) auCon.control(whSound,false); }


function playsound_other(tslil){
//	if(is_msie==false&&is_firefox==false){ document.getElementById("sound_element").innerHTML="<embed src='"+aySound_other[tslil].src+"' hidden=true autostart=true loop=false>";}
}
function stopsound_other(tslil){
//	if(is_msie==false&&is_firefox==false){ document.getElementById("sound_element").innerHTML="<embed src='' hidden=true autostart=true loop=false>";}
}

