{% load i18n admin_static %}

{% block extrastyle %}
<link rel="stylesheet" type="text/css" href="{% static "css/cms.ckeditor.css" %}" />
{% endblock %}

{% block extrahead %}
<script type="text/javascript" src="{% static "cms/js/plugins/cms.base.js" %}"></script>
<script type="text/javascript" src="{% static "ckeditor/ckeditor.js" %}"></script>
<script type="text/javascript" src="{% static "js/cms.ckeditor.js" %}"></script>
{% endblock %}

<script type="text/javascript">
(function($) {
// CMS.$ will be passed for $
$(document).ready(function () {

	// get the container id
	var container = 'id_{{ name }}';
	var prefixPos = container.indexOf('-__prefix__');
    if (prefixPos != - 1) {
        // in case the textarea is in an inline, we need to perform some replacements
        var name = container.substring(0, prefixPos);
        var replacement = $('#' + name + "-TOTAL_FORMS").val();
        container = container.replace('__prefix__', replacement);
    }

    // initialize ckeditor only if the container exists
	if($('#'+container).length > 0) {
		CMS.CKEditor.init(container, {{ settings|safe }}, {
			'static_url': '{{ STATIC_URL }}'{% if placeholder %},
			'add_plugin_url': '{{ placeholder.get_add_url }}',
            'placeholder_id': {{ placeholder.pk|default:"''" }},
			'plugin_id': {{ plugin_pk }},
			'plugin_language': '{{ plugin_language }}',
			'lang': {
				'toolbar': '{% filter escapejs %}{% trans "CMS Plugins" %}{% endfilter %}',
				'add': '{% filter escapejs %}{% trans "Add CMS Plugin" %}{% endfilter %}',
				'edit': '{% filter escapejs %}{% trans "Edit CMS Plugin" %}{% endfilter %}',
				'aria': '{% filter escapejs %}{% trans "CMS Plugins" %}{% endfilter %}'
			},
			'plugins': [
				{% regroup installed_plugins by module as module_list %}
				{% for module in module_list %}
					{ group: '{% filter escapejs %}{% if module.grouper %}{{ module.grouper|capfirst }}{% else %}{% trans "Standard Plugins" %}{% endif %}{% endfilter %}', items: [
						{% for plugin in module.list %}
							{ 'title': '{% filter escapejs %}{{ plugin.name }}{% endfilter %}', 'type': '{% filter escapejs %}{{ plugin.value }}{% endfilter %}' }{% if not forloop.las %},{% endif %}
						{% endfor %}
					]}{% if not forloop.las %},{% endif %}
				{% endfor %}
			]{% endif %}
		});
    }

});
})(CMS.$);
</script>