function criaxmlhttp() {
   try {
      xmlhttp = new XMLHttpRequest();
   } catch(ee) {
      try {
         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(e) {
         try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(E) {
            xmlhttp = false;
        }
      }
   }
}

function execCodigo(url, div) {
   if (document.getElementById(div).innerHTML != "") {
      document.getElementById(div).innerHTML = "";
      return;
   }
   criaxmlhttp();
   document.getElementById(div).innerHTML = "<img src=\"progress.gif\" alt=\"\" />";
   url += "&timestamp="+new Date().getTime(); 
   xmlhttp.open("GET", url, true);
   xmlhttp.onreadystatechange=function() {
     if (xmlhttp.readyState==4) {
         if (xmlhttp.status == 200) {
            var c=document.getElementById(div)
            var aDados= xmlhttp.responseText;
            c.innerHTML = aDados;
         }
      }
   }  
   xmlhttp.send(null)
}

function leCodigo(url, div) {
   if (document.getElementById(div).innerHTML != "") {
      document.getElementById(div).innerHTML = "";
      return;
   }
   criaxmlhttp();
   document.getElementById(div).innerHTML = "<img src=\"progress.gif\" alt=\"\" />";
    
   xmlhttp.open("GET", url, true);
   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
         if (xmlhttp.status == 200) {
            var c=document.getElementById(div)
            var aDados= xmlhttp.responseText;
            aDados = aDados.replace(/</g, "&lt;");
            aDados = aDados.replace(/>/g, "&gt;");
            c.innerHTML = "<pre><code>" + aDados + "</code></pre>";
         }
      }
   }  
   xmlhttp.send(null)
}

function leXML(url) {
   if (document.getElementById("cabecalho").hasChildNodes()) {
      limpaResultado();
      return;
   }
   criaxmlhttp();
   
   document.getElementById("cabecalho").innerHTML = "<img src=\"progress.gif\" alt=\"\" />"
    
   xmlhttp.open("POST", url, true);
   xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
         if (xmlhttp.status == 200) {
            limpaResultado();
            montaResultado();
         }
      }
   }  
   xmlhttp.send('1')
}
function limpaResultado() {
   var header = document.getElementById("cabecalho");
   while(header.childNodes.length > 0) {
      header.removeChild(header.childNodes[0]);
   }
   var tabela = document.getElementById("r_body");
   while(tabela.childNodes.length > 0) {
      tabela.removeChild(tabela.childNodes[0]); 
   }
}

function montaResultado() {
   var tr = document.createElement("tr");
   var celula = document.createElement("th");
   var textoNode = document.createTextNode('Id');
   celula.appendChild(textoNode);
   tr.appendChild(celula); 
   celula = document.createElement("th");
   textoNode = document.createTextNode('Nome Categoria');
   celula.appendChild(textoNode);
   tr.appendChild(celula); 
   celula = document.createElement("th");
   textoNode = document.createTextNode('Posts');
   celula.appendChild(textoNode);
   tr.appendChild(celula); 
   document.getElementById("r_body").appendChild(tr);
   tr.style.cssText = "background-color: #404e2a; color: #ffffff; padding: 5px; text-align: center";

   var aDados = xmlhttp.responseXML;
   var categorias = aDados.getElementsByTagName("categoria");
   for (var i = 0; i < categorias.length; i++) {
      cat = categorias[i];
      id = cat.getElementsByTagName("id")[0].firstChild.nodeValue;
      nome = cat.getElementsByTagName("nome")[0].firstChild.nodeValue;
      posts = cat.getElementsByTagName("posts")[0].firstChild.nodeValue;
      adicionaLinha(id, nome, posts)
   }
   var header = document.createElement("h2");
   var texto_header = document.createTextNode("Resultado: Posts por Categoria");
   header.appendChild(texto_header);
   document.getElementById("cabecalho").appendChild(header);
   document.getElementById("resultado").style.cssText = "border-bottom: 5px solid #404e2a;";
}

