/* Minified: jquery.selectAttachmentsAdvanced.js, jquery.selectAttachmentsBasic.js, widgets.ObjectAttachments.js */
(function($){var public_methods={init:function(options){return this.each(function(){var wrapper=$(this);wrapper.addClass("basic_upload");this.sab_data={settings:jQuery.extend({},settings,options),items:{}};this.sab_data.form=wrapper.parents("form:first");this.sab_data.hidden_wrapper=wrapper.find("div.upload_button");this.sab_data.attachments_table=wrapper.find("table.select_attachments_list");this.sab_data.control=$('<input type="file" name="attachment">').appendTo($('<div class="uploading_control"></div>').appendTo(this.sab_data.hidden_wrapper)).css({opacity:0});this.sab_data.button=this.sab_data.hidden_wrapper.find("a.link_button").click(function(){wrapper.get(0).sab_data.control.click()});this.sab_data.control.change(function(){upload_file.apply(wrapper.get(0),[$(this)])});init_deleting_attachments.apply(this);add_initial_attachments.apply(this)})},reset:function(){return this.each(function(){reset.apply(this)})}};var upload_file=function(control){var wrapper=$(this);var parent_form=wrapper.parents("form:first");var filename=(""+control.val()).replace(/^.*[\\\/]/,"");var row=insert_attachment_row.apply(this,[filename,null]);parent_form.ajaxSubmit({url:this.sab_data.settings.upload_url,data:{submitted:"submitted"},dataType:"json",success:function(response){finalize_attachment_row.apply(wrapper.get(0),[row,response.id])},error:function(response){finalize_attachment_row.apply(wrapper.get(0),[row]);App.Wireframe.Events.trigger("async_operation_error",[App.Wireframe.Utils.responseToErrorMessage(response),response])}});control.val("")};var insert_attachment_row=function(attachment_filename,attachment_id){var extension;var extension_icon;var dot_index=attachment_filename.lastIndexOf(".");if(dot_index>=0){extension=attachment_filename.split(".").pop().toLowerCase();extension_icon=App.Wireframe.Utils.imageUrl("/file-types/16x16/"+extension+".png","environment")}else{extension_icon=App.Wireframe.Utils.imageUrl("/file-types/16x16/default.png","environment")}var current_icon=extension_icon;if(!attachment_id){current_icon=App.Wireframe.Utils.indicatorUrl()}var row=$('<tr real_attachment_id="'+(attachment_id?attachment_id:"")+'" real_icon="'+extension_icon+'" class="'+(attachment_id?attachment_id:"uploading")+'"><td class="fileicon"><img src="'+current_icon+'"></td><td class="filename"><div title="'+App.clean(attachment_filename)+'">'+App.excerpt(App.clean(attachment_filename),20)+'</div><input type="hidden" name="'+this.sab_data.settings.name+'" value="'+(attachment_id?attachment_id:"")+'"></td><td class="delete_attachment"><a href="#">Remove</a></td></tr>').appendTo(this.sab_data.attachments_table);if(!attachment_id){this.sab_data.form.addClass("uploading")}return row};var finalize_attachment_row=function(row,attachment_id){if(attachment_id){row.find("td.fileicon img").attr("src",row.attr("real_icon"));row.find('td.filename input[type="hidden"]').attr("name",this.sab_data.settings.pending_field_name).attr("value",attachment_id);row.removeClass("uploading")}else{row.remove()}if(!this.sab_data.attachments_table.find("tr.uploading").length){this.sab_data.form.removeClass("uploading")}};var init_deleting_attachments=function(){var wrapper=$(this);this.sab_data.attachments_table.delegate("td.delete_attachment a","click",function(){var anchor=$(this);var row=anchor.parents("tr:first");var attachment_id=row.find('td.filename input[type="hidden"]').val();if(attachment_id){wrapper.append('<input type="hidden" name="'+wrapper.get(0).sab_data.settings.delete_name+'" value="'+attachment_id+'" class="attachment_pending_deletion" />')}row.remove();if(!wrapper.get(0).sab_data.attachments_table.find("tr.uploading").length){wrapper.get(0).sab_data.form.removeClass("uploading")}})};var add_initial_attachments=function(){var wrapper=$(this);if(!this.sab_data.settings.existing_attachments){return false}$.each(this.sab_data.settings.existing_attachments,function(index,attachment){insert_attachment_row.apply(wrapper.get(0),[attachment.filename,attachment.id])})};var reset=function(){this.sab_data.attachments_table.empty();$(this).find(".attachment_pending_deletion").remove()};var plugin_name="selectAttachmentsBasic";var settings={};$.fn[plugin_name]=function(method){if(public_methods[method]){return public_methods[method].apply(this,Array.prototype.slice.call(arguments,1))}else{if(typeof method==="object"||!method){return public_methods.init.apply(this,arguments)}else{$.error("Method "+method+" does not exist in jQuery."+plugin_name)}}}})(jQuery);
App.widgets.ObjectAttachments=function(){var init_attachments=function(attachments_table){var attachments=attachments_table.find("li.attachment");attachments.each(function(){init_attachment($(this),attachments,attachments_table)});align_attachments(attachments_table)};var align_attachments=function(attachments_table){return false;var previous_attachment_top=0;var counter=-1;var attachment_rows=new Array();var attachments=attachments_table.find("li.attachment");attachments.each(function(){var attachment=$(this);if(attachment.offset().top!=previous_attachment_top){counter++;attachment_rows[counter]={};attachment_rows[counter].max_height=0;attachment_rows[counter].attachments=new Array()}previous_attachment_top=attachment.offset().top;attachment_rows[counter].max_height=Math.max(attachment_rows[counter].max_height,attachment.find("img.image:first").outerHeight());attachment_rows[counter].attachments.push(attachment)});if(attachment_rows.length){for(var x=0;x<attachment_rows.length;x++){if(attachment_rows[x].attachments.length){for(var y=0;y<attachment_rows[x].attachments.length;y++){var image=attachment_rows[x].attachments[y].find("img.image:first");image.css("marginTop",attachment_rows[x].max_height-image.outerHeight())}}}}};var init_attachment=function(attachment,attachments,attachments_table){var attachment_options=attachment.find(".attachment_options:first");attachment.hover(function(){attachment.addClass("hover")},function(){attachment.removeClass("hover")});attachment.find("img.image, a.shortened_filename").click(function(event){var url=$(this).parents("li.attachment").find("a.download").attr("href");window.location=url;return false});attachment.find("a.preview").click(function(event){var attachment=$(this).parents("li.attachment");var wrapper_id=attachments_table.parent().attr("id");App.widgets.GalleryPopup.init(wrapper_id,attachment.attr("attachment_order"),null,function(){App.widgets.ObjectAttachments.refresh(wrapper_id)});return false});attachment.find("a.delete").click(function(event){event.stopPropagation();if(!confirm(App.lang("Are you sure that you want to permanently remove this attachment? There is no Undo!"))){return false}link=$(this);attachment.addClass("deleting");$.ajax({url:App.extendUrl(link.attr("href"),{async:1}),type:"POST",data:{submitted:"submitted"},success:function(){attachment.remove();attachment.removeClass("deleting")},error:function(){attachment.removeClass("deleting")}});return false})};return{init:function(wrapper_id){var attachments_table=$("#"+wrapper_id).find("ul.attachments_table");init_attachments(attachments_table)},refresh:function(wrapper_id){var wrapper=$("#"+wrapper_id);var reference=this;wrapper.block();var refresh_url=App.extendUrl(wrapper.attr("manage_url"),{async:1,list_only:1});$.ajax({url:refresh_url,success:function(response){wrapper.empty();wrapper.html(response);reference.init(wrapper_id)},error:function(response,response_text){}})}}}();
