/**@file
	Yualah: Buscador de bombas - js
	(c) Yualah, Roberto González Vázquez- all rights reserved
	_roberto@yualah.com_
	@author Roberto González Vázquez
	@version 1.0
	@ext_document bombas.js.ayu
*/

juego_en_curso = true;
num_vistas 	= 0
segundero 	= 0;
tempo		= null;
modo		= 'pasos';
v_minas 	= t_num_minas;

function Reloj()
{
	segundero++;
	document.getElementById('segundos').innerHTML = segundero+' s';
	tempo = setTimeout('Reloj()', 1000);
}

function Click(obj, forzar_paso)
{
	if (!juego_en_curso) return;
	if (!tempo) tempo = setTimeout('Reloj()', 1000);
	var x 		= obj.getAttribute('x');
	var y 		= obj.getAttribute('y');
	
	if (t_visitas[y][x]) return;
	
	var numero 	= tablero	[y][x];
	
	if (modo=='pasos' && t_marcas[y][x]>0 && !forzar_paso)
	{
		document.getElementById('c'+x+'_'+y).style.backgroundImage='';
		if (t_marcas[y][x]==1) v_minas++;
		document.getElementById('visor').innerHTML = v_minas+' minas';
		t_marcas[y][x] = 0;
	}
	
	else if (modo=='pasos' || forzar_paso)
	{
		if (numero==0) 
			Sangrar (x,y)
		
		Mostrar (x,y);
		
		if (numero>8)
		{	
			Mostrar_bombas();
			juego_en_curso = false;
			if (tempo) clearTimeout(tempo);
			mum_vistas = 0 - t_ancho*t_alto;
			document.getElementById('careto').src='triste.gif';					
		}
		
		if ((num_vistas+t_num_minas)==(t_ancho*t_alto))
		{
			Mostrar_bombas();		
			juego_en_curso = false;
			if (tempo) clearTimeout(tempo);		
			document.getElementById('careto').src='alegre.gif';
		}
	}
	else
	{
		if 		(t_marcas[y][x]==1) v_minas++;
		else if (t_marcas[y][x]==0) v_minas--;
		document.getElementById('visor').innerHTML = v_minas+' minas';
		t_marcas[y][x] = (0+t_marcas[y][x]+1)%3;
		var aux= new Array('.','bandera','nose');
		document.getElementById('c'+x+'_'+y).style.backgroundImage='url('+aux[t_marcas[y][x]]+'.gif)';
	}
}

function Mostrar_bombas()
{
	for (var x=0; x<t_ancho; x++)
		for (var y=0; y<t_alto;y++)
			if (tablero[y][x]>8 && !t_visitas[y][x])
				document.getElementById('c'+x+'_'+y).style.backgroundImage='url(bomba.gif)';						
}

function Mostrar (x,y)
{					
	if (t_visitas[y][x]) return;
	var obj = document.getElementById('c'+x+'_'+y)
	var numero = tablero[y][x];
	t_visitas[y][x]=true;
	num_vistas++;
	obj.className	= 'bajo';	
	
	var img='';
	if 		(numero>8) 	img='explosion';
	else if (numero>0) 	img=numero;
	
	if (img!='')
		obj.style.backgroundImage='url('+img+'.gif)';
}


function Modear(que)
{
	modo=que;
	if (que=='pasos')
	{
		document.getElementById('pasos').className='modo_sel';
		document.getElementById('dudas').className='modo';
	}
	else
	{
		document.getElementById('pasos').className='modo';
		document.getElementById('dudas').className='modo_sel';
	}
}


function Buscar_tirada()
{
	var x,y,xx,yy, ax, ay;
	ax = Math.floor(Math.random() * t_ancho);
	ay = Math.floor(Math.random() * t_alto);
    for (xx=0; xx<t_ancho; xx++)
		for (yy=0; yy<t_alto; yy++)
		{
			x=(ax+xx)%t_ancho;
			y=(ay+yy)%t_alto;
			
			if (t_visitas[y][x] || tablero[y][x]>8) continue;

			Click (document.getElementById('c'+x+'_'+y),true);
			return;
		}		
}

function Sangrar (x,y)
{
	var cola 	= new Array();
	
	cola.push ({x:x-0, y:y-0})
							
	var t,elemento;
	
	while (cola.length >0)
	{
		elemento=cola.pop();
		x		=elemento.x;
		y		=elemento.y;

		if (x<0 || x>=t_ancho || y<0 || y>=t_alto) continue;
		if (tablero[y][x]>8 || t_visitas[y][x]) continue;
		
		Mostrar(x,y);
		
		if (tablero[y][x]>0) continue;
		
		cola.push ({x:x	 , y:y-1});
		cola.push ({x:x	 , y:y+1});
		cola.push ({x:x-1, y:y	});
		cola.push ({x:x-1, y:y+1});
		cola.push ({x:x-1, y:y-1});
		cola.push ({x:x+1, y:y	});
		cola.push ({x:x+1, y:y+1});
		cola.push ({x:x+1, y:y-1});	
	}        
}
						
					