/*
 * XenForo attachment_editor_new.min.js
 * Copyright 2010-2017 XenForo Ltd.
 * Released under the XenForo License Agreement: http://xenforo.com/license-agreement
 */
(function(g,n,o){var m=XenForo.speed.normal,k=XenForo.speed.fast;XenForo.AttachmentUploader=function(b){var d=g(b.data("trigger"));b.closest("form");var a={},c=function(a,c,e){(c=b.data("err-"+c)||e)||(c=b.data("err-unknown"));a?XenForo.alert(c+"<br /><br />"+XenForo.htmlspecialchars(a.name)):XenForo.alert(c)},e=b.data("maxfilesize");b.data("maxuploads");var f=b.data("extensions"),i=b.data("uniquekey"),f=f.replace(/[;*.]/g,"").replace(/,{2,}/g,",").replace(/^,+/,"").replace(/,+$/,"");b.show();var h;
if(n.Flow){var l=!1,j={target:b.data("action"),allowDuplicateUploads:!0,fileParameterName:b.data("postname"),query:function(){var c=g.extend({_xfToken:XenForo._csrfToken,_xfNoRedirect:1,_xfResponseType:l?"json-text":"json"},a);b.find(".HiddenInput").each(function(a,e){var b=g(e);c[b.data("name")]=b.data("value")});return c},simultaneousUploads:1,testChunks:!1,chunkSize:4294967296};h=new Flow(j);h.support||(h=new FustyFlow(j),l=!0);j=g("<span />").insertAfter(d).append(d);h.assignBrowse(j[0],!1,!1,
{accept:"."+f.toLowerCase().replace(/,/g,",.")});l&&(f=d.outerWidth(),f>30&&j.css("width",f));d.on("click BeforeOverlayTrigger",function(a){a.preventDefault()});h.on("fileAdded",function(a){var f=!1;a.id=a.uniqueIdentifier;switch(a.name.substr(a.name.lastIndexOf(".")).toLowerCase()){case ".jpg":case ".jpeg":case ".jpe":case ".png":case ".gif":f=!0}var d=g.Event("AttachmentQueueValidation");d.file=a;d.flow=h;d.isImage=f;b.trigger(d);if(d.isDefaultPrevented())return!1;if(a.size>e&&!f)return c(a,110),
!1;d=g.Event("AttachmentQueued");d.file=a;d.flow=h;d.isImage=f;b.trigger(d)});h.on("filesSubmitted",function(){h.upload()});h.on("fileProgress",function(a){a.id=a.uniqueIdentifier;b.trigger({type:"AttachmentUploadProgress",file:a,bytes:Math.round(a.progress()*a.size),flow:h})});h.on("fileSuccess",function(a,c){try{l&&c.substr(0,1)=="<"&&(c=g(g.parseHTML(c)).text());var e=g.parseJSON(c)}catch(f){console.warn(f);return}a.id=a.uniqueIdentifier;e.error?b.trigger({type:"AttachmentUploadError",file:a,ajaxData:e,
flow:h}):b.trigger({type:"AttachmentUploaded",file:a,ajaxData:e,flow:h})});h.on("fileError",function(a,c){a.id=a.uniqueIdentifier;b.trigger({type:"AttachmentUploadError",file:a,errorCode:0,message:c,ajaxData:{error:[b.data("err-unknown")]},flow:h})})}else console.error("flow.js must be loaded");g(o).bind("AutoInlineUploadComplete",function(a){if(i&&a.ajaxData&&i!==a.ajaxData.key)return!1;if(g(a.target).is("form.AttachmentUploadForm"))return d.overlay()&&d.overlay().close(),b.trigger({type:"AttachmentUploaded",
ajaxData:a.ajaxData}),!1});return{getSwfUploader:function(){return null},getFlowUploader:function(){return h},swfAlert:c,attachmentErrorAlert:c}};XenForo.AttachmentEditor=function(b){this.setVisibility=function(a){var c=b.closest(".ctrlUnit"),e=b.find(".AttachmentInsertAllBlock"),f=b.find(".AttachedFile:not(#AttachedFileTemplate)"),d=f.filter(".AttachedImage");console.log("Attachments changed, total files: %d, images: %d",f.length,d.length);c.length==0&&(c=b);a===!0?f.length?(d.length>1?e.show():
e.hide(),c.show()):c.hide():f.length?(d.length>1?c.is(":hidden")?e.show():e.xfFadeDown(XenForo.speed.fast):c.is(":hidden")?e.hide():e.xfFadeUp(XenForo.speed.fast,!1,XenForo.speed.fast,"swing"),c.xfFadeDown(XenForo.speed.normal)):(e.slideUp(XenForo.speed.fast),c.xfFadeUp(XenForo.speed.normal,!1,!1,"swing"))};this.setVisibility(!0);g("#AttachmentUploader").bind({click:function(){g("textarea.BbCodeWysiwygEditor").each(function(){var a=g(this).data("XenForo.BbCodeWysiwygEditor");a&&a.blurEditor()})},
AttachmentQueued:function(a){var c=a.file,e=c.uniqueIdentifier||c.id;console.info("Queued file %s (%d bytes).",c.name,c.size);var f=g("#AttachedFileTemplate").clone().attr("id",e);f.find(".Filename").text(c.name);f.find(".ProgressCounter").text("0%");f.find(".ProgressGraphic span").css("width","0%");a.isImage&&f.addClass("AttachedImage");f.xfInsert("appendTo",".AttachmentList.New",null,m);f.find(".AttachmentCanceller").css("display","block").click(function(){a.swfUpload?a.swfUpload.cancelUpload(a.file.id):
c.flowObj&&c.cancel();f.xfRemove(null,function(){b.trigger("AttachmentsChanged")},k,"swing")});b.trigger("AttachmentsChanged")},AttachmentUploadProgress:function(a){var c=a.file,a=a.bytes,e=c.uniqueIdentifier||c.id;console.log("Uploaded %d/%d bytes.",a,c.size);var c=Math.min(100,Math.ceil(a*100/c.size)),a=c+"%",e=g("#"+e),b=e.find(".ProgressCounter"),d=e.find(".ProgressGraphic");b.text(a);d.css("width",a);c>=100&&e.find(".AttachmentCanceller").prop("disabled",!0).addClass("disabled");d.width()>b.outerWidth()&&
b.appendTo(d)},AttachmentUploadError:function(a){var c="",b=a.file,d=b.uniqueIdentifier||b.id;g.each(a.ajaxData.error,function(a,b){c+=b+"\n"});XenForo.alert(c+"<br /><br />"+XenForo.htmlspecialchars(b.name));var b=g("#"+d),i=b.closest(".AttachmentEditor");b.xfRemove(null,function(){i.trigger("AttachmentsChanged")},k,"swing");console.warn("AttachmentUploadError: %o",a)},AttachmentUploaded:function(a){if(a.file){var c=a.file,d=g("#"+(c.uniqueIdentifier||c.id)),f=d.find(".AttachmentText"),i=g(a.ajaxData.templateHtml),
h;f.fadeOut(XenForo.speed.fast,function(){i.find(".AttachmentText").xfInsert("insertBefore",f,"fadeIn",XenForo.speed.fast);h=d.find(".Thumbnail");h.html(i.find(".Thumbnail").html());f.xfRemove();d.attr("id","attachment"+a.ajaxData.attachment_id)})}else d=g("#attachment"+a.ajaxData.attachment_id),d.length||(d=g(a.ajaxData.templateHtml).xfInsert("appendTo",b.find(".AttachmentList.New"),null,m));b.trigger("AttachmentsChanged")}});var d=g.context(this,"setVisibility");g("#QuickReply").bind("QuickReplyComplete",
function(){b.find(".AttachmentList.New li:not(#AttachedFileTemplate)").xfRemove().promise().always(d)});b.bind("AttachmentsChanged",d)};XenForo.AttachmentInserter=function(b){b.click(function(d){var a=b.closest(".AttachedFile").find(".Thumbnail a"),c=a.data("attachmentid"),e;e=a.find("img").attr("src");a=a.attr("href");d.preventDefault();b.attr("name")=="thumb"?(d="[ATTACH]"+c+"[/ATTACH] ",e='<img src="'+e+'" class="attachThumb bbCodeImage" alt="attachThumb'+c+'" /> '):(d="[ATTACH=full]"+c+"[/ATTACH] ",
e='<img src="'+a+'" class="attachFull bbCodeImage" alt="attachFull'+c+'" /> ');if(c=XenForo.getEditorInForm(b.closest("form"),":not(.NoAttachment)"))if(c.$editor){c.insertHtml(e);var f=c.$editor.data("xenForoElastic");f&&(setTimeout(function(){f()},250),setTimeout(function(){f()},1E3))}else c.val(c.val()+d)})};XenForo.AttachmentDeleter=function(b){b.css("display","block").click(function(b){var a=g(b.target),c=a.attr("href")||a.data("href"),e=a.closest(".AttachedFile"),b=a.closest(".AttachedFile").find(".Thumbnail a").data("attachmentid");
if(c){e.xfFadeUp(XenForo.speed.normal,null,k,"swing");XenForo.ajax(c,"",function(a){if(XenForo.hasResponseError(a))return e.xfFadeDown(XenForo.speed.normal),!1;var b=e.closest(".AttachmentEditor");e.xfRemove(null,function(){b.trigger("AttachmentsChanged")},k,"swing")});if(b&&(a=XenForo.getEditorInForm(a.closest("form"),":not(.NoAttachment)"))&&a.$editor)a.$editor.find("img[alt=attachFull"+b+"], img[alt=attachThumb"+b+"]").remove(),(b=a.$editor.data("xenForoElastic"))&&b();return!1}console.warn("Unable to locate href for attachment deletion from %o",
a)})};XenForo.AttachmentInsertAll=function(b){b.click(function(){g(".AttachmentInserter[name="+b.attr("name")+"]").each(function(b,a){g(a).trigger("click")})})};XenForo.AttachmentDeleteAll=function(b){b.click(function(){g(".AttachmentDeleter").each(function(b,a){g(a).trigger("click")})})};XenForo.register(".AttachmentUploader","XenForo.AttachmentUploader");XenForo.register(".AttachmentEditor","XenForo.AttachmentEditor");XenForo.register(".AttachmentInserter","XenForo.AttachmentInserter");XenForo.register(".AttachmentDeleter",
"XenForo.AttachmentDeleter");XenForo.register(".AttachmentInsertAll","XenForo.AttachmentInsertAll");XenForo.register(".AttachmentDeleteAll","XenForo.AttachmentDeleteAll")})(jQuery,this,document);
