
function ziel() {
	ZeitStop();
	// Überschreibe Cursortasten 
	picmatrix = '';
	Movekeys.innerHTML= picmatrix;
	picmatrix += '<IMG SRC=\"blank.gif" HEIGHT="2" WIDTH="270"><BR>';
	picmatrix += 'Glückwunsch! Sie haben das Ziel erreicht.';
	picmatrix += '<BR>Genießen Sie den Überblick!<BR>';
	picmatrix += '<IMG SRC=\"b';
	picmatrix += mazepic;
	picmatrix += '.gif\" style=\"margin-top:5pt;margin-bottom:5pt;margin-left:5pt;margin-right:5pt;\"><BR>';
	Bild.innerHTML= picmatrix;
}


// ----------------------------------------------
// Status Anzeige

Schritte = 0;

function ZeitStart() { 
    Stop = "0";
    Jetzt = new Date();
    Start = Jetzt.getTime();
    ZeitAnzeigen();
}

function ZeitStop() { 
    Stop = "1";
}

function ZeitAnzeigen() { 
    var absSekunden = Math.round(ZeitBerechnen()); 
    var relSekunden = absSekunden % 60;
    var absMinuten = Math.round((absSekunden-30)/60);
    var anzSekunden ="" + ((relSekunden > 9) ? relSekunden : "0" + relSekunden);
    var anzMinuten ="" + ((absMinuten > 9) ? absMinuten : "0" + absMinuten);
    Laufdauer = anzMinuten + ":" + anzSekunden;
//    window.document.Anzeige.Zeit.value = anzMinuten + ":" + anzSekunden;
    if (Stop == "0") {
	Zeit.innerHTML = Laufdauer;
	window.setTimeout('ZeitAnzeigen()',1000); 
    }
}
   
function ZeitBerechnen() { 
    var Immernoch = new Date(); 
    return((Immernoch.getTime() - Start)/1000); 
}

// ---------------------------------------------------------------

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Display

function stopcheck() {
	if (direction=="left") {
	    if (goal == "0") {
		if (show[2][3] == "1" && show[4][3] == "1" && show[3][2] != "G") window.setTimeout("go_left()",200)
		if (show[2][3] == "1" && show[3][2] == "1" && show[4][3] != "G") window.setTimeout("go_down()",200)
		if (show[4][3] == "1" && show[3][2] == "1" && show[2][3] != "G") window.setTimeout("go_up()",200)
	}}
	if (direction=="right") {
	    if (goal == "0") {
		if (show[2][3] == "1" && show[4][3] == "1" && show[3][4] != "G") window.setTimeout("go_right()",200)
		if (show[2][3] == "1" && show[3][4] == "1" && show[4][3] != "G") window.setTimeout("go_down()",200)
		if (show[4][3] == "1" && show[3][4] == "1" && show[2][3] != "G") window.setTimeout("go_up()",200)
	}}
	if (direction=="up") {
	    if (goal == "0") {
		if (show[3][2] == "1" && show[3][4] == "1" && show[2][3] != "G") window.setTimeout("go_up()",200)
		if (show[2][3] == "1" && show[3][4] == "1" && show[3][2] != "G") window.setTimeout("go_left()",200)
		if (show[2][3] == "1" && show[3][2] == "1" && show[3][4] != "G") window.setTimeout("go_right()",200)
	}}
	if (direction=="down") {
	    if (goal == "0") {
		if (show[3][2] == "1" && show[3][4] == "1" && show[4][3] != "G") window.setTimeout("go_down()",200)
		if (show[4][3] == "1" && show[3][4] == "1" && show[3][2] != "G") window.setTimeout("go_left()",200)
		if (show[4][3] == "1" && show[3][2] == "1" && show[3][4] != "G") window.setTimeout("go_right()",200)
	}}
}

