var resultados=new Array();
var index_res=new Array();
var salida_indice='';
var queryLang = TrimPath.makeQueryLang(columnDefs);
var imprimir=0;
var marcar=0;
var destino='';
var termino_busqueda='';

function saca_hijos(id_capitulo, profundidad)
{
	var query="SELECT capitulos.ID, capitulos.NOMBRE, capitulos.ORDEN FROM capitulos WHERE capitulos.ID_PADRE=="+id_capitulo+" ORDER BY capitulos.ORDEN";
	var statement = queryLang.parseSQL(query);
	var results = statement.filter(tableData);
	for (var r = 0; r < results.length; r++)
    {
		var pad=10+(profundidad*7);
		if(profundidad)
			pad=0;
		salida_indice+="<tr>\r\n<td style='padding-bottom:2px;padding-left:"+pad+"px;";
		if (profundidad==0)
			salida_indice+="font-weight:bold;color:#000000;";
		if(r>0 & profundidad == 0)
			salida_indice+="padding-top:10px;";
		//Miramos si hay páginas en este capítulo
		var query2="SELECT paginas.NOMBRE, paginas.FICHERO FROM paginas WHERE paginas.ID_CAPITULO=="+results[r]['ID'];
		var statement2 = queryLang.parseSQL(query2);
		var results2 = statement2.filter(tableData);
/*
	if (results2.length>0)
			salida_indice+="cursor:pointer;cursor:hand;' onmouseover=\"this.style.background='#F1F5CB';\" onmouseout=\"this.style.background='';\" onclick=\"document.contenido.location.href='./contenidos/"+results2[0]['FICHERO']+"'\"";
*/
	if (results2.length>0)
			salida_indice+="' onclick=\"//document.contenido.location.href='./contenidos/"+results2[0]['FICHERO']+"'\"";
		else salida_indice+="'";
		salida_indice+=">";
		if (results2.length>0)
			salida_indice+="<div style=\"padding-left:20px; font-size:11px;\" class=\"men\"><a target='contenido' class='amenu' href='./contenidos/"+results2[0]['FICHERO']+"'>"+results[r]['NOMBRE']+"</a></div></td></tr>";
		else
			salida_indice+=""+results[r]['NOMBRE']+"</td></tr>\r\n";

        saca_hijos(results[r]['ID'],profundidad+1);
    }
}

function coloca_indice()
{
	salida_indice="<table width='100%' cellpadding='2' cellspacing='0' class='tx11'>";
	saca_hijos(0,0);
	salida_indice+="</table>";
	document.getElementById('content').innerHTML=salida_indice;
}
function quita_char_(tx)
{
	var pos=0;
	pos=tx.indexOf('Á');
	while (pos!=-1)
	{
		tx=tx.replace('Á','A');
		pos=tx.indexOf('Á',pos+1);
	}
	pos=tx.indexOf('É');
	while (pos!=-1)
	{
		tx=tx.replace('É','E');
		pos=tx.indexOf('É',pos+1);
	}
	pos=tx.indexOf('Í');
	while (pos!=-1)
	{
		tx=tx.replace('Í','I');
		pos=tx.indexOf('Í',pos+1);
	}
	pos=tx.indexOf('Ó');
	while (pos!=-1)
	{
		tx=tx.replace('Ó','O');
		pos=tx.indexOf('Ó',pos+1);
	}
	pos=tx.indexOf('Ú');
	while (pos!=-1)
	{
		tx=tx.replace('Ú','U');
		pos=tx.indexOf('Ú',pos+1);
	}
	pos=tx.indexOf('Ñ');
	while (pos!=-1)
	{
		tx=tx.replace('Ñ','n');
		pos=tx.indexOf('Ñ',pos+1);
	}
	return tx;
}
function inicia_busqueda()
{
	document.getElementById('selector').style.display='none';
	document.getElementById('f1').style.display='none';
	document.getElementById('f2').style.display='none';
	document.getElementById('f3').style.display='inline';
	document.getElementById('f4').style.display='inline';
	document.getElementById('selector').style.display='inline';
	init2=0;
	document.getElementById('content2').innerHTML='';
	document.getElementById('content2').style.top='0px';

	var termino=document.getElementById('txbusca').value;
	termino_busqueda=termino;
	if (termino=='')
		return;
	resultados=new Array();
	index_res=new Array();
	resultados_on();
	ini_progress();

	if (online==0)
	{
		//Paginas con el termino en su titulo
		var terminos=termino.toUpperCase();
		terminos=quita_char_(terminos);
		for (x = 0; x < titulos.length; x++)
    	{
			var cad=titulos[x].toUpperCase();
			cad=quita_char_(cad);
			var idx=cad.indexOf(terminos);
			if (idx!=-1)
				recibe_resultado(x,0);
		}
		//Paginas con el termino en el titulo de su capitulo
		var query="SELECT capitulos.ID, capitulos.NOMBRE FROM capitulos ORDER BY capitulos.ORDEN";
		var statement = queryLang.parseSQL(query);
		var results = statement.filter(tableData);
		for (var r = 0; r < results.length; r++)
		{
			var cad=results[r]['NOMBRE'].toUpperCase();
			cad=quita_char_(cad);
			var idx=cad.indexOf(terminos);
			if (idx!=-1)
			{
				var query2="SELECT paginas.FICHERO, paginas.NOMBRE FROM paginas WHERE paginas.ID_CAPITULO=="+results[r]['ID']+" ORDER BY paginas.ORDEN";
				var statement2 = queryLang.parseSQL(query2);
				var results2 = statement2.filter(tableData);
				for (var rr = 0; rr < results2.length; rr++)
					for (x=0;x<paginas.length;x++)
						if (paginas[x]==results2[rr]['FICHERO'])
							recibe_resultado(x,0);
			}
		}
		//Paginas con el termino en su contenido
		document.getElementById('ifr').src='./contenidos/'+paginas[0]+'?busqueda='+termino+'&pos=0&rand='+Math.random()+Math.random()+Math.random()+Math.random();
	}
	else
		busca_online(termino);
	return false;
}

