// Detecta o Browser
var agent = navigator.userAgent;
function browserDet(){
	if (agent.indexOf('Gecko') != -1){tipo = 'Mozilla';}
	else if (agent.indexOf('Opera') != -1){tipo = 'Opera';}
	else if (agent.indexOf('MSIE ') != -1){tipo = 'IE';}
	this.tipo = tipo;
}
browser = new browserDet();
// Aplica CSS para IE
if (browser.tipo == 'IE'){document.write('<style type="text/css">@import "../../css/ie.css";</style>');}



// SETA AUTOMATICAMENTE FUNÇÃO DE ONMOUSEOVER E ONMOUSEOUT PARA BOTÕES
function btnOver(obj){
	getSrc = obj.src;
	if (obj.src.indexOf('_over.gif') != -1){obj.src = getSrc.replace('_over.gif', '_out.gif');}
	else {obj.src = getSrc.replace('_out.gif', '_over.gif');}
}
function setEventBtn(){
	// PEGA TODOS OS ELEMENTOS <A>
	getLinks = document.getElementsByTagName('a');
	i=0;

	// TESTA TODOS OS <A> EM BUSCA DOS QUE TEM UMA <IMG> COMO NODE
	while(i < getLinks.length){
		i2=0;
		getNodes = getLinks[i].childNodes[i2];
		while(i2 < getLinks[i].childNodes.length){
			// ENCONTRANDO UM NODE <IMG> IRÁ TESAR SE É UMA IMAGEM CAMBEAVEL - PROCURANDO POR "_out.gif"
			if(getLinks[i].childNodes[i2].nodeName == 'IMG'){
				getSrc = getLinks[i].childNodes[i2].src;
				if (getSrc.indexOf('_out.gif') != -1){

					// APLICA O setAttribute CONFORME O CASO - IE / OUTROS
					if (browser.tipo != 'IE'){
						getLinks[i].childNodes[i2].setAttribute('onmouseover', 'btnOver(this)');
						getLinks[i].childNodes[i2].setAttribute('onmouseout', 'btnOver(this)');
					}
					else {
						getLinks[i].childNodes[i2].setAttribute('onmouseover', function anonymous(){btnOver(this);});
						getLinks[i].childNodes[i2].setAttribute('onmouseout', function anonymous(){btnOver(this);});
					}
				}
			}
			i2++;
		}
		i++;
	}
}


// SETA AUTOMATICAMENTE UM BGR TRANSPARENTE PARA OS ELEMENTOS
function setBGRTransparency(){
	// PEGA TODOS OS ELEMENTOS <DIV>
	getDiv = document.getElementsByTagName('DIV');
//	alert(getDiv.length);

	i=0;
	arr=0;
	nodeMedidas = new Array();
	while(i < getDiv.length){
		// IDENTIFICA ONDE HÁ UM NODE ONDE IRÁ TRANSPARÊNCIA
		if(getDiv[i].className == 'jsOverflow'){
			// PEGA AS MEDIDAS DOS NODES ONDE ESTÃO OS ITENS ANTES DE APLICAR A TRANSPARENCIA.
			nodeMedidas[arr] = new Array(2);
				nodeMedidas[arr][0] = i;
				nodeMedidas[arr][1] = getDiv[i].parentNode.clientWidth;
//				alert(getDiv[i].parentNode.innerHTML);
				nodeMedidas[arr][2] = getDiv[i].parentNode.clientHeight;
			arr++;
		}
		i++;
	}
	ni=0;
	// APLICA AS MEDIDAS NO DIV jsOverflow E NO jsTransp FILHO DO MESMO
	while(ni < arr){
		getIndex = nodeMedidas[ni][0];
		getDiv[getIndex].style.width = nodeMedidas[ni][1] + 'px';
		getDiv[getIndex].style.height = nodeMedidas[ni][2] + 'px';

		// +1 PQ O DIV SEQUINTE É SEMPRE O jsTransp
		getDiv[getIndex+1].style.width = nodeMedidas[ni][1] + 'px';
		getDiv[getIndex+1].style.height = nodeMedidas[ni][2] + 'px';
		ni++;
	}
}

