var metodo;
var cor_escolhida;

sistema_adicionarEvento(window,"load",editorHTML_actualizarFrames);
sistema_adicionarEvento(window,"submit",editorHTML_actualizarInputs);

function editorHTML_actualizarFrames(){
	forms_estadoSubmit(false);
	var frames_area_html=document.getElementsByTagName("iframe");
	for(i=0;i<frames_area_html.length;i++){
		if(frames_area_html[i].tipo=="areahtml"){
			var idFrame=frames_area_html[i].id;
			idFrame=idFrame.substr(0,(idFrame.length-6));
			editorHTML_modoDesign(idFrame);
			editorHTML_actualizarFrame(idFrame);
		}
	}
	forms_estadoSubmit(true);
}

function editorHTML_actualizarFrame(idFrame){
	if(frames[idFrame + "_frame"].document){
		setTimeout(function(){
			frames[idFrame + "_frame"].document.body.innerHTML = document.getElementById(idFrame).value;
		},500);
	} else if(document.getElementById(idFrame + "_frame").contentDocument){
		document.getElementById(idFrame + "_frame").contentDocument.body.innerHTML=document.getElementById(idFrame).value;
	} else {
		alert("ocorreu um erro com este browser");
	}
}

function editorHTML_actualizarInputs(){
	var frames_area_html=document.getElementsByTagName("iframe");
	for(i=0;i<frames_area_html.length;i++){
		if(frames_area_html[i].tipo=="areahtml"){
			var idInput=frames_area_html[i].id;
			idInput=idInput.substr(0,(idInput.length-6));
			editorHTML_actualizarInput(idInput);
		}
	}
}

function editorHTML_actualizarInput(idInput){
	if(frames[idInput + "_frame"].document){
		document.getElementById(idInput).value = frames[idInput + "_frame"].document.body.innerHTML;
	} else if(document.getElementById(idInput + "_frame").contentDocument){
		document.getElementById(idInput).value = document.getElementById(idInput + "_frame").contentDocument.body.innerHTML;
	} else {
		alert("ocorreu um erro com este browser");
	}
}

function editorHTML_modoDesign(id){
	if(document.getElementById(id + "_frame").contentDocument){
		document.getElementById(id + "_frame").contentDocument.designMode = "on";
		if(sistema_browser()=="ff"){
			document.getElementById(id + "_frame").contentDocument.designMode = "off";
			document.getElementById(id + "_frame").contentDocument.designMode = "on";
			
			document.getElementById(id + "_frame").contentDocument.body.style.fontFamily="Verdana, Helvetica, Arial, sans-serif";
			document.getElementById(id + "_frame").contentDocument.body.style.fontSize="12px";
			document.getElementById(id + "_frame").contentDocument.body.style.color="#555555";
			
			sistema_adicionarEvento(document.getElementById(id + "_frame").contentDocument,'keyup',function(){ editorHTML_estadoBotao(id + "_frame"); });
			sistema_adicionarEvento(document.getElementById(id + "_frame").contentDocument,'mouseup',function(){ editorHTML_estadoBotao(id + "_frame"); });
			sistema_adicionarEvento(document.getElementById(id + "_frame").contentDocument,'blur',function(){ editorHTML_estadoBotao(id + "_frame"); });
		}
	} else if(frames[id + "_frame"].document){
		frames[id + "_frame"].document.designMode = "On";
		
		sistema_adicionarEvento(frames[id + "_frame"].document,'keyup',function(){ editorHTML_estadoBotao(id + "_frame"); });
		sistema_adicionarEvento(frames[id + "_frame"].document,'mouseup',function(){ editorHTML_estadoBotao(id + "_frame"); });
		sistema_adicionarEvento(frames[id + "_frame"].document,'blur',function(){ editorHTML_estadoBotao(id + "_frame"); });
		
		setTimeout(function(){
			frames[id + "_frame"].document.body.style.fontFamily="Verdana, Helvetica, Arial, sans-serif";
			frames[id + "_frame"].document.body.style.fontSize="12px";
			frames[id + "_frame"].document.body.style.color="#555555";
		},100);
	} else {
		alert("ocorreu um erro com este browser");
	}
}

function editorHTML_comando(nome_frame, comando, parametro1, parametro2){
	if(comando!=null){
		nome_frame.document.execCommand(comando, parametro1, parametro2);
		editorHTML_estadoBotao(nome_frame.name);
		nome_frame.focus();
	}
}

