
function menu_onchange( el ) {
	var opt = el.options[el.selectedIndex];
	if ( opt.value == '#' ) {
		return;
	}
	document.location = opt.value;
}
function menu( el ) {
	el.blur();
	var pa = el.parentNode;
	if ( pa.className.indexOf("expand") != -1 ) {
		pa.className = "colapse";
	} else {
		pa.className = "expand";
	}
	var submenu = pa.nextSibling;
	if ( submenu.nodeType != 1 ) {
		submenu = submenu.nextSibling;
	}
	if ( submenu.className.indexOf("submenu") == -1 ) {
		return;
	}
	submenu.style.display = ( pa.className.indexOf("expand") != -1 )?'none':'';
}




var Ac = {
	size:16,
	letraMaior:function() {
		if ( this.size < 20 ) {
			this.size++;
createCookie("fs", this.size, 365);
			document.body.style.fontSize = this.size+'px';
		}
	},
	letraMenor:function() {
		if ( this.size > 16 ) {
			this.size--;
createCookie("fs", this.size, 365);
			document.body.style.fontSize = this.size+'px';
		}
	},
	contraste:function() {
		setActiveStyleSheet('Acessível')
	},
	normal:function() {
		setActiveStyleSheet('Padrão');

	},
	impressao:function() {
		setActiveStyleSheet('Impressão');
		var print = document.getElementById("print_preview_aviso");
		if ( print != null ) {
			print.innerHTML = "";
			var aviso = document.createElement("div");
			aviso.className = "aviso";
			var strong = document.createElement("strong");
			strong.appendChild(document.createTextNode("Atenção, esta é uma versão para impressão da página que você estava."));
			aviso.appendChild(strong);
			aviso.appendChild(document.createElement("br"));
			aviso.appendChild(document.createTextNode("O que será impresso é o documento a partir do tracejado abaixo."));
			aviso.appendChild(document.createElement("br"));
			var a = document.createElement("a");
			a.appendChild(document.createTextNode("Após a impressão, clique aqui para voltar a página anterior."));
			a.onclick = function(){setActiveStyleSheet('Padrão'); this.parentNode.parentNode.removeChild(this.parentNode); return false;};
			a.href = "javascript:;";
			aviso.appendChild(a);
			print.appendChild(aviso);
			print.appendChild(document.createElement("hr"));
var img = document.createElement("img");
img.src = "../imagens/logo_print.gif";
document.getElementById("logo_print").appendChild(img);

		}
	}

}


function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function goanchor(an) {document.location = (document.location+"").split("#")[0]+"#"+an;}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  var fs = readCookie("fs");
  if ( !isNaN(parseInt(fs)) ) {
    document.body.style.fontSize = parseInt(fs)+"px";
    Ac.size = parseInt(fs);
  }
  iniciarDestaque();
}


if ( document.body ) {
  var fs = readCookie("fs");
  if ( !isNaN(parseInt(fs)) ) {
    document.body.style.fontSize = parseInt(fs)+"px";
    Ac.size = parseInt(fs);
  }
}



