{% extends "base_title.html" %}

{% block extra_head %}
<link href="{{ STATIC_URL }}grappelli/jquery/ui/css/custom-theme/jquery-ui-1.8.custom.css" rel="stylesheet" type="text/css" media="screen" title="no title" charset="utf-8" />
<script src="{{ STATIC_URL }}grappelli/jquery/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="{{ STATIC_URL }}grappelli/jquery/ui/js/jquery-ui-1.8.15.custom.min.js" type="text/javascript"></script>
<link href="{{ STATIC_URL }}grappelli/css/tools.css" rel="stylesheet" type="text/css" />
<link href="{{ STATIC_URL }}grappelli/css/jquery-ui-grappelli-extensions.css" rel="stylesheet" type="text/css" />
{% endblock %}

{% block realcontent %}
<script type="text/javascript">
var django = {
    "jQuery": jQuery.noConflict(true)
};

(function($) {
    cloneMore = function(selector, type) {
        var newElement = $(selector).clone(true);
        var total = $('#id_' + type + '-TOTAL_FORMS').val();
        newElement.find(':input').each(function() {
            var name = $(this).attr('name').replace('-' + (total-1) + '-','-' + total + '-');
            var id = 'id_' + name;
            $(this).attr({'name': name, 'id': id}).val('').removeAttr('checked');
        });
        newElement.find('label').each(function() {
            var newFor = $(this).attr('for').replace('-' + (total-1) + '-','-' + total + '-');
            $(this).attr('for', newFor);
        });
        total++;
        $('#id_' + type + '-TOTAL_FORMS').val(total);
        $(selector).after(newElement);
    }
    deleteButtonHandler = function(elem) {
        elem.bind("click", function() {
            var deleteInput = $(this).prev().prev(),
                form = $(this).parents(".dynamic-form").first();
            // callback
            // toggle options.predeleteCssClass and toggle checkbox
            if (form.hasClass("has_original")) {
                form.toggleClass("predelete");
                if (deleteInput.attr("checked")) {
                    deleteInput.attr("checked", false);
                } else {
                    deleteInput.attr("checked", true);
                }
            }
            // callback
        });
    }; 
    $(document).ready(function($) {
        deleteButtonHandler($("table#bda_formset tbody.bda_formset_content").find("a.delete-handler"));
        $("table#bda_formset tbody.bda_formset_content").sortable({
            handle: "a.drag-handler",
            items: "tr",
            axis: "y",
            appendTo: 'body',
            forceHelperSize: true,
            placeholder: 'ui-sortable-placeholder',
            forcePlaceholderSize: true,
            containment: 'form#bda_form',
            tolerance: 'pointer',
            start: function(evt, ui) {
                var template = "",
                    len = ui.item.children("td").length;
                for (var i = 0; i < len; i++) {
                    template += "<td style='height:" + (ui.item.outerHeight() + 12 ) + "px' class='placeholder-cell'>&nbsp;</td>"
                }
                template += "";
                ui.placeholder.html(template);
            },
            stop: function(evt, ui) {
                // Toggle div.table twice to remove webkits border-spacing bug
                $("table#bda_formset").toggle().toggle();
            },
        });
        $("#bda_form").bind("submit", function(){
            var sortable_field_name = "priority";
            var i = 1;
            $(".bda_formset_content").find("tr").each(function(){
                var fields = $(this).find("td :input[value]"),
                    select = $(this).find("td select");
                if (select.val() && fields.serialize()) {
                    $(this).find("input[name$='"+sortable_field_name+"']").val(i);
                    i++;
                }
            });
        });
        
    });
})(django.jQuery);
</script>

                <h2>Inscription au tirage au sort du  BDA</h2>
                {% if success %}
                <p class="success">Votre inscription a été mise à jour avec succès !</p>
                {% endif %}
                <form id="bda_form" method="post" action="{% url 'bda-tirage-inscription' %}">
                {% csrf_token %}
                {% include "inscription-formset.html" %}
                <input type="button" class="btn-addmore" value="Ajouter un autre v&oelig;u" id="add_more">
                <script>
                    django.jQuery('#add_more').click(function() {
                        cloneMore('tbody.bda_formset_content tr:last-child', 'choixspectacle_set');
                    });
                </script>
                <input type="submit" class="btn-submit" value="Enregistrer" />
                Prix total actuel : {{ total_price }}€
                <hr />
                <p class="footnotes">
                    <sup>1</sup>: demander deux places pour ce spectacle<br />
                    <sup>2</sup>: abandonner une place si impossible d'en obtenir une seconde pour ce spectacle (si vous avez coché l'option <tt>Deux places</tt> pour ce spectacle)<br />
                    <sup>3</sup>: cette liste de v&oelig;u est ordonnée (du plus important au moins important), pour ajuster la priorité vous pouvez déplacer chaque v&oelig;u<br />
                </p>
                </form>
{% endblock %}