function editorHTML_estadoBotao(nome_frame){
	var objecto=document.getElementById(nome_frame).contentWindow;
	var id_objecto=objecto.name;

	//Negrito
	if(objecto.document.queryCommandState("bold")){
		sistema_mudarEstadoBotao(id_objecto + "_negrito","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_negrito","desmarcado");
	}
	
	//Sublinhado
	if(objecto.document.queryCommandState("underline")){
		sistema_mudarEstadoBotao(id_objecto + "_sublinhado","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_sublinhado","desmarcado");
	}
	
	//Itálico
	if(objecto.document.queryCommandState("italic")){
		sistema_mudarEstadoBotao(id_objecto + "_italico","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_italico","desmarcado");
	}
	
	//Rasurado
	if(objecto.document.queryCommandState("strikethrough")){
		sistema_mudarEstadoBotao(id_objecto + "_rasurado","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_rasurado","desmarcado");
	}
	
	//Alinhar esquerda
	if(objecto.document.queryCommandState("justifyleft")){
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_esquerda","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_esquerda","desmarcado");
	}
	
	//Alinhar direita
	if(objecto.document.queryCommandState("justifyright")){
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_direita","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_direita","desmarcado");
	}
	
	//Alinhar centro
	if(objecto.document.queryCommandState("justifycenter")){
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_centro","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_centro","desmarcado");
	}
	
	//Alinhar justificar
	if(objecto.document.queryCommandState("justifyfull")){
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_justificar","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_alinhar_justificar","desmarcado");
	}
	
	//Sobrescrito
	if(objecto.document.queryCommandState("superscript")){
		sistema_mudarEstadoBotao(id_objecto + "_sobrescrito","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_sobrescrito","desmarcado");
	}
	
	//Subscrito
	if(objecto.document.queryCommandState("subscript")){
		sistema_mudarEstadoBotao(id_objecto + "_subscrito","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_subscrito","desmarcado");
	}
	
	//Numeracao
	if(objecto.document.queryCommandState("insertorderedlist")){
		sistema_mudarEstadoBotao(id_objecto + "_numeracao","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_numeracao","desmarcado");
	}
	
	//Marcas
	if(objecto.document.queryCommandState("insertunorderedlist")){
		sistema_mudarEstadoBotao(id_objecto + "_marcas","marcado");
	} else {
		sistema_mudarEstadoBotao(id_objecto + "_marcas","desmarcado");
	}
}

function editorHTML_adicionarImagem(imagem,link,comprimento,altura,alinhamento,border,titulo){
	var resultado;
	var link_ini="";
	var link_fim="";
	if(link!=""){
		link_ini="<a href='" + link + "' target='_self'>";
		link_fim="</a>";
	}
	resultado="<img src='" + imagem + "' border='" + border + "' title='" + titulo + "'";
	if(comprimento>0){
		resultado+=" width='" + comprimento + "'";
	}
	if(altura>0){
		resultado+=" width='" + altura + "'";
	}
	if(alinhamento=="esquerda"){
		resultado+=" style='float:left;'";
	} else if(alinhamento=="direita"){
		resultado+=" style='float:right;'";
	}
	resultado+=">";
	
	resultado=link_ini + resultado + link_fim;
	
	return(resultado);

}

/* ESTUDAR ESTAS FUNÇÕES PARA UMA FUTURA IMPLEMENTAÇÃO DE SMILES E OUTRAS POSSIBILIDADES DO GÈNERO */
/*function substituir(origem, objecto){
	texto_alterado=origem.replace( new RegExp("<? echo addslashes("\<(\/|)[a-zA-Z0-9\"\'\=\.\@\+\:\;\&\/\?\#\-\_\?\s]+(|\/)\>"); ?>","g"), " " );	
	texto_alterado=texto_alterado.replace( new RegExp("\\n","g"), "<br>" );
	<? for($i=0;$i<mysql_num_rows($sql);$i++){ ?>
		texto_alterado=texto_alterado.replace( new RegExp("<? echo addslashes(mysql_result($sql,$i,'cfg_tag')); ?>","g"), "<? echo mysql_result($sql,$i,'cfg_tag_alterado'); ?>" );
	<? } ?>
	

		
	document.getElementById(objecto).innerHTML = texto_alterado;
}

function alterar_texto(objecto, tag_inicio, tag_fim, destino) { 
    if(document.selection) { 
 		// Para o IE
		objecto.focus();
		var teste = document.selection.createRange().text;
		var resultado = tag_inicio + teste + tag_fim;
		document.selection.createRange().text=resultado;
		resultado = tag_inicio + teste + tag_fim;
	
    } else if(objecto.selectionStart>=0) { 
        // Para browsers que usem Gecko (como o Firefox)
		// verificar a posição inicial e final 
		var posicao_inicio = objecto.selectionStart; 
		var posicao_fim = objecto.selectionEnd; 
		var texto_inicio = objecto.value.substr(0,posicao_inicio);
		var texto_seleccionado = objecto.value.substr(posicao_inicio,posicao_fim-posicao_inicio);
		var texto_fim = objecto.value.substr(posicao_fim);
		
		// trocar o texto do objecto com a nova tag
		var resultado= texto_inicio + tag_inicio + texto_seleccionado + tag_fim + texto_fim;
		objecto.value = texto_inicio + tag_inicio + texto_seleccionado + tag_fim + texto_fim;
		
    } else { 
        // Caso seja outro browser qualquer
    }
	
	posicionar(objecto,posicao_fim+tag_inicio.length+tag_fim.length)
	//if(destino!=""){
		substituir(resultado,destino);
	//}
}

function posicionar(objecto, posicao) { 
    if(objecto.createTextRange) { 
        // reposicionar o cursor para a posição da substituição
        var range = objecto.createTextRange(); 
        range.move("character", posicao); 
        range.select(); 
    } else if(objecto.selectionStart>=0) { 
        objecto.focus(); 
        objecto.setSelectionRange(posicao, posicao);
		
    } 
} */
