var executou = 0;
var total    = "";
var num      = "";
var vet      = "";
var vet_w    = "";
var vet_h    = "";
var vet_leg  = "";

function initGal(tabela, campo_chave, id, ultimo_id){
   ajax = new ajax_enguine;
   if(ajax){
      ajax.open("POST", "window_nucleo.php", true);
      ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      ajax.onreadystatechange = function(){
         if(ajax.readyState == 4 ){
            if(ajax.responseXML)
               processXML(ajax.responseXML);
         }
      }
      var params = "tabela=" + tabela + "&campo_chave=" + campo_chave + "&id=" + id + "&ultimo_id=" + ultimo_id;
      ajax.send(params);
   }
}
function processXML(obj){
   var dataArray = obj.getElementsByTagName("sub");
   for(var i=0; i<dataArray.length; i++){
      var item      = dataArray[i];
      this.total    = item.getElementsByTagName("total")[0].firstChild.nodeValue;
      this.num      = item.getElementsByTagName("num")[0].firstChild.nodeValue;
      var temp_nome = item.getElementsByTagName("temp_nome")[0].firstChild.nodeValue;
      var temp_wid  = item.getElementsByTagName("temp_wid")[0].firstChild.nodeValue;
      var temp_hei  = item.getElementsByTagName("temp_hei")[0].firstChild.nodeValue;
      var temp_leg  = item.getElementsByTagName("legenda")[0].firstChild.nodeValue;
      this.vet      = temp_nome.split("|up|");
      this.vet_w    = temp_wid.split("|up|");
      this.vet_h    = temp_hei.split("|up|");
      if(temp_leg == "null")
         temp_leg = "";
      this.vet_leg = temp_leg.split("|up|");
      if(this.total == "" || this.total == "0"  || this.total == "1" ){
         Hide(document.all.bt_ant);
         Hide(document.all.bt_pro);
      }
   }
   openGal(num);
}
function closeGal(){
   clearTimeout(temporizador);
   Hide(document.all.molduraRod);
   Hide(document.all.molduraImg);
   Hide(document.all.overlay);
   Hide(document.all.imgFix);
   Show(document.all.imgLoading);
   document.all.imgFix.src = "imgs/px-b.gif";
   document.all.molduraImg.style.width  = 50;
   document.all.molduraImg.style.height = 50;
   document.all.molduraRod.style.width  = 50;
   document.all.molduraRod.style.top    = 50;
}
function openGal(num){
   this.num  = num;
   var image = vet[num];
   var w     = vet_w[num];
   var h     = vet_h[num];
   document.getElementById("legenda").innerHTML = vet_leg[num];
   var window_h = 0;
   var window_w = 0;
   if(document.body.scrollHeight > screen.height)
      window_h = document.body.scrollHeight;
   else
      window_h = screen.height;
   window_w = screen.width;
	document.all.molduraImg.style.top  = document.body.scrollTop + 50;
	document.all.molduraRod.style.top  = document.body.scrollTop + 50;
   document.all.molduraImg.style.left = (window_w / 2) - 25;
   document.all.molduraRod.style.left = (window_w / 2) - 25;
   document.all.imgFix.style.filter   = "alpha(opacity=0)";
   document.all.imgFix.style["-moz-opacity"] = "0.0";
   document.all.imgFix.style.opacity = "0.0";
   Show(document.all.overlay);
   Show(document.all.molduraImg);
   document.all.overlay.style.height = window_h;
   document.all.imgFix.src    = image;
   document.all.imgFix.width  = w;
   document.all.imgFix.height = h;
   animLay(image, w, h, 'ini');
}
function trocaImg(num){
   this.num  = num;
   var image = vet[num];
   var w     = vet_w[num];
   var h     = vet_h[num];
   document.getElementById("legenda").innerHTML = vet_leg[num];
   document.all.imgFix.style.filter = "alpha(opacity=0)";
   document.all.imgFix.style["-moz-opacity"] = "0.0";
   document.all.imgFix.style.opacity = "0.0";
   document.all.imgFix.src    = image;
   document.all.imgFix.width  = w;
   document.all.imgFix.height = h;
   animLay(image, w, h, "troca");
}
function animLay(image, w, h, modo){
   var molduraHeight = document.all.molduraImg.style.height.replace("px","");
   var molduraWidth  = document.all.molduraImg.style.width.replace("px","");
   var molduraLeft   = document.all.molduraImg.style.left.replace("px","");
   var molduraRodTop = document.all.molduraRod.style.top.replace("px","");
   var w_real = parseInt(w) + 20;
   var h_real = parseInt(h) + 20;
   if(modo == "ini"){
      if(molduraHeight == "")
         molduraHeight = 50;
      if(molduraWidth == "")
         molduraWidth = 50;
      if(molduraLeft == "")
         molduraLeft = 50;
      if(molduraRodTop == "")
         molduraRodTop = 50;
      if(molduraHeight < h_real){
         if(molduraHeight > 50)
            clearTimeout(temporizador);
         document.all.molduraImg.style.height = parseInt(molduraHeight) + 20;
         if(parseInt(document.all.molduraImg.style.height.replace("px","")) > h_real)
            document.all.molduraImg.style.height = h_real;
         document.all.molduraRod.style.top = (parseInt(document.all.molduraImg.style.height) + parseInt(document.all.molduraImg.style.top)) - 26;
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      else if(molduraHeight > h_real){
         clearTimeout(temporizador);
         document.all.molduraImg.style.height = parseInt(molduraHeight) - 20;
         if(parseInt(document.all.molduraImg.style.height.replace("px","")) < h_real)
            document.all.molduraImg.style.height = h_real;
         document.all.molduraRod.style.top = (parseInt(document.all.molduraImg.style.height) + parseInt(document.all.molduraImg.style.top)) - 26;
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      if(molduraWidth < w_real && molduraHeight == h_real){
         clearTimeout(temporizador);
         document.all.molduraImg.style.width = parseInt(molduraWidth) + 20;
         document.all.molduraImg.style.left = parseInt(molduraLeft) - 10;
         if(parseInt(document.all.molduraImg.style.width.replace("px","")) > w_real)
            document.all.molduraImg.style.width = w_real;
         document.all.molduraRod.style.width = parseInt(document.all.molduraImg.style.width.replace("px",""));
         document.all.molduraRod.style.left = parseInt(document.all.molduraImg.style.left.replace("px",""));
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      if(molduraWidth > w_real && molduraHeight == h_real){
         clearTimeout(temporizador);
         document.all.molduraImg.style.width = parseInt(molduraWidth) - 20
         document.all.molduraImg.style.left = parseInt(molduraLeft) + 10;
         if(parseInt(document.all.molduraImg.style.width.replace("px","")) < w_real)
            document.all.molduraImg.style.width = w_real;
         document.all.molduraRod.style.width = parseInt(document.all.molduraImg.style.width.replace("px",""));
         document.all.molduraRod.style.left = parseInt(document.all.molduraImg.style.left.replace("px",""));
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      if(molduraWidth == w_real && molduraHeight == h_real){
         clearTimeout(temporizador);
         Show(document.all.imgFix);
         Show(document.all.molduraRod);
         fadeImg(1, "in", "");
      }
   }
   if(modo == "troca"){
      if(molduraHeight < h_real){
         clearTimeout(temporizador);
         document.all.molduraImg.style.height = parseInt(molduraHeight) + 8;
         if(parseInt(document.all.molduraImg.style.height.replace("px","")) > h_real)
            document.all.molduraImg.style.height = h_real;
         document.all.molduraRod.style.top = parseInt(document.all.molduraImg.style.height) + 50;
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      else if(molduraHeight > h_real){
         clearTimeout(temporizador);
         document.all.molduraImg.style.height = parseInt(molduraHeight) - 8;
         if(parseInt(document.all.molduraImg.style.height.replace("px","")) < h_real)
            document.all.molduraImg.style.height = h_real;
         document.all.molduraRod.style.top = parseInt(document.all.molduraImg.style.height) + 50;
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      if(molduraWidth < w_real && molduraHeight == h_real){
         clearTimeout(temporizador);
         document.all.molduraImg.style.width = parseInt(molduraWidth) + 8;
         document.all.molduraImg.style.left  = parseInt(molduraLeft) - 4;
         if(parseInt(document.all.molduraImg.style.width.replace("px","")) > w_real)
            document.all.molduraImg.style.width = w_real;
         document.all.molduraRod.style.width = parseInt(document.all.molduraImg.style.width.replace("px",""));
         document.all.molduraRod.style.left  = parseInt(document.all.molduraImg.style.left.replace("px",""));
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      if(molduraWidth > w_real && molduraHeight == h_real){
         clearTimeout(temporizador);
         document.all.molduraImg.style.width = parseInt(molduraWidth) - 8;
         document.all.molduraImg.style.left  = parseInt(molduraLeft) + 4;
         if(parseInt(document.all.molduraImg.style.width.replace("px","")) < w_real)
            document.all.molduraImg.style.width = w_real;
         document.all.molduraRod.style.width = parseInt(document.all.molduraImg.style.width.replace("px",""));
         document.all.molduraRod.style.left  = parseInt(document.all.molduraImg.style.left.replace("px",""));
         temporizador = setTimeout("animLay('"+image+"', "+w+", "+h+", '"+modo+"')", 1);
      }
      if(molduraWidth == w_real && molduraHeight == h_real){
         clearTimeout(temporizador);
         Hide(document.all.imgLoading);
         Show(document.all.imgFix);
         Show(document.all.molduraRod);
         setTimeout("fadeImg(1, 'in', 'static_rod')", 1000);
      }
   }
}
function fadeImg(grau, modo, acao){
   Hide(document.all.imgLoading);
   if(modo == "in"){
      if(grau <= 10){
         var molduraRodTop = document.all.molduraRod.style.top.replace("px","");
         if(acao == "")
            document.all.molduraRod.style.top = parseInt(molduraRodTop) + 3;
         document.all.imgFix.style.filter = "alpha(opacity=" + (grau * 10) + ")";
         document.all.imgFix.style["-moz-opacity"] = (grau / 10);
         document.all.imgFix.style.opacity = (grau / 10);
         grau++;
         temporizador = setTimeout("fadeImg("+grau+", '"+modo+"', '"+acao+"')", 1);
      }
      else
         clearTimeout(temporizador);
   }
   if(modo == "out"){
      if(grau >= 0){
         document.all.imgFix.style.filter = "alpha(opacity=" + (grau * 10) + ")";
         document.all.imgFix.style["-moz-opacity"] = (grau / 10);
         document.all.imgFix.style.opacity = (grau / 10);
         grau--;
         temporizador = setTimeout("fadeImg("+grau+", '"+modo+"', '"+acao+"')", 1);
      }
      else{
         clearTimeout(temporizador);
         if(acao == "previous"){
            this.num--;
            Hide(document.all.imgFix);
            Show(document.all.imgLoading);
         }
         if(acao == "next"){
            this.num++;
            Hide(document.all.imgFix);
            Show(document.all.imgLoading);
         }
         document.all.imgFix.src = "imgs/px-b.gif";
         if(this.num > this.total - 1)
            this.num = 0;
         if(this.num < 0)
            this.num = this.total - 1;
         trocaImg(this.num);
      }
   }
}
