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