{% extends "admin/page_change_form.html" %}
{% load i18n admin_modify adminmedia cms_tags cms_admin %}
{% block js_import %}
{% endblock %}
{% block extrahead %}
{{ block.super }}
{% if not add %}
	<script type="text/javascript" src="{{ CMS_MEDIA_URL }}js/change_form.js"></script>
{% endif %}
<script type="text/javascript" src="{% admin_media_prefix %}js/urlify.js"></script>

{% if add %}
	<script type="text/javascript">
	$(document).ready(function(){
		$("#id_title").keyup(function() {
	        var e = $("#id_slug")[0];
	        if (!e._changed) {
	            e.value = URLify(this.value, 64);
	        }
	    });
	});
	</script>
{% endif %}


{% endblock %}

{% block content_title %}
	{% if moderation_delete_request %}
	<h1 class="moderation-approve-deletion">{% trans "Approve page deletion" %}</h1>

	{% else %}

	<h1>{{ title }}
	{% if CMS_MODERATOR %}
		{% if moderation_required %}<span class="moderation-requires-approvement">{% blocktrans %}(requires approvement at {{ moderation_level }} level){% endblocktrans %}</span>
		{% else %}<span>{% trans '(you can perform actions on this page directly)' %}</span>
		{% endif %}
	{% endif %}
	</h1>
	{% endif %}
	
{% endblock %}

{% block content %}<div id="content-main">

{% block object-tools %}
{% if change %}{% if not is_popup %}
  <ul class="object-tools">
	{% if moderation_delete_request %}<li><a href="remove-delete-state/" class="approvelink">{% trans "Remove delete request" %}</a></li>{% endif %}  	
	{% if moderator_should_approve %}
	<li><a href="approve/" class="approvelink">{% if moderation_delete_request %}{% trans "Approve delete" %}{% else %}{% trans "Approve" %} {% trans "draft" %}{% endif %}</a></li>
	<li><a href="{% if cl.current_site %}{% ifnotequal cl.current_site site %}http://{{ cl.current_site.domain }}{% endifnotequal %}{% endif %}{{ page.get_absolute_url }}?preview&draft=1" class="previewdraftlink">{% trans "Preview" %} {% trans "draft" %}</a></li>
	{% endif %}
	
	<li><a href="history/" class="historylink">{% trans "History" %}</a></li>
  	{% if has_absolute_url %}<li><a href="{% if cl.current_site %}{% ifnotequal cl.current_site site %}http://{{ cl.current_site.domain }}{% endifnotequal %}{% endif %}{{ page|preview_link:language }}?preview" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif%}
  </ul>
{% endif %}{% endif %}
{% endblock %}


<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="?language={{ language }}{%if request.GET.target %}&amp;target={{ request.GET.target }}{% endif %}&amp;{%if request.GET.target %}position={{ request.GET.position }}{% endif %}" method="post" id="page_form">{% block form_top %}{% endblock %}
{{ adminForm.fields.parent }}

<input type="hidden" name="language" value="{{ language }}" />

{% if show_language_tabs %}
<div id="page_form_lang_tabs">
    {% for lang_code, lang_name in traduction_language %}
        <input type="button" onclick="trigger_lang_button(this,'./?language={{lang_code}}');"
            class="language_button {% ifequal lang_code language %}selected{% endifequal %}"
            id="debutton" name="{{lang_code}}" value="{{lang_name}}" /> 
    {% endfor %}
</div>
{% endif %}