function init()
{
	if (online==1)
	{
		inicia_XML();
	}
	coloca_indice();
	document.getElementById('txbusca').focus();
}

function resultados_on()
{
	document.getElementById('resulon').style.display='inline';
	document.getElementById('resuloff').style.display='none';
	document.getElementById('buscaoff').style.display='inline';
	document.getElementById('buscaon').style.display='none';
	document.getElementById('selector').style.display='none';
	document.getElementById('f1').style.display='none';
	document.getElementById('f2').style.display='none';
	document.getElementById('f3').style.display='inline';
	document.getElementById('f4').style.display='inline';
	document.getElementById('selector2').style.display='inline';
	init2=0;
	document.getElementById('content2').style.top='0px';
}

function buscador_on()
{
	document.getElementById('resuloff').style.display='inline';
	document.getElementById('resulon').style.display='none';
	document.getElementById('buscaon').style.display='inline';
	document.getElementById('buscaoff').style.display='none';
	document.getElementById('selector2').style.display='none';
	document.getElementById('f3').style.display='none';
	document.getElementById('f4').style.display='none';
	document.getElementById('f1').style.display='inline';
	document.getElementById('f2').style.display='inline';
	document.getElementById('selector').style.display='inline';
	document.getElementById('content').innerHTML='';
	init1=0;
	document.getElementById('content').style.top='0px';
	coloca_indice();
}

function compara(a,b)
{
	return b-a;
}

function fin_busqueda(termino)
{
	clearInterval(id_timer_progress);
	document.getElementById('progress_bar').style.display='none';
	document.getElementById('progress_bar2').style.display='none';
	//Listado de resultados
	var l=resultados.length;
	if (l>0)
	{
		var out="<table width='100%' cellpadding='4' cellspacing='0' class='tx11'><tr><td align='center'><b>"+l+" resultado";
		if (l>1) out+='s';
		out+=" encontrado";
		if (l>1) out+='s';
		out+="</td></tr>";
		for (x=0;x<l;x++)
		{
			out+="<tr><td style='cursor:pointer;cursor:hand;padding-left:10px;font-size:11px;' onclick=\"document.contenido.location.href='./contenidos/"+paginas[index_res[x]]+"'\"><a class='amenu' target='contenido' href='./contenidos/"+paginas[index_res[x]]+"?resalta="+termino+"' style='height:17px'>"+titulos[index_res[x]]+"</a></td></tr>";
		}
		out+='</table>';
	}
	else
	{
		var out="<table width='100%' cellpadding='4' cellspacing='0' class='tx11'><tr><td align='center'><b>No se han encontrado resultados</b></td></tr></table>";
	}
	document.getElementById('content2').innerHTML=out;
}

function writeArray()
{
	var cad='';
	for (x=0;x<resultados.length;x++)
		cad+=x+' -> '+resultados[x]+'              '+x+' -> '+index_res[x]+'\r\n';
   alert(cad);
}


function recibe_resultado(index,ocurrencias)
{
	var l=resultados.length;
/*	if (l==0)
	{
		resultados[0]=ocurrencias;
		index_res[0]=index;
	}
	var aniadido=0;
	for (x=0;x<l;x++)
	{
		if (resultados[x]<ocurrencias)
		{
			resultados.splice(x,0,ocurrencias);
			index_res.splice(x,0,index);
			aniadido=1;
			break;
		}
	}
	if (!aniadido)
	{*/
	var yaesta=0;
	for (var x=0;x<l;x++)
	{
		if (index_res[x]==index)
			yaesta=1;
	}
	if (!yaesta)
	{
		resultados[l]=ocurrencias;
		index_res[l]=index;
	}
	//}
}

function ve_a_marcadores()
{
	destino='marcadores.html';
	setTimeout("destino=''",600);
}

function ve_a_pdf()
{
	destino='pdf.html';
	setTimeout("destino=''",600);
}
