jQuery(document).ready(function(){
 
    var custom_uploader_hami;
	
 
    jQuery('#sortable').on('click','#upload_image_button_hami',function(e) {
		
        e.preventDefault();
        var id = jQuery(this).next().val();
        //If the uploader object has already been created, reopen the dialog
        /*if (custom_uploader) {
            custom_uploader.open();
            return;
        }*/
        //Extend the wp.media object
        custom_uploader_hami = wp.media.frames.custom_uploader_hami = wp.media({
				title: 'انتخاب آیکون برای منوی اپلیکیشن',
				library: {type: 'image'},
				button: {text: 'Select'},
				multiple: false
            
        });
		
		
        //When a file is selected, grab the URL and set it as the text field's value
        custom_uploader_hami.on('select', function() {
        
			attachment = custom_uploader_hami.state().get('selection').first().toJSON();
            jQuery('#upload_image').val(attachment.url);
			imageUrl = attachment.url;
            jQuery.ajax({
                url: ajaxurl,
                data: {
                    action: "menu_appost_upload_icon",
                    "id" : id,
                    "url" : imageUrl
                },
                success:function(data) {
                    data = data.substring(0,data.length -1);
                    if(data == 1){
                        /*this is code for show icons in menus*/
                            id = jQuery('#select_menu_appost').val();
                            jQuery.ajax({
                                url: ajaxurl,
                                data: {
                                    action: "menu_appost",
                                    "id" : id
                                },
                                success:function(data) {
                                    // This outputs the result of the ajax request
                                    data = data.substring(1,data.length -1);
                                    m = id;
                                    jQuery("#menu_app_hami").val(m) ;
                                    jQuery("#menu_app_hami_planning").val(m) ;
                                    jQuery("#sortable").html(data) ;
                                }
                            }); 
                        /*this is code for show icons in menus*/
                        jQuery("#order_sucess").html("آیکون شما برای این آیتم ذخیره شد.");
                        jQuery("#order_sucess").fadeIn("fast", function() {
                                jQuery('#order_sucess').removeClass('hide');
                                jQuery('#order_sucess').fadeOut(5000);
                        });
                    }
                }
             }); 
			/*jQuery('#set_splash').css('background-image', 'url(' + imageUrl + ')');
			jQuery('#set_splash').css('background-size', '100% 100%');
			jQuery('#set_splash').css('background-repeat', 'no-repeat');
			jQuery('#set_splash').removeClass('hide');*/
			
        });
 
        //Open the uploader dialog
        custom_uploader_hami.open();
 
    });
	
 
 
});
function img_icon_menu_appost(id){
    if( confirm("میخواهید آیکون این منو را پاک کنید ؟")){//begin confirm
    jQuery.ajax({
            url: ajaxurl,
            data: {
                action: "menu_appost_icon_delete",
                "id" : id
            },
            success:function(data) {
                // This outputs the result of the ajax request
                data = data.substring(1,data.length -1);
                if(data == 1){
                    /*this is code for show icons in menus*/
                            id = jQuery('#select_menu_appost').val();
                            jQuery.ajax({
                                url: ajaxurl,
                                data: {
                                    action: "menu_appost",
                                    "id" : id
                                },
                                success:function(data) {
                                    // This outputs the result of the ajax request
                                    data = data.substring(1,data.length -1);
                                    m = id;
                                    jQuery("#menu_app_hami").val(m) ;
                                    jQuery("#menu_app_hami_planning").val(m) ;
                                    jQuery("#sortable").html(data) ;
                                }
                            }); 
                        /*this is code for show icons in menus*/
                    jQuery("#order_sucess").html("آیکون شما حذف شد.");
                    jQuery("#order_sucess").fadeIn("fast", function() {
                            jQuery('#order_sucess').removeClass('hide');
                            jQuery('#order_sucess').fadeOut(5000);
                    });
                    
                }
            }
        }); 
    }//end confirm
}