var rybenaServer = "http://www.rybenamobile.com.br/";
var txt = "";
var primeiraVezLibras = true;
var primeiraVezVoz = true;
var isLibrasAppletCarregado = false;
var soundEmbed = null;
var isVozAppletCarregado = false;

function carregaRybenaLibras() {
	document.getElementById("libras").innerHTML =	
	"<div style='background-image: url(" + rybenaServer + "RybenaWEBServer/img/borda_libras.png); height: 297px; width: 190px;'> "
  + "	<div id='libras' class='drag' onclick='fechaLibras();' title='Fechar' " 
  + "		style='background-image: url(" + rybenaServer + "RybenaWEBServer/img/borda_fechar.png); height: 22px; width: 22px; position: relative; top: 3px; left: 162px;'> "
  + "	</div> "
  + "	<div style='position: relative; top: 10px; left: 10px;'> "
  + "		<applet id='rybenalibras' "
  + "		    	archive='" + rybenaServer + "RybenaWEBServer/RybenaApplet.jar' "
  + "			    code='br.com.cts.rybena.libras.applet.LibrasApplet' "
  + "			    width='170' "
  + "			    height='255'> "
  + "		   <param name='host' value='" + rybenaServer + "'> "
  + "		   <param name='tam' value='m'> "
  + "		</applet> "
  + "	</div> "
  + "</div> ";
	isLibrasAppletCarregado = true;
}


function carregaRybenaVoz() {
	document.getElementById("voz").innerHTML =
	"<div id='voz' class='drag' style='background-image: url(" + rybenaServer + "RybenaWEBServer/img/borda_voz.png); height: 96px; width: 150px;'> "
  + "	<div id='libras' class='drag' onclick='fechaVoz();' title='Fechar' " 
  + "		style='background-image: url(" + rybenaServer + "RybenaWEBServer/img/borda_fechar.png); height: 22px; width: 22px; position: relative; top: 3px; left: 123px;'> "
  + "	</div> "
  + "	<div style='position: relative;  top: 10px; left: 10px;'> "
  + "		<applet id='rybenaVoz' "
  + "				archive='" + rybenaServer + "RybenaWEBServer/RybenaApplet.jar' "
  + "				code='br.com.cts.rybena.voz.applet.VozApplet' "
  + "			 	width='130' "
  + "			 	height='55'> "
  + "			<param name='host' value='" + rybenaServer + "'> "
  + "  			<param name='tam' value='m'> "
  + "		</applet>"
  + "	</div> "
  + "</div>	";
	isVozAppletCarregado = true;
}

function fechaLibras() {
	document.getElementById("libras").style.display = "none";
}

function chamaLibras(){
	if(isVozAppletCarregado == true){
		fechaVoz();
	}	
	document.getElementById("libras").style.display = "block";
	libras();
}

function chamaVoz(){	
	if(isLibrasAppletCarregado == true){
		fechaLibras();
	}	
	document.getElementById("voz").style.display = "block";
	voz();
}

function verificaSelecao(){
	if (document.selection) { 
  		document.selection.empty();
	}else if(window.getSelection().removeAllRanges){ 
    		window.getSelection().removeAllRanges();
  	} else if(window.getSelection().empty){
    		window.getSelection().empty();
  	}
}

function fechaVoz() {
	document.getElementById("voz").style.display = "none";
	isVozAppletCarregado	= false;
}

