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

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


    function initMCE(){
        tinyMCE.init({mode : "none",
					theme : "advanced",
					content_css : '/public/stylesheets/master.css',
					media_strict : false,
					plugins :"safari,pagebreak,style,table,emotions,inlinepopups,media,paste,directionality,noneditable,searchreplace,visualchars,nonbreaking,template,preview,htmlcharcount" ,
					//htmlcharcount_maxchars: 500,
                                        theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "left",
					theme_advanced_statusbar_location : "bottom",
					theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontsizeselect,bullist,numlist,|,preview,blockquote,|,undo,redo,|,replace",
					theme_advanced_buttons2: "forecolor,backcolor,|,pastetext,pasteword,|,tablecontrols,|,link,unlink,code,image,media,emotions",
					theme_advanced_disable : "hr,removeformat,charmap,cleanup,visualaid,sub,sup,cite,abbr,indent,outdent,acronym,del,ins,attribs,help",
					// theme_advanced_buttons4 : "tablecontrols",
					theme_advanced_resizing : true,
          theme_advanced_path : false,
					extended_valid_elements : "object[width|height|classid|codebase],param[name|value],embed[src|type|width|height|flashvars|wmode]"
					});
			  }

    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();
        }
    });