
var CropDraggable=Class.create();Object.extend(Object.extend(CropDraggable.prototype,Draggable.prototype),{initialize:function(element){this.options=Object.extend({drawMethod:function(){}},arguments[1]||{});this.element=$(element);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(point){var pos=Position.cumulativeOffset(this.element);var d=this.currentDelta();pos[0]-=d[0];pos[1]-=d[1];var p=[0,1].map(function(i){return(point[i]-pos[i]-this.offset[i])}.bind(this));this.options.drawMethod(p);}});var Cropper={};Cropper.Img=Class.create();Cropper.Img.prototype={initialize:function(element,options){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},options||{});this.img=$(element);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;$A(document.getElementsByTagName('script')).each(function(s){if(s.src.match(/cropper\.js/)){var path=s.src.replace(/cropper\.js(.*)?/,'');var style=document.createElement('link');style.rel='stylesheet';style.type='text/css';style.href='http://media.shuttercal.com/css/cropper.css';style.media='screen';document.getElementsByTagName('head')[0].appendChild(style);}});if(this.options.ratioDim.x>0&&this.options.ratioDim.y>0){var gcd=this.getGCD(this.options.ratioDim.x,this.options.ratioDim.y);this.ratioX=this.options.ratioDim.x/gcd;this.ratioY=this.options.ratioDim.y/gcd;}
this.subInitialize();if(this.img.complete||this.isWebKit)this.onLoad();else Event.observe(this.img,'load',this.onLoad.bindAsEventListener(this));},getGCD:function(a,b){if(b==0)return a;return this.getGCD(b,a%b);},onLoad:function(){var cNamePrefix='imgCrop_';var insertPoint=this.img.parentNode;var fixOperaClass='';if(this.isOpera8)fixOperaClass=' opera8';this.imgWrap=Builder.node('div',{'class':cNamePrefix+'wrap'+fixOperaClass});this.north=Builder.node('div',{'class':cNamePrefix+'overlay '+cNamePrefix+'north'},[Builder.node('span')]);this.east=Builder.node('div',{'class':cNamePrefix+'overlay '+cNamePrefix+'east'},[Builder.node('span')]);this.south=Builder.node('div',{'class':cNamePrefix+'overlay '+cNamePrefix+'south'},[Builder.node('span')]);this.west=Builder.node('div',{'class':cNamePrefix+'overlay '+cNamePrefix+'west'},[Builder.node('span')]);var overlays=[this.north,this.east,this.south,this.west];this.dragArea=Builder.node('div',{'class':cNamePrefix+'dragArea'},overlays);this.handleN=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleN'});this.handleNE=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleNE'});this.handleE=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleE'});this.handleSE=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleSE'});this.handleS=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleS'});this.handleSW=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleSW'});this.handleW=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleW'});this.handleNW=Builder.node('div',{'class':cNamePrefix+'handle '+cNamePrefix+'handleNW'});this.selArea=Builder.node('div',{'class':cNamePrefix+'selArea'},[Builder.node('div',{'class':cNamePrefix+'marqueeHoriz '+cNamePrefix+'marqueeNorth'},[Builder.node('span')]),Builder.node('div',{'class':cNamePrefix+'marqueeVert '+cNamePrefix+'marqueeEast'},[Builder.node('span')]),Builder.node('div',{'class':cNamePrefix+'marqueeHoriz '+cNamePrefix+'marqueeSouth'},[Builder.node('span')]),Builder.node('div',{'class':cNamePrefix+'marqueeVert '+cNamePrefix+'marqueeWest'},[Builder.node('span')]),this.handleN,this.handleNE,this.handleE,this.handleSE,this.handleS,this.handleSW,this.handleW,this.handleNW,Builder.node('div',{'class':cNamePrefix+'clickArea'})]);this.imgWrap.appendChild(this.img);this.imgWrap.appendChild(this.dragArea);this.dragArea.appendChild(this.selArea);this.dragArea.appendChild(Builder.node('div',{'class':cNamePrefix+'clickArea'}));insertPoint.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(registration){for(var i=0;i<this.handles.length;i++){var handle=$(this.handles[i]);if(registration){var hideHandle=false;if(this.fixedWidth&&this.fixedHeight)hideHandle=true;else if(this.fixedWidth||this.fixedHeight){var isCornerHandle=handle.className.match(/([S|N][E|W])$/)
var isWidthHandle=handle.className.match(/(E|W)$/);var isHeightHandle=handle.className.match(/(N|S)$/);if(isCornerHandle)hideHandle=true;else if(this.fixedWidth&&isWidthHandle)hideHandle=true;else if(this.fixedHeight&&isHeightHandle)hideHandle=true;}
if(hideHandle)handle.hide();else Event.observe(handle,'mousedown',this.resizeBind);}else{handle.show();Event.stopObserving(handle,'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 startCoords={x1:0,y1:0,x2:0,y2:0};var validCoordsSet=false;if(this.options.onloadCoords!=null){startCoords=this.cloneCoords(this.options.onloadCoords);validCoordsSet=true;}else if(this.options.ratioDim.x>0&&this.options.ratioDim.y>0){startCoords.x1=Math.ceil((this.imgW-this.options.ratioDim.x)/2);startCoords.y1=Math.ceil((this.imgH-this.options.ratioDim.y)/2);startCoords.x2=startCoords.x1+this.options.ratioDim.x;startCoords.y2=startCoords.y1+this.options.ratioDim.y;validCoordsSet=true;}
this.setAreaCoords(startCoords,false,false,1);if(this.options.displayOnInit&&validCoordsSet){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(e){var dir={x:0,y:0};if(!this.dragging){switch(e.keyCode){case(37):dir.x=-1;break;case(38):dir.y=-1;break;case(39):dir.x=1;break
case(40):dir.y=1;break;}
if(dir.x!=0||dir.y!=0){if(e.shiftKey){dir.x*=10;dir.y*=10;}
this.moveArea([this.areaCoords.x1+dir.x,this.areaCoords.y1+dir.y]);Event.stop(e);}}},calcW:function(){return(this.areaCoords.x2-this.areaCoords.x1)},calcH:function(){return(this.areaCoords.y2-this.areaCoords.y1)},moveArea:function(point){this.setAreaCoords({x1:point[0],y1:point[1],x2:point[0]+this.calcW(),y2:point[1]+this.calcH()},true,false);this.drawArea();},cloneCoords:function(coords){return{x1:coords.x1,y1:coords.y1,x2:coords.x2,y2:coords.y2};},setAreaCoords:function(coords,moving,square,direction,resizeHandle){if(moving){var targW=coords.x2-coords.x1;var targH=coords.y2-coords.y1;if(coords.x1<0){coords.x1=0;coords.x2=targW;}
if(coords.y1<0){coords.y1=0;coords.y2=targH;}
if(coords.x2>this.imgW){coords.x2=this.imgW;coords.x1=this.imgW-targW;}
if(coords.y2>this.imgH){coords.y2=this.imgH;coords.y1=this.imgH-targH;}}else{if(coords.x1<0)coords.x1=0;if(coords.y1<0)coords.y1=0;if(coords.x2>this.imgW)coords.x2=this.imgW;if(coords.y2>this.imgH)coords.y2=this.imgH;if(direction!=null){if(this.ratioX>0)this.applyRatio(coords,{x:this.ratioX,y:this.ratioY},direction,resizeHandle);else if(square)this.applyRatio(coords,{x:1,y:1},direction,resizeHandle);var mins=[this.options.minWidth,this.options.minHeight];var maxs=[this.options.maxWidth,this.options.maxHeight];if(mins[0]>0||mins[1]>0||maxs[0]>0||maxs[1]>0){var coordsTransX={a1:coords.x1,a2:coords.x2};var coordsTransY={a1:coords.y1,a2:coords.y2};var boundsX={min:0,max:this.imgW};var boundsY={min:0,max:this.imgH};if((mins[0]!=0||mins[1]!=0)&&square){if(mins[0]>0)mins[1]=mins[0];else if(mins[1]>0)mins[0]=mins[1];}
if((maxs[0]!=0||maxs[0]!=0)&&square){if(maxs[0]>0&&maxs[0]<=maxs[1])maxs[1]=maxs[0];else if(maxs[1]>0&&maxs[1]<=maxs[0])maxs[0]=maxs[1];}
if(mins[0]>0)this.applyDimRestriction(coordsTransX,mins[0],direction.x,boundsX,'min');if(mins[1]>1)this.applyDimRestriction(coordsTransY,mins[1],direction.y,boundsY,'min');if(maxs[0]>0)this.applyDimRestriction(coordsTransX,maxs[0],direction.x,boundsX,'max');if(maxs[1]>1)this.applyDimRestriction(coordsTransY,maxs[1],direction.y,boundsY,'max');coords={x1:coordsTransX.a1,y1:coordsTransY.a1,x2:coordsTransX.a2,y2:coordsTransY.a2};}}}
this.areaCoords=coords;},applyDimRestriction:function(coords,val,direction,bounds,type){var check;if(type=='min')check=((coords.a2-coords.a1)<val);else check=((coords.a2-coords.a1)>val);if(check){if(direction==1)coords.a2=coords.a1+val;else coords.a1=coords.a2-val;if(coords.a1<bounds.min){coords.a1=bounds.min;coords.a2=val;}else if(coords.a2>bounds.max){coords.a1=bounds.max-val;coords.a2=bounds.max;}}},applyRatio:function(coords,ratio,direction,resizeHandle){var newCoords;if(resizeHandle=='N'||resizeHandle=='S'){newCoords=this.applyRatioToAxis({a1:coords.y1,b1:coords.x1,a2:coords.y2,b2:coords.x2},{a:ratio.y,b:ratio.x},{a:direction.y,b:direction.x},{min:0,max:this.imgW});coords.x1=newCoords.b1;coords.y1=newCoords.a1;coords.x2=newCoords.b2;coords.y2=newCoords.a2;}else{newCoords=this.applyRatioToAxis({a1:coords.x1,b1:coords.y1,a2:coords.x2,b2:coords.y2},{a:ratio.x,b:ratio.y},{a:direction.x,b:direction.y},{min:0,max:this.imgH});coords.x1=newCoords.a1;coords.y1=newCoords.b1;coords.x2=newCoords.a2;coords.y2=newCoords.b2;}},applyRatioToAxis:function(coords,ratio,direction,bounds){var newCoords=Object.extend(coords,{});var calcDimA=newCoords.a2-newCoords.a1;var targDimB=Math.floor(calcDimA*ratio.b/ratio.a);var targB;var targDimA;var calcDimB=null;if(direction.b==1){targB=newCoords.b1+targDimB;if(targB>bounds.max){targB=bounds.max;calcDimB=targB-newCoords.b1;}
newCoords.b2=targB;}else{targB=newCoords.b2-targDimB;if(targB<bounds.min){targB=bounds.min;calcDimB=targB+newCoords.b2;}
newCoords.b1=targB;}
if(calcDimB!=null){targDimA=Math.floor(calcDimB*ratio.a/ratio.b);if(direction.a==1)newCoords.a2=newCoords.a1+targDimA;else newCoords.a1=newCoords.a1=newCoords.a2-targDimA;}
return newCoords;},drawArea:function(){var areaWidth=this.calcW();var areaHeight=this.calcH();var px='px';var params=[this.areaCoords.x1+px,this.areaCoords.y1+px,areaWidth+px,areaHeight+px,this.areaCoords.x2+px,this.areaCoords.y2+px,(this.img.width-this.areaCoords.x2)+px,(this.img.height-this.areaCoords.y2)+px];var areaStyle=this.selArea.style;areaStyle.left=params[0];areaStyle.top=params[1];areaStyle.width=params[2];areaStyle.height=params[3];var horizHandlePos=Math.ceil((areaWidth-6)/2)+px;var vertHandlePos=Math.ceil((areaHeight-6)/2)+px;this.handleN.style.left=horizHandlePos;this.handleE.style.top=vertHandlePos;this.handleS.style.left=horizHandlePos;this.handleW.style.top=vertHandlePos;this.north.style.height=params[1];var eastStyle=this.east.style;eastStyle.top=params[1];eastStyle.height=params[3];eastStyle.left=params[4];eastStyle.width=params[6];var southStyle=this.south.style;southStyle.top=params[5];southStyle.height=params[7];var westStyle=this.west.style;westStyle.top=params[1];westStyle.height=params[3];westStyle.width=params[0];this.subDrawArea();this.forceReRender();},forceReRender:function(){if(this.isIE||this.isWebKit){var n=document.createTextNode(' ');var d,el,fixEL,i;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 classList=['SE','S','SW'];for(i=0;i<classList.length;i++){el=document.getElementsByClassName('imgCrop_handle'+classList[i],this.selArea)[0];if(el.childNodes.length)el.removeChild(el.childNodes[0]);el.appendChild(d);}}
fixEl.appendChild(n);fixEl.removeChild(n);}},startResize:function(e){this.startCoords=this.cloneCoords(this.areaCoords);this.resizing=true;this.resizeHandle=Event.element(e).classNames().toString().replace(/([^N|NE|E|SE|S|SW|W|NW])+/,'');Event.stop(e);},startDrag:function(e){this.selArea.show();this.clickCoords=this.getCurPos(e);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(e);Event.stop(e);},getCurPos:function(e){var el=this.imgWrap,wrapOffsets=Position.cumulativeOffset(el);while(el.nodeName!='BODY'){wrapOffsets[1]-=el.scrollTop||0;wrapOffsets[0]-=el.scrollLeft||0;el=el.parentNode;}
return curPos={x:Event.pointerX(e)-wrapOffsets[0],y:Event.pointerY(e)-wrapOffsets[1]}},onDrag:function(e){if(this.dragging||this.resizing){var resizeHandle=null;var curPos=this.getCurPos(e);var newCoords=this.cloneCoords(this.areaCoords);var direction={x:1,y:1};if(this.dragging){if(curPos.x<this.clickCoords.x)direction.x=-1;if(curPos.y<this.clickCoords.y)direction.y=-1;this.transformCoords(curPos.x,this.clickCoords.x,newCoords,'x');this.transformCoords(curPos.y,this.clickCoords.y,newCoords,'y');}else if(this.resizing){resizeHandle=this.resizeHandle;if(resizeHandle.match(/E/)){this.transformCoords(curPos.x,this.startCoords.x1,newCoords,'x');if(curPos.x<this.startCoords.x1)direction.x=-1;}else if(resizeHandle.match(/W/)){this.transformCoords(curPos.x,this.startCoords.x2,newCoords,'x');if(curPos.x<this.startCoords.x2)direction.x=-1;}
if(resizeHandle.match(/N/)){this.transformCoords(curPos.y,this.startCoords.y2,newCoords,'y');if(curPos.y<this.startCoords.y2)direction.y=-1;}else if(resizeHandle.match(/S/)){this.transformCoords(curPos.y,this.startCoords.y1,newCoords,'y');if(curPos.y<this.startCoords.y1)direction.y=-1;}}
this.setAreaCoords(newCoords,false,e.shiftKey,direction,resizeHandle);this.drawArea();Event.stop(e);}},transformCoords:function(curVal,baseVal,coords,axis){var newVals=[curVal,baseVal];if(curVal>baseVal)newVals.reverse();coords[axis+'1']=newVals[0];coords[axis+'2']=newVals[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.minWidth>0&&this.options.minHeight>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.minWidth+'px',height:this.options.minHeight+'px'});this.previewWrap.appendChild(this.previewImg);}},subDrawArea:function(){if(this.hasPreviewImg){var calcWidth=this.calcW();var calcHeight=this.calcH();var dimRatio={x:this.imgW/calcWidth,y:this.imgH/calcHeight};var posRatio={x:calcWidth/this.options.minWidth,y:calcHeight/this.options.minHeight};var calcPos={w:Math.ceil(this.options.minWidth*dimRatio.x)+'px',h:Math.ceil(this.options.minHeight*dimRatio.y)+'px',x:'-'+Math.ceil(this.areaCoords.x1/posRatio.x)+'px',y:'-'+Math.ceil(this.areaCoords.y1/posRatio.y)+'px'}
var previewStyle=this.previewImg.style;previewStyle.width=calcPos.w;previewStyle.height=calcPos.h;previewStyle.left=calcPos.x;previewStyle.top=calcPos.y;}}});cropping=false;function enablecropping(){if(!cropping){cropping=true;myCropper=new Cropper.ImgWithPreview('picture',{minWidth:100,minHeight:100,ratioDim:{x:100,y:100},onEndCrop:cropphoto,previewWrap:'preview'});}}
function cropphoto(coords,dimensions){$('croppedbool').value="1";$('x1').value=coords.x1;$('y1').value=coords.y1;$('x2').value=coords.x2;$('y2').value=coords.y2;}