/* 
	Photoshow - Exibi dados de um Álbum de Fotos em XML 
	por Adriano Ribeiro - versão 08.2006
	adriano dot ribeiro at gmail dot com
*/

function loadXML(type)
{
	// Defini de que forma o script irá funcionar
	showType = type;
	
	// Carrega o XML 
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = startPhotoshow;
	}
	// Carrega o XML para o IE 
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) startPhotoshow()
		};
 	}
	else
	{
		alert('Não foi possível carregar este script');
		return;
	}
	xmlDoc.load("/img/home/fotos.xml");
}

// Função que seleciona um nó aleatoriamente
function randomNode(parentNode) 
{
	totalItens = 0;
	
	// Verifica o total de nós
	for (i=0; i<parentNode.length; i++)
	{
		if (parentNode[i].nodeType != 1) continue;
			totalItens += 1;
	}
	randomNumber = Math.floor (Math.random() * totalItens) + 1;
	return randomNumber;
}

// Inicia a exibição
function startPhotoshow()
{
	// Variáveis
	fotos = xmlDoc.getElementsByTagName('fotos');
	albumPointer  = 0;

	// Defini o álbum selecionado de acordo com a variável showType
	
	// 1. Escolhe o álbum através da querystring
	if (showType == 'title') {
		albumSelected = aValue;
	}
	// 2. Escolhe o álbum aleatoriamente
	else {
		albumSelected = randomNode(fotos[0].childNodes);
	}	
		
	for (i=0; i<fotos[0].childNodes.length; i++)
	{
		if (fotos[0].childNodes[i].nodeType != 1) continue;

		albumPointer += 1;	
		// Verifica se é o álbum selecionado
		if (albumPointer == albumSelected) {
		
			albumTit = document.createTextNode(fotos[0].childNodes[i].getAttribute('titulo'));
			albumData = document.createTextNode(fotos[0].childNodes[i].getAttribute('data'));
			albumId = document.createTextNode(fotos[0].childNodes[i].getAttribute('id'));
			albumNode = i;
			
			if (showType == 'title') {
				document.getElementById('albumTitle').appendChild(albumTit);
				break;
			} else {
				showThumb();
			}	

		}
	}

}

// Exibi o thumbnail
function showThumb() {

	var fotoPointer = 0;
	var container = document.createElement('div');
//	var divfoto = document.createElement('div');
	var path = document.createElement('a');
	var br = document.createElement('br');
	var thumb = document.createElement('img');
	var legenda = document.createElement('small');
//	divfoto.className="foto";
	
	albumNode = 0;
	
	// Seleciona uma foto aleatória a partir do álbum previamente selecioando
	fotoSelected = randomNode(fotos[0].childNodes);
	
	for (i=0; i<fotos[0].childNodes.length; i++) {
		if (fotos[0].childNodes[i].nodeType != 1) continue;

		fotoPointer += 1;

		// Verifica se é a foto selecionado
		if (fotoPointer == fotoSelected) {
		
			var theUrl = document.createTextNode(fotos[0].childNodes[i].getAttribute('id'));
			fotoNumber = i;
			
		// Aplica a url da imagem e o link
				
			thumb.src= "/img//home/" + theUrl.nodeValue +".jpg";

		//	path.href= "/fotos/index.htm?f=" + fotoSelected;
			path.href= "/guia/index.php";
			
			path.appendChild(thumb);
//			divfoto.appendChild(path);
			
			// Aplica o crédito

			// Verifica se a imagem tem legenda ou crédito no IE
			if (fotos[0].childNodes[fotoNumber].childNodes.length > 0) {
			
				for (i=0; i<fotos[0].childNodes[fotoNumber].childNodes.length; i++) {
					// Verifica se a imagem tem legenda
					if (fotos[0].childNodes[fotoNumber].childNodes[i].nodeName == "credito") {
							// Aplica a legenda
							var credito = document.createTextNode(fotos[0].childNodes[fotoNumber].childNodes[i].firstChild.nodeValue);
							legenda.appendChild(credito);
//							divfoto.appendChild(legenda);
					}
				}
			} 

			// Monta o botão "Mais Fotos"
			var bt_mais_text = document.createTextNode("Conheça as  melhores dicas de roteiro turístico e cultural da cidade")
			var bt_mais_link = document.createElement('a');
			var bt_mais = document.createElement('em');
			
			bt_mais.className="mais";
			bt_mais_link.href="/guia/index.php";
			
			bt_mais_link.appendChild(bt_mais_text);
			bt_mais.appendChild(bt_mais_link);
			container.appendChild(bt_mais);
			
			// Monta o código
			container.appendChild(legenda);
			container.appendChild(br);
			container.appendChild(path);
			container.appendChild(bt_mais);
			
			// Exibi a imagem
			document.getElementById('thumbshow').appendChild(container);
			break;
		} 
	}
}

// Retire o comentário para rodar o js sem chamada no HTML
// window.onload = loadXML;
