{% block tinymce_script %}
	{% if TINYMCE_JS_URL %}
		<script type="text/javascript" src="{{ TINYMCE_JS_URL }}"></script>
	{% endif %}
{% endblock %}

{% block tinymce_init %}
	<script type="text/javascript">
		{% block functions %}{% endblock %}
    {% if TINYMCE_DOMAIN %}
      document.domain = '{{ TINYMCE_DOMAIN }}';
    {% endif %}

    (function($){
        var tinymce_added = {};
        var tinymce_options = {
{% block settings %}
            height: '300',
            {% if TINYMCE_CONTENT_CSS_URL %}content_css: "{{ TINYMCE_CONTENT_CSS_URL }}",{% endif %}
            {% if TINYMCE_LINK_LIST_URL %}link_list: "{{ TINYMCE_LINK_LIST_URL }}",{% endif %}
            plugins: "{% block plugins %}fullscreen paste link{% endblock %}",
            paste_auto_cleanup_on_paste: true,
            relative_urls: false,
            invalid_elements: 'script',
            statusbar: false,
            menubar : false
{% endblock %}
        };

        function feincms_richtext_remove_tinymce(field) {
            var id = field ? field.id : this.id;
            if(tinymce_added[id]) {
                tinyMCE.execCommand('mceRemoveEditor', false, id)
                tinymce_added[id] = false;
            }
        }

        function feincms_richtext_add_tinymce(field) {
            var id = field ? field.id : this.id;
            if(!tinymce_added[id]) {
                tinyMCE.execCommand('mceAddEditor', false, id)
                tinymce_added[id] = true;
                }
        }

        var richtext_init_fn = function(){
            tinyMCE.init(tinymce_options);
            $('{% block selectors %}.order-machine textarea.item-richtext{% endblock %}').each(function(){
                feincms_richtext_add_tinymce(this);
            });
        }

			{% block enable %}
        contentblock_init_handlers.push(richtext_init_fn);
        contentblock_move_handlers.poorify.push(function(item) {
            try {
                tinyMCE.triggerSave();
            } catch (error) {
                alert("tinymce failed: " + error);
            }
            item.find('textarea.item-richtext').each(feincms_richtext_remove_tinymce);
        });
        contentblock_move_handlers.richify.push(function(item) {
            item.find('textarea.item-richtext').each(feincms_richtext_add_tinymce);
        });
			{% endblock %}
    })(feincms.jQuery);
	</script>
{% endblock %}
