jQuery(function(jQuery) {
    
    var file_frame,
    awl_image_gallery = {
        ul: '',
        init: function() {
            this.ul = jQuery('.sbox');
            this.ul.sortable({
                placeholder: '',
				revert: false,
            });			
			
            /**
			 * Add Slide Callback Funtion
			 */
            jQuery('.add-new-images').on('click', function(event) {
                event.preventDefault();
                if (file_frame) {
                    file_frame.open();
                    return;
                }
                file_frame = wp.media.frames.file_frame = wp.media({
                    multiple: true
                });

                file_frame.on('select', function() {
                    var images = file_frame.state().get('selection').toJSON();
					images.forEach(myFunction);
					function myFunction(item, index) {
					 awl_image_gallery.get_thumbnail(item['id']);
					}
                });
				
                file_frame.open();
            });
			
			/**
			 * Delete Slide Callback Function
			 */
            this.ul.on('click', '#remove-image', function() {
                if (confirm('Are sure to delete this images?')) {
                    jQuery(this).parent().fadeOut(700, function() {
                        jQuery(this).remove();
                    });
                }
                return false;
            });
			
			/**
			 * Delete All Slides Callback Function
			 */
			jQuery('#remove-all-images').on('click', function() {
                if (confirm('Are sure to delete all images?')) {
                    awl_image_gallery.ul.empty();
                }
                return false;
            });
           
        },
        get_thumbnail: function(id, cb) {
			
			
			
			/* var data = {
				imageId: id,
                action: 'pfg_gallery_js'
                
            }, */
				
		   
		   cb = cb || function() {
            };
           
			jQuery.ajax({
			type: 'POST',
			url: ajaxurl,
			async: false,
			dataType: 'html',
			data: { imageId : id, action : 'pfg_gallery_js' },
			complete: function() { },
				success: function(response) { 
					awl_image_gallery.ul.append(response);
					 cb();
				}
			});
            /* jQuery.post(ajaxurl,  data, function(response) {
                awl_image_gallery.ul.prepend(response);
                cb();
				console.log(data);
				
            }); */
        }
    };
    awl_image_gallery.init();
});