jQuery(document).ready(function(){
    $("#dialogo").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 460,
        height: 400,
        modal: true,
        buttons: {
            'Enviar': function() {
                var destino = $("#agregarComentario").attr('action');
                jQuery.ajax({
                    url: destino,
                    type: "POST",
                    data: jQuery("#agregarComentario").serialize(),
                    cache: false,
                    success: function(html){
                        jQuery("#dialogo").html(html);
                    },
                    beforeSend: function(){
                        jQuery("#cargador").show();
                    },
                    complete: function(){
                        jQuery("#cargador").hide();
                    }
                });
            },
            'Cancelar': function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            spag(destinoDefault);
        }
    });
    
    $('#btn-agregar').click(function(e) {
        $("#dialogo").dialog('open');
        e.preventDefault();
    });
    
});

function spag(destino) {
    jQuery.ajax({
        url: destino,
        type: "GET",
        cache: false,
        success: function(html){
            jQuery("#comentarios").html(html);
        },
        beforeSend: function(){
            jQuery("#cargador").show();
        },
        complete: function(){
            jQuery("#cargador").hide();
        }
    });
    return false;
}
