/*
 * XenForo attachment_editor.min.js
 * Copyright 2010-2017 XenForo Ltd.
 * Released under the XenForo License Agreement: http://xenforo.com/license-agreement
 */
(function(f,m,k){var i=XenForo.speed.normal,g=XenForo.speed.fast,l=!1;XenForo.AttachmentUploader=function(e){l||(l=!0,f.browser.mozilla&&typeof SWFUpload=="function"&&f(function(){var a=XenForo.isRTL()?"9999em":"-9999em",b=f('<object width="100" height="100" type="application/x-shockwave-flash" style="visibility: hidden; position: absolute; top: 0; left: '+a+'" />').appendTo("body");setTimeout(function(){b.remove()},250)}));var d=f(e.data("trigger"));e.closest("form");var a=f(e.data("placeholder")),
b={},c=null,j=null,n=e.data("maxfilesize");e.data("maxuploads");var h=e.data("extensions"),g=e.data("uniquekey"),h=h.replace(/[;*.]/g,"").replace(/,{2,}/g,",").replace(/^,+/,"").replace(/,+$/,"");e.show();var i=XenForo.canonicalizeUrl(e.data("flashurl")||"js/swfupload/Flash/swfupload.swf");console.info("flash url: %s",i);typeof SWFUpload=="function"&&!m.navigator.userAgent.match(/Android|iOS|iPhone|iPad|Mobile Safari/i)&&(c=new SWFUpload({upload_url:e.data("action"),file_post_name:e.data("postname"),
file_types:"*."+h.toLowerCase().replace(/,/g,";*."),post_params:f.extend({_xfToken:XenForo._csrfToken,_xfNoRedirect:1,_xfResponseType:"json"},b),button_placeholder_id:a.attr("id"),button_width:1,button_height:1,button_window_mode:SWFUpload.WINDOW_MODE.TRANSPARENT,button_cursor:SWFUpload.CURSOR.HAND,flash_url:i,prevent_swf_caching:!1,swfupload_loaded_handler:function(){this.setButtonDimensions(d.outerWidth(),d.outerHeight());f(this.movieElement).css("top",d.position().top);e.find(".HiddenInput").each(function(a,
b){c.addPostParam(f(b).data("name"),f(b).data("value"))});f(k).bind("CSRFRefresh",function(a){a.ajaxData&&(c.addPostParam("_xfToken",a.ajaxData.csrfToken),c.addPostParam("_xfSessionId",a.ajaxData.sessionId))})},file_dialog_complete_handler:function(){try{this.getStats().files_queued>0&&this.startUpload(this.getFile(0).ID)}catch(a){this.debug(a)}},file_queued_handler:function(a){var b;switch(a.name.substr(a.name.lastIndexOf(".")).toLowerCase()){case ".jpg":case ".jpeg":case ".jpe":case ".png":case ".gif":b=
!0;break;default:b=!1}var c=f.Event("AttachmentQueueValidation");c.file=a;c.swfUpload=this;c.isImage=b;e.trigger(c);if(!c.isDefaultPrevented())a.size>n&&!b?(this.cancelUpload(a.id,!1),typeof this.settings.file_queue_error_handler=="function"&&this.settings.file_queue_error_handler.call(this,a,SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT,"The uploaded file is too large.")):(c=f.Event("AttachmentQueued"),c.file=a,c.swfUpload=this,c.isImage=b,e.trigger(c))},file_queue_error_handler:function(a,b,c){var d=
f.Event("AttachmentQueueError");d.file=a;d.errorCode=b;d.message=c;d.swfUpload=this;e.trigger(d);d.isDefaultPrevented()||j(a,b,c)},upload_start_handler:function(a){console.log("Uploading %s",a.name)},upload_progress_handler:function(a,b){e.trigger({type:"AttachmentUploadProgress",file:a,bytes:b,swfUpload:this})},upload_success_handler:function(a,b){try{var c=f.parseJSON(b)}catch(j){console.warn(j);return}c.error?e.trigger({type:"AttachmentUploadError",file:a,ajaxData:c,swfUpload:this}):e.trigger({type:"AttachmentUploaded",
file:a,ajaxData:c,swfUpload:this})},upload_error_handler:function(a,b,c){b!=SWFUpload.UPLOAD_ERROR.FILE_CANCELLED&&(console.warn("Upload failed: %o",arguments),e.trigger({type:"AttachmentUploadError",file:a,errorCode:b,message:c,ajaxData:{error:[e.data("err-unknown")]},swfUpload:this}))},upload_complete_handler:function(){try{this.getStats().files_queued>0?this.startUpload(this.getFile(0).ID):console.info("All files uploaded.")}catch(a){this.debug(a)}}}),j=function(a,b,c){b=e.data("err"+b)||c;a?XenForo.alert(b+
"<br /><br />"+XenForo.htmlspecialchars(a.name)):XenForo.alert(b)});f(k).bind("AutoInlineUploadComplete",function(a){if(g&&a.ajaxData&&g!==a.ajaxData.key)return!1;if(f(a.target).is("form.AttachmentUploadForm"))return d.overlay()&&d.overlay().close(),e.trigger({type:"AttachmentUploaded",ajaxData:a.ajaxData}),!1});return{getSwfUploader:function(){return c},swfAlert:j}};XenForo.AttachmentEditor=function(e){this.setVisibility=function(a){var b=e.closest(".ctrlUnit"),c=e.find(".AttachmentInsertAllBlock"),
f=e.find(".AttachedFile:not(#AttachedFileTemplate)"),d=f.filter(".AttachedImage");console.log("Attachments changed, total files: %d, images: %d",f.length,d.length);b.length==0&&(b=e);a===!0?f.length?(d.length>1?c.show():c.hide(),b.show()):b.hide():f.length?(d.length>1?b.is(":hidden")?c.show():c.xfFadeDown(XenForo.speed.fast):b.is(":hidden")?c.hide():c.xfFadeUp(XenForo.speed.fast,!1,XenForo.speed.fast,"swing"),b.xfFadeDown(XenForo.speed.normal)):(c.slideUp(XenForo.speed.fast),b.xfFadeUp(XenForo.speed.normal,
!1,!1,"swing"))};this.setVisibility(!0);f("#AttachmentUploader").bind({click:function(){f("textarea.BbCodeWysiwygEditor").each(function(){var a=f(this).data("XenForo.BbCodeWysiwygEditor");a&&a.blurEditor()})},AttachmentQueued:function(a){var b=a.file,c=b.uniqueIdentifier||b.id;console.info("Queued file %s (%d bytes).",b.name,b.size);var d=f("#AttachedFileTemplate").clone().attr("id",c);d.find(".Filename").text(b.name);d.find(".ProgressCounter").text("0%");d.find(".ProgressGraphic span").css("width",
"0%");a.isImage&&d.addClass("AttachedImage");d.xfInsert("appendTo",".AttachmentList.New",null,i);d.find(".AttachmentCanceller").css("display","block").click(function(){a.swfUpload?a.swfUpload.cancelUpload(a.file.id):b.flowObj&&b.cancel();d.xfRemove(null,function(){e.trigger("AttachmentsChanged")},g,"swing")});e.trigger("AttachmentsChanged")},AttachmentUploadProgress:function(a){var b=a.file,a=a.bytes,c=b.uniqueIdentifier||b.id;console.log("Uploaded %d/%d bytes.",a,b.size);var b=Math.min(100,Math.ceil(a*
100/b.size)),a=b+"%",c=f("#"+c),e=c.find(".ProgressCounter"),d=c.find(".ProgressGraphic");e.text(a);d.css("width",a);b>=100&&c.find(".AttachmentCanceller").prop("disabled",!0).addClass("disabled");d.width()>e.outerWidth()&&e.appendTo(d)},AttachmentUploadError:function(a){var b="",c=a.file,e=c.uniqueIdentifier||c.id;f.each(a.ajaxData.error,function(a,c){b+=c+"\n"});XenForo.alert(b+"<br /><br />"+XenForo.htmlspecialchars(c.name));var c=f("#"+e),d=c.closest(".AttachmentEditor");c.xfRemove(null,function(){d.trigger("AttachmentsChanged")},
g,"swing");console.warn("AttachmentUploadError: %o",a)},AttachmentUploaded:function(a){if(a.file){var b=a.file,c=f("#"+(b.uniqueIdentifier||b.id)),d=c.find(".AttachmentText"),g=f(a.ajaxData.templateHtml),h;d.fadeOut(XenForo.speed.fast,function(){g.find(".AttachmentText").xfInsert("insertBefore",d,"fadeIn",XenForo.speed.fast);h=c.find(".Thumbnail");h.html(g.find(".Thumbnail").html());d.xfRemove();c.attr("id","attachment"+a.ajaxData.attachment_id)})}else c=f("#attachment"+a.ajaxData.attachment_id),
c.length||(c=f(a.ajaxData.templateHtml).xfInsert("appendTo",e.find(".AttachmentList.New"),null,i));e.trigger("AttachmentsChanged")}});var d=f.context(this,"setVisibility");f("#QuickReply").bind("QuickReplyComplete",function(){e.find(".AttachmentList.New li:not(#AttachedFileTemplate)").xfRemove().promise().always(d)});e.bind("AttachmentsChanged",d)};XenForo.AttachmentInserter=function(e){e.click(function(d){var a=e.closest(".AttachedFile").find(".Thumbnail a"),b=a.data("attachmentid"),c;c=a.find("img").attr("src");
a=a.attr("href");d.preventDefault();e.attr("name")=="thumb"?(d="[ATTACH]"+b+"[/ATTACH] ",c='<img src="'+c+'" class="attachThumb bbCodeImage" alt="attachThumb'+b+'" /> '):(d="[ATTACH=full]"+b+"[/ATTACH] ",c='<img src="'+a+'" class="attachFull bbCodeImage" alt="attachFull'+b+'" /> ');if(b=XenForo.getEditorInForm(e.closest("form"),":not(.NoAttachment)"))if(b.$editor){b.insertHtml(c);var f=b.$editor.data("xenForoElastic");f&&(setTimeout(function(){f()},250),setTimeout(function(){f()},1E3))}else b.val(b.val()+
d)})};XenForo.AttachmentDeleter=function(e){e.css("display","block").click(function(d){var a=f(d.target),b=a.attr("href")||a.data("href"),c=a.closest(".AttachedFile"),d=a.closest(".AttachedFile").find(".Thumbnail a").data("attachmentid");if(b){c.xfFadeUp(XenForo.speed.normal,null,g,"swing");XenForo.ajax(b,"",function(a){if(XenForo.hasResponseError(a))return c.xfFadeDown(XenForo.speed.normal),!1;var b=c.closest(".AttachmentEditor");c.xfRemove(null,function(){b.trigger("AttachmentsChanged")},g,"swing")});
if(d&&(a=XenForo.getEditorInForm(a.closest("form"),":not(.NoAttachment)"))&&a.$editor)a.$editor.find("img[alt=attachFull"+d+"], img[alt=attachThumb"+d+"]").remove(),(d=a.$editor.data("xenForoElastic"))&&d();return!1}console.warn("Unable to locate href for attachment deletion from %o",a)})};XenForo.AttachmentInsertAll=function(e){e.click(function(){f(".AttachmentInserter[name="+e.attr("name")+"]").each(function(d,a){f(a).trigger("click")})})};XenForo.AttachmentDeleteAll=function(e){e.click(function(){f(".AttachmentDeleter").each(function(d,
a){f(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);
