function initLightbox(){
  $('a[@rel*=lightbox]').lightBox();
}

function initImg(){
    if(document.body){
        if(screen.width <= 1024){
            document.body.style.backgroundImage = "url('images/bg1024.jpg')";
        } else {
            if(screen.height < 1024){
                document.body.style.backgroundImage = "url('images/bgwide.jpg')";
            } else {
                document.body.style.backgroundImage = "url('images/bg1280.jpg')";
            }
        }    
    }
}

$(document).ready(function(){
  $('#container').css({width: '927px', overflow: 'hidden'});
  
  initImg();
  
  PagProjetos(1);
  PagImpressao(0, 1);
  PagNoticias(1, 0);
  initLightbox();
  
  $('#menu a, ul.nav a, div.leia_mais a').livequery('click', function(event) { 
    initImg();
    
    var link = $(this);
    var href = link.attr('href');
    var anchor = typeof link.attr('rel') == 'undefined' ? href : '#' + link.attr('rel');
    var offset = $('#content').offset()['left'];
    
    if(anchor != "#home"){
        if($(anchor).size() == 0){
          $.ajax({
            type: "GET",
            url: href,
            data: "ajax=true",
            cache: false,
            success: function(msg){
              $('#content').css({width: $('#content').width() + 950 +'px'}).append(msg);
              $('#container').animate( { scrollLeft: $(anchor).offset()['left'] - offset} , 1000);
              if(anchor == "#projetos"){
                  PagProjetos(1);
                  initLightbox();
              }
              if(anchor == "#impressoes"){
                  PagImpressao(0, 1);
              }
              if(anchor == "#news"){
                  PagNoticias(1, 0);
              }          
            }
          });
        } else {
          $('#container').animate( { scrollLeft: $(anchor).offset()['left'] - offset} , 1000);
        }
        return false;
    } else {
        return true;
    }
  });
});

function PagProjetos(pag){
    id = "pg=" + pag;
    $.ajax({
      type: "GET",
      url: "pagprojetos.aspx",
      data: id,
      cache: false,
      success: function(msg){
        $('#pagprojetos').replaceWith(msg);
      }
  });
}

function ListarProjetos(pag){
    var id = "pg=" + pag;
    $.ajax({
        type: "GET",
        url: "listarprojetos.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $('#listaprojetos').replaceWith(msg);
            PagProjetos(pag);
        }
    });
}

function AbrirProjeto(projeto){
    var id = "id=" + projeto;
    $.ajax({
        type: "GET",
        url: "projetoform.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $('#projeto-form').replaceWith(msg);
            initLightbox();
        }
    });
}

function VerProjeto(projeto){
    var href = "projetos.aspx";
    var anchor = "#projetos";
    var offset = $('#content').offset()['left'];
    if($(anchor).size() == 0){
      $.ajax({
        type: "GET",
        url: href,
        data: "ajax=true",
        cache: false,
        success: function(msg){
          $('#content').css({width: $('#content').width() + 950 +'px'}).append(msg);
          var id = "id=" + projeto;
          $.ajax({
              type: "GET",
              url: "projetoform.aspx",
              data: id,
              cache: false,
              success: function(msg){
                  $('#projeto-form').replaceWith(msg);
                  PagProjetos(1);
                  initLightbox();
                  $('#container').animate( { scrollLeft: $(anchor).offset()['left'] - offset} , 1000);
              }
          });
        }
      });
    } 
}

function AbrirFlash(projeto) {
	day = new Date();
	id = day.getTime();
	URL = "unidade_360.aspx?id=" + projeto;
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=no,statusbar=0,menubar=no,resizable=0,location=no,screenX=50,screenY=100,width=720,height=564');");
}

function AbrirVideo(projeto, video){
    day = new Date();
    id = day.getTime();
    URL = "abrirvideo.aspx?id=" + projeto + "&pP=" + video;
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=588,left = 425,top = 332');");
   }

function AbrirImpressao(impressao){
    var id = "id=" + impressao;
    $.ajax({
        type: "GET",
        url: "impressaoform.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $('#impressoes-form').replaceWith(msg);
        }
    });
}

function PagImpressao(atual,pag){
    id = "atual=" + atual + "&pg=" + pag;
    $.ajax({
      type: "GET",
      url: "pagimpressao.aspx",
      data: id,
      cache: false,
      success: function(msg){
        $('#pag-impressao').replaceWith(msg);
      }
  });
}

function ListarImpressoes(atual,pag){
    var id = "pg=" + atual;
    $.ajax({
        type: "GET",
        url: "listarimpressoes.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $('#lista-impressoes').replaceWith(msg);
            PagImpressao(atual,pag);
        }
    });
}

function AbrirNoticia(noticia){
    var id = "id=" + noticia;
    $.ajax({
        type: "GET",
        url: "noticiaform.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $("#noticia-form").replaceWith(msg);
        }
    });
}

function ListarNoticias(pg, cat){
    var id = "pg=" + pg + "&cat=" + cat;
    $.ajax({
        type: "GET",
        url: "listarnoticias.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $("#categorias").replaceWith(msg);
            PagNoticias(pg, cat);
        }
    });
}

function PagNoticias(pg, cat){
    id = "pg=" + pg + "&cat=" + cat;
    $.ajax({
      type: "GET",
      url: "pagnoticias.aspx",
      data: id,
      cache: false,
      success: function(msg){
        $('#pagnoticias').replaceWith(msg);
      }
  });
}

