
$url_root = "http://trancedownloader.ru/";

// Esse array global controla o estado de cada letra no menu.
// Cada item do array contem um objeto do tipo MenuItem, que guarda se o item 
// de menu está Aberto/Fechado e se já foi ou não preenchido.
var arrayLetras = new Array();

// Classe MenuItem.
function MenuItem(aberto, preenchido) {
	this.mbAberto = aberto;
	this.mbPreenchido = preenchido;
}

// Função chamada no OnLoad do <body>. Serve pra inicializar o array
// de letras com seus estados inicias, ou seja, todos estão fechados
// e tb nunca foram preenchidos.
function CriaMenuItens() {
	var a;
	
	// Vou da 'A' a 'Z'
	for (var i = 48; i <= 90; i++) {

		// Crio novo MenuItem
		var menuItem = new MenuItem(false, false);

		a = String.fromCharCode(i);
		arrayLetras[a] = menuItem;
	}
}

// Funcao q mostra ou esconde o item do menu.
// Se o item de menu ainda nao foi preenchido nenhuma vez, irá
// chamar um Ajax pra preencher SOMENTE UMA VEZ. ;)
function Toggle(letra) {
       
    // Se tava aberto, fecho a bagaça
	if (EstaAberta(letra)) {                
		FechaMenu(letra);
	}
	else {

		// Tava fechado, então abro a bagaça
		AbreMenu(letra);

		// Verifico se o menu já foi preenchido com os artistas
		if (!EstaPreenchido(letra)) {

			// Mostra a DIV de carregamento ... 
 	        divArtista = $('div_letra_' + letra);
			divArtista.innerHTML = "<div align='center'><br/><img src='" + url_root + "img/ajax-loader.gif'/><br/><br/></div>";						

			var url =  location.protocol + "//" + location.host + '/ajax/menu_lista_artistas.php';	
			if (window.XMLHttpRequest)
			  {// code for IE7+, Firefox, Chrome, Opera, Safari
			  xmlhttp=new XMLHttpRequest();
			  }
			else
			  {// code for IE6, IE5
			  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
				document.getElementById('div_letra_' + letra).innerHTML = xmlhttp.responseText;
				}
			  }			  
			xmlhttp.open("GET", url + '?LETRA_ART=' + letra, true);
			xmlhttp.send();
    
			SetPreenchido(letra);

		}
		else {
			// Ja tava preenchido, nao faço nada...
			//letraTemp = null;
		}                
	}
}

// Verifica se o item de menu tá aberto
function EstaAberta(letra) {
	return (arrayLetras[letra].mbAberto == true);
}

// Verifica se o item de menu tá preenchido
function EstaPreenchido(letra) {
	return (arrayLetras[letra].mbPreenchido == true);
}

// Marca um item de menu como preenchido
function SetPreenchido(letra) {
	arrayLetras[letra].mbPreenchido = true;
}

// Fecha um item de menu tanto visualmente (DIV) quanto logicamente
// no array de controle
function FechaMenu(letra) {
	var divLetra = document.getElementById('div_letra_' + letra);
	divLetra.style.display = 'none';
	arrayLetras[letra].mbAberto = false;
}

// Abre um item de menu tanto visualmente (DIV) quanto logicamente
// no array de controle
function AbreMenu(letra) {
	var divLetra = document.getElementById('div_letra_' + letra);
	divLetra.style.display = '';
	arrayLetras[letra].mbAberto = true;
}

// Pega um item do XML
function pegaValor(no) {

	if (no.childNodes.length > 0) {
		return no.firstChild.nodeValue; //Tem filho
	}
	else {
		try {
			return no.nodeValue;
		}
		catch (e) {
			return "";
		}
	}
}
