"use strict";

$(document).ready(function() {

    $(".favorite-checkbox").click(function(e) {
        var chkbox = $(e.target);
        var form = $(e.target).parents('form')[0];
        $.post($(form).attr('action'), $(form).serialize(), function(data) {
            if(chkbox.prop('checked') !== data.renkan.favorite) {
                chkbox.prop('checked', data.renkan.favorite);
            }
        }).fail(function() {
            chkbox.prop('checked', !chkbox.prop('checked'));
        });
        
    });
    
    
    $(".renkan-publish-action").mousedown(function(e) {
        vex.dialog.confirm({
            message: renkanActionMessages.publish,
            callback: function(value) {
                if(value) {
                    e.target.click();
                }
            }
        });
        e.preventDefault();
    });
    
    $(".renkan-unpublish-action").mousedown(function(e) {
        vex.dialog.prompt({
            message: renkanActionMessages.unpublish,
            input: "<textarea name=\"vex\" class=\"vex-dialog-prompt-input\" placeholder=\"message\" rows=\"3\" />",
            callback: function(message) {
                if(message !== false) {
                    $('.action-message',$(e.target).parent()).val(message);
                    e.target.click();
                }
            }
        });
        e.preventDefault();
    });

    $(".renkan-reject-action").mousedown(function(e) {
        vex.dialog.prompt({
            message: renkanActionMessages.reject,
            input: "<textarea name=\"vex\" class=\"vex-dialog-prompt-input\" placeholder=\"message\" rows=\"3\" />",
            callback: function(message) {
                if(message !== false) {
                    $('.action-message',$(e.target).parent()).val(message);
                    e.target.click();
                }
            }
        });
        e.preventDefault();
    });

    
});
