if (Node === undefined) {
    var Node = new Object();
}
if (Node.ELEMENT_NODE === undefined) {
    Node.ELEMENT_NODE = 1;
}

function acrescentarAutor(ulNodeId,strNome) {
    var ulNode = $(ulNodeId);
    var count = ulNode.getElementsByTagName("li").length+1;
    var idNovo = "trabalho[autores]["+count+"]";
    var liParam = new Object();
    liParam.type="text";
    liParam.name="trabalho[autores][]";
    liParam.id=idNovo;
    liParam.value=strNome;
    var inputParam = new Object();
    inputParam.type="button";
    inputParam.name="retirar["+count+"]";
    inputParam.id="retirar["+count+"]";
    inputParam.value="-";
    inputParam.onclick="retirarAutor('"+ulNodeId+"',"+count+")";
    var novoAutor = Builder.node("li"
      ,[Builder.node("input",liParam),
        Builder.node("input",inputParam)]);
    ulNode.appendChild(novoAutor);
}

function retirarAutor(ulNodeId,idxAutor) {
    var ulNode = $(ulNodeId);
    var delAutor = $("trabalho[autores]["+idxAutor+"]").parentNode;
    ulNode.removeChild(delAutor);
}

function mostrarAutores(lstAutores,ulNodeId) {
    //alert(lstAutores);
    document.getElementById("trabalho[autores][1]").value =
      lstAutores[0];
    for(var idx=1;idx<lstAutores.length;idx++) {
        acrescentarAutor(ulNodeId,lstAutores[idx]);
    }
}

/**/

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/* Inicializações */

function flipFlash(strUF) {
    if (strUF != gstrUF) {
        flipRepresentante(strUF);
        window.location.hash = "#"+strUF;
    }
}

function flipRepresentante(strUF) {
    var strSafeUF = strUF.substring(0,2);
    escondeRepresentante(gstrUF);
    mostraRepresentante(strSafeUF);
    gstrUF = strSafeUF;
}

function mostraRepresentante(strUF) {
    //alert('mostra:'+strUF);
    if (document.getElementById(strUF) != undefined) {
        var objEstado = document.getElementById(strUF);
        if (objEstado.firstChild.innerHTML !== undefined) {
            objEstado.innerHTML = objEstado.firstChild.innerHTML;
        }
        objRepresentantes = objEstado.nextSibling;
        while (objRepresentantes.nodeType != Node.ELEMENT_NODE){
            objRepresentantes = objRepresentantes.nextSibling;
        }
        objRepresentantes.style.display = 'block';
    }
}

function escondeRepresentante(strUF) {
    //alert('esconde:'+strUF);
    if (strUF == 'topo') return;
    if (document.getElementById(strUF) != undefined) {
        var objEstado = document.getElementById(strUF);
        var strEstado = objEstado.innerHTML;
        objEstado.innerHTML = '';
        objEstado.innerHTML = '<a href="#'
          +strUF
          +'" onclick="flipRepresentante(\''
          +strUF+'\')">'
          +strEstado+'</a>';
        var objRepresentantes = objEstado.nextSibling;
        while (objRepresentantes.nodeType != Node.ELEMENT_NODE){
            objRepresentantes = objRepresentantes.nextSibling;
        }
        //alert(objRepresentantes.nodeName);
        objRepresentantes.style.display = 'none';
    }
}

function inicializaContact() {
    if (document.getElementById('listaContact') !== undefined) {
        var objContact = document.getElementById('listaContact');
        var lstDDs = document.getElementsByTagName('DD');
        for (var count=0;count<lstDDs.length;count++) {
            if (lstDDs[count].parentNode == objContact) {
                lstDDs[count].style.display = 'none';
            }
        }
    }
}

var gstrUF;
function inicializaRepresentantes() {
    if (document.getElementById('listaRepresentantes') !== undefined) {
        gstrUF = window.location.hash.substr(1);
        if (gstrUF !== undefined) {
            var objRepresentantes =
              document.getElementById('listaRepresentantes');
            var objEstado;  
            var count,lstDTs = document.getElementsByTagName('DT');
            for (count=0; count<lstDTs.length; count++) {
                if (lstDTs[count].id == gstrUF) {
                    mostraRepresentante(gstrUF);
                } else if (lstDTs[count].parentNode ==
                  objRepresentantes){
                    escondeRepresentante(lstDTs[count].id);
                }
            }
        }
    }
}

function flipDD(objLink) {
    var objDD = objLink.parentNode.nextSibling;
    while (objDD.nodeType != Node.ELEMENT_NODE){
        objDD = objDD.nextSibling;
    }
    objDD.style.display =
      objDD.style.display == 'block' ? 'none' : 'block';
}

