(function($){

    var $document = $(document);

    $document.off('initUserClipartsUploader');
    $document.on('initUserClipartsUploader', function(event, files){
        event.stopPropagation();
        event.preventDefault();
        var data = new FormData();
        $.each(files, function(key, value)
        {
            data.append(key, value);
        });

        $.ajax({
            url: '/modules/myswissstyle/ajax.php?subject=Builder&action=add_user_clipart',
            type: 'POST',
            data: data,
            cache: false,
            dataType: 'json',
            processData: false,
            contentType: false,
            success: function(data) {
                if (data.status == 'success') {
                    data = $.parseJSON(data.message);
                    if (data.message != 'false') {
                        $.coreUtilites.ajaxRequest(
                            {
                                action: 'set_selected_category',
                                category_id: 'my_images'
                            },
                            function() {
                                $document.trigger('builder.category.init');
                            }
                        );
                    }
                } else {
                    console.log('Error: file no uploaded');
                }
            }
        });
    });

    $document.off('initDesignUploader');
    $document.on('initDesignUploader', function() {
        $document.off('click','.design_upload_user_clipart');
        $document.on('click','.design_upload_user_clipart', function() {
            $("#image").click();
            return false;
        });
    });

    $document.off('change', '#image');
    $document.on('change', '#image', function(element) {
        var files = element.target.files;

        $document.trigger('initUserClipartsUploader',[files]);
        return false;
    });

})(SWjQuery);