/* Vocabulario de términos en ajax con Jquery, Alberto Fortes, septiembre 08
http://www.albertofortes.com
info@albertofortes.com    */

$(document).ready(function(){
	$("#ListVoc dt").css("text-transform", "none"); //convertimos por si acaso, las primeras letras de los términos a mayúsculas
	$("#vocab_nav").css("display", "block");
	var meta= $("#leyenda_voc");
	$("#ListVoc").css("display", "none");
	$("#leyenda_voc").css("display", "block");
	var LVoc= $("#ListVoc dt");
	var LVocdd= $("#ListVoc dd");
	LVoc.css("display", "none");
	LVocdd.css("display", "none");
	
	$("#vocab_nav li a").each(function(i){
   		this.id = "GoTo_" + i;
 	});
	$('#ListVoc dt').each(function(i) {
 		paragraph = this;
 		node = paragraph;
 		while (node.childNodes.length) {
			node = node.firstChild;
		}
		text = node.nodeValue;
		first_letter = text.substr(0,1);
		$(this).addClass( "letra_" +first_letter.toLowerCase());
		
 	});
	letters = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
	for (var l=0; l<letters.length; l++)
	{
		$("#GoTo_"+l).click(function(l) {
            var t = $(l.target); 
            //comprobamos si tenemos resultados
            if($(".letra_" + t.text()).text() !='')
            {
                meta.hide("fast");
				$("#ListVoc").css("display", "block");
				$(".letra_" + t.text()).show("fast");
			    $(".letra_" + t.text()+ " +dd").show("slow");
			    $("#ListVoc dt").css("display", "none");
	            $("#ListVoc dd").css("display", "none");
            }
            else
            {   
                $("#ListVoc").hide("fast");
				meta.html("<p class='no_result'>Lo sentimos no existen resultados para la letra <span>"+ t.text()+"</span>.</p>");
                meta.show("fast");
            }

			return false;
		});
	};
});
