<?php
$url_mur = v('url_mur');
$url_messages = v('url_messages');
?>
<div id="tabs-mur" class="ui-tabs-hide tab-pane">
    <div id="murs">
        <?php
            if ( ZAuth::isAuth(1) ){
                echo include_template('mur_form');
            }
        ?>
        <h2>Messages du mur</h2>
        <div id="murs-messages"></div>
    </div>
</div>

<script type="text/javascript">
    $(function() {

        $.ajax({
            type: 'GET',
            url: '<?php echo $url_messages; ?>',
            success: function(data) {
                $("#murs-messages").html(data);
            }
        });

        $(".messages-suivants").live('click', function() {
            $(this).remove();
            $.ajax({
                type: 'GET',
                url: $(this).attr('href'),
                success: function(data) {
                    $("#murs-messages").append(data);
                }
            });
            return false;
        });

        $("#form_mur").live('submit', function() {
            $('input[type=submit]', this).attr('disabled', 'disabled');
            $.ajax({
                type: 'POST',
                url: $(this).attr("action"),
                data: $(this).serialize(),
                success: function(data) {
                    $("#form_mur textarea").val('');

                    $.ajax({
                        type: 'GET',
                        url: '<?php echo $url_messages; ?>',
                        success: function(data) {
                            $("#murs-messages").html(data);
                            $('#form_mur input[type=submit]').removeAttr('disabled', 'disabled');
                        }
                    });

                },
                error: function(xhr) {
                    console.log(xhr);
                    alert(xhr.responseText);
                }
            });
            return false;
        });

        $(".com-delete").live('click',function(){
            var elt = $(this).parent().parent();
            $.ajax({
                type: 'GET',
                url: $(this).attr('href'),
                success: function(data) {
                    elt.remove();
                }
            });
        });
    });
</script>