var CropDraggable=Class.create();Object.extend(Object.extend(CropDraggable.prototype,Draggable.prototype),{initialize:function(A){this.options=Object.extend({drawMethod:function(){}},arguments[1]||{});this.element=$(A);this.handle=this.element;this.delta=this.currentDelta();this.dragging=false;this.eventMouseDown=this.initDrag.bindAsEventListener(this);Event.observe(this.handle,"mousedown",this.eventMouseDown);Draggables.register(this)},draw:function(A){var D=Position.cumulativeOffset(this.element);var C=this.currentDelta();D[0]-=C[0];D[1]-=C[1];var B=[0,1].map(function(E){return(A[E]-D[E]-this.offset[E])}.bind(this));this.options.drawMethod(B)}});var Cropper={};Cropper.Img=Class.create();Cropper.Img.prototype={initialize:function(C,A){this.options=Object.extend({ratioDim:{x:0,y:0},minWidth:0,minHeight:0,displayOnInit:false,onEndCrop:Prototype.emptyFunction,captureKeys:true,onloadCoords:null,maxWidth:0,maxHeight:0},A||{});this.img=$(C);this.clickCoords={x:0,y:0};this.dragging=false;this.resizing=false;this.isWebKit=/Konqueror|Safari|KHTML/.test(navigator.userAgent);this.isIE=/MSIE/.test(navigator.userAgent);this.isOpera8=/Opera\s[1-8]/.test(navigator.userAgent);this.ratioX=0;this.ratioY=0;this.attached=false;this.fixedWidth=(this.options.maxWidth>0&&(this.options.minWidth>=this.options.maxWidth));this.fixedHeight=(this.options.maxHeight>0&&(this.options.minHeight>=this.options.maxHeight));if(typeof this.img=="undefined"){return }if(this.img.width==0||this.img.height==0){return }$A(document.getElementsByTagName("script")).each(function(E){if(E.src.match(/cropper\.js/)){var F=E.src.replace(/cropper\.js(.*)?/,"");var D=document.createElement("link");D.rel="stylesheet";D.type="text/css";D.href=F+"cropper.css";D.media="screen";document.getElementsByTagName("head")[0].appendChild(D)}});if(this.options.ratioDim.x>0&&this.options.ratioDim.y>0){var B=this.getGCD(this.options.ratioDim.x,this.options.ratioDim.y);this.ratioX=this.options.ratioDim.x/B;this.ratioY=this.options.ratioDim.y/B}this.subInitialize();if(this.img.complete||this.isWebKit){this.onLoad()}else{Event.observe(this.img,"load",this.onLoad.bindAsEventListener(this))}},getGCD:function(B,A){if(A==0){return B}return this.getGCD(A,B%A)},onLoad:function(){var B="imgCrop_";var D=this.img.parentNode;var A="";if(this.isOpera8){A=" opera8"}this.imgWrap=Builder.node("div",{"class":B+"wrap"+A});this.north=Builder.node("div",{"class":B+"overlay "+B+"north"},[Builder.node("span")]);this.east=Builder.node("div",{"class":B+"overlay "+B+"east"},[Builder.node("span")]);this.south=Builder.node("div",{"class":B+"overlay "+B+"south"},[Builder.node("span")]);this.west=Builder.node("div",{"class":B+"overlay "+B+"west"},[Builder.node("span")]);var C=[this.north,this.east,this.south,this.west];this.dragArea=Builder.node("div",{"class":B+"dragArea"},C);this.handleN=Builder.node("div",{"class":B+"handle "+B+"handleN"});this.handleNE=Builder.node("div",{"class":B+"handle "+B+"handleNE"});this.handleE=Builder.node("div",{"class":B+"handle "+B+"handleE"});this.handleSE=Builder.node("div",{"class":B+"handle "+B+"handleSE"});this.handleS=Builder.node("div",{"class":B+"handle "+B+"handleS"});this.handleSW=Builder.node("div",{"class":B+"handle "+B+"handleSW"});this.handleW=Builder.node("div",{"class":B+"handle "+B+"handleW"});this.handleNW=Builder.node("div",{"class":B+"handle "+B+"handleNW"});if(this.options.s60v2==1){x=Builder.node("div",{"class":"s60v2fix"},[Builder.node("span")])}else{x=Builder.node("div",{"class":"s60v2fix",style:"display: none"},[Builder.node("span")])}this.selArea=Builder.node("div",{"class":B+"selArea"},[x,Builder.node("div",{"class":B+"marqueeHoriz "+B+"marqueeNorth"},[Builder.node("span")]),Builder.node("div",{"class":B+"marqueeVert "+B+"marqueeEast"},[Builder.node("span")]),Builder.node("div",{"class":B+"marqueeHoriz "+B+"marqueeSouth"},[Builder.node("span")]),Builder.node("div",{"class":B+"marqueeVert "+B+"marqueeWest"},[Builder.node("span")]),this.handleN,this.handleNE,this.handleE,this.handleSE,this.handleS,this.handleSW,this.handleW,this.handleNW,Builder.node("div",{"class":B+"clickArea"}),]);this.imgWrap.appendChild(this.img);this.imgWrap.appendChild(this.dragArea);this.dragArea.appendChild(this.selArea);this.dragArea.appendChild(Builder.node("div",{"class":B+"clickArea"}));D.appendChild(this.imgWrap);this.startDragBind=this.startDrag.bindAsEventListener(this);Event.observe(this.dragArea,"mousedown",this.startDragBind);this.onDragBind=this.onDrag.bindAsEventListener(this);Event.observe(document,"mousemove",this.onDragBind);this.endCropBind=this.endCrop.bindAsEventListener(this);Event.observe(document,"mouseup",this.endCropBind);this.resizeBind=this.startResize.bindAsEventListener(this);this.handles=[this.handleN,this.handleNE,this.handleE,this.handleSE,this.handleS,this.handleSW,this.handleW,this.handleNW];this.registerHandles(true);if(this.options.captureKeys){this.keysBind=this.handleKeys.bindAsEventListener(this);Event.observe(document,"keypress",this.keysBind)}new CropDraggable(this.selArea,{drawMethod:this.moveArea.bindAsEventListener(this)});this.setParams()},registerHandles:function(B){for(var D=0;D<this.handles.length;D++){var G=$(this.handles[D]);if(B){var A=false;if(this.fixedWidth&&this.fixedHeight){A=true}else{if(this.fixedWidth||this.fixedHeight){var C=G.className.match(/([S|N][E|W])$/);var F=G.className.match(/(E|W)$/);var E=G.className.match(/(N|S)$/);if(C){A=true}else{if(this.fixedWidth&&F){A=true}else{if(this.fixedHeight&&E){A=true}}}}}if(A){G.hide()}else{Event.observe(G,"mousedown",this.resizeBind)}}else{G.show();Event.stopObserving(G,"mousedown",this.resizeBind)}}},setParams:function(){this.imgW=this.img.width;this.imgH=this.img.height;$(this.north).setStyle({height:0});$(this.east).setStyle({width:0,height:0});$(this.south).setStyle({height:0});$(this.west).setStyle({width:0,height:0});$(this.imgWrap).setStyle({width:this.imgW+"px",height:this.imgH+"px"});$(this.selArea).hide();var B={x1:0,y1:0,x2:0,y2:0};var A=false;if(this.options.onloadCoords!=null){B=this.cloneCoords(this.options.onloadCoords);A=true}else{if(this.options.ratioDim.x>0&&this.options.ratioDim.y>0){B.x1=Math.ceil((this.imgW-this.options.ratioDim.x)/2);B.y1=Math.ceil((this.imgH-this.options.ratioDim.y)/2);B.x2=B.x1+this.options.ratioDim.x;B.y2=B.y1+this.options.ratioDim.y;A=true}}this.setAreaCoords(B,false,false,1);if(this.options.displayOnInit&&A){this.selArea.show();this.drawArea();this.endCrop()}this.attached=true},remove:function(){if(this.attached){this.attached=false;this.imgWrap.parentNode.insertBefore(this.img,this.imgWrap);this.imgWrap.parentNode.removeChild(this.imgWrap);Event.stopObserving(this.dragArea,"mousedown",this.startDragBind);Event.stopObserving(document,"mousemove",this.onDragBind);Event.stopObserving(document,"mouseup",this.endCropBind);this.registerHandles(false);if(this.options.captureKeys){Event.stopObserving(document,"keypress",this.keysBind)}}},reset:function(){if(!this.attached){this.onLoad()}else{this.setParams()}this.endCrop()},handleKeys:function(B){var A={x:0,y:0};if(!this.dragging){switch(B.keyCode){case (37):A.x=-1;break;case (38):A.y=-1;break;case (39):A.x=1;break;case (40):A.y=1;break}if(A.x!=0||A.y!=0){if(B.shiftKey){A.x*=10;A.y*=10}this.moveArea([this.areaCoords.x1+A.x,this.areaCoords.y1+A.y]);Event.stop(B)}}},calcW:function(){return(this.areaCoords.x2-this.areaCoords.x1)},calcH:function(){return(this.areaCoords.y2-this.areaCoords.y1)},moveArea:function(A){this.setAreaCoords({x1:A[0],y1:A[1],x2:A[0]+this.calcW(),y2:A[1]+this.calcH()},true,false);this.drawArea()},cloneCoords:function(A){return{x1:A.x1,y1:A.y1,x2:A.x2,y2:A.y2}},setAreaCoords:function(I,A,M,H,K){if(A){var J=I.x2-I.x1;var F=I.y2-I.y1;if(I.x1<0){I.x1=0;I.x2=J}if(I.y1<0){I.y1=0;I.y2=F}if(I.x2>this.imgW){I.x2=this.imgW;I.x1=this.imgW-J}if(I.y2>this.imgH){I.y2=this.imgH;I.y1=this.imgH-F}}else{if(I.x1<0){I.x1=0}if(I.y1<0){I.y1=0}if(I.x2>this.imgW){I.x2=this.imgW}if(I.y2>this.imgH){I.y2=this.imgH}if(H!=null){if(this.ratioX>0){this.applyRatio(I,{x:this.ratioX,y:this.ratioY},H,K)}else{if(M){this.applyRatio(I,{x:1,y:1},H,K)}}var B=[this.options.minWidth,this.options.minHeight];var L=[this.options.maxWidth,this.options.maxHeight];if(B[0]>0||B[1]>0||L[0]>0||L[1]>0){var G={a1:I.x1,a2:I.x2};var E={a1:I.y1,a2:I.y2};var D={min:0,max:this.imgW};var C={min:0,max:this.imgH};if((B[0]!=0||B[1]!=0)&&M){if(B[0]>0){B[1]=B[0]}else{if(B[1]>0){B[0]=B[1]}}}if((L[0]!=0||L[0]!=0)&&M){if(L[0]>0&&L[0]<=L[1]){L[1]=L[0]}else{if(L[1]>0&&L[1]<=L[0]){L[0]=L[1]}}}if(B[0]>0){this.applyDimRestriction(G,B[0],H.x,D,"min")}if(B[1]>1){this.applyDimRestriction(E,B[1],H.y,C,"min")}if(L[0]>0){this.applyDimRestriction(G,L[0],H.x,D,"max")}if(L[1]>1){this.applyDimRestriction(E,L[1],H.y,C,"max")}I={x1:G.a1,y1:E.a1,x2:G.a2,y2:E.a2}}}}this.areaCoords=I},applyDimRestriction:function(D,F,E,C,B){var A;if(B=="min"){A=((D.a2-D.a1)<F)}else{A=((D.a2-D.a1)>F)}if(A){if(E==1){D.a2=D.a1+F}else{D.a1=D.a2-F}if(D.a1<C.min){D.a1=C.min;D.a2=F}else{if(D.a2>C.max){D.a1=C.max-F;D.a2=C.max}}}},applyRatio:function(C,A,E,B){var D;if(B=="N"||B=="S"){D=this.applyRatioToAxis({a1:C.y1,b1:C.x1,a2:C.y2,b2:C.x2},{a:A.y,b:A.x},{a:E.y,b:E.x},{min:0,max:this.imgW});C.x1=D.b1;C.y1=D.a1;C.x2=D.b2;C.y2=D.a2}else{D=this.applyRatioToAxis({a1:C.x1,b1:C.y1,a2:C.x2,b2:C.y2},{a:A.x,b:A.y},{a:E.x,b:E.y},{min:0,max:this.imgH});C.x1=D.a1;C.y1=D.b1;C.x2=D.a2;C.y2=D.b2}},applyRatioToAxis:function(I,F,H,B){var J=Object.extend(I,{});var E=J.a2-J.a1;var A=Math.floor(E*F.b/F.a);var G;var C;var D=null;if(H.b==1){G=J.b1+A;if(G>B.max){G=B.max;D=G-J.b1}J.b2=G}else{G=J.b2-A;if(G<B.min){G=B.min;D=G+J.b2}J.b1=G}if(D!=null){C=Math.floor(D*F.a/F.b);if(H.a==1){J.a2=J.a1+C}else{J.a1=J.a1=J.a2-C}}return J},drawArea:function(){var H=this.calcW();var E=this.calcH();var G="px";var C=[this.areaCoords.x1+G,this.areaCoords.y1+G,H+G,E+G,this.areaCoords.x2+G,this.areaCoords.y2+G,(this.img.width-this.areaCoords.x2)+G,(this.img.height-this.areaCoords.y2)+G];var F=this.selArea.style;F.left=C[0];F.top=C[1];F.width=C[2];F.height=C[3];var I=Math.ceil((H-6)/2)+G;var D=Math.ceil((E-6)/2)+G;this.handleN.style.left=I;this.handleE.style.top=D;this.handleS.style.left=I;this.handleW.style.top=D;this.north.style.height=C[1];var A=this.east.style;A.top=C[1];A.height=C[3];A.left=C[4];A.width=C[6];var J=this.south.style;J.top=C[5];J.height=C[7];var B=this.west.style;B.top=C[1];B.height=C[3];B.width=C[0];this.subDrawArea();this.forceReRender()},forceReRender:function(){if(this.isIE||this.isWebKit){var F=document.createTextNode(" ");var D,B,E,A;if(this.isIE){fixEl=this.selArea}else{if(this.isWebKit){fixEl=document.getElementsByClassName("imgCrop_marqueeSouth",this.imgWrap)[0];D=Builder.node("div","");D.style.visibility="hidden";var C=["SE","S","SW"];for(A=0;A<C.length;A++){B=document.getElementsByClassName("imgCrop_handle"+C[A],this.selArea)[0];if(B.childNodes.length){B.removeChild(B.childNodes[0])}B.appendChild(D)}}}fixEl.appendChild(F);fixEl.removeChild(F)}},startResize:function(A){this.startCoords=this.cloneCoords(this.areaCoords);this.resizing=true;this.resizeHandle=Event.element(A).classNames().toString().replace(/([^N|NE|E|SE|S|SW|W|NW])+/,"");Event.stop(A)},startDrag:function(A){this.selArea.show();this.clickCoords=this.getCurPos(A);this.setAreaCoords({x1:this.clickCoords.x,y1:this.clickCoords.y,x2:this.clickCoords.x,y2:this.clickCoords.y},false,false,null);this.dragging=true;this.onDrag(A);Event.stop(A)},getCurPos:function(B){var A=this.imgWrap,C=Position.cumulativeOffset(A);while(A.nodeName!="BODY"){C[1]-=A.scrollTop||0;C[0]-=A.scrollLeft||0;A=A.parentNode}return curPos={x:Event.pointerX(B)-C[0],y:Event.pointerY(B)-C[1]}},onDrag:function(E){if(this.dragging||this.resizing){var B=null;var A=this.getCurPos(E);var D=this.cloneCoords(this.areaCoords);var C={x:1,y:1};if(this.dragging){if(A.x<this.clickCoords.x){C.x=-1}if(A.y<this.clickCoords.y){C.y=-1}this.transformCoords(A.x,this.clickCoords.x,D,"x");this.transformCoords(A.y,this.clickCoords.y,D,"y")}else{if(this.resizing){B=this.resizeHandle;if(B.match(/E/)){this.transformCoords(A.x,this.startCoords.x1,D,"x");if(A.x<this.startCoords.x1){C.x=-1}}else{if(B.match(/W/)){this.transformCoords(A.x,this.startCoords.x2,D,"x");if(A.x<this.startCoords.x2){C.x=-1}}}if(B.match(/N/)){this.transformCoords(A.y,this.startCoords.y2,D,"y");if(A.y<this.startCoords.y2){C.y=-1}}else{if(B.match(/S/)){this.transformCoords(A.y,this.startCoords.y1,D,"y");if(A.y<this.startCoords.y1){C.y=-1}}}}}this.setAreaCoords(D,false,E.shiftKey,C,B);this.drawArea();Event.stop(E)}},transformCoords:function(B,A,E,D){var C=[B,A];if(B>A){C.reverse()}E[D+"1"]=C[0];E[D+"2"]=C[1]},endCrop:function(){this.dragging=false;this.resizing=false;this.options.onEndCrop(this.areaCoords,{width:this.calcW(),height:this.calcH()})},subInitialize:function(){},subDrawArea:function(){}};Cropper.ImgWithPreview=Class.create();Object.extend(Object.extend(Cropper.ImgWithPreview.prototype,Cropper.Img.prototype),{subInitialize:function(){this.hasPreviewImg=false;if(typeof (this.options.previewWrap)!="undefined"&&this.options.preWidth>0&&this.options.preHeight>0){this.previewWrap=$(this.options.previewWrap);this.previewImg=this.img.cloneNode(false);this.previewImg.id="imgCrop_"+this.previewImg.id;this.options.displayOnInit=true;this.hasPreviewImg=true;this.previewWrap.addClassName("imgCrop_previewWrap");this.previewWrap.setStyle({width:this.options.preWidth+"px",height:this.options.preHeight+"px"});this.previewWrap.appendChild(this.previewImg);if(this.options.s60v2==1){this.previewWrap.appendChild(Builder.node("div",{"class":"s60v2fix"},[Builder.node("span")]))}}},subDrawArea:function(){if(this.hasPreviewImg){var D=this.calcW();var E=this.calcH();var F={x:this.imgW/D,y:this.imgH/E};var C={x:D/this.options.preWidth,y:E/this.options.preHeight};var A={w:Math.ceil(this.options.preWidth*F.x)+"px",h:Math.ceil(this.options.preHeight*F.y)+"px",x:"-"+Math.ceil(this.areaCoords.x1/C.x)+"px",y:"-"+Math.ceil(this.areaCoords.y1/C.y)+"px"};var B=this.previewImg.style;B.width=A.w;B.height=A.h;B.left=A.x;B.top=A.y}}});