window.onunload = function(e) {
  var title = getActiveStyleSheet();
  if (title != 'Impressão') {
	  createCookie("style", title, 365);
  }
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


var slides = {
	fx:null,
	i:1,
	paused:true,
	go:function( n ) {
		if ( this.i == n  || (this.fx != null && this.fx.now != 1) ) {
			return;
		}
		
		this.i = n 
		$( 'd' + this.i ).setStyle('opacity', 0);
		
		var el = $('d'+this.i);
		this.fx = new Fx.Style(el, 'opacity', {duration:500});
		this.fx.custom(0, 1);
		el.setStyle('display', '');
		$("b1").className = (n==1)?"selected":"";
		$("b2").className = (n==2)?"selected":"";
		$("b3").className = (n==3)?"selected":"";
		$("b4").className = (n==4)?"selected":"";
		$("b5").className = (n==5)?"selected":"";
	},
	pause:function() {
		if ( this.paused ) {
			this.ti = setInterval(this.forward.bind(this), 12000);
		} else {
			clearInterval(this.ti);
		}
		this.paused = !this.paused;
		if ( this.paused ) {
			$("pause").className = "paused";
		} else {
			$("pause").className = "playing";
		}
	},
	forward:function() {
		var t = this.i + 1;
		if ( t > 5 ) {
			t = 1;
		}
		this.go( t );
	},
	rewind:function() {
		var t = this.i - 1;
		if ( t < 1 ) {
			t = 5;
		}
		this.go( t );
	}
};

// as duas funcões abaixo são responsaveis pelos destaques ficarem "girando"
function destaqueAutoRun(){
  if(meu_destaque) {
    meu_destaque.autorun();
    if(typeof meu_destaque.autoruntimer!="undefined")
      setTimeout(destaqueAutoRun,meu_destaque.autoruntimer);
  }
}

function iniciarDestaque(){
  if(meu_destaque) {
    meu_destaque.autoruntimer = 6000;
    if(tempoDestaqueGira)
      meu_destaque.autoruntimer = tempoDestaqueGira;
    else
      meu_destaque.autoruntimer = 6000;
    setTimeout(destaqueAutoRun,meu_destaque.autoruntimer);
  }
}

function imprime(txt,perc) {
	if(typeof perc == "undefined") {
		perc=0.8;
	}
	var impressao=window.open('','','resizable=1,left=0,top=0,width='+screen.availWidth*perc+',height='+screen.availHeight*perc+'');
	impressao.document.write(txt);
	impressao.document.close();
	impressao.focus();
	impressao.print();
	impressao.close();
}

//Retorna um array de links {ref:..,desc:...}
function pegaPapai(obj) {
	var ret=null;
	if(obj!=null) {
		ret=obj;
		while(ret.id=="") {
			ret=ret.parentNode;
		}
	}
	return ret;
}

function buscaLinks(div_id) {
	var elems=document.getElementsByTagName("a");
	if(elems.length>0) {
		var links=Array();
		for(var i=0; i<elems.length; i++) {
			if(pegaPapai(elems[i].parentNode).id==div_id) {
				links.push({"ref": elems[i].href, "desc": elems[i].innerHTML});
			}
		}
		return links.length==0?null:links;
	} else {
		return null;
	}
}

//Retorna uma lista (<ul>) de links
function formataLinks(links) {
	if(links!=null) {
		var ret="<br/><hr/><br/>Resumo de links<br/><br/>";
		ret+="<ul>\n";
		for(var l in links) {
			ret+="<li><a href=\""+links[l]["ref"]+"\">"+links[l]["desc"]+"</a></li>";
		}
		ret+="</ul>";
		return ret;
	} else {
		return "";
	}
}

function formEnviaEmail(assunto,link) {
  link=location.protocol + '//' + location.host + link;
  Shadowbox.open(
    {
      player:  'html',
      title:   'Enviar Notícia',
      content: '<div style="background-color: #ffffff; border:2px solid #000000; padding: 15px;">'
                  + assunto + '<br/>'
                  + '. . . . . . . . . . . . . . . . . . . . . . . . . . . . .<br/>'
                  + 'ASCOM - TRE-MG<br/><br/><br/>'
                  + 'ENVIE:<br/><br/><br/>'
                  + '<form id="frmEnviaEmail" action="http://rmgstre02.tre-mg.gov.br/misc/index.jsp" method="POST">'
                    + '<input id="frmEnviaEmail_tipo_servico" type="hidden" value="EMAIL"/>'
                    + '<input id="frmEnviaEmail_link" type="hidden" value="' + link + '"/>'
                    + '<input id="frmEnviaEmail_assunto" type="hidden" value="' + assunto + '"/>'
                    + 'E-mail(s) do(s) destinatário(s): (separados por vírgulas)<br/>'
                    + '<input id="frmEnviaEmail_para" type="text" value="" size="60"/><br/><br/>'
                    + 'Seu nome:<br/>'
                    + '<input id="frmEnviaEmail_de" type="text" value="" size="60"/><br/><br/>'
                    + 'Seu email:<br/>'
                    + '<input id="frmEnviaEmail_de_email" type="text" value="" size="60"/><br/><br/>'
                    + 'Comentário:<br/>'
                    + '<textarea id="frmEnviaEmail_corpo" type="text" rows="9" cols="47"></textarea><br/><br/>'
                    + '<input type="button" value="Enviar" onclick="processaEmail(\'frmEnviaEmail\')"/>'
                  + '</form>'
                + '</div>',
      width:   400,
      height:  560
    }
  );
}

function processaEmail(id) {
  if(document.getElementById('frmEnviaEmail_para').value=='') {
    alert('Por favor informe pelo menos 1 destinatário.');
    document.getElementById('frmEnviaEmail_para').focus();
  } else if(document.getElementById('frmEnviaEmail_de_email').value=='') {
    alert('Por favor informe pelo menos 1 destinatário.');
    document.getElementById('frmEnviaEmail_de_email').focus();
  } else {
    var para=document.getElementById('frmEnviaEmail_para').value.replace(/ /g,"");
    var de_txt=document.getElementById('frmEnviaEmail_de').value;
    var de=document.getElementById('frmEnviaEmail_de_email').value.replace(/ /g,"");
    var corpo="Seu amigo(a) ";
    if(de_txt!="") {
      de="\""+de_txt+"\" <"+de+">";
      corpo+=de_txt;
    } else {
      corpo+=de;
    }
    var assunto=document.getElementById('frmEnviaEmail_assunto').value;
    var mensagem=document.getElementById('frmEnviaEmail_corpo').value;
    var link=document.getElementById('frmEnviaEmail_link').value;
    corpo+=" indicou esta notícia da Sala de Imprensa do TRE-MG para você.\n\n" + assunto + " (" + link + ")";
    if(mensagem!="") {
	    corpo+="\n\nMensagem: " + mensagem;
    }
    var xmlhttp;
    if (window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    } else {
      alert("Por favor visite esta página com outro navegador.");
    }
    xmlhttp.open("POST",location.protocol+"//"+location.host+"/misc/index.jsp",false);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.send("tipo_servico=EMAIL&de="+de+"&para="+para+"&corpo="+corpo+"&assunto="+assunto);
    if(xmlhttp.responseText=='1') {
      alert("Notícia enviada com sucesso.");
      Shadowbox.close();
    } else {
      alert("Houve um problema no envio de email, favor tentar mais tarde.");
    }
  }
}

function formataImpressao(id,data,hora) {
	var txt=data+' - '+hora;
	imprime(txt+document.getElementById(id).innerHTML);
}
