$.fn.tinymce = function(options){
   return this.each(function(){
      tinyMCE.execCommand("mceAddControl", true, this.id);
   });
}

function initMCE(){
   tinyMCE.init({
		mode : "none",
		theme: "advanced", 
		relative_urls : false,
		plugins : "phpimage,spellchecker,inlinepopups,media,contextmenu,fullscreen,paste,autoresize,inlinepopups",
		theme_advanced_disable: "image,advimage",
		theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1 : "fontselect,fontsizeselect,forecolor,backcolor,bold,italic,underline,strikethrough,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,numlist,bullist,outdent,indent,separator,pastetext,pasteword,selectall,pastetext",
		theme_advanced_buttons2 : "removeformat,visualaid,separator,sub,sup,separator,charmap,seperator,undo,redo,separator,anchor,cleanup,spellchecker,separator,hr,link,unlink,separator,phpimage,media,separator,code,pastetext,fullscreen,help",
		theme_advanced_buttons3 : "",
      theme_advanced_resizing : true,
      theme_advanced_resizing_min_height : 340,
      dialog_type : "modal"
	});
}

initMCE();

$.editable.addInputType('mce', {
   element : function(settings, original) {
      var textarea = $('<textarea id="'+$(original).attr("id")+'_mce"/>');
      if (settings.rows) {
         textarea.attr('rows', settings.rows);
      } else {
         textarea.height(settings.height);
      }
      if (settings.cols) {
         textarea.attr('cols', settings.cols);
      } else {
         textarea.width(settings.width);
      }
      $(this).append(textarea);
         return(textarea);
      },
   plugin : function(settings, original) {
      tinyMCE.execCommand("mceAddControl", true, $(original).attr("id")+'_mce');
      },
   submit : function(settings, original) {
      tinyMCE.triggerSave();
     	tinyMCE.execCommand("mceRemoveControl", true, $(original).attr("id")+'_mce');
      },
   reset : function(settings, original) {
      tinyMCE.execCommand("mceRemoveControl", true, $(original).attr("id")+'_mce');
      original.reset();
   }
});

$(function() {
	$(".editable_textarea").editable("/evopages/html-save.php?EvoID=" + EvoID + '&PageID=' + PageID, {
		loadurl : '/evopages/html-load.php?EvoID=' + EvoID + '&PageID=' + PageID, 
	   indicator : '<img src="' + TemplateURL + '/images/indicator.gif">',
	   type : 'mce',
	   submitdata: { _method: "put" },
	   select : true,
	   tooltip : 'Click to edit',
	   submit : 'OK',
	   cancel : 'Cancel',
	   cssclass : "editable",
	   onblur : "ignore"
	});
   $("a.clickme").click(function() {
   	// user clicked on a form based html editor.. use some special coding to make everything work.
		$("#edit_html").editable("/evopages/html-save.php?EvoID=" + EvoID + '&PageID=' + PageID, {
			loadurl : '/evopages/html-load.php?EvoID=' + EvoID + '&PageID=' + PageID, 
		   indicator : '<img src="' + TemplateURL + '/images/indicator.gif">',
		   type : 'mce',
		   submitdata: { _method: "put" },
		   select : true,
		   tooltip : 'Click to edit',
		   submit : 'OK',
		   cancel : 'Cancel',
		   cssclass : "editable",
		   onblur : "ignore",
		   callback:  function(value, settings) {
		   	$("#edit_html").load("/evopages/html-convert.php?EvoID=" + EvoID + '&PageID=' + PageID, { HTML : value});
			}
		});
	   $('#edit_html').click();
	   $('#edit_html').disable();
   });
});


