/* indicates that the solver should be stopped */
var stop;

/**
* We want to show the solution being generated dynamically, so replace the outer
* while() loop in the build() method with a timer that checks the stack
* every x milliseconds.  This gives the browser a chance to refresh the display;
* otherwise it would only show the final state.
*/
function solve()
{    

/* re-seed with a random number */
var mazeque = document.getElementById("maze").innerHTML
var copymazeque = document.getElementById("box2")
copymazeque.innerHTML = mazeque
rnd.seed = Math.floor(Math.random() * 1000000000);

/* initialize the flag */
stop = false;

/* Initialize the stack at the first element */
var stack = [ { x: instance.start.x, y: instance.start.y, neighbors: dirs.shuffle() } ] ;

/* Add a new breadcrumb every zillisecond */
solver(instance, stack) 

/* Disable the form button again */

}

/**
* Indicate we want to stop
*/
function stopSolver()
{
	stop = true;
}

/**
* Main algorithm to solve the maze
*/
function solver(maze, stack)
{
	if(stop)
	{  
		alert("end")
	return;
	}

	var current = stack.peek();

	x = current.x;
	y = current.y;
	neighbors = current.neighbors;
	var cell = maze.cells[x][y];
    
	cell.visited = true;
	cell.token.style.backgroundPosition = "center";
		var uri = location.hostname
			
			if(uri.indexOf("www")!=-1)
			{
				cell.token.style.backgroundImage = "url(http://"+uri+"/images/breadcrumb.png)";
			}
			else
			{
			 cell.token.style.backgroundImage = "url(http://"+uri+"/leafling/images/breadcrumb.png)";
			}
			cell.token.style.backgroundRepeat = "no-repeat";
	// see if we're at the exit
	if((x == (columns - 1)) && (y == (rows - 1)))
	{
		stopSolver();  // done
		return;
	}

	var found = false;

	/* look for a connected neighbor that hasn't been visited yet */
	while(neighbors.length > 0)
	{
		dir = neighbors.pop();

		if(cell.wall[dir] == false)
		{
			dx = x + delta.x[dir];
			dy = y + delta.y[dir];
			if(dx >= 0 && dy >= 0 && dx < columns && dy < rows)
			{
				if(maze.cells[dx][dy].visited == false)
				{
					stack.push( { x: dx, y: dy, neighbors: dirs.shuffle() } );
					found = true;
					break;
				}
			}
		}
	}

	if(neighbors.length == 0)
	{
		if(found == false)
		{
			stack.pop();
			if((x == maze.start.x) && (y == maze.start.y))
			{
				stopSolver(); // we're back at the beginning; must not be able to compelete the maze
			}
			var uri = location.hostname
			
			if(uri.indexOf("www")!=-1)
			{
				cell.token.style.backgroundImage = "url(http://"+uri+"/images/spacer.gif)";
			}
			else
			{
			 cell.token.style.backgroundImage = "url(http://"+uri+"/leafling/images/spacer.gif)";
			}

		}
	}

	if(! stop)
	{
		 solver(maze, stack)
	}
}
