var yh$richEditConfig;

var tinyMCEwbOptions = {
  basic : {
    theme :'advanced',
    plugins:'imgtool,paste,inlinepopups',
    theme_advanced_buttons1 : "copy,paste,pasteword,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,formatselect,|,bullist,numlist,|,outdent,indent,|,link,unlink,|,help",
    theme_advanced_buttons2 : '',
    theme_advanced_buttons3 : '',
//  popup_css:'/custom/dialog.css',
    forced_root_block:'p'
  },
  simple : {
    theme :'advanced',
    plugins:'imgtool,advlink,contextmenu,paste,style,fullscreen,searchreplace,inlinepopups',
	  theme_advanced_buttons2_add : "styleprops,pastetext,pasteword,selectall,imgtool,fullscreen,search,replace",
    theme_advanced_buttons2_add_before : "forecolor,backcolor,fontsizeselect,|", // ,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,|,hr,removeformat,visualaid,|,sub,sup,|,charmap",
    theme_advanced_buttons1_add : "|,hr,removeformat,visualaid,|,sub,sup,|,charmap",
    theme_advanced_buttons3 : '',
    theme_advanced_resize_horizontal:1,
    theme_advanced_resizing : true,
//  popup_css:'/custom/dialog.css',
    forced_root_block:'p'
  },
  stylesheet : {
    // Theme that attempts to prevent authors from over-riding stylesheet formatting.
    theme :'advanced',
    plugins:'imgtool,advlink,paste,table,fullscreen,searchreplace,inlinepopups,imgmap',
    extended_valid_elements : "img[id|usemap|style|class|src|border|color|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],map[id|name],area[shape|alt|coords|href|target],iframe[src|width|height|name|align]",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_buttons1: "styleselect,|copy,paste,pasteword,pastetext,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,formatselect,|,bullist,numlist,|,outdent,indent,|,link,unlink",
	  theme_advanced_buttons2: "tablecontrols,styleprops,pasteword,selectall,imgtool,fullscreen,search,replace,imgmap,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,code,help",
    theme_advanced_buttons3 : '',
    theme_advanced_resize_horizontal:1,
    theme_advanced_resizing : true,
//  popup_css:'/custom/dialog.css',
    forced_root_block:'p'
  },
  advanced : {
    theme :'advanced',
    plugins:'imgtool,advlink,contextmenu,paste,table,style,fullscreen,searchreplace,inlinepopups,imgmap',
    extended_valid_elements : "img[id|usemap|style|class|src|border|color|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],map[id|name],area[shape|alt|coords|href|target],iframe[src|width|height|name|align]",
    theme_advanced_statusbar_location : "bottom",
	  theme_advanced_buttons2_add : "tablecontrols,styleprops,pastetext,pasteword,selectall,imgtool,fullscreen,search,replace,imgmap",
    theme_advanced_buttons1_add_before : "forecolor,backcolor,fontsizeselect,styleselect,|", // ,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,|,hr,removeformat,visualaid,|,sub,sup,|,charmap",
    theme_advanced_buttons1_add : "|,hr,removeformat,visualaid,|,sub,sup,|,charmap",
    theme_advanced_buttons3 : '',
    theme_advanced_resize_horizontal:1,
    theme_advanced_resizing : true,
//  popup_css:'/custom/dialog.css',
    forced_root_block:'p'
  },
  advcompact : {
    theme :'advanced',
    plugins:'imgtool,advlink,contextmenu,paste,table,style,fullscreen,searchreplace,inlinepopups,imgmap',
    extended_valid_elements : "img[id|usemap|style|class|src|border|color|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],map[id|name],area[shape|alt|coords|href|target],iframe[src|width|height|name|align]",
    theme_advanced_statusbar_location : "bottom",
	  theme_advanced_buttons3 : "tablecontrols,styleprops,pastetext,pasteword,selectall,imgtool,fullscreen,search,replace,imgmap",
    theme_advanced_buttons1_add_before : "forecolor,backcolor,fontsizeselect,styleselect,|", // ,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,|,hr,removeformat,visualaid,|,sub,sup,|,charmap",
    theme_advanced_buttons2_add : "|,hr,removeformat,visualaid,|,sub,sup,|,charmap",
//  theme_advanced_buttons3 : '',
    theme_advanced_resize_horizontal:1,
    theme_advanced_resizing : true,
//  popup_css:'/custom/dialog.css',
    forced_root_block:'p'
  }
}

