﻿function ZoomBild(Bild){

	//Bildname = document.getElementById(Bild).src;
	Bildname = Bild.src;
	Bildname = Bildname.substr(Bildname.lastIndexOf("images"));
	Bildname = Bildname.replace("klein", "gross");
	BilderZoom(Bildname);
}
function GoTo(Url){
	document.location.replace(Url);
}

function zentrieren(url,fenstername,features,fensterbreite,fensterhoehe){
		posx = (screen.Width/2) - (fensterbreite/2);
		posy = (screen.Height/2) - (fensterhoehe/2);
		//wird nur gemacht um die optische Mitte zu bekommen, also nicht echt vert. zetriert
		var posy = eval(posy - 30);
		fenster = window.open(url,fenstername,features);
		fenster.moveTo(posx,posy);
		fenster.focus();
	}
		
function aufbauen(breite,hoehe){
	var i = 1;
	var j = 1;
	
	var geschwindigkeit = 10;
	
	while (i<breite){
		fenster.resizeTo(i,5);
		fenster.focus();
		i = i + geschwindigkeit;
	}
	while (j<hoehe){
		fenster.resizeTo(i,j);
		fenster.focus();
		j = j + geschwindigkeit;
	}						
}

function BilderZoom(Bild){
	if(Bild != ""){
		BildUrl = "BilderZoom.aspx?Bild=" + Bild;
		
		var features="toolbar=no,width=650,height=500,directories=no,status=yes,scrollbars=yes,resizable=1,menubar=no";
		var fenstername = "BilderZoom";
		var fensterbreite = 650;
		var fensterhoehe = 500;

		zentrieren(BildUrl,fenstername,features,fensterbreite,fensterhoehe);
		//aufbauen(fensterbreite,fensterhoehe);
	
		}else{
		alert("Kein Bild angegeben");
	}
}



function Toogle(id){
	if(document.getElementById(id).style.display == "block"){
		document.getElementById(id).style.display = "none";
	
	}else{
	document.getElementById(id).style.display = "block";
	}


}





//////////MEMORY/////////////






//Gobale Variablen zur Speicherung der aktuellen Bilder und Keys

//global Variables for storage of images and keys
var ErstesBildKey = -1;
var ZweitesBildKey = -1;
var ErstesBildID = "";
var ZweitesBildID = "";

var ErstesBildPfad = "";
var ErstesBildCaller = "";

var MatchCount = 0;
				 

function DoNothing(){
}

function DisableEvent(id){

	document.getElementById(id).onclick = function (){DoNothing();};
	document.getElementById(id).style.cursor = "none"; 
}


function EnableEvent(id, Key, BildPfad, caller){



	//document.getElementById(id).onclick = function (){DoNothing();};
	
	
		var test = "alert('hallo');";
	
	
	document.getElementById(id).onclick = function (){ClickBild(id, Key, BildPfad, caller);};
	
	document.getElementById(id).style.cursor = "hand"; 
	


// document.getElementById("TestLink").onclick = function ale() {alert('asd');};
//    document.getElementById("TestLink").style.cursor = "hand";
}




function Preload(){
	for (var i = 0; i <= BilderBuffer.length-1; i++){
		tmp = new Image();
		tmp.src = "images/memory/" + BilderBuffer[i];
	}
	

}

///Wechselt die Bilder
//Changes the images
function ClickBild(id, Key, BildPfad, caller){
	

	//BildID immer iBild anstatt hBild
	//Replace hBild with iBild for getting the id of the <img after
	//clicking the Hyperling
	var BildID = id.replace("hBild", "iBild");
	var LinkID = id;

	
	if (ErstesBildKey == -1 && ZweitesBildKey == -1){
		//Wenn das erste Bild geklickt wurde
		//if the first image has been clicked
		
		//alert(BildPfad);
		document.getElementById(BildID).src =  BildPfad;
		//document.getElementByName(BildID).src = HTTPPfad + BildPfad;
		
		
		//Bild speichern
		//save image
		ErstesBildKey = Key;
		
		//ID speichern
		//save the ID
		ErstesBildID = BildID;
		
		
		//save Image path
		ErstesBildPfad =  BildPfad;
		
		
		//save the caller
		ErstesBildCaller = caller;
		
		///Disable the onclick Event
		DisableEvent(LinkID);
		
		
		
		
		
		
		
	}else if((ErstesBildKey > -1 && ZweitesBildKey == -1) || (ErstesBildKey > -1 && ZweitesBildKey > -1)){
		//Wenn das erste Bereits geklickt wurde und nochmal wo glickt wurde
		//If the first image has been clicked and the user has clicked another image
		
	
		
		if(ErstesBildKey == Key){
		
			//Wenn nochmal auf das erste geklickt wurde
			//if the user has click on the first image again
	

	
			//Erstes Bild schlissen
			//Close first image
			//Hier kann das Cheaten eingeblendet werden
			//cheating can be implemented 
			//Dann kann das erste aufgemacht und wieer geschlossen werden
			//then the first image can be opened an closed again by clicking
			
			//document.getElementById(BildID).src = BildGeschlossen;
			
			//Bildspeicher zurücksetzen
			//reset the image store
			ErstesBildKey = -1;
			ZweitesBildKey = -1;
			

			
			
		}else{
			//Wenn auf das zweite Bild geklickt wurde
			//if the user cklicke the second image
			//Zweites Bild Key speichern
			//save second image
			ZweitesBildKey = Key;
				
			if(BilderBuffer[ErstesBildKey] == BilderBuffer[ZweitesBildKey]){
				//Wenn die Bildnamen übereinstimmen
				//if the image names fit
		
				//Zweites Bild zuweisen
				//set second image

				document.getElementById(BildID).src = BildPfad;
				//ID speichern
				//save the id
				
				ZweitesBildID = BildID;

				//Bildspeicher zurücksetzen
				//reset image storage
				ErstesBildKey = -1;
				ZweitesBildKey = -1;
				
				//MatchCount hochzählen
				//icrement Matchcount
				MatchCount++;
				
				//Disable Event First image
				var LinkIDFirstImage = ErstesBildID.replace("iBild", "hBild");
				DisableEvent(LinkIDFirstImage);

				//Disable Event Second image
				DisableEvent(id);

			}
			else
			{
		
				//Wenn das zweite Bild nicht mit dem ersten übereinstimmt
				//if the second image doesen't fit with the first image

				//Zweites Bild öffnen und kurz offen lassen
				//open second image and leave it open a short time
				if(caller == "Seite"){
					document.getElementById(BildID).src = BildPfad;
					setTimeout("ClickBild('" + id +  "','" + Key + "','"  + BildPfad +  "','SetTimeout');",BildDelay);
				}else{
					
					//Bilder zurücksetzen auf geschlossen	
					//reset images to closed
					document.getElementById(ErstesBildID).src = BildGeschlossen;
					document.getElementById(BildID).src = BildGeschlossen;
					
				
					var LinkID = ErstesBildID.replace("iBild", "hBild");
					EnableEvent(LinkID,ErstesBildKey, ErstesBildPfad, ErstesBildCaller);
	
									
					//Bildspeicher zurücksetzen
					//reset image storage
					ErstesBildKey = -1;
					ZweitesBildKey = -1;
					
							
						
				}
			}
		}		
	}
	
	//Prüfen ob fertig, wenn ja Meldung ausgeben
	//check if done and show message
	if(MatchCount == (BilderBuffer.length/2)){
		alert("Geschafft");
		
		//Neustarten
		//restart the whole game
		var neustarten = confirm("Noch einmal spielen?");
		if (neustarten == true){
			document.location.reload();
		}
		
	}

}