function existeForm(strIdioma,frm){
    var frmex = document.getElementById(frm);
    if (frmex!=null){
        tau_inicializaValidacao(strIdioma,frm);
    } 
}

function inicializaHome(strIdioma) {

    //buscaRapida_init();
    new Autocompleter.Local(
       "q"
      ,"sugestoes"
      ,tau__lstSugestoes
      ,{
          choices:8,
          tau__urlBusca:"/busca/search."+strIdioma+".php?q="
      }
    );
}

function inicializaInterno(strIdioma) {
    inicializaRepresentantes();
    inicializaContact();
    //buscaRapida_init();
    new Autocompleter.Local(
       "q"
      ,"sugestoes"
      ,tau__lstSugestoes
      ,{
          choices:8,
          tau__urlBusca:"/busca/search."+strIdioma+".php?ul=/"+strIdioma+"/&q="
      }
    );
    existeForm(strIdioma,'formcontato');
    existeForm(strIdioma,'formcadastro');
}

function TAU_openBrWindow(theURL,winName,features,largura,altura,f) { //v2.0

  if(f==1){
    l=0;t=0;largura=(screen.availWidth-10);altura=(screen.availHeight-26);
  }else{
    var l = ((screen.availWidth)/2) - (largura/2);
    var t = ((screen.availHeight)/2) - ((altura/2)+20);
  }
  features = features + ",width=" + largura + ",height=" + altura + ",left=" + l + ",top=" + t;
  window.open(theURL,winName,features);
}

var gstrUltProduto = '';
function detalhes(obj,strProduto) {
    gstrUltProduto = strProduto;
    window.setTimeout(function () {
        timeoutedDetalhes(obj,strProduto);
    },250);
}

function timeoutedDetalhes(obj,strProduto) {
    if (gstrUltProduto != strProduto) {
        return;
    }
    gstrUltProduto = '';
    var objTarget = document.getElementById('detalhes_ajax');
    objTarget.innerHTML = '';
    var xmlReq = ajax('POST','/scripts/detalhes.php'
      ,'produto='+strProduto);
    var objDetalhes = xmlReq.responseText;
    //alert(findPosX(obj)+":"+findPosY(obj));
    //alert(objDetalhes.firstChild.nodeName);
    //objTarget.appendChild(objDetalhes.firstChild);
    objTarget.innerHTML = xmlReq.responseText;
    objTarget.innerHTML +=
      '<a href="javascript:fecharDetalhe();" title="Fechar">X</a>';
    objTarget.style.left = (findPosX(obj))+'px';
    objTarget.style.top =
      (findPosY(obj)-objTarget.offsetHeight)+'px';
}

function fecharDetalhe() {
    var objTarget = document.getElementById('detalhes_ajax');
    objTarget.innerHTML = '';
    objTarget.style.top = '-1000px';
    gstrUltProduto = '';
}


/* Suggest

var buscaRapida = new Object();
function buscaRapida_init() {
    $('q').setAttribute("autocomplete","off");
    $('q').onkeyup = buscaRapida_keyUp;
    buscaRapida.theSel = 'sugestoes';
    buscaRapida.searchBox = 'q';
    buscaRapida.searchBox.onkeyup = buscaRapida_keyUp;
    //searchBox.focus();
}

function buscaRapida_keyUp(evt) {
        buscaRapida_load(buscaRapida.searchBox.value);
}

function buscaRapida_load(s) {
    buscaRapida.theSel.innerHTML = "<strong>Now loading</strong>";
    var opt = {
        asynchronous : true,
        method       : 'GET',
        getBody     : 's+'+s,
        onSuccess    : atualizaLista,
        onFailure    : exibeErro 
    }
    new Ajax.Request("/scripts/buscarapida.php",opt);

    function atualizaLista (req) {
        alert(req.responseText);
        buscaRapida.theSel.innerHTML = req.responseText;
        buscaRapida.theSel.style.visibility = "visible";
    }
    function exibeErro(req) {
        buscaRapida.theSel.innerHTML = "";
        buscaRapida.theSel.style.visibility = "hidden";
    }
}
*/

function Tau_showHideLayers() { //v3.0
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
dom = (ns4|ie4)? false:true;
	var camada,condicao,args=Tau_showHideLayers.arguments;
	for(var i=0;i<(args.length/2);i+=1){
		camada=MM_findObj(args[2*i]);
		condicao=args[2*i+1];
		if(ns4) camada.visibility=condicao;
		if(ie4) camada.style.visibility=condicao;
		if(dom) document.getElementById(args[2*i]).style.visibility=condicao;
	}
}
