/*--------------------------------------------------------------------
JAVASCRIPT "usados ao longo do site" 

Version: 	2.0 - 2009/2010
author: 	Burocratik (alexandre gomes)
email: 		alex@burocratik.com
website: 	http://www.burocratik.com
-----------------------------------------------------------------------*/
/* =Open new window e Mandar Imprimir
-----------------------------------------------------------------------*/
document.onclick = function(e){
  var target = e ? e.target : window.event.srcElement; //teste para FF-IE
  while (target && !/^(a|body)$/i.test(target.nodeName)){target = target.parentNode;}
	//Open new window
  if (target && target.getAttribute("rel")&& target.rel == "external"){
    var linkExterno = window.open(target.href);
    return linkExterno.closed;
  }
	//Mandar Imprimir
  if (target && target.getAttribute("rel")&& target.rel == "print"){
    var imprimir = window.print();
    return false;
  }
}

/*-----------------------------------------------------------------------
 =HIGHLIGHTS e RODAPE
-----------------------------------------------------------------------*/
var W3CDOM = (document.getElementsByTagName && document.createElement);
if (W3CDOM){
	var link = document.createElement('link');
	link.setAttribute("rel", "stylesheet");
	link.setAttribute("type", "text/css");
	link.setAttribute("href", "/scripts/javascript-corpo.css");

	link.setAttribute("media", "screen");
	document.getElementsByTagName("head")[0].appendChild(link);
}

/*-----------------------------------------------------------------------
 =HIGHLIGHTS
-----------------------------------------------------------------------*/
function tempo(){setInterval("tempoHighlights()", 10000)}
function tempoHighlights(){
	//no inicio o n=0 ja tem show
	if(globalN == (globalNmax-1)){
    globalN = 0;
	}else{
    globalN += 1;
	}
  //
  for (var i=0; i<globalNmax; i++ ) {
    if(globalN == i){
    	addClass("add",globalHigh[globalN],"show");
    }else{
      addClass("remove",globalHigh[i],"show");
    }
	}
}
function iniciarHighlights(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("highl")) return false;
	var  highlights=document.getElementById("highl");
	globalHigh=highlights.getElementsByTagName("li");
	globalNmax=globalHigh.length;
	//global Contador (iniciar o contador)
	globalN=0;
  tempo();
}

/*-----------------------------------------------------------------------
 =RODAPE
-----------------------------------------------------------------------*/
function fVisivelFooter(id) {
  var divs = document.getElementsByTagName("div");
	var link = document.getElementById("linkFooter");
  for (var i=0; i<divs.length; i++ ) {
    if (divs[i].className.indexOf("txtFooter") == -1) continue;
    if (divs[i].getAttribute("id") == id) {
			if (addClass("check",divs[i],"show")){ //verifica se ja apliquei a class (isto e' se ja estava aberto)
					addClass("remove",divs[i],"show"); //tira a class
					addClass("remove",link,"on");
			} else {
				addClass("add",divs[i],"show"); //adiciona nova class, esta funcao esta em geral.js
		  	addClass("add",link,"on");
			}
    }
  }
}

function iniciarLinksFooter() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	var link = document.getElementById("linkFooter");
 	var sectionId = link.getAttribute("href").split("#")[1];
  if (!document.getElementById(sectionId)) return false;
		link.destination = sectionId;
    link.onclick = function() {
    	fVisivelFooter(this.destination);
      //return false;
  	}
}

