/*
 Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
 For licensing, see LICENSE.md or http://ckeditor.com/license
*/
(function(){function l(a){if(!(a.name in{div:1,p:1}))return!1;var b=a.children;if(1!==b.length)return!1;var b=b[0],c=b.name;return"img"!=c&&!("figure"==c&&b.hasClass("caption"))?!1:"center"==CKEDITOR.tools.parseCssText(a.attributes.style||"",!0)["text-align"]?!0:!1}function z(a){var b=a.data,b={width:b.width,height:b.height},a=a.parts.image,c;for(c in b)b[c]?a.setAttribute(c,b[c]):a.removeAttribute(c)}function A(a){var b=a.editor,c=b.editable(),d=b.document,e=d.createElement("span");e.addClass("cke_image2_resizer");
e.setAttribute("title",b.lang.image2.resizer);e.append(new CKEDITOR.dom.text("​",d));if(a.inline)a.wrapper.append(e);else{var f=a.element.getFirst(),h=d.createElement("span");h.addClass("cke_image2_resizer_wrapper");h.append(a.parts.image);h.append(e);a.element.append(h,!0);f.is("span")&&f.remove()}e.on("mousedown",function(f){function h(a,b,c){var g=CKEDITOR.document,i=[];d.equals(g)||i.push(g.on(a,b));i.push(d.on(a,b));if(c)for(a=i.length;a--;)c.push(i.pop())}function g(){o=k+m*r;p=Math.round(o/
q)}function i(){p=t-n;o=Math.round(p*q)}var v=a.parts.image,m="right"==a.data.align?-1:1,j=f.data.$.screenX,l=f.data.$.screenY,k=v.$.clientWidth,t=v.$.clientHeight,q=k/t,w=[],y="cke_image2_s"+(!~m?"w":"e"),x,o,p,u,r,n,s;b.fire("saveSnapshot");h("mousemove",function(a){x=a.data.$;r=x.screenX-j;n=l-x.screenY;s=Math.abs(r/n);1==m?0>=r?0>=n?g():s>=q?g():i():0>=n?s>=q?i():g():i():0>=r?0>=n?s>=q?i():g():i():0>=n?g():s>=q?g():i();15<=o&&15<=p?(v.setAttributes({width:o,height:p}),u=!0):u=!1},w);h("mouseup",
function(){for(var g;g=w.pop();)g.removeListener();u&&(a.setData({width:o,height:p}),b.fire("saveSnapshot"));c.removeClass(y);e.removeClass("cke_image2_resizing");u=!1},w);c.addClass(y);e.addClass("cke_image2_resizing")});a.on("data",function(){e["right"==a.data.align?"addClass":"removeClass"]("cke_image2_resizer_left")})}function B(a){var b=[];return function(c){var d=a.getCommand("justify"+c);if(d){b.push(function(){d.refresh(a,a.elementPath())});if(c in{right:1,left:1,center:1})d.on("exec",function(d){var f=
t(a);if(f){f.setData("align",c);for(f=b.length;f--;)b[f]();d.cancel()}});d.on("refresh",function(b){var d=t(a),h={right:1,left:1,center:1};d&&(this.setState(d.data.align==c?CKEDITOR.TRISTATE_ON:c in h?CKEDITOR.TRISTATE_OFF:CKEDITOR.TRISTATE_DISABLED),b.cancel())})}}}function t(a){return(a=a.widgets.focused)&&"image2"==a.name?a:null}var C=/^\s*(\d+\%)\s*$/i;CKEDITOR.plugins.add("image2",{lang:"af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,ug,uk,vi,zh,zh-cn",
requires:"widget,dialog",icons:"image2",hidpi:!0,onLoad:function(){CKEDITOR.addCss(".cke_editable.cke_image2_sw, .cke_editable.cke_image2_sw *{cursor:sw-resize !important}.cke_editable.cke_image2_se, .cke_editable.cke_image2_se *{cursor:se-resize !important}.cke_image2_resizer{display:none;position:absolute;width:10px;height:10px;bottom:-5px;right:-5px;background:#000;outline:1px solid #fff;cursor:se-resize;}.cke_image2_resizer_wrapper{position:relative;display:inline-block;line-height:0;}.cke_image2_resizer.cke_image2_resizer_left{right:auto;left:-5px;cursor:sw-resize;}.cke_widget_wrapper:hover .cke_image2_resizer,.cke_image2_resizer.cke_image2_resizing{display:block}")},
init:function(a){var b=a.config,c=a.lang.image2;b.filebrowserImage2BrowseUrl=b.filebrowserImageBrowseUrl;b.filebrowserImage2UploadUrl=b.filebrowserImageUploadUrl;k.pathName=c.pathName;k.editables.caption.pathName=c.pathNameCaption;a.widgets.add("image2",k);a.ui.addButton&&a.ui.addButton("image2",{label:a.lang.common.image,command:"image2",toolbar:"insert,10"});a.contextMenu&&(a.addMenuGroup("image2",10),a.addMenuItem("image2",{label:c.menu,command:"image2",group:"image2"}));CKEDITOR.dialog.add("image2",
this.path+"dialogs/image2.js")},afterInit:function(a){var b={left:1,right:1,center:1,block:1},a=B(a),c;for(c in b)a(c)}});var k={allowedContent:{div:{match:l,styles:"text-align"},figcaption:!0,figure:{classes:"!caption",styles:"float,display"},img:{attributes:"!src,alt,width,height",styles:"float"},p:{match:l,styles:"text-align"}},contentTransformations:[["img[width]: sizeToAttribute"]],editables:{caption:{selector:"figcaption",allowedContent:"br em strong sub sup u s; a[!href]"}},parts:{image:"img",
caption:"figcaption"},dialog:"image2",template:'<img alt="" src="" />',data:function(){var a=this,b=a.editor;a.shiftState({element:a.element,oldState:a.oldData,newState:a.data,destroy:function(){this.destroyed||(b.widgets.focused==a&&(this.focused=!0),b.widgets.destroy(a),this.destroyed=!0)},init:function(c){if(this.destroyed)a=b.widgets.initOn(c,"image2",a.data),this.focused&&(a.focus(),delete this.focused),delete this.destroyed;else{var c=a,d=c.wrapper,e=c.data.align;"center"==e?(c.inline||d.setStyle("text-align",
"center"),d.removeStyle("float")):(c.inline||d.removeStyle("text-align"),"none"==e?d.removeStyle("float"):d.setStyle("float",e))}}});a.parts.image.setAttributes({src:a.data.src,"data-cke-saved-src":a.data.src,alt:a.data.alt});z(a);a.oldData=CKEDITOR.tools.extend({},a.data)},init:function(){var a=CKEDITOR.plugins.image2,b=this.parts.image,c={hasCaption:!!this.parts.caption,src:b.getAttribute("src"),alt:b.getAttribute("alt")||"",width:b.getAttribute("width")||"",height:b.getAttribute("height")||"",
lock:this.ready?a.checkHasNaturalRatio(b):!0};c.align||(c.align=this.element.getStyle("float")||b.getStyle("float")||"none",this.element.removeStyle("float"),b.removeStyle("float"));c.hasCaption||this.wrapper.setStyle("line-height","0");this.setData(c);A(this);this.shiftState=a.stateShifter(this.editor);this.on("contextMenu",function(a){a.data.image2=CKEDITOR.TRISTATE_OFF});this.on("dialog",function(a){a.data.widget=this},this)},upcast:function(a,b){var c={width:1,height:1},d=a.name,e;if(!a.attributes["data-cke-realelement"]&&
(l(a)?("div"==d&&(e=a.getFirst("figure"),a.replaceWith(e),a=e),b.align="center",e=a.getFirst("img")):"figure"==d&&a.hasClass("caption")?e=a.getFirst("img"):"img"==d&&(e=a),e)){for(var f in c)(d=e.attributes[f])&&d.match(C)&&delete e.attributes[f];return a}},downcast:function(a){var b=a.attributes,c=this.data.align;if(!this.inline){var d=a.getFirst("span"),e=d.getFirst("img");d.replaceWith(e)}c&&"none"!=c&&(d=CKEDITOR.tools.parseCssText(b.style||""),"center"==c&&"p"!=a.name?a=a.wrapWith(new CKEDITOR.htmlParser.element("img"==
a.name?"p":"div",{style:"text-align:center"})):c in{left:1,right:1}&&(d["float"]=c),CKEDITOR.tools.isEmpty(d)||(b.style=CKEDITOR.tools.writeCssText(d)));return a}};CKEDITOR.plugins.image2={stateShifter:function(a){function b(a,b){return a.oldState?a.oldState[b]!==a.newState[b]:!1}function c(a){var b=f.createElement(e,{styles:{"text-align":"center"}});d(b,a);a.move(b);return b}function d(b,c){if(c.getParent()){var d=a.createRange();d.moveToPosition(c,CKEDITOR.POSITION_BEFORE_START);h.insertElementIntoRange(b,
d);c.remove()}else b.replace(c)}var e=a.config.enterMode==CKEDITOR.ENTER_P?"p":"div",f=a.document,h=a.editable(),k=["hasCaption","align"],l={align:function(a,d,e){var f=a.newState.hasCaption,j=a.element;if(b(a,"align")){if(!f&&("center"==e&&(a.destroy(),a.element=c(j)),!b(a,"hasCaption")&&"center"==d&&"center"!=e))a.destroy(),d=j.findOne("img"),d.replace(j),a.element=d}else"center"==e&&(b(a,"hasCaption")&&!f)&&(a.destroy(),a.element=c(j));j.is("figure")&&("center"==e?j.setStyle("display","inline-block"):
j.removeStyle("display"))},hasCaption:function(a,c,e){if(b(a,"hasCaption"))if(c=a.element,a.destroy(),e){var e=c.findOne("img")||c,m=CKEDITOR.dom.element.createFromHtml('<figure class="caption"><img alt="" src="" /><figcaption>Caption</figcaption></figure>',f);d(m,c);e.replace(m.findOne("img"));a.element=m}else e=c.findOne("img"),e.replace(c),a.element=e}};return function(a){for(var b=a.oldState,c=a.newState,d,e=0;e<k.length;e++)d=k[e],l[d](a,b?b[d]:null,c[d]);a.init(a.element)}},checkHasNaturalRatio:function(a){var b=
a.$,a=this.getNatural(a);return Math.round(b.clientWidth/a.width*a.height)==b.clientHeight||Math.round(b.clientHeight/a.height*a.width)==b.clientWidth},getNatural:function(a){if(a.$.naturalWidth)a={width:a.$.naturalWidth,height:a.$.naturalHeight};else{var b=new Image;b.src=a.getAttribute("src");a={width:b.width,height:b.height}}return a}}})();