// Utilizado para evitar de digitar: document.getElementById toda hora, tornando o processo mais prático
function gE(ID) {
	return document.getElementById(ID);
}

// Utilizado para evitar de digitar: document.getElementsByTagName toda hora, tornando o processo mais prático
function gEs(tag) {
	return document.getElementsByTagName(tag);
}



startList = function() {
if (document.all&&document.getElementById) {
menu3 = document.getElementById("menu2");
for (i=0; i<menu3.childNodes.length; i++) {
node = menu3.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
  }
  node.onmouseout=function() {
  this.className=this.className.replace
	(" over", "");
   }
   }
  }
 }
}




function valida_form(){
		var nome  = gE('nome').value;
		var msg  = gE('msg').value;

		if(nome==""){ alert("Digite seu Nome"); gE('nome').focus(); return false; } 
		if(msg==""){ alert("Escreva sua Mensagem"); gE('msg').focus(); return false; } 
	}




// Esta função instancia o objeto XMLHttpRequest
function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}




// Função que chama outras funções
function loadFunctions() {
	ativarBtDados();
}




// Função que ativa a visualizacao das fotos
function ativarBtDados() {
	// Seleciona todas as tags a, os links.
	linques = gEs('a');
	for(var x=0;x<linques.length;x++) {
		atrlinques = linques[x].getAttribute('rel');
		 if(atrlinques=="foto") {	
			 linques[x].onclick = function() {
				// Pego o ID da foto
				// Se fazer sem a palavra-chave this, o script sempre pegará o último da lista
				// O this neste caso é IMPORTANTISSÍMO
				var ident = this.getAttribute('id');
				var identidade = this.getAttribute('rel');
				// Executa a função que cria o fundo sobre página
				exibirBgBody();
				// Cria um div - definida como boxCad - que armazenará a foto
				boxCad(identidade);
				// Inicia o Ajax, através da variável Ajax
				var ajax = openAjax();
				// A tag bgBody conterá os dados
				var recipiente = gE('boxCad');
				// Informamos o método e a página que será requisitada
				ajax.open('GET', 'foto.asp?ajax=true&nome=' + ident, true); 
				// bla
				ajax.onreadystatechange = function() {
					if (ajax.readyState == 1) {
						// Cria o efeito de loading
						loading(true);	
					} // if->readyState->1
					if (ajax.readyState == 4) {
						if (ajax.status == 200) {
							// Remove o efeito de loading
							loading(false);
							// Pega o conteúdo - HTML - da página requisitada: foto.asp?ajax=true e coloca dentra da div definida na variável recipiente
							recipiente.innerHTML = ajax.responseText;
							// Chama a função que trabalha sobre os links para outras paginas e o voltar
							btFechar1();
						} // if-status->200
					} // if->readyState->4
				} // ajax->onreadystatechange
				// Envia a requisição
				ajax.send(null);
				// Evita o reload da página
				return false;
			 }
		}
	}
}
	
// Utilizado para criar o fundo sobre a página (viewport), body.
function exibirBgBody() {
	// Seleciona a tag body. item(0) por que só existe uma tag body
	var tagBody = gEs('body').item(0);
	var sizesPage = getPageSize();
	// Vamos criar uma tag div
	var bgBody = document.createElement('div');
	// Setar o atributo ID a div criada
	bgBody.setAttribute('id','bgBody');
	// Essa div terá o tamanho exato da página, menos a margen do topo
	bgBody.style.height = (arrayPageSize[1] - 191) + 'px';
	// E as seguintes medidas
	bgBody.style.width = '703px';
	bgBody.style.marginTop = '130px'; 
	bgBody.style.marginLeft = '26px'; 
	// Evita criar a div novamente
	if (!gE('bgBody')) {
		tagBody.insertBefore(bgBody, tagBody.firstChild);
	}	
}

// Cria a div denominada como boxCad, a qual conterá a foto
function boxCad(margem) {
	// Cria um 'container' que comportará a foto
	var objBody = gEs('body').item(0);
	var boxCad = document.createElement('div');
	boxCad.setAttribute('id','boxCad');
	objBody.insertBefore(boxCad, objBody.firstChild);
}

// Utilizado para criar o efeito de loading
function loading(opt) {
	if (opt == true) {
		// A tag que receberá a img de loading
		var refer = gE('bgBody');
		// O tamanho da referida tag
		var referHeight = refer.offsetHeight;
		// Dizemos que os elementos dentro dela será alinhado ao centro
		refer.style.textAlign = 'center';
		// Criamos uma imagem, img.
		var img = document.createElement('img');
		// Informamos o caminho da img
		img.setAttribute('src','img/imgLoading.gif');
		// Setamos um atributo ID na img criada
		img.setAttribute('id','loading');
		// Definimos seu tamanho
		img.setAttribute('width','126');
		// Dizemos que o margin-top será a metada do tamanho da div
		img.style.marginTop = (referHeight /2) + 'px';
		// Evita que seja criada duas ou mais img de loading
		if (!document.getElementById('loading')) {
			// Insere a img na tag informada na variável refer
			refer.insertBefore(img, refer.firstChild);
		}
	} else if (opt == false) {
		// Referenciamos a img de login através de seu ID
		var imgLoading = gE('loading');
		// Removemos a img de loading
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
}

// Funções que será vinculadas ao botão Fechar
function btFechar1() {
	// Se não houver o botao aborta a função
	if (!gE('btFechar1')) return false;

	gE('btFechar1').onclick = function() {
		// Elimina o fundo criado para o body e a div - boxCad.
		removerDivs();
		// Cancela a função do link
		return false;
	}
}


// Esta função elimina da página o fundo criado sobre o body e o boxCad;
function removerDivs() {
	var bgBody = gE('bgBody');
	var boxCad = gE('boxCad');
	bgBody.parentNode.removeChild(bgBody);
	boxCad.parentNode.removeChild(boxCad);
}




window.onload = function(){
	startList();
	loadFunctions();
}




/* Funções de terceiros */
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 

}