var qual = 1;
var status = 0;
var mensagemAguarde = '<img src="/img/ajax_loading.gif" class="loading_geral_img"><span class="loading_geral_texto"><b>Aguarde</b><br>Carregando conteúdo ...</span>';
var intervalo = '';

function createXHR(){

    var request = false;

        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }

        catch (err2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
                try {
                        request = new XMLHttpRequest();
                }
                catch (err1)
                {
                        request = false;
                }
            }
        }

    return request;

} // Fim function

function request (secao, adicional){

      // Esperando ...
      document.getElementById(secao+ '_geral').innerHTML = mensagemAguarde;

      var requestUnica = createXHR();

      requestUnica.onreadystatechange  = function(){

         if (requestUnica.readyState  == 4){

            if (requestUnica.status  == 200){
               document.getElementById(secao+ '_geral').innerHTML = requestUnica.responseText;
            }else{
               document.getElementById(secao+ '_geral').innerHTML = "Erro: " +requestUnica.status;
            }

         }

      }

      requestUnica.open('GET', secao+ '.php?' +adicional,  true);
      requestUnica.send('NULL');

} // Fim function

function checkMail (x) {

   var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

   if (filter.test(x)){
      return true;
   } else {
      return false;
   }

} // Fim function

function fale_envia (){

   var erro = '';
   var mensagem = document.getElementById('fale_mensagem').value;
   var email = document.getElementById('fale_email').value;
   var nome = document.getElementById('fale_nome').value;

   if (!mensagem){
      erro = 'Digite a sua <b>mensagem</b> no campo correspondente.';
   }else if (mensagem && mensagem.length < 20){
      erro = 'Digite a sua <b>mensagem</b> com no mínimo 20 caracteres.';
   }

   if (!email){
      erro = 'Informe o seu <b>e-mail</b> no campo correspondente.';
   }else if (email && !checkMail(email)){
      erro = 'O <b>e-mail</b> informado não é correto.';
   }

   if (!nome){
      erro = 'Informe o seu <b>nome</b> no campo correspondente.';
   }else if (nome && nome.length < 5){
      erro = 'Informe o seu <b>nome</b> com no mínimo 5 caracteres.';
   }

   if (erro){
      document.getElementById('fale_texto').innerHTML = '<img src="/img/alert_32.gif" align="absmiddle"><span class="fale_erro">' +erro+ '</span>';
   }else{


      // Loading
      document.getElementById('fale_texto').innerHTML = '<img src="/img/ajax_loading.gif"><span class="fale_aguarde"><b>Aguarde</b><br>Enviando o seu formulário ...</span>';

      var requestFale = createXHR();

      requestFale.onreadystatechange  = function(){

         if (requestFale.readyState  == 4){

            if (requestFale.status  == 200){
               document.getElementById('fale_geral').innerHTML = requestFale.responseText;
            }else{
               document.getElementById('fale_geral').innerHTML = "Erro: " +requestFale.status;
            }

         }

      }

      requestFale.open('POST', 'fale.php',  true);
      requestFale.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      requestFale.send('acao=insere&nome=' +nome+ '&email=' +email+ '&mensagem=' +mensagem);

   }

} // Fim function

function initScrollLayer() {
   var wndo = new dw_scrollObj('wn', 'lyr1', 'conteudo_scroll');
}

function slide_home_aparece (){

   // Muda o botão
   slide_botoes_muda();

   // Mostra a Imagem
   document.getElementById('slidec_img').innerHTML = '<a href="javascript:;" onclick="' +slide_link[qual]+ '"><img src="' +slide_img[qual]+ '" width="400" height="200" class="slidec_img_img"></a>';

   // Começa o movimento de aparecer a imagem
   comecaImagem = new OpacityTween(document.getElementById('slidec_img'),Tween.regularEaseIn, 20, 100, 1);
   comecaImagem.start();

   // Status = 1: Só a imagem
   status = 1;

   // Terminou de aparecer com a imagem
   comecaImagem.onMotionFinished = function(){

      // Começa o movimento de aparecer a barra;
      comecaBarra = new OpacityTween(document.getElementById('slidec_barra'),Tween.regularEaseIn, 0, 50, 1);
      comecaBarra.start();

      // Status = 2: Imagem e Barra
      status = 2;

      // Terminou de aparecer com a barra
      comecaBarra.onMotionFinished = function(){

         // Começa o movimento de aparecer o texto
         document.getElementById('slidec_texto').innerHTML = '<a href="javascript:;" onclick="' +slide_link[qual]+ '">' +slide_txt[qual]+ '</a><br><p><a href="javascript:;" onclick="' +slide_link[qual]+ '">' +slide_txt_c[qual]+ '</a></p>';
         comecaTexto = new Tween(document.getElementById('slidec_texto').style,'left',Tween.strongEaseOut,-390,0,1,'px');
         comecaTexto.start();

         // Status = 3: Imagem, Barra e Texto
         status = 3;

         // Terminou de aparecer com o texto
         comecaTexto.onMotionFinished = function(){

            // Próxima imagem
            qual++;
            if (!slide_img[qual]) qual = 1;
            tempo = setTimeout('slide_home_some()', 5000);

            // Status = 4: Já tá no timeout e já aumentou qual
            status = 4;

         }

      }

   }

} // Fim function