function disp_maze() {  
// display maze
	picmatrix = '';
	for ( i = 0; i < showmatrix ; i++ ) {
		picline = '';
		for ( j = 0; j < showmatrix ; j++ ) {
			str = show[i][j];
			     if ( str == "0" ) picline += '<IMG SRC=\"bw0.gif\" BORDER=\"0\">';
			else if ( str == "1" ) picline += '<IMG SRC=\"bw1.gif\" BORDER=\"0\">';
			else if ( str == "R" ) picline += '<IMG SRC=\"bwr.gif\" BORDER=\"0\">';
			else if ( str == "M" ) picline += '<IMG SRC=\"bws.gif\" BORDER=\"0\">';
			else if ( str == "S" ) picline += '<IMG SRC=\"bw0.gif\" BORDER=\"0\">';
			else if ( str == "G" ) picline += '<IMG SRC=\"bwg.gif\" BORDER=\"0\">';
		}
		picline += '<br>';
		picmatrix += picline;
	}
	Bild.innerHTML= picmatrix;
	stopcheck();
}

function zuweisen() {
// Werte dem anzuzeigenden Array zuweisen
	for ( i = 0; i < showmatrix ; i++ ) {
		for ( j = 0; j < showmatrix ; j++ ) {
			show[i][j] = maze[aktZeile-rand+i][aktReihe-rand+j];
		}
	}
	show[3][3] = "M";
	disp_maze();
}

function count() {
// Werte dem anzuzeigenden Array zuweisen
	Schritte += 1;
	SchritteOutput = "Schritte: " + Schritte;
	Steps.innerHTML = SchritteOutput;
	if (Schritte == 1) { ZeitStart(); }
	zuweisen();
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initialisierung

function make_show_array() {
// Show Array erzeugen
	show = new Array(showmatrix);
	for ( i = 0; i < showmatrix ; i++ ) 
		show[i] = new Array(showmatrix);
	zuweisen();
}

function search_start() {
// Maze Array nach Startpunkt durchsuchen
	for ( i = 0; i < mazeheight ; i++ )
		for ( j = 0; j < mazewidth ; j++ )
			if (maze[i][j] == "S") 
			{
				aktZeile = i;
				aktReihe = j;
			}
}

function twodim_array() {
// zweidimensionales Array erzeugen und Werte aus Originalmatrix übertragen
	showmatrix = 7;
	zusatz = showmatrix-1;
	rand = zusatz / 2;
	mazewidth += zusatz; 
	mazeheight += zusatz; 
	maze = new Array(mazeheight);
	for ( i = 0; i < mazeheight ; i++ ) {
		maze[i] = new Array(mazewidth);
		for ( j = 0; j < mazewidth ; j++ )
			maze[i][j] = "R";
	}
	heightend = mazeheight-rand;
	widthend = mazewidth-rand;
	for ( i = rand; i < heightend; i++ ) {
		for ( j = rand; j < widthend; j++ ) {
			maze[i][j] = imgstr[i-rand].charAt(j-rand);
		}
	}
}

function ask_matrix_size() {
// Höhe und Breite der Originalmatrix abfragen
	mazeheight = imgstr.length;
	mazewidth = imgstr[1].length;
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Play

function go_up() {
	direction="up";
	if (show[2][3] == "0") {
		aktZeile -= 1;
		count();
	}
	else if (show[2][3] == "S") {
		aktZeile -= 1;
		count();
	}
	else if (show[2][3] == "G") {
		aktZeile -= 1;
		goal = 1;
		count();
		ziel();
	}	
}

function go_left() {
	direction="left";
	if (show[3][2] == "0") {
		aktReihe -= 1;
		count();
	}
	else if (show[3][2] == "S") {
		aktReihe -= 1;
		count();
	}
	else if (show[3][2] == "G") {
		aktReihe -= 1;
		goal = 1;
		count();
		ziel();
	}	
}

function go_right() {
	direction="right";
	if (show[3][4] == "0") {
		aktReihe += 1;
		count();
	}
	else if (show[3][4] == "S") {
		aktReihe += 1;
		count();
	}
	else if (show[3][4] == "G") {
		aktReihe += 1;
		goal = 1;
		count();
		ziel();
	}	
}

function go_down() {
	direction="down";
	if (show[4][3] == "0") {
		aktZeile += 1;
		count();
	}
	else if (show[4][3] == "S") {
		aktZeile += 1;
		count();
	}
	else if (show[4][3] == "G") {
		aktZeile += 1;
		goal = 1;
		count();
		ziel();
	}	
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Call

function disp_image() {
	goal = 0;
	ask_matrix_size();
	twodim_array();
	search_start();
	make_show_array();
}


