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