tinyMCE.init({
   theme : "advanced",
   skin : "scr",
   skin_variant : "black",
   mode: "textareas",
   editor_selector : "rich",
   plugins : "bbcode,paste",
   paste_use_dialog : false,
   paste_auto_cleanup_on_paste : true,
   paste_convert_headers_to_strong : false,
   paste_strip_class_attributes : "all",
   paste_remove_spans : true,
   paste_remove_styles : true,
   paste_preprocess : function(pl, o) { // remove newlines at end
       o.content = o.content.replace(/<br[^<>]*>$/ig, ""); 
   },
   /* for a localized version: install your language file, uncomment next line and adapt it */
   /* language : "it", */
   language : "en",
   theme_advanced_buttons1 : "bold,italic,underline,bullist,numlist,code,image,link,unlink,fontsizeselect,forecolor,undo,redo,removeformat",
   theme_advanced_buttons2 : "",
   theme_advanced_buttons3 : "",
   theme_advanced_toolbar_location : "top",
   theme_advanced_toolbar_align : "center",
   /* for a localized version: you should adapt next line by translating texts */
   theme_advanced_font_sizes : "tiny=50%,small=85%,normal=100%,big=150%,huge=200%",
   content_css : "styles/prosilver/theme/bbcode.css",
   convert_fonts_to_spans: true,
   font_size_style_values : "8,10,12,14,18,24,36",
   entity_encoding : "raw",
   add_unload_trigger : false,
   remove_linebreaks : false,
   convert_newlines_to_brs : true,
   setupcontent_callback : "inizia",
   force_br_newlines : true,
   force_p_newlines : false,
   forced_root_block : false,
   apply_source_formatting : false,
   media_types : "flash=swf",
   convert_urls : false,
   remove_script_host : false,
   relative_urls : true,
   theme_advanced_toolbar_align : "left"      
});
function toggla(bb)
{
  var bottone = document.getElementById('bottonecambia');
  if (tinyMCE.getInstanceById('message') != null) {
    tinyMCE.execCommand('mceRemoveControl', false, 'message');
    bb.style.display = '';
    bottone.removeChild(bottone.firstChild);
    bottone.appendChild(document.createTextNode('visual editor'));
  } else {
    tinyMCE.execCommand('mceAddControl', false, 'message');
    bb.style.display = 'none';
    bottone.removeChild(bottone.firstChild);
    bottone.appendChild(document.createTextNode('bb source'));
  }
}
function inizia(editor_id, body, doc)
{
  rte = tinyMCE.getInstanceById(editor_id);
  var bb = document.getElementById('format-buttons');
  if (bb && bb.style.display != 'none') {
    bb.style.display = 'none';
    var sottobb = document.createElement('div');
    bb.parentNode.insertBefore(sottobb, bb.nextSibling);
    var a1 = document.createElement('a');
    a1.setAttribute('id', 'bottonecambia');
    a1.appendChild(document.createTextNode('bb source'));
    tinymce.dom.Event.add(a1, 'click', function(e) {toggla(bb);});
    sottobb.appendChild(a1);
  }
  var boxfaccine = document.getElementById('smiley-box');
  if (!boxfaccine) {
    return;
  }
  var imgs = boxfaccine.getElementsByTagName('img');
  var ia;
  for (var i = 0; i < imgs.length; i ++) {
    ia = imgs[i].parentNode;
    ia.onclick = null;
    if (ia.getAttribute('onclick') == 'javascript:return false;') {
      continue;
    }
    ia.setAttribute('onclick', 'javascript:return false;');
    tinymce.dom.Event.add(ia, 'click', function(e) {
      var bb = document.getElementById('format-buttons');
      if (bb && bb.style.display != 'none') {
        insert_text(e.target.alt, true);
      } else {
        rte.execCommand('mceInsertContent', false, rte.dom.createHTML('img', {src: e.target.src}));
      }
      tinymce.dom.Event.stop(e);
    });
  }
  rte.focus();
}
