var current_size=1;

function changeContentFontSize(i) {
   if(i==0) {
      document.getElementById('content').style.fontSize="1em";
      current_size=1;
      return false;
   }
   
   current_size+=i;
   document.getElementById('content').style.fontSize=current_size + 'em';
}

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

clickok = true;

function carrega(name) {
    //Exibe o texto carregando no div conteúdo
    var conteudo=document.getElementById("content")
    conteudo.innerHTML='<div class="loading">carregando...</div>'

    //Abre a url
    xmlhttp.open("GET","base.php?n="+name,true);
   
    //Executada quando o navegador obtiver o código
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //Lê o texto
            var texto=xmlhttp.responseText

            //Desfaz o urlencode
            texto=texto.replace(/\+/g," ")
            texto=unescape(texto)

            //Exibe o texto no div conteúdo
            var conteudo=document.getElementById("content")
            conteudo.innerHTML=texto
            
            clickok = true;
        }
    }
    xmlhttp.send(null)
}

/*
   showmenu
   
   menuobj : referencia ao link do menu a ser mostrado
   
   Seleciona o menu pedido
*/
function showmenu(menuobj,submenuobj) {
   if(!clickok) {
      return false;
   }
   clickok = false;

   var menu=document.getElementById("mainnav")
   var links=menu.getElementsByTagName("a")

   for(var i=0;i<links.length;i++) links[i].id="";
   menuobj.id = "selected";
   
   var submenu=document.getElementById("sn_" + menuobj.getAttribute("href").replace(/.*=/,""));
   var submenulinks=submenu.getElementsByTagName("a");
   if(submenuobj==null) {
      showsubmenu(submenulinks[0],true);
   }
   else {
      showsubmenu(submenuobj,true);
   }
}

/*
   showsubmenu
*/
function showsubmenu(submenuobj,override) {
   if(override==undefined) override=false;
   
   if(!override) {
      if(!clickok) {
         return false;
      }
      clickok = false;
   }

   // Encontra todas as div's de submenu
   var submenuparent = submenuobj.parentNode.parentNode.parentNode;
   var submenudiv=document.getElementById("subnav");
   var submenus=submenudiv.getElementsByTagName("div");

   // Esconde demais submenus e mostra o atual
   for(var i=0;i<submenus.length;i++) submenus[i].className="invisible";
   submenuparent.className="";

   // Limpa os items selecionados
   var submenulinks = submenuparent.getElementsByTagName("a");

   for(var j=0;j<submenulinks.length;j++) {
      if(submenulinks[j].className != "innerlink") submenulinks[j].id="";
   }

   submenuobj.id = "subselected";
   
   // HTTP request para pagina correspondente
   var name = submenuobj.getAttribute("href").replace(/.*=/,"");
   carrega(name);
   
   dhtmlHistory.add('hist_'+name,name);
}


function menuclick(e){
   showmenu(this,null);
   return false;
}


function submenuclick(e) {
   showsubmenu(this);
   return false;
}

function genclick(name) {
   // Vamos buscar os objectos referentes ao menu
   // e submenu correspondentes ao nome especificado
   var menu=document.getElementById("mainnav")
   var menulinks=menu.getElementsByTagName("a")
   var submenudiv=document.getElementById("subnav");
   var submenus=submenudiv.getElementsByTagName("div");

   var found = false;

   for(var i=0;i<menulinks.length;i++) {
      if(found) break;
      
      // Todo link do menu principal deve ter uma DIV de submenu correspondente
      // mesmo que vazia, com id composta por 'sn_' + nome do menu
      var submenuitems=submenus[i].getElementsByTagName("a");
      var menuobj = menulinks[i];
      
      for(var j=0;j<submenuitems.length;j++) {
         if(found) break;
         
         if(submenuitems[j].getAttribute("href").replace(/.*=/,"")==name) {
            var submenuobj = submenuitems[j];
            found=true;
         }
      }
   }
   
   if(found) showmenu(menuobj,submenuobj);
   else alert('erro')
   return false;
}

function init() {
   // Inicializa variaveis globais
   clickok = true;
   
   //Obtém os links do menu
   var menu=document.getElementById("mainnav");
   var links=menu.getElementsByTagName("a");

   //Atribui o evento
   for(var i=0;i<links.length;i++) {
      links[i].onclick=menuclick;
   }
   
   //Obtém os links dos submenus
   var submenu=document.getElementById("subnav");
   var items=submenu.getElementsByTagName("div");

   //Atribui o evento
   for(var i=0;i<items.length;i++) {
      var subitems=items[i].getElementsByTagName("a");
   
      for(var j=0;j<subitems.length;j++) {
         // Se for um link comum nao coloca o evento
         if( (subitems[j].className.indexOf("innerlink")<0) && (subitems[j].getAttribute("href").indexOf('?')>=0) ) {
            subitems[j].onclick=submenuclick;
         }
      }
   }

   // Mostra conteudo (somente se esse codigo Javascript estiver rodando, senao
   // vai aparecer somente o conteudo de noscript)
   document.getElementById("inner").className="";

   var initialLocation = dhtmlHistory.getCurrentLocation();
   if ( (initialLocation == null) || (initialLocation == '')) {
      initialLocation = "hist_apresentacao";
   }

   // initialize the DHTML History framework
   dhtmlHistory.initialize();   
   dhtmlHistory.addListener(historyChange);

   // Primeira vez que esta carregando a pagina?
   if (dhtmlHistory.isFirstLoad()) {
      dhtmlHistory.add("hist_apresentacao", 'apresentacao');
   }
   
   genclick(initialLocation.substr(5));
}