function VerNoticia(noticia){
    var href = "noticias.aspx";
    var anchor = "#news";
    var offset = $('#content').offset()['left'];
    if($(anchor).size() == 0){      
      $.ajax({
        type: "GET",
        url: href,
        data: "ajax=true",
        cache: false,
        success: function(msg){
          $('#content').css({width: $('#content').width() + 950 +'px'}).append(msg);
          var id = "id=" + noticia;
          $.ajax({
              type: "GET",
              url: "noticiaform.aspx",
              data: id,
              cache: false,
              success: function(msg){
                  $("#noticia-form").replaceWith(msg);
                  $('#container').animate( { scrollLeft: $(anchor).offset()['left'] - offset} , 1000);
              }
          });
        }
      });
    } 
}

function ListarParcerias(categoria, pg){
    var id = "categoria=" + categoria + "&pg=" + pg;
    $.ajax({
        type: "GET",
        url: "listarparcerias.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $('#'+categoria).replaceWith(msg);
            PagParcerias(categoria, pg);
        }
    });
}

function PagParcerias(categoria, pg){
    var id = "categoria=" + categoria + "&pg=" + pg;
    $.ajax({
        type: "GET",
        url: "pagparcerias.aspx",
        data: id,
        cache: false,
        success: function(msg){
            $('#pag'+categoria).replaceWith(msg);
        }
    });
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string") {
        if(er.test(mail)) { return true; }
	}
	else if(typeof(mail) == "object") {
		if(er.test(mail.value)) { return true; }
	}
    else {
        return false;
    }
}

function CadastrarInformativo(){
    if(document.getElementById('news_nome').value == "" || document.getElementById('news_nome').value == "nome"){
        alert("O nome é obrigatório!");
        document.getElementById('news_nome').focus();
        return false;
    }
    if(document.getElementById('news_email').value == "" || document.getElementById('news_email').value == "e-mail"){
        alert("O e-mail é obrigatório!");
        document.getElementById('news_email').focus();
        return false;
    }
    if(!checkMail(document.getElementById("news_email").value)) {
		alert('E-mail inválido. Tente novamente.');
		document.getElementById("news_email").focus();	
		return false;
	}
    var id = "nome=" + document.getElementById('news_nome').value + "&email=" + document.getElementById('news_email').value;
    $.ajax({
        type: "GET",
        url: "newsletter.aspx",
        data: id,
        cache: false,
        success: function(msg){
            msg = msg.replace('<span id="l_mensagem">', '');
            msg = msg.replace('</span>', '');
            alert(msg);
            $('#news_nome').val("nome");
            $('#news_email').val("e-mail");
        }
    });
}

function CadastrarContato(){
    if(document.getElementById('nome').value == "" || document.getElementById('nome').value == "nome"){
        alert("O nome é obrigatório!");
        document.getElementById('nome').focus();
        return false;
    }
    if(document.getElementById('empresa').value == "empresa"){
        document.getElementById('empresa').value = "";
    }
    if(document.getElementById('email').value == "" || document.getElementById('email').value == "e-mail"){
        alert("O e-mail é obrigatório!");
        document.getElementById('email').focus();
        return false;
    }
    if(!checkMail(document.getElementById("email").value)) {
		alert('E-mail inválido. Tente novamente.');
		document.getElementById("email").focus();	
		return false;
	}
    if(document.getElementById('mensagem').value == "" || document.getElementById('mensagem').value == "mensagem"){
        alert("A mensagem é obrigatória!");
        document.getElementById('mensagem').focus();
        return false;
    }
    if(document.getElementById('telefone').value == "telefone"){
        document.getElementById('telefone').value = "";
    }
    var id = "nome=" + document.getElementById('nome').value + "&email=" + document.getElementById('email').value + "&empresa=" + document.getElementById('empresa').value + "&telefone=" + document.getElementById('telefone').value + "&mensagem=" + document.getElementById('mensagem').value;
    $.ajax({
        type: "GET",
        url: "enviar.aspx",
        data: id,
        cache: false,
        success: function(msg){
            msg = msg.replace('<span id="l_mensagem">', '');
            msg = msg.replace('</span>', '');
            alert(msg);
            $('#nome').val("nome");
            $('#email').val("e-mail");
            $('#empresa').val("empresa");
            $('#mensagem').val("mensagem");
            $('#telefone').val("telefone");
        }
    });
}

function configurar_nome(oElem,iValue,blankValue) {
	if(oElem.value==iValue || oElem.value=='') {
		oElem.value = (blankValue)?'':iValue;
	}
}

function Accordion(div){
    if(div == "QuemSomos"){
        if(document.getElementById("div"+div).style.display == "none"){
            $("#divOrigem").hide("slow");
            $("#divQuemSomos").show("slow");
        } else {
            $("#divQuemSomos").hide("slow");
        }
    } else {
        if(document.getElementById("div"+div).style.display == "none"){
            $("#divQuemSomos").hide("slow");
            $("#divOrigem").show("slow");
        } else {
            $("#divOrigem").hide("slow");
        }
    }   
}

function setStyle(campo){
    campo.className = "hover";
}

function clearStyle(campo){
    campo.className = "";
}

function AbrirRelease(){
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('release.aspx', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=700,height=600,left=440,top=312');");
}