function back(){history.back();}
function galPopup(galeria, sub){
	sWidth = screen.width;
	sHeight = screen.height;

	if (browser.tipo == 'IE'){
		sWidth = sWidth - 10;
		sHeight = sHeight - 65;
	}

	getURL = document.location.toString();

	if (getURL.indexOf('home/index.aspx') != -1){
		window.open ('../fotografia/popups.aspx?gal='+galeria+'&sub='+sub+'&img=ini', 'Galeria', 'width=' + sWidth + ', height=' + sHeight + ', top=0, left=0, scrollbars=yes');
	}
	else{
		window.open ('popups.aspx?gal='+galeria+'&sub='+sub+'&img=ini', 'Galeria', 'width=' + sWidth + ', height=' + sHeight + ', top=0, left=0, scrollbars=yes');
	}
}

function flogPopup(nome, numero){
	sWidth = screen.width;
	sHeight = screen.height;

	if (browser.tipo == 'IE'){
		sWidth = sWidth - 10;
		sHeight = sHeight - 65;
	}

	getURL = document.location.toString();

	if (getURL.indexOf('home/index.aspx') != -1){
		window.open ('../fotolog/popups.aspx?flog='+nome+'&nflog='+numero+'&img=ini', 'Flog', 'width=' + sWidth + ', height=' + sHeight + ', top=0, left=0, scrollbars=yes');
	}
	else{
		window.open ('popups.aspx?flog='+nome+'&nflog='+numero+'&img=ini', 'Flog', 'width=' + sWidth + ', height=' + sHeight + ', top=0, left=0, scrollbars=yes');
	}
}


// FUNÇÕES PARA FORMULARIOS
function jsGetResult(jsQuest) {
    result = confirm(jsQuest);
    document.getElementById('jsPostResult').value = result;
}



onload = autoSetOnLoad;
function autoSetOnLoad(){
	setHeightXSpace();
	setBGRTransparency();

	setEventBtn();
	abreLinksExternos();
}




function abreLinksExternos() {
    if(document.getElementsByTagName) {
        var anchors = document.getElementsByTagName('a');
        for(var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if(anchor.getAttribute("href") && anchor.getAttribute('rel')=='externo') { // <-- É necessário inserir rel="externo" no link
                anchor.target = '_blank';
                //var title = anchor.title + ' (Este link abre uma nova janela)'; // <-- Insere este texto no final do Title do link
                //anchor.title = title;
            }
        }
    }
}



// SETA AUTOMATICAMENTE A ALTURA DOS DIVS EM COLUNAS
function setHeightXSpace(){
	// PEGA TODOS OS ELEMENTOS <DIV>
	getDiv = document.getElementsByTagName('DIV');


	i=0;
	arr=0; // Pegará o numero total de Divs que devem ser testadas
	nodeMedidas = new Array();
	// PEGA A ALTURA DE TODAS AS DIVS COM O xSpace E ANOTA SUA ALTURA NO ARRAY
	while(i < getDiv.length){
		if(getDiv[i].className == 'xSpace'){
			nodeMedidas[arr] = new Array(1);
				nodeMedidas[arr][0] = i; // Pega o index da Div em questão
				nodeMedidas[arr][1] = getDiv[i].clientHeight; // Pega altura do div
			arr++;
		}
		i++;
	}


	i=0;
	tD = arr/3; // Total de DIVs por coluna
	tD2 = tD*2;
	// APLICA AS MEDIDAS NOS DIVS space_cX_X QUE TENHAM a CLASSE xSpace
	while (i < tD)
	{
		comparaMedidas = new Array(2);
		
		// Carrega alturas para compara-las e determinar qual usar
		comparaMedidas[0] = nodeMedidas[i][1];
		comparaMedidas[1] = nodeMedidas[i+tD][1];
		comparaMedidas[2] = nodeMedidas[i+tD2][1];

		// Determina qual é maior [ficará no index 2 do array]
		comparaMedidas.sort(sortIt);

		if (comparaMedidas[2] > 24)
		{
			maiorAltura = (comparaMedidas[2] + 5) + 'px';
		}
		else
		{
			maiorAltura = (comparaMedidas[2]) + 'px';
		}

		// Aplica as alturas aos DIVs determinados
		document.getElementById('space_c1_' + i).style.height = maiorAltura;
		document.getElementById('space_c2_' + i).style.height = maiorAltura;
		document.getElementById('space_c3_' + i).style.height = maiorAltura;
		
		i++;
	}
}

// FUNÇÃO QUE INVERTE O ARREY USANDO O MÉTODO nomeArray.sort(sortIt)
function sortIt(a,b){
	return(a-b);
}

