var _navegador = navigator.userAgent;
var ie = /msi/i.test(_navegador);
var op = /opera/i.test(_navegador);
var mo = /gecko/i.test(_navegador);
var otro = !(ie || mo);
var _insertor, _insertar, _formulario, _texto, _lector = "";
var elEditor;
var ID_OBJETCT;
var CONTENIDO_EDITOR='';

/***********************************************
DATOS COSTUMIZADOS
************************************************/

function vistaHtml(){
	
	var str =elEditor.value; 
	str = new String(str.replace(/\n/g," <br />"));
	str = new String(str.replace(/\[\*\]/g, '<li>'));
	if(document.getElementById('vista_prev_'+elEditor.id)){
	   document.getElementById('vista_prev_'+elEditor.id).innerHTML	= str;    
	}

}
function editor_salir(Id){ 
   if(confirm('Salir sin guardar cambios?')){
     document.getElementById('DIV_EDITOR').style.display = 'none';
   }
   
}
function editor_aceptar(Id){ 
     CONTENIDO_EDITOR= document.getElementById('editor_'+Id).value;
     document.getElementById('DIV_EDITOR').style.display = 'none';
   
   
}
function inserta_img(Img){
  _insertar(elEditor,  _lector() + '['+Img+']');
}

function _funct(NameFunct){
	
	switch(NameFunct){
	  case "negritas":
	  	 _insertar(elEditor, '<b>' +  _lector() + '</b>');
	     break;
	  case "subrayado":
	     _insertar(elEditor, '<u>' +  _lector() + '</u>');
		 break;
	  case "lista":
	     _insertar(elEditor, '<li>' +  _lector() + '</li>');
		 break;
	  case "uri":
	     var enlace = prompt("Introduzca la URL:", "http://");
	     _insertar(elEditor, '<a href="' + enlace + '">' + prompt("enlace:", _lector()) + '</a>');
		 break;
	  case "italica":
	     _insertar(elEditor, '<i>' +  _lector() + '</i>');
		 break;

      default:
	     alert('There is no such function: '+NameFunct);
	     break;
	}

}
function displayPallet(idPallet){
    var obj = document.getElementById(idPallet);
	if(obj.style.display == 'none'){
   	  obj.style.display = 'block';
    }else{
	  obj.style.display = 'none'; 	
	}

}
function header_style(idH){
	 if(idH>0){
       HeadSt = 'h'+idH;
	   _insertar(elEditor, '<'+HeadSt+'>' + _lector() + '</'+HeadSt+'>');	 
	 }	 
}
function selectColor(Backg_FontC,Color){

    _insertar(elEditor, '<span style='+Backg_FontC+':'+Color+'>' + _lector() + '</span>');
	displayPallet('palet_'+Backg_FontC+'_'+elEditor.id);
	
	
}

function previewColor(Color){
   
}

			
/********************************
no modificar a partir de aqui
*********************************/
function datos_ie() {
	_texto = document.selection.createRange().text;
	if (_formulario.createTextRange)
		_formulario.posi = document.selection.createRange().duplicate();
	return true;
}

function captura_ie()	{
	return _texto;
}

function captura_mo() {
	with (_formulario) return value.substring(selectionStart, selectionEnd);
}

function captura_otro()	{
	return "";
}

function poner_mo(f, x)	{//alert(x);
	var _ini = f.selectionStart;
	var _fin = f.selectionEnd;
	var inicio = f.value.substr(0, _ini);
	var fin = f.value.substr(_fin, f.value.length);

	f.value = inicio + x + fin;
	if (_ini == _fin)	{
		f.selectionStart = inicio.length + x.length;
		f.selectionEnd = f.selectionStart;
	}
	else	{
		f.selectionStart = inicio.length;
		f.selectionEnd = inicio.length + x.length;
	}
	f.focus();
}

function poner_otro(f, x)	{// opera u otros navegadores desconocidos
	f.value += x;//alert(x);
	f.focus();
}

function poner_ie(f, x)	{
	f.focus();
	if (f.createTextRange)	{// && f.posi)	{
		if (!f.posi)	datos_ie();
		with(f)	{
			var actuar = (posi.text == "");
			posi.text = x;
			if (!actuar)
				posi.moveStart("character", -x.length);
			posi.select();
		}
	}
}

function ini_editor(formu)	{
	_formulario = formu;
	
	if (op || mo)	{//alert("mozilla u opera");
		_insertar = function(f, x) {poner_mo(f, x);vistaHtml();};
		_lector = captura_mo;
	}

else	if (otro)	{//alert("otro");
		_insertar = function(f, x) {poner_otro(f, x);vistaHtml();};
		_lector = captura_otro;
	}

else	if (ie)	{
		_formulario.onchange = datos_ie;
		_formulario.onclick  = datos_ie;
		_insertar = function(f, x) {poner_ie(f, x);vistaHtml();};
		_lector = captura_ie;
	}
	return formu;
}

