//EDITORES DE TEXTO COMPLEJOS
                      
//Editor simple de texto
tinyMCE.init({
    mode : "textareas",
    editor_selector : "textoEditorSimple",
    content_css : "/zonaprivada/css/pv-editor.css",
    force_br_newlines : true,
    valid_elements : "strong/b,br,a[href],u,i",
    language: "es",
    plugins : "print,paste,searchreplace",
    theme:"advanced",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,underline,italic,separator,enlace,unlink,separator,undo,redo,search,replace,pastetext,pasteword,separator,sub,sup,separator,charmap,separator,print",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    setup : function(ed) {
        ed.addButton('enlace',contenidoEnlace(ed));
    }
});

//Editor complejo de texto
tinyMCE.init({
    mode : "textareas",
    editor_selector : "textoEditorContenido",
    content_css : "/zonaprivada/css/pv-editor.css",
    valid_elements : "strong/b,p,a[href],u,i,ul,li,h6",
    language: "es",
    plugins : "print,paste,searchreplace",
    theme:"advanced",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,underline,italic,separator,enlace,unlink,separator,undo,redo,search,replace,pastetext,pasteword,separator,sub,sup,separator,charmap,separator,titulo,bullist,separator,print",
    theme_advanced_buttons2 : "",
    theme_advanced_buttons3 : "",
    setup : function(ed) {
        ed.addButton('enlace',contenidoEnlace(ed));
        ed.addButton('titulo',contenidoTitulo(ed));
    }
});             

//botones
function contenidoEnlace(ed){
    //registramos el selector
    registrarSelector("e"+ed.id,"/zonaprivada/lib/selectores/navegacion/selector-enlace.php");

    return {
             title : 'Enlazar el texto seleccionado',
             image : '/zonaprivada/img/iconos/botones/editor/enlace.gif',
             onclick : function() {
                abrirSelector("e"+ed.id,"tipoEnlace","idDestino=e"+ed.id+"&idEditor="+ed.id,true);
             }
    };
}

function contenidoImagen(ed){
    //registramos el selector
    registrarSelector("i"+ed.id,"/zonaprivada/lib/selectores/navegacion/selector-archivo.php");
    return {
             title : 'Insertar una imagen',
             image : '/zonaprivada/img/iconos/botones/editor/imagen.gif',
             onclick : function() {  
                abrirSelector("i"+ed.id,"idDestino=i"+ed.id+"&idEditor="+ed.id+"&t=1",true);
             }
    };
}


function contenidoTitulo(ed){
    return {
             title : 'Insertar un título',
             image : '/zonaprivada/img/iconos/botones/editor/titulo.gif',
             onclick : function() {
                ed.selection.setContent("<h6>"+ed.selection.getContent({format : 'text'})+"</h6>");
             }
    };
}

//funciones de aplicacion de formato
function aplicarEnlace(idEditor,valorEnlace){
    var editor = tinyMCE.get(idEditor);
    if(editor)
        editor.selection.setContent("<a href=\""+valorEnlace+"\">"+editor.selection.getContent({format : 'text'})+"</a>");
    cerrarVentanaSelector();
}

function aplicarImagen(idEditor,idArchivo,idVersion,alineacion){
    var editor = tinyMCE.get(idEditor);
    if(editor){
        var urlArchivo = ejecutarAjaxSync("/zonaprivada/lib/ajax/ruta-archivo.php","id="+idArchivo+"&v="+idVersion,"POST");
        var clase = "";
        if(alineacion=="der") clase = " class=\"colR\"";
        if(alineacion=="izq") clase = " class=\"colL\"";
        var img = "<img src=\""+urlArchivo+"\" arc=\""+idArchivo+"\""+clase+" />";
        if(editor.selection.isCollapsed() && esIE()) //bug en IE
            editor.setContent(editor.getContent({format : 'raw'})+img, {format : 'raw'});
        else
            editor.selection.setContent(img);
    }
    cerrarVentanaSelector();
}