<div id="lang_tab_content">
{% if show_language_tabs %}
<h2 class="header"></h2>
{% endif %}
{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
{% if save_on_top %}{% submit_row %}{% endif %}
{% if errors %}
    <p class="errornote">
    {% blocktrans count errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
    </p>
    <ul class="errorlist">{% for error in adminform.form.non_field_errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}

{% for fieldset in adminform %}
  {% include "admin/cms/page/includes/fieldset.html" %}
{% endfor %}


{% for inline_admin_formset in inline_admin_formsets %}
    {% include inline_admin_formset.opts.template %}
{% endfor %}


{% if not add %}
	{% if CMS_PERMISSION and has_change_permissions_permission %}
		<div class="inline-group">
			<div class="tabular inline-related">
				<fieldset id="inherited_permissions" class="module aligned collapse">
				  <h2>{% trans 'All permissions' %}</h2>
				  <div class="loading">{% trans 'Loading...' %}</div>
				  <div class="load">./permissions/</div>
				</fieldset>
			</div>
		</div>
	{% endif %}

	{% if CMS_MODERATOR and has_moderate_permission %}
		<div class="inline-group">
			<div class="tabular inline-related">
				<fieldset id="inherited_permissions" class="module aligned collapse">
				  <h2 class="load_remote">{% trans 'Page states' %}</h2>
				  <div class="loading">{% trans 'Loading...' %}</div>
				  <div class="load">./moderation-states/</div>
				</fieldset>
			</div>
		</div>
	{% endif %}

	{% if moderation_delete_request %}<script type="text/javascript">
		$(function(){
			// disable all fields
			function lockControls(){
				$('input,select,textarea').attr("disabled", "disabled");
				$('a[id^=add_],span[class^=add-plugin],a[class^=selector-],p[class^=selector-]').remove();
			}
			$('fieldset[class=collapsed]').remove();
			lockControls();
			setTimeout(lockControls,200);
		});
	</script>{% endif %}

	{% if CMS_MODERATOR and moderation_required %}
		<div id="dialog" title="Approve dialog">
			<p>{% blocktrans %}This page must be moderated at level {{ moderation_level }}, post a message for moderator.{% endblocktrans %}</p>
			<div class="row"><input type="text" name="df_moderator_message" id="id_df_moderator_message" class="wide" value=""/></div>
			<div class="row"><input type="checkbox" name="df_moderator_state" id="id_df_moderator_state" value="1"/><label for="id_df_moderator_reguest">{% trans 'Request approvemet' %}</label></div>
		</div>

		<script type="text/javascript">
			$(function(){
				// override standard uncolapse_all javascript function, we need to call it
				// only if there is submit, but submit might be cancelled over dialog.
				var old_uncollapse_all = CollapsedFieldsets.uncollapse_all;
				CollapsedFieldsets.uncollapse_all = function() {}

				var submitActor;
				var forceSubmit = false;
				var seen = false;

				// change submit states, open dialog
				$('#page_form').submit(function(event){
					return showDialog(event);
				});

				$('#page_form input[name=_continue]').click(function(event){
					return showDialog(event, this);
				});

				function showDialog(event, actor) {
					if (forceSubmit) return true; // escape sequence

					if (!seen) {
						// show previously posted message if there were an error
						$('#id_df_moderator_message').val($('#id_moderator_message').val());
					}
					seen = true
					event.preventDefault();
					submitActor = actor;
					$('#dialog').dialog('open');
					return false;
				}

				function dialogSave(){
					// assign values from dialog form to real form
					$('#id_moderator_message').val($('#id_df_moderator_message').val());
					$('#id_moderator_state').val(
						$('#id_df_moderator_state').is(':checked') ? $('#id_df_moderator_state').val() : 0 
					);

					old_uncollapse_all(); // uncolapse all fields django way
					$('#dialog').dialog('close');
					forceSubmit = true;
					$(submitActor || '#page_form input[name=_save]').click();
					return false;
				}

				$('#dialog').dialog({
					bgiframe: true,
					autoOpen: false,
					height: 200,
					width: 400,
					modal: true,
					buttons: {
						Cancel: function() {
							$(this).dialog('close');
						},
						Save: function() {
							dialogSave();
						}

					},
					open: function(){
						var val = $('#id_moderator_state').val();
						$('#id_df_moderator_state').attr('checked', val >= 1 ? 'checked': '');
						$('#id_df_moderator_message')
							.focus()
							.keydown(function(event) { if (event.keyCode == 13) dialogSave()});

						// add default button class to last rendered button (Save)
						$(this.uiDialogButtonPane).find('div.ui-dialog-buttonpane button:last').addClass('default');
					},
					close: function(){
						forceSubmit = false;
					}
				});
			});
		</script>
	{% endif %}

{% endif %}

{% block after_related_objects %}{% endblock %}

{% if add %}
	<div class="submit-row"{% if is_popup %} style="overflow: auto;"{% endif %}>
		<input type="submit" name="_save" class="default" value="{% trans 'Save' %}" {{ onclick_attrib }}/>
		<input type="submit" name="_continue" value="{% trans 'Save and continue editing' %}" {{ onclick_attrib }}/>
	</div>
{% else %}
	{% if not moderation_delete_request %}{% submit_row %}{% endif %}
{% endif %}

{% if add %}
   <script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
{% endif %}

{# JavaScript for prepopulated fields #}
{% prepopulated_fields_js %}

</div>
</form></div>
{% endblock %}
