var desmarca = false;
var querystring = location.search.substring(1);
var pares = querystring.split("&");
var cadena = unescape(get_valor(pares, "cadena"));
cadena = cadena.replace(/_/gi," ");
var como = parseInt(get_valor(pares, "como"));

$(document).ready(function() {
	if (cadena!="") {
		$(".botonera").prepend("<img id=\"boton_marca_desmarca\" style=\"cursor:hand;\" onclick=\"marca_desmarca();\" src=\"imagenes/subrayado_desactivado.gif\" />");		
		marca_desmarca();
	}
})

function get_valor(arr_parejas,variable) {
	var valor = "";
	var encontrado = false;
	var contador = 0;
	while (!encontrado && contador<arr_parejas.length) {
		auxiliar = arr_parejas[contador].split("=");
		if(auxiliar[0]==variable) {
			valor = auxiliar[1];
			encontrado = true;
		}
		contador++;
	}
	return(valor);
}

function marca_desmarca() {
	if (como==1) 
			marca_desmarca_palabras();
	else
			marca_desmarca_frase();
	if (!desmarca)
		$("#boton_marca_desmarca").attr("src","imagenes/subrayado_desactivado.gif");
	else
		$("#boton_marca_desmarca").attr("src","imagenes/subrayado_activado.gif");
	desmarca = !desmarca;		
}

function marca_desmarca_frase() {	 
	 if(desmarca) {
			rExp = new RegExp("<span class=\"marcado\">" + cadena + "</span>","gi");
			if ($("h1.titulo").length>0) $("h1.titulo").html($("h1.titulo").html().replace(rExp,cadena));
			if ($(".antesubtitulo").length>0) $(".antesubtitulo").html($(".antesubtitulo").html().replace(rExp,cadena));
			if ($(".entradilla").length>0) $(".entradilla").html($(".entradilla").html().replace(rExp,cadena));
			if ($(".texto").length>0) $(".texto").html($(".texto").html().replace(rExp,cadena));
	 } else {
			rExp = new RegExp("([¿¡ ])"+cadena+"([,\.;\?! ])","gi");
			cad = "$1<span class=\"marcado\">" + cadena + "</span>$2";
			if ($("h1.titulo").length>0) $("h1.titulo").html($("h1.titulo").html().replace(rExp,cad));
			if ($(".antesubtitulo").length>0) $(".antesubtitulo").html($(".antesubtitulo").html().replace(rExp,cad));
			if ($(".entradilla").length>0) $(".entradilla").html($(".entradilla").html().replace(rExp,cad));
			if ($(".texto").length>0) $(".texto").html($(".texto").html().replace(rExp,cad));			
	 }
}

function marca_desmarca_palabras() {
	 var palabras = cadena.split(" ");
	 for (var i=0;i<palabras.length;i++) {
			if(desmarca) {
				 rExp = new RegExp("<span class=\"marcado\">" + palabras[i] + "</span>","gi");
				 if ($("h1.titulo").length>0) $("h1.titulo").html($("h1.titulo").html().replace(rExp,palabras[i]));
				 if ($(".antesubtitulo").length>0) $(".antesubtitulo").html($(".antesubtitulo").html().replace(rExp,palabras[i]));
				 if ($(".entradilla").length>0) $(".entradilla").html($(".entradilla").html().replace(rExp,palabras[i]));
				 if ($(".texto").length>0) $(".texto").html($(".texto").html().replace(rExp,palabras[i]));
			} else {
					rExp = new RegExp("([¿¡ ])"+palabras[i]+"([,\.;\?! ])","gi"); // no añadir comillas simples ni dobles para evitar que se marquen  cadenas como : src="perro.jpg"
					cad = "$1<span class=\"marcado\">" + palabras[i] + "</span>$2";
					if ($("h1.titulo").length>0) $("h1.titulo").html($("h1.titulo").html().replace(rExp,cad));
					if ($(".antesubtitulo").length>0) $(".antesubtitulo").html($(".antesubtitulo").html().replace(rExp,cad));
					if ($(".entradilla").length>0) $(".entradilla").html($(".entradilla").html().replace(rExp,cad));
					if ($(".texto").length>0) $(".texto").html($(".texto").html().replace(rExp,cad));
			}
	 }	 
}
