$(
    function() {
        $(document)
            .on(
                'change', '.gw-image-input', function(e) {
                    var input = this;
                    $.ajax(
                        {
                            url: '/admin/imagemanager/manager/view',
                            type: "POST",
                            data: {
                                'ImageManager_id': input.value
                            },
                            success: function(data) {
                                $(
                                    '#' + $(input)
                                        .data('img')
                                )
                                    .attr('src', data.image);
                                $(
                                    '#' + $(input)
                                        .data('name')
                                )
                                    .html(data.fileName);
                            }
                        }
                    );
                }
            );

        $(document)
            .on(
                'click', '#add-image', function(e) {
                    var hash = Math.floor(Math.random() * 89999) + 10000;
                    var content = '<div class="col-md-2 gw-item"><input id="' + hash + '" class="gw-image-input" name ="images[' + hash + ']" ' + 'data-img="' + hash + '_img" data-name="' + hash + '_name" type ="hidden"> ' + '<div class="image_gall"><img id="' + hash + '_img" alt=""></div>' + '<p id="' + hash + '_name" class="text-info"></p>' + '<button type="button" class="open-modal-imagemanager btn btn-primary" data-aspect-ratio="" data-crop-view-mode="1" ' + 'data-input-id="' + hash + '"><i class="fa fa-folder-open"></i></button><button type="button" class="remove-img btn btn-danger">' + '<i class="fa fa-trash"></i></button></div>';
                    $('.gw-container')
                        .append(content);
                    //                    if (document.querySelectorAll(".gw-container > .gw-item").length % 3 === 0) {
                    //                        $('.gw-container').append('<div class="clearfix"></div>');
                    //                    }
                }
            );
        $(document)
            .on(
                'click', '.remove-img', function(e) {
                    $(this)
                        .parent()
                        .remove();
                }
            )
    }
);