visitors_edits_tools={
    data:{},
    clearTag:function(content,tag){
        var $contentDiv=jQuery("<div/>").html(content);
        $contentDiv.find(tag).each(function(){
            $tag=jQuery(this);
            $tag.remove();
        });
        if($contentDiv.find(tag).length>0)
            return visitors_edits_tools.clearTag($contentDiv.html(),tag);
        return $contentDiv.html();
    },
    clearTagName:function(content,tag){
        var $contentDiv=jQuery("<div/>").html(content);
        $contentDiv.find(tag).each(function(){
            $tag=jQuery(this);
            $tag.replaceWith($tag.html());
        });
        if($contentDiv.find(tag).length>0)
            return visitors_edits_tools.clearTagName($contentDiv.html(),tag);
        return $contentDiv.html();
    },
    clearClassName:function(content,classname){
        var $contentDiv=jQuery("<div/>").html(content);
        $contentDiv.find("."+classname).each(function(){
            $elm=jQuery(this);
            $elm.removeClass(classname);
        });
        if($contentDiv.find("."+classname).length>0)
            return visitors_edits_tools.clearTagName($contentDiv.html(),classname);
        return $contentDiv.html();
    },
    clear:function(ed){
        var elt=ed.selection.getNode();
        elt.remove();
    },
    clean:function(ed){
        var elt=ed.selection.getNode();
        var eltContent=elt.outerHTML;
        eltContent=visitors_edits_tools.clearTagName(eltContent,"ins");
        eltContent=visitors_edits_tools.clearTagName(eltContent,"del");
        elt.remove();
        ed.execCommand('mceInsertContent', 0, eltContent);
    }
};
(function($){
	//Create plugin
	tinymce.create('tinymce.plugins.visitors_edits', {
        init : function(ed, url) {
            //Add buttons
            ed.addButton('visitors_edits_approve', {
                title : 'Approve',
                cmd : 'visitors_edits_approve',
                image : url + '/../img/approve.png'
            });
            ed.addButton('visitors_edits_reject', {
                title : 'Reject',
                cmd : 'visitors_edits_reject',
                image : url + '/../img/reject.png',
            });
            ed.addButton('visitors_edits_clean', {
                title : 'Clean All',
                cmd : 'visitors_edits_clean',
                image : url + '/../img/clean.png'
            });
            //Add Commands
            ed.addCommand('visitors_edits_approve', function() {
                var elt=ed.selection.getNode();
                if(elt.tagName.toLowerCase()=="ins"){
                    visitors_edits_tools.clean(ed);
                }
                if(elt.tagName.toLowerCase()=="del"){
                    visitors_edits_tools.clear(ed);
                }
            });
            ed.addCommand('visitors_edits_reject', function() {
            	var elt=ed.selection.getNode();
                if(elt.tagName.toLowerCase()=="del"){
                    visitors_edits_tools.clean(ed);
                }
                if(elt.tagName.toLowerCase()=="ins"){
                    visitors_edits_tools.clear(ed);
                }
            });
            ed.addCommand('visitors_edits_clean',function(){
            	var content=ed.getContent();
            	content=visitors_edits_tools.clearTagName(content,"ins");
            	content=visitors_edits_tools.clearTag(content,"del");
                content=visitors_edits_tools.clearClassName(content,"diffmod");
            	ed.setContent(content);
                console.log("Cleaned");
            });
            //Load content
            setTimeout(function(){
                //Procedce diff Html
                var diffDiv=document.querySelector("#visitors_edits_diff");
                var $diffDiv=jQuery(diffDiv);
                $diffDiv.find("ins").each(function(){
                    var $ins=$(this);
                    if([""," "].indexOf($ins.html())>-1){
                        $ins.remove();
                    }
                });
                ed.setContent($diffDiv.html());
                //Clean content
                var $diffDiv=jQuery("<div/>").html(ed.getContent());
                $diffDiv.find("p").each(function(){
                    var $p=$(this);
                    if(["&nbsp;"].indexOf($p.html())>-1){
                        $p.remove();
                    }
                });
                ed.setContent($diffDiv.html());
            },0);
        },
    });
    // Register plugin
    tinymce.PluginManager.add( 'visitors_edits', tinymce.plugins.visitors_edits );
})(jQuery);
function editSubmit(){
	tinyMCE.activeEditor.execCommand("visitors_edits_clean");
    return true;
}