function TextHtmlMCE(id, cid) { this.id = id; this.cid = cid; this.initialized = false; this.content = ''; }
TextHtmlMCE.prototype.Edit = function() {
  this.content = $(this.id).innerHTML;
  if (this.initialized == false) {
  tinyMCE.init({
    		mode : "textareas",
    		theme : "advanced",
    		height : "600",
    		plugins : "imagemanager,filemanager,emotions,spellchecker,advhr,insertdatetime,preview,table,iframe,style",	
    		// Theme options - button# indicated the row# only
      	theme_advanced_buttons1 : "insertimage,newdocument,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,fontselect,fontsizeselect,formatselect,styleselect,iframe",
      	theme_advanced_buttons2 : "cut,copy,paste,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,|,code,preview,|,forecolor,backcolor",
      	theme_advanced_buttons3 : "insertdate,inserttime,|,spellchecker,advhr,,removeformat,|,sub,sup,|,charmap,emotions,|table",	
      	theme_advanced_toolbar_location : "top",
      	theme_advanced_toolbar_align : "left",
      	theme_advanced_statusbar_location : "bottom", 
        theme_advanced_buttons3_add : "tablecontrols",
      	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 : 240,
      	table_row_limit : 30,
      	table_col_limit : 20, 
        extended_valid_elements : "iframe[src|width|height|name|align]", 
        theme_advanced_styles : "PDF=filelink pdf"
    });
  this.initialized = true;
  } else {
      tinyMCE.activeEditor.show();
      tinyMCE.activeEditor.isNotDirty = true;
  }
  $(this.id + '__save').show('inline');
  $(this.id + '__finish').show('inline');
  $(this.id + '__edit').hide();
}
TextHtmlMCE.prototype.Save = function() {
	LoadingInfo.Show(GetLabel('SavingContent'));
	tinyMCE.activeEditor.save();
	var s = $(this.id).innerHTML.replace(/\.\.\/pages\/images/g, 'images');
	this.content = s;
  BA.Call({parameters:{service: 'Ctrl.textHtmlMCE.save', id: this.cid, content:s}, 
      onComplete: this.OnSaved.bindAsEventListener(this)});
}

TextHtmlMCE.prototype.Finish = function() {
  if (tinyMCE.activeEditor.isDirty() && !confirm('Do you really want to quit without saving?'))
      return;
  tinyMCE.activeEditor.hide();
  $(this.id + '__save').hide();
  $(this.id + '__finish').hide();
  $(this.id + '__edit').show('inline');
  $(this.id).innerHTML = this.content;
}
TextHtmlMCE.prototype.OnSaved = function() {
  tinyMCE.activeEditor.isNotDirty = true;
  LoadingInfo.Hide();
  alert(GetLabel('ContentSaved'));
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
