/**
 * FirebugX
 */
(function(){
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}
})();


// Versão 2.0
var Album =  {

	"$": function () { return document.getElementById(argumes[0]); },

	// objeto da tabela
	"objTable": null,
	
	// total de imagens
	"qtdImages": legenda.length,

	// objeto imagem
	"objImagem": null,
	
	// Objeto da última TR clicada
	"objLastTr": null,
	"numLastFoto": null,

	// monta a primeira lista de imagens
	"montar": function () {

		// obj Table
		this.objTable = $("tblListaFotos").getElementsByTagName("tbody")[0];

    console.log("thumb order: " + thumb_order);

    if(thumb_order == "DESC") {
		  for(var i = this.qtdImages - 1; i >= 0; i--) {
		    console.log("add image: " + i);
        this.createThumbs(i);
      }
		}
		else {
		  for(var i = 0; i < this.qtdImages; i++){
		    console.log("add image: " + i);
        this.createThumbs(i);
      }
		}

		/* RegExp created via www.regexp.com.br */
		var regExp = new RegExp('(abrefoto=[0-9]{1,3})', 'gi');
		var myString = location.href;

		if(myString.match(regExp) != null) eval("var " + myString.match(regExp));
		else															 var abrefoto = this.qtdImages;

    /*if(thumb_order == "DESC") {
      if(myString.match(regExp))
        abrefoto = this.qtdImages - (abrefoto - 1);
      else
        abrefoto = this.qtdImages;
    }*/
    
    console.log("abrefoto = " + abrefoto);

		if(abrefoto > this.qtdImages || abrefoto < 1)
			abrefoto = this.qtdImages;

    if(thumb_order == "ASC")
		  this.showImage(abrefoto - 1 , this.objTable.getElementsByTagName("tr")[ abrefoto - 1 ])
		else
      this.showImage(abrefoto - 1 , this.objTable.getElementsByTagName("tr")[ this.qtdImages - abrefoto ])

		this.scrollThumbs();

	},

	// Cria a lista de thumbs
	"createThumbs": function (i, acao) {

		// nova TR
		var objTr = document.createElement("tr");
				objTr.onclick = function () { Album.showImage(i, this); };

		// nova TD
		var objTd = document.createElement("td");

		// Cria a imagem para a TD
	  var objImg = document.createElement("img");
	  		objImg.src = path_fotos + "/" + id_album + "_t_" + (i+1 > 99  ? (i+1) : (( i+1 > 9 ) ? "0"+(i+1) : "00"+(i+1))) + ".jpg?time="+new Date().getTime();

		// nova TH
		var objTh  = document.createElement("th");
				objTh.className = this.numLastFoto == i ? "clicado" : "";

		var objTxt = document.createTextNode(chapeu[i]);

		// Cria todos os elementos
		objTh.appendChild(objTxt);
		objTd.appendChild(objImg);
		objTr.appendChild(objTd);
		objTr.appendChild(objTh);

		this.objTable.appendChild(objTr);

		return objTr;

	},


	// Exibe o div "carregando..."
	"carregando": function () {
		// obj div carregando
		var objCarregando = document.getElementById("divCarregando");
		var objAlbumFoto  = document.getElementById("albumFoto");
		var objLegenda    = document.getElementById("albumLegenda");
		
		// tira o link de 'enviar por e-mail'
		if(document.getElementById("envieEmail")) {
			document.getElementById("envieEmail").style.display = "none";
		}
		
		if(document.getElementById("imprimirAlbum")) {
			document.getElementById("imprimirAlbum").style.display = "none";
		}

		objCarregando.style.display = "block";
		objAlbumFoto.style.display = objLegenda.style.display = "none";

		// se img está completa
		if(this.objImagem.complete)
		{
			objCarregando.style.display = "none";
			objAlbumFoto.style.display = objLegenda.style.display = "block";
			
			// mostra o link de 'enviar por e-mail'
			if(document.getElementById("envieEmail")) {
				document.getElementById("envieEmail").style.display = "block";
				// document.getElementById("envieEmail").getElementsByTagName("a")[0].href = "http://noticias.bol.uol.com.br/enviar.jhtm?url=http://fotos.noticias.bol.uol.com.br/"+location.href.replace(/http:\/\/fotos\.noticias\.bol\.uol\.com\.br\/(.*?)\/(.*).jhtm/, "$1")+"/"+id_album+"_album.jhtm?abrefoto="+(this.numLastFoto+1);
  			// document.getElementById("envieEmail").getElementsByTagName("a")[0].href = "http://noticias.bol.uol.com.br/enviar.jhtm?url=http://fotos.noticias.bol.uol.com.br/"+location.href.replace(/\?abrefoto=([0-9]{1,3})/, "").replace(/http:\/\/fotos\.noticias\.bol\.uol\.com\.br\/(.*?)\/(.*).jhtm/, "$1")+"/"+id_album+"_album.jhtm?abrefoto="+(this.numLastFoto+1);
  			
  			//if(thumb_order == "ASC")
          document.getElementById("envieEmail").getElementsByTagName("a")[0].href = "http://noticias.bol.uol.com.br/indicar-link.jhtm?title="+escape( (typeof nome_album == "undefined" ? titulo_album : nome_album) )+"&url=http://fotos.noticias.bol.uol.com.br/"+location.href.replace(/\?abrefoto=([0-9]{1,3})/, "").replace(/http:\/\/fotos\.noticias\.bol\.uol\.com\.br\/(.*?)\/(.*).jhtm/, "$1")+"/"+id_album+"_album.jhtm?abrefoto="+(this.numLastFoto+1);
        //else
        //  document.getElementById("envieEmail").getElementsByTagName("a")[0].href = "http://noticias.bol.uol.com.br/indicar-link.jhtm?title="+escape( (typeof nome_album == "undefined" ? titulo_album : nome_album) )+"&url=http://fotos.noticias.bol.uol.com.br/"+location.href.replace(/\?abrefoto=([0-9]{1,3})/, "").replace(/http:\/\/fotos\.noticias\.bol\.uol\.com\.br\/(.*?)\/(.*).jhtm/, "$1")+"/"+id_album+"_album.jhtm?abrefoto="+( this.qtdImages - this.numLastFoto );
			}
			
			if(document.getElementById("imprimirAlbum")) {
			 document.getElementById("imprimirAlbum").style.display = "block";
			}
		}
		else
			setTimeout("Album.carregando()", 300);
	},

	// Toda preparação (e execução) para exibir uma imagem
	"showImage": function (i) {

    console.log("show image number: " + i);

		this.numLastFoto = i;
		this.objImagem = null;

		// Retira a classe da antiga TR ativa
		if(this.objLastTr != null)
		{
			this.objLastTr.getElementsByTagName("td")[0].className = "";
			this.objLastTr.getElementsByTagName("th")[0].className = "";
		}

		// Guarda o objeto da TR clicada
		this.objLastTr = typeof arguments[1] == "undefined" ? this.objLastTr : arguments[1];

		// altera a classe da TR clicada
		var objTd = this.objLastTr.getElementsByTagName("td")[0].className = "clicado";
		var objTh = this.objLastTr.getElementsByTagName("th")[0].className = "clicado";

		// Obj do div onde a imagem irá aparecer
		var objFoto = document.getElementById("albumFoto");

		// Remove a imagem anterior
		if(objFoto.getElementsByTagName("img").length > 0)
			objFoto.removeChild(objFoto.getElementsByTagName("img")[0]);

		// Obj do div onde a legenda irá aparecer
		var objLegenda = document.getElementById("albumLegenda");
				objLegenda.innerHTML = "";

		// Pré-carrega as imagens
		this.objImagem = new Image();
		this.objImagem.src = path_fotos + "/" + id_album + "_f_" + (i+1 > 99  ? (i+1) : (( i+1 > 9 ) ? "0"+(i+1) : "00"+(i+1))) + ".jpg?time="+new Date().getTime();

		// div carregando...
		this.carregando();

		// cria o obj da imagem...
		var objImg = document.createElement("img");
		// ... e altera o source para a imagem já carregada		
		objImg.src = this.objImagem.src;

		// cria o objeto
		objFoto.appendChild(objImg);

		// adiciona a legenda
		objLegenda.innerHTML = legenda[this.numLastFoto];

		if(url[this.numLastFoto] != "")
			objLegenda.innerHTML += " <a href='"+url[this.numLastFoto]+"'>Mais</a>"

		objLegenda.innerHTML += "<br /><cite>" + credito[this.numLastFoto] + "</cite>";

		// se houver enquete
		if(typeof enquete != "undefined" && enquete)
			objLegenda.innerHTML += "<div class='btEnquete'><a href='javascript://' onclick=\"location.href='"+urlTplEnquete.replace("NUMIMG", (this.numLastFoto+1))+"'\"><img src='http://f.i.bol.com.br/bt_votar.gif' title='Votar nesta foto' alt='Votar nesta foto' /></a><br />"+ (tamanho[this.numLastFoto] == "440x320" ? "" : "<br />") +"<a href='javascript://' onclick=\"location.href='"+urlTplEnquete.replace("&voto=NUMIMG", "#r")+"'\">Ver resultado</a></div>"

		// tamanho da imagem
		var tamanhoImg = new Array();
				tamanhoImg = tamanho[this.numLastFoto].split("x");

		objImg.className = tamanho[this.numLastFoto] == "300x200" || tamanhoImg[0] > 325 ? "legendaEmBaixo" : "legendaDoLado";
		document.getElementById("divFoto").className = "ftTam"+tamanho[this.numLastFoto];

		// dá um hit no servidor
		//parent.albx.location.href = id_album + "_album-hit.htm";
		//parent.albx.location.href = id_album + "_album.jhtm";
		parent.albx.location.replace(id_album + "_album.jhtm");

		// Omniture
		if( typeof uol_sc != "undefined" && (typeof uol_sc.t).toLowerCase() == 'function' ){
      uol_sc.prop5='fotoId ' + (this.numLastFoto+1);
      uol_sc.t();
    }
	},

	"scrollThumbs": function () {
    if(thumb_order == "DESC")
		  var calc = this.qtdImages - this.numLastFoto;
		else
		  var calc = this.numLastFoto;

		var position = 76 * (calc - 4);
		$("blFotos").scrollTop = position;
    console.log("scroll thumbs: " + position);
	},

	"proximo": function () {

    console.log("number last image: " + this.numLastFoto);

    if(thumb_order == "DESC") {
  		// se for a ultima foto, volta para o inicio	
  		var lastFoto = !this.numLastFoto ? true : false;

      if(lastFoto)
        this.numLastFoto = this.qtdImages;

      console.log("last image? " + (lastFoto ? "yes" : "no"));
      console.log("next image: " + (this.numLastFoto - 1));

      this.showImage( this.numLastFoto - 1 );
    }
    else {
  		// se for a ultima foto, volta para o inicio	
  		var lastFoto = (this.numLastFoto == this.qtdImages - 1) ? true : false;

      if(lastFoto)
        this.numLastFoto = -1;

      console.log("last image? " + (lastFoto ? "yes" : "no"));
      console.log("next image: " + (this.numLastFoto + 1));

      this.showImage( this.numLastFoto + 1 ); 
    }

		// remove a classe da antiga foto ativa
		this.objLastTr.getElementsByTagName("td")[0].className = "";
		this.objLastTr.getElementsByTagName("th")[0].className = "";

		if(lastFoto)
			this.objLastTr = this.objTable.getElementsByTagName("tr")[0];
		else
			this.objLastTr = this.objLastTr.nextSibling;

		this.objLastTr.getElementsByTagName("td")[0].className = "clicado";
		this.objLastTr.getElementsByTagName("th")[0].className = "clicado";
		
		this.scrollThumbs();
	},
	
	"anterior": function () {

    if(thumb_order == "DESC") {
  		// se for a primeira foto, vai para o fim
  		var firstFoto = this.numLastFoto+1 == this.qtdImages ? true : false;
  
  		if(firstFoto)
  			this.numLastFoto = -1;
  
      console.log("next image: " + (this.numLastFoto - 1));
  		this.showImage( this.numLastFoto + 1 );
  	}
  	else {
      // se for a última foto, vai para o início
      var firstFoto = this.numLastFoto == 0 ? true : false;
      
  		if(firstFoto)
  			this.numLastFoto = this.qtdImages;

      console.log("next image: " + (this.numLastFoto - 1));
  		this.showImage( this.numLastFoto - 1 );
  	}

		// remove a classe da antiga foto ativa
		this.objLastTr.getElementsByTagName("td")[0].className = "";
		this.objLastTr.getElementsByTagName("th")[0].className = "";

		if(firstFoto)
			this.objLastTr = this.objTable.getElementsByTagName("tr")[this.qtdImages-1];
		else
			this.objLastTr = this.objLastTr.previousSibling;

		this.objLastTr.getElementsByTagName("td")[0].className = "clicado";
		this.objLastTr.getElementsByTagName("th")[0].className = "clicado";
		
		this.scrollThumbs();
	}
}

window.onload = function () { Album.montar() };











