var dimensaoImg = "";
var imagensSrc = "";
var contImg = 0;

function ajaxConfiguraFotoNoticiaVer(imagem){

	if (imagem == ""){
		var x = document.getElementById("NoticiaConteudo").getElementsByTagName("img");

		if (x.length > 0)
			imagem = x[0].src;
	}

	if (imagem != ""){
		var url="../ajax/configurafotonoticiaver/configurafotonoticiaver.php?imagem="+imagem;
		
		requisicaoHTTP("GET",url,true,50);
	} else {
		trataDadosConfiguraFotoNoticiaVer2();

	}
}


function trataDadosConfiguraFotoNoticiaVer2(){
	AcaoEnquete('', 0);
}

function trataDadosConfiguraFotoNoticiaVer(){
	// Chama objetos
	this.dimensaoImg = ajax.responseText;
	configuraFotoNoticiaVer(this.contImg);
//	AcaoEnquete('', 0);
}

function configuraFotoNoticiaVer(i){
	var x = document.getElementById("NoticiaConteudo").getElementsByTagName("img");

	if (i < x.length){
		if (x[i].src != "imagens/balao.gif"){
			
			var criarAmpliar = "";
			var dimensao = Array();
			dimensao = lg_explode(';', this.dimensaoImg);

			if (x[i].width > 730 || x[i].height > 730){
				if (x[i].width > 730)
					var novaWidth = 730;
					
				if (x[i].height > 730)
					var novaHeight = 730;
				
				if ((x[i].width < x[i].height)) {
				   novaWidth = "" + parseInt((730 / x[i].height) * x[i].width);
				} else {
				   novaHeight = "" + parseInt((730 / x[i].width) * x[i].height);
				}
				
				x[i].width = novaWidth;
				x[i].height = novaHeight;
				
				// Retira o endereco do site (ex: www.portalcatalao.com.br/) do caminho da imagem
				var srcImg = lg_explode('painel/', x[i].src);
				srcImg = lg_array_reverse(srcImg, false);
				srcImg[0] = "painel/"+srcImg[0]; 
				
				x[i].src = "../thumbperfil.php?img="+srcImg[0]+"&w="+novaWidth+"&h="+novaHeight;
				
			} else if (dimensao[0] > x[i].width || dimensao[1] > x[i].height){
				// Retira o endereco do site (ex: www.portalcatalao.com.br/) do caminho da imagem
				var srcImg = lg_explode('painel/', x[i].src);
				srcImg = lg_array_reverse(srcImg, false);
				srcImg[0] = "painel/"+srcImg[0]; 
				
				x[i].src = "../thumbperfil.php?img="+srcImg[0]+"&w="+x[i].width+"&h="+x[i].height;
				
			} else {
				criarAmpliar = "NAO";
			}
			
			if (criarAmpliar != "NAO"){
				// Configura a imagem
				x[i].border = 0;
				x[i].style.cursor = "pointer";
				x[i].onclick = function() {msgFotoNoticiaVer(x[i].src, x[i].alt, dimensao[0], dimensao[1]);};
				
				// Cria link para quando clicar ir para o início da página
				linkImg = document.createElement('a'); 
				linkImg.setAttribute('href', '#');

				var paiAtual = x[i].parentNode;
				paiAtual.insertBefore(linkImg, x[i]);
				
				linkImg.appendChild(x[i]);
				
				// cria a camada responsável por ampliar a imagem
				var novaCamada = document.createElement('div');
				novaCamada.className = 'div_img_noticiaver';
				novaCamada.setAttribute('id','divNotVer'+i);
				novaCamada.setAttribute('bgcolor','#00CC00');
				novaCamada.setAttribute('align','left');
				novaCamada.setAttribute('width',x[i].width);
				novaCamada.setAttribute('height',x[i].height);
				novaCamada.innerHTML = "<a href='#'><img src='../imagens/ampliar.png' border='0' vspace='2' onclick=\"msgFotoNoticiaVer('"+x[i].src+"', '"+x[i].alt+"', '"+dimensao[0]+"', '"+dimensao[1]+"')\"></a>";

				document.body.appendChild(novaCamada);
		
				var esq = calcularPosJanela(x[i],'offsetLeft');
				var cima = calcularPosJanela(x[i],'offsetTop');

				divv = document.getElementById("divNotVer"+i);
				divv.style.width = x[i].width + "px";
				divv.style.left = esq + "px";
				divv.style.top = cima + "px";
			}
		}

		if (x.length > i+1){
			this.contImg ++;
			this.dimensaoImg = "";
			ajaxConfiguraFotoNoticiaVer(x[i+1].src);
		}
	}
}
function msgFotoNoticiaVer(img, alt, widthOrig, heightOrig){
	// Cria os div	
	if (document.getElementById("divJanelaFotoNoticiaVer") == null)
		criarDiv("divJanelaFotoNoticiaVer", "janelaTransparente2");

	if (document.getElementById("divJanelaInternaFotoNoticiaVer") == null)
		criarDiv("divJanelaInternaFotoNoticiaVer", "janelaNormal");

	// Recupera objetos
	var divPrincipal =  document.getElementById("bodySite");
	var divJanela =  document.getElementById("divJanelaFotoNoticiaVer");
	var divJanelaInt =  document.getElementById("divJanelaInternaFotoNoticiaVer");	
	
	// Oculta a barra de rolagem
	var bSite = document.getElementById("bodySite");
	bSite.style.overflowY = "hidden";
	bSite.style.overflowX = "hidden";

	// Coloca todos os div's como visíveis
	divPrincipal.style.display = "block";
	divJanela.style.display = "block";
	divJanelaInt.style.display = "block";

	// Pega as caracteristicas da dimensão da tela
	var cima = calcularPosJanela(divPrincipal,'offsetTop') + divPrincipal.offsetHeight;

	// Configura janela transparente
	divJanela.style.height = cima + "px";

	divJanelaInt.innerHTML = "<table width='100%' height='600px' border='0' cellspacing='0' cellpadding='0'><tr><td align='center' valign='middle'><table width='100%' height='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center' valign='bottom'><div id='divFotoNoticiaVerFechar' style='background-color:#FFFFFF; width:0px;' class='subtitulo'></div></td></tr><tr><td align='center' valign='top' ><div id='divFotoNoticiaVer' style='background-color:#FFFFFF; width:0px; height:0px'></div></td></tr></table></td></tr></table>";

	objDivFotoNoticiaVer = document.getElementById("divFotoNoticiaVer");
	objDivFotoNoticiaVerFechar = document.getElementById("divFotoNoticiaVerFechar");

	objDivFotoNoticiaVer.style.width = "0px";
	objDivFotoNoticiaVer.style.height = "0px";
	objDivFotoNoticiaVerFechar.style.width = "0px";
	animaJanela_numWidth = 0;
	animaJanela_numHeight = 0;
	
	if (alt != "")
		heightOrig = (parseInt(heightOrig)+40);
	
	animaJanelaFotoNoticiaVer("divFotoNoticiaVer", img, alt, widthOrig, heightOrig);

}