function historyChange(newLocation, historyData) {
   genclick(historyData);
}

if(xmlhttp) window.onload=init

function validate_email(field) {
with (field) {
      apos=value.indexOf("@")
      dotpos=value.lastIndexOf(".")
      if (apos<1||dotpos-apos<2) {
         return false;
      }
      else {
         return true;
      }
   }
}

// Formulario de contato
function validate_form(thisform) {
   if(thisform.name.value=="") {
      thisform.name.focus();
      document.getElementById("response").innerHTML='Campo nome obrigatório. Por favor verifique.';
      return false;
   }
   
   if (validate_email(thisform.email)==false) {
      thisform.email.focus();
      document.getElementById("response").innerHTML='Email inválido. Por favor verifique.';
      return false;
   }
   
   if (thisform.phone.value=="") {
      thisform.phone.focus();
      document.getElementById("response").innerHTML='Campo telefone obrigatório. Por favor verifique.';
      return false;
   }

   if (thisform.message.value=="") {
      thisform.message.focus();
      document.getElementById("response").innerHTML='Campo mensagem obrigatório. Por favor verifique.';
      return false;
   }

   // Esconde formulário e mostra mensagem de progresso
   document.getElementById("formcont").className='invisible';
   document.getElementById("formmessage").className='invisible';
   document.getElementById("response").innerHTML="<div id='warning'>Aguarde, enviando dados...</div>";

   xmlhttp.open("GET","form.php?name="+thisform.name.value+"&email="+thisform.email.value+"&phone="+thisform.phone.value+"&message="+thisform.message.value,true);
   
   //Executada quando o navegador obtiver o código
   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
         var texto=xmlhttp.responseText;
   
         var resp=document.getElementById("response");
         resp.innerHTML=texto;
      }
   }
   xmlhttp.send(null)
}

// Formulario de download
function validate_download(thisform) {
   if (validate_email(thisform.email)==false) {
      thisform.email.focus();
      document.getElementById("response").innerHTML='Email inválido. Por favor verifique.';
      return false;
   }
   
   if (thisform.accept.checked==false) {
      thisform.accept.focus();
      document.getElementById("response").innerHTML='É preciso ler e aceitar o termo de licenciamento. Por favor verifique.';
      return false;
   }

   // Esconde formulário e mostra mensagem de progresso
   document.getElementById("formcont").className='invisible';
   document.getElementById("formmessage").className='invisible';
   document.getElementById("response").innerHTML="<div id='warning'>Aguarde, enviando dados...</div>";
   
   xmlhttp.open("GET","download.php?email="+thisform.email.value,true);
   
   //Executada quando o navegador obtiver o código
   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
         var texto=xmlhttp.responseText;
   
         document.getElementById("response").innerHTML=texto;
      }
   }
   xmlhttp.send(null)
}

// Formulario de pedido de CD
function validate_cdform(thisform) {
   if (validate_email(thisform.email)==false) {
      thisform.email.focus();
      document.getElementById("response").innerHTML='Email inválido. Por favor verifique.';
      return false;
   }
   
   if (thisform.phone.value=="") {
      thisform.phone.focus();
      document.getElementById("response").innerHTML='Campo telefone obrigatório. Por favor verifique.';
      return false;
   }

   // Esconde formulário e mostra mensagem de progresso
   document.getElementById("formcont").className='invisible';
   document.getElementById("formmessage").className='invisible';
   document.getElementById("response").innerHTML="<div id='warning'>Aguarde, enviando dados...</div>";

   xmlhttp.open("GET","cd.php?email="+thisform.email.value+"&phone="+thisform.phone.value,true);
   
   //Executada quando o navegador obtiver o código
   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4){
         var texto=xmlhttp.responseText;
   
         var resp=document.getElementById("response");
         resp.innerHTML=texto;
      }
   }
   xmlhttp.send(null)
}

function qatoggle(qnode){
   this.mostra = function() {
      var nodes=qnode.getElementsByTagName("span");
      
      if(nodes[0].style.display.indexOf("block") >= 0)
         nodes[0].style.display="none";
      else
         nodes[0].style.display="block";
   }
   return this;
}

function external_link(node) {
   node.target="_blank";
}