function startRichEditors() {
  for (var i=0;i<richEditorList.length;i++) {
    tinyMCE.settings.content_css = richEditorList[i].content_css+'?p='+Math.floor(1000*Math.random());
    tinyMCE.settings.theme_advanced_styles = richEditorList[i].theme_advanced_styles;
    tinyMCE.settings.document_base_url = richEditorList[i].baseURL;

    var modeSel = 'advanced';

    if (richEditorList[i].mode!=null && (richEditorList[i].mode in ({basic:1,simple:1,stylesheet:1,advanced:1,advcompact:1})))
      modeSel = richEditorList[i].mode;

    var onMode=tinyMCEwbOptions[modeSel];

    for (var m in tinyMCEwbOptions) {
      modeDef = tinyMCEwbOptions[m];
      if (m!=modeSel) {
        for (var f in modeDef)
          delete tinyMCE.settings[f];
      }
    }
    for (var f in onMode)
      tinyMCE.settings[f] = onMode[f];

    tinyMCE.execCommand("mceAddControl", true, richEditorList[i].id)
  }
}

var tinyMCEyhConfig = {
  theme_advanced_toolbar_location : "top",
//theme_advanced_layout_manager : 'simplelayout',
//debug:1,
  button_tile_map : true,
  mode : "none",
  relative_urls:false,
	table_styles : "Header 1=header1;Header 2=header2;Header 3=header3",
	table_cell_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Cell=tableCel1",
	table_row_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1",
	table_cell_limit : 100,
	table_row_limit : 20,
	table_col_limit : 10,
  inline_styles:0,
  gecko_spellcheck : true,
  onpageload:'startRichEditors',

  skin : "o2k7",
  skin_variant : "silver",
	convert_fonts_to_spans : 1,
  theme_advanced_font_sizes : "80%=80%,100%=100%,120%=120%,140%=140%,180%=180%,200%=200%,240%=240%",
  font_size_style_values : "80%,100%,120%,140%,180%,200%,240%",
//valid_child_elements : "h1/h2/h3/h4/h5/h6/a[%itrans_na],strong/b/p/div/em/i/td[%itrans|#text],body[%btrans|#text]",
	fix_table_elements : true,
  paste_create_linebreaks : true,
	paste_use_dialog : true,
	paste_auto_cleanup_on_paste : false,
	paste_convert_middot_lists : false,
	paste_unindented_list_class : "unindentedList",
	paste_convert_headers_to_strong : true,
  paste_remove_styles : true,
//paste_insert_word_content_callback : "convertWord",
  paste_strip_class_attributes : 'mso'
}

if (yh$richEditConfig!=null) {
  for (var f in yh$richEditConfig) {
    tinyMCEyhConfig[f] = yh$richEditConfig[f];
  }
}
function convertWord(type, content) {
  function repFn(s) {
    s=s.toLowerCase();
    if (s.indexOf('<p>')==0) n='';
    else if (s.indexOf('<li>')==0) n='<li>';
    else if (s.indexOf('</p>')==0 && s.indexOf('<li>')>0) n='</li><li>';
    else if (s.indexOf('</p>')==0) n='</li>';
    // alert("TRANSLATE "+s+" TO "+n);
    return n==null?s:n;
  }
	switch (type) {
		// Gets executed before the built in logic performes it's cleanups
		case "before":
//		content = content.toLowerCase(); // Some dummy logic
			break;

		// Gets executed after the built in logic performes it's cleanups
		case "after":
      // alert("CONTENT IN : "+content);
			content = content.replace(/(\<p\>\s*\<br\>\s*\<\/p\>)|(\<li\>\s*\<p\>)|(\<\/p\>\s*\<\/li\>)|(\<\/p\>\s*\<li\>\s*\<p\>)|(\<\/p\>\s*\<li\>)/gi,repFn);
      // alert("CONTENT OUT : "+content);
			break;
	}

	return content;
}
tinyMCE.init(tinyMCEyhConfig);
