$(document).ready(function() {
    $('#replie_form').submit(function() {
        var options = {
            success: recall.success,
            dataType: 'json'
        }
        $(this).ajaxSubmit(options);
        return false;
    }).append('<input type="hidden" name="ajax_flag" value="1"/>');
})

recall = {
    roles: {1: 'Товарищ', 2: 'Чувиха', 3: 'Гражданка', 4: 'Чувак'},
    shown : false,
    
    add: function() {
        $('#abs_back, #abs_form').fadeIn();
    },
    
    cancel: function() {
        $('#replie_form').clearForm();
        $('$role_1').attr('checked', true);
        $('.error, .success, .roles').hide();
        $('#abs_form, #abs_back').fadeOut();
    },
    
    success: function(res) {
        $('.error, .success').hide();
        if (res.errors) {
            $('#recall_error').show();
            $.each(res.errors, function(i, n) {
                $('#error_'+i).show();
            });
            document.location.href = '#error';
        }
        if (res.success) {
            $('#recall_success').show();
            $('#replie_form').clearForm();
            $('.roles').hide();
            $('#role_1').attr('checked', true);
            document.location.href = '/replies/';
        }
    },
    
    check_role: function() {
        if (recall.shown) {
            $('div.roles').hide();
            recall.shown = false;
        } else {
            $('div.roles').show();
            recall.shown = true;
        }
    },
    
    set_role: function(n) {
        $('input[name=role]').val(n);
        $('#role_link').html(recall.roles[n]);
        recall.check_role();
    },
    
    self_role: function() {
        $('#role_l_c').hide();
        $('#self_role_p').show().next().show();
    },
    
    select_role: function() {
        $('#role_l_c').show();
        $('#self_role_p').hide().next().hide();
    }
}