/*-----------------------------------------------------------------------
=Limpar Formulario de Pesquisa
-----------------------------------------------------------------------*/
function limparForms(qualform) {
  for (var i=0; i<qualform.elements.length; i++) {
    var element = qualform.elements[i];
    if (element.type == "submit")continue; //se o elemento for um submit passa a proxima iteracao do loop
	  if (element.type == "button") continue; //se o elemento for um button passa a proxima iteracao do loop
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}
//passar cada object form para a funcao limparForms()
function iniciarForms() {
  formId = document.forms["formPesquisar"]; //forms k quero que possam ser limpos
	formId2 = document.forms["formNewsletter"]; //forms k quero que possam ser limpos
  for (var i=0; i<document.forms.length; i++) {
    if (document.forms[i]!= formId && document.forms[i]!= formId2) continue; //se nao e' o form k defini, sai do loop
    var thisform = document.forms[i];
    limparForms(thisform);
  }
}

/*-----------------------------------------------------------------------
=Inserir E-mails correctos: Obrigatorio class="email", e [-at-]
-----------------------------------------------------------------------*/
 function iniciarEmails() {
	if (!document.getElementsByTagName) return false;
	var vMails = getElementsByClassName(document, "a", "email");
  for (var i=0; i<vMails.length; i++) {
  	if(vMails[i].className && vMails[i].className=="email"){
    	var mailToReplace = vMails[i].firstChild;
      var mailReal = mailToReplace.nodeValue.replace("[-at-]","@");
      mailToReplace.nodeValue = mailReal;
      mailToReplace.parentNode.setAttribute("href","mailto:"+mailReal);
  	}
	}
}

/*-----------------------------------------------------------------------
=SUBSCREVER NEWSLETTER
-----------------------------------------------------------------------*/
function iniciarNewsletter() {
	if (!document.getElementById) return false;
	if (!document.getElementById("aNews")) return false;
	var vDiv = getElementsByClassName(document, "div", "newsL");
	var h3 = vDiv[0].getElementsByTagName("h3");
	var link = document.getElementById("aNews");
	var p = document.getElementById("pNews");
	var form = document.getElementById("formNewsletter");
	//var msgErro = getElementsByClassName(document, "p", "msgErro");
  //var msgSent = getElementsByClassName(document, "p", "msgSent");
  link.onclick = function() {
   	addClass("add",p,"hide");
		addClass("add",form,"show");
		h3[0].style.backgroundPosition="left bottom";
		//if(msgErro[0] != null) addClass("add",msgErro[0],"hide");
		//if(msgSent[0] != null) addClass("add",msgSent[0],"hide");
    return false;
  }
}
/*-----------------------------------------------------------------------
 =NEWSLETTER VALIDACAO: without jquery
-----------------------------------------------------------------------*/
function iniciarValidacao() {
  if (!document.getElementById) return false;
	if (!document.getElementById("formNewsletter")) return false;
	if (!document.getElementById("hizdj-hizdj")) return false;
  var form = document.getElementById("formNewsletter");
	var email = document.getElementById("hizdj-hizdj");
	var msgErro = getElementsByClassName(document, "p", "msgErro");
  //
	email.onblur=function(){validateEmail();}
	//On Submitting
	form.onsubmit = function(){
		if(validateEmail()){
     return true;
		}else {
     return false;
		}
	};
  //
	function validateEmail(){
  	var a = email.value;
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
  	if(filter.test(a)){
  	  email.style.color ="#5e5e61";
			msgErro[0].style.display="none";
     	return true;
  	}else{
      email.style.color ="#ca311e";
      msgErro[0].style.display="block";
			return false;
  	}
	};
}

/*-----------------------------------------------------------------------
 =ABRIR e FECHAR CONTEUDOS
-----------------------------------------------------------------------*/
function fOpenTexto(id) {
  var divs = document.getElementsByTagName("div");
  for (var i=0; i<divs.length; i++ ) {
    if (divs[i].className.indexOf("openTxt") == -1) continue;
    if (divs[i].getAttribute("id") == id) {
			if (addClass("check",divs[i],"showTxt")){
					addClass("remove",divs[i],"showTxt");
			} else {
					addClass("add",divs[i],"showTxt");
			}
    } else {
	  		addClass("remove",divs[i],"showTxt");
    }
  }
}
// =Desactivar o botao ao clicar
function fBtnLink(clicado){
	var links = getElementsByClassName(document, "a", "openLink");
	for (var i=0; i<links.length; i++ ) {
  	links[i] == clicado ?  links[i].style.display="none" : links[i].style.display="inline";
	}
}

function iniciarAcordeao() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	var links = getElementsByClassName(document, "a", "openLink");
  var texto = getElementsByClassName(document, "div", "openTxt");
	if (links[0]==null) return false; //se nao tem links sai da iteracao
  //Colocar hrefs e Ids  (numero de link tem de ser igual a texto)
  for (var i=0; i<links.length; i++ ) {
  	links[i].setAttribute("href","#idOPEN"+i) ;
    texto[i].setAttribute("id","idOPEN"+i);
  }
  //
	for (var i=0; i<links.length; i++ ) {
   	var sectionId = links[i].getAttribute("href").split("#")[1];
			links[i].destination = sectionId;
    	links[i].onclick = function() {
      	fOpenTexto(this.destination);
				fBtnLink(this);
      	return false;
    	}
   }

}

/* =Preparar Eventos
-----------------------------------------------------------------------*/
addLoadEvent(iniciarLinksFooter);
addLoadEvent(iniciarHighlights);
addLoadEvent(iniciarForms);
addLoadEvent(iniciarEmails);
addLoadEvent(iniciarNewsletter);
addLoadEvent(iniciarAcordeao);
addLoadEvent(iniciarValidacao);  


