jQuery(document).ready(function(){


// Add Widget Add Form
var html_string =	'<div style="height: 15px;clear: both;" ></div><div class="themeum-sidebar-generator" id="themeum-sidebar-generator"><div class="themeum-widget-title" >Add Widget Area</div><form  method="POST" id="widget-form" ><input style="width: 350px;margin-top: 10px;height: 34px;" type="text" name="sidebar-name" /><input type="hidden" name="action" value="themeum_widget_form"><input type="submit" value="Add" /></form></div>';
jQuery('#widgets-right').append(html_string);


// Get all ID start with themeum_wi
var elements = [];
jQuery("div[id^='themeum_wi']").each(function(){ elements.push(this.id); });


jQuery.each( elements, function( key, value ) {
  	jQuery( '#'+value ).prepend( '<form  method="POST" id="widget-delete" ><input type="hidden" name="sidebar-id" value="'+value+'" /><input type="hidden" name="action-del" value="themeum_widget_form_del"><input type="submit" value="Delete" /></form>' );
});





function media_upload( button_class) {
    var _custom_media = true,
    _orig_send_attachment = wp.media.editor.send.attachment;
    jQuery('body').on('click',button_class, function(e) {
        var button_id ='#'+jQuery(this).attr('id');
        var button_class ='.'+jQuery(this).attr('id');
        var site_url = jQuery(this).data('url');
        /* console.log(button_id); */
        var self = jQuery(button_id);
        var send_attachment_bkp = wp.media.editor.send.attachment;
        var button = jQuery(button_id);
        var id = button.attr('id').replace('_button', '');
        _custom_media = true;
        wp.media.editor.send.attachment = function(props, attachment){
            if ( _custom_media  ) {
                var attachment_url = attachment.url;
                var attachment_url = attachment_url.replace(site_url,'');
                // console.log(attachment_url);
                jQuery('.upload_image_url').val(attachment_url);
                jQuery('.image-view').attr('src',attachment.url).css('display','block');
                jQuery(button_class).val(attachment_url); 
                jQuery('img'+button_class).attr('src',attachment.url);
            } else {
                return _orig_send_attachment.apply( button_id, [props, attachment] );
            }
        }
        wp.media.editor.open(button);

        return false;
    });
}
media_upload('.custom-upload');


    


});