	/** popup.js
	  * 
	  * Este arquivo varre todas as tags img da página e acham aquelas que têm a classe "popup".
	  * Então ele coloca um link nessa tag para que quando clicada ela abra um popup com a versão
	  * ampliada da imagem, que está um nível acima da pasta atual da imagem (../);
	  */

	/**
	  * Seleciona os elementos de acordo com a tag e a classe especificada
	  */
	function getElementsByClassName( pai, classe, tag ) {
		var filhos = pai.getElementsByTagName( tag );
		var resultado = Array();
		
		for ( var i = 0; i < filhos.length; i++  ) {
			/* Vê se a tag possui a classe em seu className */
			if ( ( " " + filhos[i].className + " " ).indexOf(" " + classe + " ") > -1 )
				resultado[ resultado.length ] = filhos[i];
		}
		
		return resultado;
	}
	
	function abrePopup( imagem ) {
		var img = document.createElement( "img" );
		img.setAttribute( "src", imagem );
		img.onload = function() {
			var popup = window.open( "about:blank", "_blank", "width=" + ( img.width + 20 ) + ", height=" + ( img.height + 50 ) );
			//popup.document.appendChild( img );
			popup.document.write( "<img src=\"" + img.src + "\" \/>" );
			// Daria muito trabalho usar CSS corretamente aqui :P
			popup.document.write( "<div style=\"text-align: center; margin-top: .5em; font-family: Verdana, Arial, Helvetica; font-size: 11px;\"><a href=\"javascript:window.close()\" style=\" text-decoration: none; color: #58688A;\">[Fechar]</a></div>" );
			popup.document.title = "Imagem ampliada";
		}
	}
	
	/**
	  * Coloca um link para ampliar a imagem selecionada
	  */
	function criaPopup( img ) {
		var ancora = document.createElement( "a" );
		var href = img.getAttribute("src");
		var hrefAmp = href.substring( 0, href.substring( 0, href.lastIndexOf( "/" ) ).lastIndexOf( "/" ) ) + href.substring( href.lastIndexOf( "/" ), href.length );
		
		ancora.setAttribute( "href", "javascript:void(0);" );
		//ancora.setAttribute( "onclick", "abrePopup( \"" + hrefAmp + "\" )" );
		ancora.onclick = function() { eval("abrePopup( \"" + hrefAmp + "\" )"); };
		
		/* Insere a âncora antes da imagem */
		img.parentNode.insertBefore( ancora, img );
		img.parentNode.removeChild( img );
		
		ancora.appendChild( img );
	}
	
	function iniciaPopups() {
		var imagens = getElementsByClassName( document, "popup", "img" );
		for ( var i = 0; i < imagens.length; i++ )
			criaPopup( imagens[i] );
	}
	
	if ( window.addEventListener )
		window.addEventListener( "load", iniciaPopups, false );
	else if ( window.attachEvent )
		window.attachEvent( "onload", iniciaPopups );
