var Mega = { init: false, init: function() { if (this.initialized) return; this.initialized = true; this.mega(); }, mega: function() { this.mega_sortable(); this.mega_del_item(); this.mega_del_all_item(); this.mega_add_single(); this.mega_add_folder(); this.megae_upload_files(); this.mega_click_upload(); }, mega_update: function() { this.mega_maxid(); this.mega_del_item(); AveAdmin.fancy_box(); AveAdmin.tooltip(); }, mega_maxid: function(id, doc) { var maxid = 1; $('#mega_' + doc + '_' + id).children('.mega_sortable').children('.mega_item').each(function() { maxid = Math.max(maxid, parseInt($(this).attr("data-id")) + 1); }); return maxid; }, mega_del_item: function() { $('.mega_item .delete').on('click', function(event) { event.preventDefault(); var id = $(this).attr('data-id'); jConfirm( mega_del_conf, mega_del_head, function(b) { if (b) { $('#mega_image_' + id).remove(); } } ); }); }, mega_del_all_item: function() { $('.mega_del_all').on('click', function(event) { event.preventDefault(); var c_id = $(this).parent().parent().parent('.mega').attr("data-id"); var d_id = $(this).parent().parent().parent('.mega').attr("data-doc"); jConfirm( mega_del_all_c, mega_del_all_h, function(b) { if (b) { $('#mega_' + d_id + '_' + c_id).children('.mega_sortable').children('.mega_item').each(function() { $(this).remove(); }); } } ); }); }, megae_upload_files: function() { $('.mega_upload').on('change', function(event) { var mega_input = $(this); event.preventDefault(); if (mega_input.val() == '') { return false; } var files_input = this.files.length; var max_files = mega_input.attr("data-max-files"); if (files_input > max_files) { $.jGrowl(mega_max_f_t, { header: mega_max_f_h, theme: 'error' }); mega_input.replaceWith(mega_input.val('').clone(true)); return false; } var c_id = $(this).parent('.mega').attr("data-id"); var d_id = $(this).parent('.mega').attr("data-doc"); var r_id = $(this).parent('.mega').attr("data-rubric"); $('#formDoc').ajaxSubmit({ url: 'index.php?do=fields', data: { "field_id": c_id, "rubric_id": r_id, "doc_id": d_id, "field": 'image_mega', "type": 'upload' }, beforeSend: function() { $.alerts._overlay('show'); }, dataType: "JSON", success: function(data) { if (data['respons'] == 'succes') { for (var p = 0, max = data.files.length; p < max; p++) { iid = Mega.mega_maxid(c_id, d_id); var field_value = data['dir'] + data.files[p]; var img_path = data.thumbs[p]; $('#mega_' + d_id + '_' + c_id + ' > .mega_sortable:last').prepend( '