function libras() {  	
	
	if (!isLibrasAppletCarregado)
	{
		carregaRybenaLibras();						
	}
	if (window.getSelection)
	{
		txt = window.getSelection();
		if((primeiraVezLibras) && (txt == ""))
		{
			txt = "Por favor, selecione algum texto para o Ryben&aacute;; traduzir";						
		}	        		
	} 
	else if (document.getSelection)
	{
		txt = document.getSelection();	
		if((primeiraVezLibras) && (txt == ""))
		{
			txt = "Por favor, selecione algum texto para o Ryben&aacute; traduzir";			
		}	
        	
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;			
		if((primeiraVezLibras) && (txt == ""))
		{
			txt = "Por favor, selecione algum texto e clique na imagem libras para que o Ryben&aacute;; traduza seus textos, para melhor utiliza&ccedil;&atilde;o atualize seu navegador de internet";			
		}
		if(txt == ""){
			txt = "Por favor, selecione algum texto e clique na imagem libras para que o Ryben&aacute;; traduza seus textos";
		}
	}
	else if(txt == "")
	{
		return;
	}	
	
	document.getElementById("libras").style.visibility = "visible";
	var appletD = document.getElementById("rybenalibras");	
	appletD.traduzir(txt);
	primeiraVezLibras = false;	
	window.onmouseup = libras		
	verificaSelecao();
}

function voz() {
    	
	if (!isVozAppletCarregado)
	{
		carregaRybenaVoz();				
	}	
	
	if (window.getSelection)
	{
		txt = window.getSelection();
		if((primeiraVezVoz) && (txt == ""))
		{
			txt = "Por favor, selecione algum texto para o Ryben&aacute;; voz converta para audio";
		}					
	} 
	else if (document.getSelection)
	{
		txt = document.getSelection();	
		if((primeiraVezVoz) && (txt == ""))
		{
			txt = "Por favor, selecione algum texto para o Ryben&aacute;; voz converta para audio";
		}				
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;	
		if((primeiraVezVoz) && (txt == ""))
		{
			txt = "Por favor, selecione algum texto e clique na imagem voz para que o Ryben&aacute;; converta seus textos para audio, para melhor utiliza&ccedil;&atilde;o atualize seu navegador de internet";			
		}	
		if(txt == ""){
			txt = "Por favor, selecione algum texto e clique na imagem libras para que o Ryben&aacute;; traduza seus textos";
		}
		
	}
	else if(txt == "")
	{
		return;
	}	

	document.getElementById("voz").style.visibility = "visible";	
	var appletD = document.getElementById("rybenaVoz");
	appletD.falar(txt);
	primeiraVezVoz = false;
	window.onmouseup = voz
	verificaSelecao();
}

function sizeFont (elem, acao){
    // tamanho inicial da fonte (em px)
    var tamInic = 16;
    // Tamanho m?nimo da [b]fonte (em px)
    var tamMin = 4;
    // Tamanho m?ximo da fonte (em px)
    var tamMax = 40;
    // Pega o tamanho da fonte. Se n?o foi setada ainda (primeira vez que a fun??o ? executada) ter? como tamanho padr?o 'tamInic'.
    if (document.getElementById(elem).style.fontSize == "") {
            var tamFonte = tamInic;
    }else{
            var tamFonte = parseInt(document.getElementById(elem).style.fontSize);
            }
            switch (acao){
                // Aumenta o tamanho, enquanto foi menor que 'tamMax'
                case '+':
                        if (tamFonte < tamMax)
                                document.getElementById(elem).style.fontSize = (tamFonte + 2) + "px";
                break;
                // Diminui o tamanbo, enquanto for maior que 'tamMin'
                case '-':
                        if (tamFonte > tamMin)
                                document.getElementById(elem).style.fontSize = (tamFonte - 2) + "px";
                break;
            }
}

function mudaCor(cor, cor2)
{
	document.getElementById('meio').style.background = cor;
	document.getElementById('meio').style.color = cor2;
}

var tgs = new Array( 'div' );
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;
function ts(trgt, inc) {
	if (!document.getElementById)
		return

	var d = document, cEl = null, sz = startSz, i, j, cTags;
	sz += inc;
	if (sz < 0)
		sz = 0;
	if (sz > 6)
		sz = 6;
	startSz = sz;
	if (!(cEl = d.getElementById(trgt)))
		cEl = d.getElementsByTagName(trgt)[0];

	cEl.style.fontSize = szs[sz];

	for (i = 0; i < tgs.length; i++) {
		cTags = cEl.getElementsByTagName(tgs[i]);
		for (j = 0; j < cTags.length; j++)
			cTags[j].style.fontSize = szs[sz];
	}
}