function animaJanelaFotoNoticiaVer(nomeObj, img, alt, widthOrig, heightOrig) {
	// Prepara o Obj que será manipulado
	var obj =  document.getElementById(nomeObj);
	var objFechar =  document.getElementById(nomeObj + "Fechar");

	// Atribui valores a janela caso não tenha sido estabelecido a posição
	if (obj.style.width == 0) obj.style.width = parseInt(numWidth);
	if (obj.style.height == 0) obj.style.height = parseInt(numHeight);

	if ((parseInt(obj.style.width) <= widthOrig) || (parseInt(obj.style.height) <= heightOrig)){
		if (parseInt(obj.style.width) <= widthOrig){
			animaJanela_numWidth+=30;
			obj.style.width=animaJanela_numWidth + "px";
		}
		
		if (parseInt(obj.style.height) <= heightOrig){
			animaJanela_numHeight+=30;
			obj.style.height=animaJanela_numHeight + "px";
		}

		objFechar.style.width = obj.style.width = (parseInt(obj.style.width) + 20) + "px";
		
		animaJanela_velocidade=window.setTimeout("animaJanelaFotoNoticiaVer('"+nomeObj+"', '"+img+"', '"+alt+"', '"+widthOrig+"', '"+heightOrig+"');",1);
	} else {
		objFechar.style.width = obj.style.width = (parseInt(widthOrig) + 10) + "px";
		obj.style.height = (parseInt(heightOrig) + 10) + "px";
		// Remove o codigo na url responsavel por transformar a imagem em thumb
		var srcImg = Array();
		srcImg = lg_explode('img=', img);
		srcImg = lg_explode('&w', srcImg[1]);
		
		// Insere legenda
		var textoAlt = "";
		if (alt != ""){
			textoAlt = "<tr><td valign='middle' align='center' height='40'>"+alt+"<br></td></tr>";
		}
		
		// Insere a imagem
		obj.innerHTML = "<table width='100%' height='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center' valign='middle'  height='1'><img src='../"+srcImg[0]+"' hspace='5' onclick='fecharJanelaFotoNoticiaVer()' style='cursor:pointer'></td></tr>" + textoAlt + "</table>";


		// Insere o botão fechar
		objFechar.innerHTML = "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td><img src='../imagens/nada.gif' width='5' height='5'></td><td><img src='../imagens/nada.gif' width='5' height='5'></td></tr><tr><td align='left'><span class='subtitulo'><b>&nbsp;&nbsp;Clique aqui para fechar</b></span></td><td align='right'><img src='../imagens/btn_close.gif' onclick='fecharJanelaFotoNoticiaVer()' style='cursor:pointer' width='23' height='22' border='0' hspace='8'></td></tr><tr><td><img src='../imagens/nada.gif' width='5' height='5'></td><td><img src='../imagens/nada.gif' width='5' height='5'></td></tr></table>";

		// Coloca link para fechar janela
		var divJanela =  document.getElementById("divJanelaFotoNoticiaVer");
		var divJanelaInt =  document.getElementById("divJanelaInternaFotoNoticiaVer");
	
		// Coloca link para fechar janela
		divJanela.onclick = function() {fecharJanelaFotoNoticiaVer();};
		divJanelaInt.onclick = function() {fecharJanelaFotoNoticiaVer();};
		divJanela.style.cursor = "pointer";
		divJanelaInt.style.cursor = "pointer";
	}

}

// Fecha a galeria de fotos
function fecharJanelaFotoNoticiaVer(){
	// Armazenas os objetos que irão ser apagados
	var objApagar = Array("divJanelaFotoNoticiaVer", "divJanelaInternaFotoNoticiaVer");

	for (i=0; i<objApagar.length; i++){
		if (document.getElementById(objApagar[i]) != null){
			var tmpObj = document.getElementById(objApagar[i]);
			tmpObj.innerHTML = "";
			tmpObj.style.display = "none";
			tmpObj.style.cursor = "default";
		}
	}
	
	// Oculta a barra de rolagem
	var bSite = document.getElementById("bodySite");
	bSite.style.overflowY = "visible";
	bSite.style.overflowX = "visible";
}
