var XWiki=function(d){d.AttachmentPicker=Class.create({initialize:function(a,b){this.property=a;this.dialog=b;this.gallery=b.dialogBox;this.property.down("input")||(this.directSave=!0);this.defaultValue="";if(a=this.gallery.down(".gallery_emptyChoice .gallery_attachmenttitle"))this.defaultValue=a.title;this.defaultImageSource="";if(a=this.gallery.down(".gallery_emptyChoice.gallery_image img"))this.defaultImageSource=a.src.replace(/\?.*$/,"");this.filterFormUpload();this.addDeleteListeners();this.addSelectListeners();
this.updateCurrentSelection();this.addCloseListener();var c=this.property.down("img");c&&(c.observe("load",function(){c.up("div").removeClassName("hidden")}),c.observe("error",function(){c.up("div").addClassName("hidden")}))},filterFormUpload:function(){this.gallery.select('.uploadAttachment input[type\x3d"file"]').each(function(a){a.__allowedExtensions=a.title.toLowerCase().replace(/\s*[,|; ]\s*/g," ").strip();a.__allowedExtensions=""!=a.__allowedExtensions?a.__allowedExtensions.split(" "):!1});
this.gallery.select(".uploadAttachment").invoke("observe","submit",function(a){a.stop();var b=a.element(),c=!1;b.select('input[type\x3d"file"]').each(function(e){""==e.value?(new d.widgets.Notification("Please choose a file to upload","error"),c=!0):e.__allowedExtensions&&-1==e.__allowedExtensions.indexOf(this.getFileExtension(e.value))&&(new d.widgets.Notification("Unsupported file format","error"),c=!0)}.bind(this));c||(this.directSave?b.submit():(document.observe("xwiki:document:saved",function(){new d.widgets.Notification("Uploading...",
"inprogress");b.submit()}),document.fire("xwiki:actions:save",{"continue":!0,form:this.property.up("form")})))}.bindAsEventListener(this))},getFileExtension:function(a){a=a.replace("\\","/").split("/");if(0==a.length)return null;a=a[a.length-1].split(".");return a[a.length-1].toLowerCase()},addDeleteListeners:function(){this.gallery.select(".gallery_actions .tool.delete").invoke("observe","click",this.onDelete.bindAsEventListener(this))},addSelectListeners:function(){this.gallery.select(".gallery_actions .tool.select").invoke("observe",
"click",this.onSelect.bindAsEventListener(this))},onDelete:function(a){a.stop();deleteTool=a.element();deleteTool.disabled||new d.widgets.ConfirmedAjaxRequest(deleteTool.readAttribute("href"),{onCreate:function(){deleteTool.disabled=!0},onSuccess:function(){var b=deleteTool.up(".gallery_attachmentbox");if(b){if(b.hasClassName("current")){this.updateAttachment(this.defaultValue,this.defaultImageSource);this.gallery.down(".gallery_emptyChoice")&&this.gallery.down(".gallery_emptyChoice").addClassName("current");
var c=this.gallery.down('input[type\x3d"checkbox"][name\x3d"filename"]');c.disabled=!0;c.up("label").down("strong").update("");this.directSave||(this.property.down("input").value=this.defaultValue)}b.remove()}}.bind(this),onComplete:function(){deleteTool.disabled=!1}},{confirmationText:"Are you sure you want to delete this attachment?",progressMessageText:"Deleting...",successMessageText:"Attachment deleted",failureMessageText:"Failed to delete attachment:"})},onSelect:function(a){a.stop();var b=
a.element(),c=b.up(".gallery_attachmentbox").down(".gallery_attachmenttitle").title,e=b.up(".gallery_attachmentbox.gallery_image");e&&(e=e.down(".gallery_attachmentframe img"))&&(e=e.src.replace(/\?.*$/,""));e||="";this.directSave?b.disabled||new Ajax.Request(a.element().href,{onCreate:function(){b.disabled=!0;b._x_notif=new d.widgets.Notification("Sending request...","inprogress")},onSuccess:function(){b._x_notif.hide();this.updateAttachment(c,e);this.dialog.closeDialog()}.bindAsEventListener(this),
onFailure:function(g){g=g.statusText||"Server not responding";b._x_notif?b._x_notif.replace(new d.widgets.Notification("Failed:"+g,"error")):new d.widgets.Notification(this.interactionParameters.failureMessageText+g,"error")},on0:function(g){g.request.options.onFailure(g)},onComplete:function(){b.disabled=!1}}):(this.updateAttachment(c,e),this.property.down("input").value=c,this.dialog.closeDialog())},updateAttachment:function(a,b){var c=this.property.down("img");if(c){if(c.src=c.src.replace(/^[^\?]+($|\?)/,
b+"$1"),c=c.up("a"))c.href=c.href.replace(/^[^\?]+($|\?)/,b+"$1")}else if(b=this.property.down(".displayed"))if(b.update(a),c=b.up("a"))c.href=c.href.replace(/^[^\?]+($|\?)/,a+"$1")},updateCurrentSelection:function(){if(this.directSave)this.gallery.down(".current .tool.select").addClassName("hidden");else{var a=this.property.down("input").value;a=(a=this.gallery.down('.gallery_attachmenttitle[title\x3d"'+a.replace('"',"\\22 ")+'"]'))?a.up(".gallery_attachmentbox"):this.gallery.down(".gallery_emptyChoice");
a.hasClassName("current")||(this.gallery.select(".gallery_attachmentbox.current .tool.select").invoke("removeClassName","hidden"),this.gallery.select(".gallery_attachmentbox.current").invoke("removeClassName","current"),a.addClassName("current"));a.down(".tool.select").addClassName("hidden")}},addCloseListener:function(){var a=$("attachment-picker-close");a&&a.observe("click",function(b){b.stop();this.dialog.closeDialog()}.bindAsEventListener(this))}});var h=(new Element("div",{"class":"imgcenter"})).update('\x3cimg src\x3d"/xwiki/resources/icons/xwiki/ajax-loader-large.gif?cache-version\x3d1666345755000"/\x3e'),
f;document.observe("xwiki:dom:loaded",function(){$$(".attachment-picker-start").invoke("observe","click",function(a){a.stop();var b=a.element();a=b.href;0>a.indexOf("?")&&(a+="?");a+="\x26xpage\x3dplain";f=new d.widgets.ModalPopup(h,{},{verticalPosition:"top",removeOnClose:!0,title:"Attachments"});f.shortcuts.close.keys=[];f.showDialog();f.dialog.down().setStyle({position:"fixed"});f.dialog.setStyle({top:document.viewport.getScrollOffsets().top+"px",position:"absolute"});f.dialogBox.setStyle({overflow:"hidden",
width:"80%",margin:"0 10%"});new Ajax.Updater(h.up(),a,{onComplete:function(){window.gallery_lb&&window.gallery_lb.updateImageList();if("function"==typeof d.fixLinksTargetAttribute){var c=d.contextaction;d.contextaction="view";d.fixLinksTargetAttribute(f.dialog);d.contextaction=c}new d.AttachmentPicker(b.up(".attachment-picker"),f)}})})});return d}(XWiki||{});
XWiki.widgets.ModalPopup.prototype.showDialog=function(d){d&&Event.stop(d);this.active||(this.active=!0,this.dialog||this.createDialog(),this.attachKeyListeners(),this.dialog.show())};XWiki.widgets.ModalPopup.prototype.closeDialog=function(d){d&&Event.stop(d);this.options.onClose.call(this);this.dialog.hide();this.options.removeOnClose&&this.dialog.remove();this.detachKeyListeners();this.active=!1};