function adicionaLinha(id, nome, posts) {
   var linha = document.createElement("tr");
   var celula = criaCelula(id);
   celula.style.cssText = "text-align: center;";
   linha.appendChild(celula);
   celula = criaCelula(nome);
   celula.style.cssText = "padding-left: 5px;";
   linha.appendChild(celula);
   celula = criaCelula(posts);
   celula.style.cssText = "text-align: center;";
   linha.appendChild(celula);
   linha.style.cssText = "color: #404e2a;";
   document.getElementById("r_body").appendChild(linha);
}

function criaCelula(texto) {
   var celula = document.createElement("td");
   var textoNode = document.createTextNode(texto);
   celula.appendChild(textoNode);
   return celula;
}
function resultado(theform,rotina,textarea) {
	var checa = rotina.split('?');
	var url='';
	if (typeof(checa[1]) != 'undefined') url = rotina+'&'; 
    else url = checa[0]+"?";
    for (i=0;i<theform.length;i++) {
	  if (theform.elements[i].type == 'radio') { 
	     if (theform.elements[i].checked == true)
		    url = url + theform.elements[i].name + "=" + theform.elements[i].value + "&";
		    continue;
	     } else if (theform.elements[i].type == "checkbox") {
	        if (theform.elements[i].checked == true)
		    url = url + theform.elements[i].name + "=" + theform.elements[i].value + "&";
		    continue;
         } else {
       url = url + theform.elements[i].name + "=" + (theform.elements[i].name != textarea ? url_encode(theform.elements[i].value) : url_encode(tinyMCE.getContent(textarea))) + "&";
	  }
    }	
    url += "timestamp="+new Date().getTime();
    window.open(url,"", "scrollbars=yes,toolbar=no,menubar=no,width=800,height=500,resizable=yes")
}

function url_encode(str) { 
   var hex_chars = "0123456789ABCDEF"; 
   var noEncode = /^([a-zA-Z0-9\_\-\.])$/; 
   var n, strCode, hex1, hex2, strEncode = ""; 

   for(n = 0; n < str.length; n++) { 
      if (noEncode.test(str.charAt(n))) { 
         strEncode += str.charAt(n); 
      } else { 
         strCode = str.charCodeAt(n); 
         hex1 = hex_chars.charAt(Math.floor(strCode / 16)); 
         hex2 = hex_chars.charAt(strCode % 16); 
         strEncode += "%" + (hex1 + hex2); 
      } 
   } 
   return strEncode; 
} 

var v_bg = 0;
function muda_bg() {
   var bg = document.getElementsByTagName("body");
   x = bg[0];
   if (v_bg == 0) {
      x.style.cssText = "background: #eeeeee url(http://www.nghorta.com/wp-content/themes/viche_soft/images/spring_flavour/body_vs.gif) 50% 10px repeat-y;";
	  v_bg = 1;
   }
   else if (v_bg == 3) {
      x.style.cssText = "background: #94aa23 url(http://www.nghorta.com/wp-content/themes/viche_soft/images/spring_flavour/bg_vs.jpg) repeat-x;";
	  v_bg = 0;
   }
   else if (v_bg == 1) {
      x.style.cssText = "background: url(http://www.nghorta.com/wp-content/themes/viche_soft/images/spring_flavour/bg_body.jpg) repeat;";
	  v_bg = 2;
   }
   else {
      x.style.cssText = "background: #fff url(http://www.nghorta.com/wp-content/themes/viche_soft/images/spring_flavour/bg_vs2.gif) repeat;";
	  v_bg = 3;
   }
}
function exibe_foto(foto,id,legenda) {
   var d = document.getElementById("exibe");
   d.src = "http://www.nghorta.com/progress.gif";
   var f = document.getElementById("legenda");
   d.style.cssText = "display: block; margin: 0 auto; padding: 2px; border: 1px solid #cee9ca;";
   d.src = foto;
   f.style.cssText = "display: block; text-align: center; background-color:#000; color:#fff; padding:3px 0;";
   if (legenda == '') legenda = "Sem legenda";
   f.innerHTML = legenda;
}

jQuery().ready(function(){	
	jQuery('.bvGallery').click(
		function(e) {
			e.preventDefault();
			var imgClick = jQuery(this).attr("href");
			jQuery("#imgGallery").attr("src",imgClick).css('opacity', '0.3').animate({opacity:1}, 2000);
			legend = jQuery(this).attr("title");
			jQuery("#bvgLegend").text(legend);
        }
	);
});	

