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);

var aySound = new Array();


	pic1= new Image(1152,864); pic1.src="./createnumber_images/skyline-ChicagoAtNight2.jpg"; //"./createnumber_images/skyline-ChicagoAtNight2.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"; 

	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";




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 " ; 

	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;	
	playSound(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(){
	playSound(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()*900+50);

	}

	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+")", 1150 );
	} else {
				for (i=1;i<=score ;i++ )
				{	document.getElementById("div_fireworks"+i).style.visibility="hidden";	
				}
				stopSound(1);
				clearTimeout ( alertTimerId );
				if (score%10==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


						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); }




