/*!
* medium-editor-insert-plugin v0.3.2 - jQuery insert plugin for MediumEditor
*
* https://github.com/orthes/medium-editor-insert-plugin
*
* Copyright (c) 2014 Pavel Linkesch (http://linkesch.sk)
* Released under the MIT license
*/
!function(a){a.fn.mediumInsert.registerAddon("embeds",{defaults:{urlPlaceholder:"Paste or type a link"},init:function(b){this.options=a.extend(this.defaults,b),this.$el=a.fn.mediumInsert.insert.$el,this.setEmbedButtonEvents(),this.preparePreviousEmbeds()},insertButton:function(a){var b="Embed";return("fontawesome"===a||"object"==typeof a&&a.fontawesome)&&(b=' '),"object"==typeof a&&a.embed&&(b=a.embed),''+b+" "},add:function(b){a.fn.mediumInsert.insert.deselect();var c='
';a(c).appendTo(b.prev()),setTimeout(function(){b.prev().find("input").focus()},50),a.fn.mediumInsert.insert.deselect(),this.currentPlaceholder=b,a(".mediumInsert-embedsText").focus()},preparePreviousEmbeds:function(){this.$el.find(".mediumInsert-embeds").each(function(){var b=a(this).parent();b.hasClass("mediumInsert-placeholder")||b.html(''+b.html()+"
")})},setEmbedButtonEvents:function(){var b=this;a(document).on("keypress","input.mediumInsert-embedsText",function(a){(a.which&&13===a.which||a.keyCode&&13===a.keyCode)&&(b.setEnterActionEvents(),b.removeToolbar())}),this.$el.on("blur",".mediumInsert-embedsText",function(){b.removeToolbar()}).on("paste",".mediumInsert-embedsText",function(b){a.fn.mediumInsert.insert.isFirefox&&b.originalEvent.clipboardData&&a(this).val(b.originalEvent.clipboardData.getData("text/plain"))})},setEnterActionEvents:function(){function b(b){if(!b)return alert("Incorrect URL format specified"),!1;var d=b,e=(new Date).getTime();b=a('
').append(b),c.currentPlaceholder.append(b),c.currentPlaceholder.closest("[data-medium-element]").trigger("keyup").trigger("input"),-1!==d.indexOf("facebook")&&"undefined"!=typeof FB&&setTimeout(function(){FB.XFBML.parse()},2e3)}var c=this;if(a.fn.mediumInsert.settings.enabled===!1)return!1;var d=a("input.mediumInsert-embedsText").val();if(!d)return!1;if(!this.options.oembedProxy){var e=c.convertUrlToEmbedTag(d);return b(e)}c.getOEmbedHTML(d,function(a,c){var d=!a&&c&&c.html;c&&!c.html&&"photo"===c.type&&c.url&&(d=' '),b(d)})},removeToolbar:function(){a(".mediumInsert-embedsWire").remove()},getOEmbedHTML:function(b,c){a.ajax({url:this.options.oembedProxy,dataType:"json",data:{url:b},success:function(a,b,d){c(null,a,d)},error:function(a,b,d){var e=function(){try{return JSON.parse(a.responseText)}catch(b){}}();c(e&&e.error||a.status||d.message,e,a)}})},convertUrlToEmbedTag:function(a){if(!new RegExp(["youtube","yout.be","vimeo","facebook","instagram"].join("|")).test(a))return!1;var b=a.replace(/\n?/g,"").replace(/^((http(s)?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/(watch\?v=|v\/)?)([a-zA-Z0-9\-_]+)(.*)?$/,'
').replace(/^http:\/\/vimeo\.com(\/.+)?\/([0-9]+)$/,'').replace(/^https:\/\/www\.facebook\.com\/(video.php|photo.php)\?v=(\d+).+$/,'').replace(/^http:\/\/instagram\.com\/p\/(.+)\/?$/,' ');return/<("[^"]*"|'[^']*'|[^'">])*>/.test(b)?b:!1}})}(jQuery);