function slide_home_some (){

   // Começa o movimento de sumir com o texto
   someTexto = new Tween(document.getElementById('slidec_texto').style,'left',Tween.strongEaseOut,0,-390,1,'px');
   someTexto.start();

   // Status = 5: Sumindo Texto
   status = 5;

   someTexto.onMotionFinished = function(){

      // Zera o Texto
      document.getElementById('slidec_texto').innerHTML = '';

      // Some com a barra
      someBarra = new OpacityTween(document.getElementById('slidec_barra'),Tween.regularEaseIn, 50, 0, 1);
      someBarra.start();

      // Status = 6: Some Barra
      status = 6;

      someBarra.onMotionFinished = function(){

         // Some com a imagem
         someImagem = new OpacityTween(document.getElementById('slidec_img'),Tween.regularEaseIn, 100, 20, 1);
         someImagem.start();

         // Status = 5: Sumindo Imagem
         status = 7;

         someImagem.onMotionFinished = function(){

            slide_home_aparece();

         }

      }

   }

} // Fim function

function slide_botoes(){

   // Seta opacity e display da barra, tirei do CSS para validar na porra do W3C
   var barra = document.getElementById('slidec_barra');
   barra.style.opacity = 0;
   barra.style.filter = 'alpha(opacity=0)';
   barra.style.display = 'inline';

   // Seta a opacity da imagem
   document.getElementById('slidec_img').style.filter = 'alpha(opacity=20)';
   document.getElementById('slidec_img').style.opacity = 0.2;

   var aonde = document.getElementById('conteudo_home');
   var top = 5;

   for (x = 1; x < slide_img.length; x++){

      var newdiv = document.createElement('div');
      var divIdName = 'slidec_b_' +x;
      newdiv.setAttribute('id', divIdName);
      newdiv.setAttribute('onclick', 'javascript:slide_home_muda(' +x+ ')');
      newdiv.className = 'slidec_off';
      newdiv.style.top = top+ 'px';
      newdiv.innerHTML = '<a href="javascript:void(0);" onclick="javascript:slide_home_muda(' +x+ ');">' +x+ '</a>';
      aonde.appendChild (newdiv);

   top = top + 24;
   } // Fim loop

   /*
   // Cima, Baixo
   var newdiv = document.createElement('div');
   var divIdName = 'slidec_pass';
   newdiv.setAttribute('id', divIdName);
   newdiv.className = 'slidec_pass';
   newdiv.style.top = top+ 'px';
   newdiv.innerHTML = '<img src="/img/slidec_pass_up.gif" class="slidec_pass_up"><img src="/img/slidec_pass_down.gif" class="slidec_pass_down">';
   aonde.appendChild (newdiv);
   */

} // Fim function

function slide_home_muda(isso){

   if (status == 1){

      qual = isso;
      comecaImagem.stop();
      slide_home_aparece ();

   }else if (status == 2){

      qual = isso;
      comecaBarra.stop();
      slide_home_aparece ();

   }else if (status == 3){

      qual = isso;
      comecaTexto.stop();
      slide_home_aparece ();

   }else if (status == 4){

      qual = isso;
      clearTimeout(tempo);
      slide_home_some ();

   }else if (status == 5 || status == 6 || status == 7){

      qual = isso;

   }

} // Fim function

function slide_botoes_muda(){

   for (x = 1; x < slide_img.length; x++){

      if (x == qual){
      document.getElementById('slidec_b_' +x).className = 'slidec_on';
      }else{
      document.getElementById('slidec_b_' +x).className = 'slidec_off';
      }

   } // Fim loop

} // Fim function

// Scroll Comum
function scroll_start (essescroll, sobedesce){

   if (sobedesce == 'up'){
   document.getElementById(essescroll).scrollTop = document.getElementById(essescroll).scrollTop - 4;
   }else if (sobedesce == 'down'){
   document.getElementById(essescroll).scrollTop = document.getElementById(essescroll).scrollTop + 4;
   }

   intervalo = setTimeout('scroll_start(\'' +essescroll+ '\', \'' +sobedesce+ '\')', 1);

} // Fim function

function scroll_stop (){

   if (intervalo){
   clearInterval(intervalo);
   }

} // Fim function

// Scroll Usado nos clientes e portfólio
function scroll_cliente (elemento, updown, pixels, rows){

   topOri = parseFloat(document.getElementById(elemento).style.top);

   if (!topOri){
   topOri = 0;
   linha = 0;
   }

   if (updown == 'up' && topOri){
      topMuda = topOri + pixels;
      scrollCliente = new Tween(document.getElementById(elemento).style, 'top', Tween.strongEaseOut, topOri, topMuda, 1, 'px');
      scrollCliente.start();
      linha--;
   }

   if (updown == 'down' && linha < rows){
      topMuda = topOri - pixels;
      scrollCliente = new Tween(document.getElementById(elemento).style, 'top', Tween.strongEaseOut, topOri, topMuda, 1, 'px');
      scrollCliente.start();
      linha++;
   }

} // Fim function

function abrePopUp (theURL, myWidth, myHeight) { //v3.0

   var myLeft = (screen.width-myWidth)/2;
   var myTop = (screen.height-myHeight)/2;
   window.open(theURL, 'popupunica', "left=" +myLeft+ ",top=" +myTop+ ",width=" +myWidth+ ", height=" +myHeight+ ", scrollbars='0', menubar='0', statusbar='0', resizable='0', titlebar='0', toolbar='0'");

} // Fim function

function mudaPortfolio (ID, total){

   var topOri = 35 + parseFloat(document.getElementById('portfolio_conteudo').scrollTop);
  
   document.getElementById('portfolio_detalhes_' +ID).style.top = topOri + 'px';

   for (x = 1;x <= total; x++){

      if (x != ID){
      document.getElementById('portfolio_detalhes_' +x).style.display = 'none';
      }

   }
   document.getElementById('portfolio_detalhes_' +ID).style.display = 'inline';

} // Fim function
