var JSImageCouch=Class.create();JSImageCouch.prototype={initialize:function(k,c,j,g,d,b,a,e,i,h,f){this.jsessionid=k;this.isWebKit=/Konqueror|Safari|KHTML/.test(navigator.userAgent);this.recordView=c;this.catalogID=j;this.recordID=g;this.processID=d;this.actualImageSizeWidth=b;this.actualImageSizeHeight=a;this.displayImageSize=e;this.imageContainer=$(i);this.imageCouchSlider=h;this.imageCouchSlider.setListener(function(l){if(!this.inScaleUpdate){this.inScaleUpdate=true;this.displayImageSize=Math.floor((l/100)*Math.max(this.actualImageSizeWidth,this.actualImageSizeHeight));this.updateImage();this.inScaleUpdate=false}}.bind(this));this.updateScaleSlider();this.imageCouchImageWrapper=f;this.showCropper=false;this.imageContainer.onload=this.onLoadImage.bindAsEventListener(this);Event.observe(this.imageContainer,"mouseup",this.onMouseUp.bindAsEventListener(this));Event.observe(this.imageContainer,"mousedown",this.onMouseDown.bindAsEventListener(this));this.updateImage();this.clearSelectionUI();this.myWorkHandlers={onCreate:this.showWorker.bind(this),onComplete:this.hideWorker.bind(this)};Ajax.Responders.register(this.myWorkHandlers)},openBox:function(){this.disableCropper();if(this.isPreview){this.cancelCurrentPreview()}else{this.updateImage()}},updateImage:function(){this.imageContainer.src="Preview.PreviewServlet;jsessionid="+this.jsessionid+"?imageSize="+this.displayImageSize+"&recordView="+this.recordView+"&catalogID="+this.catalogID+"&recordID="+this.recordID+"&processID="+this.processID+"&random="+new Date().getTime()},updateScaleSlider:function(){var a=(this.displayImageSize/Math.max(this.actualImageSizeWidth,this.actualImageSizeHeight))*100;this.imageCouchSlider.sliderValueElement.slider.setValue(a)},clearSelectionUI:function(){this.selectionX=undefined;this.selectionY=undefined;this.selectionWidth=undefined;this.selectionHeight=undefined;this.selectionUnit=undefined;this.selectionDPI=undefined},disableCropper:function(){this.showCropper=false;this.removeCropper();this.clearSelectionUI();this.cancelCurrentPreview()},setCropMode:function(f,e,c,b,d,a){this.showCropper=true;if(this.isPreview){this.cancelCurrentPreview()}else{this.updateImage()}this.selectionX=f;this.selectionY=e;this.selectionWidth=c;this.selectionHeight=b;this.selectionUnit=d;this.selectionDPI=a},convertSelectionToPhysicalDimensions:function(g,f,d,c){var a={x:g,y:f,width:d,height:c};if(this.selectionUnit){var e=$F(this.selectionUnit);if("cm"==e||"in"==e){var b=Number($F(this.selectionDPI));a.x*=b;a.y*=b;a.width*=b;a.height*=b;if("cm"==e){a.x/=2.54;a.y/=2.54;a.width/=2.54;a.height/=2.54}}}a.x=Math.floor(a.x);a.y=Math.floor(a.y);a.width=Math.floor(a.width);a.height=Math.floor(a.height);return a},setSelection:function(e,c,b,a){var f=this.convertSelectionToPhysicalDimensions(e,c,b,a);var d=(this.displayImageSize/Math.max(this.actualImageSizeWidth,this.actualImageSizeHeight));this.cropper.setAreaCoords({x1:Math.floor(f.x*d),y1:Math.floor(f.y*d),x2:Math.floor((f.x+f.width)*d),y2:Math.floor((f.y+f.height)*d)});this.cropper.selArea.show();this.cropper.drawArea();this.cropper.endCrop()},onLoadImage:function(){if(this.showCropper){if(this.imageContainer.complete||this.isWebKit){this.updateCropper()}else{window.setTimeout(function(){this.onLoadImage()}.bind(this),50)}}else{this.removeCropper()}},onMouseDown:function(b){var a=document.getElementById(this.imageCouchImageWrapper);this.startDragScrollTop=a.scrollTop;this.startDragScrollLeft=a.scrollLeft;this.startDragX=Event.pointerX(b);this.startDragY=Event.pointerY(b);Event.observe(this.imageContainer,"mousemove",this.onMouseMove.bindAsEventListener(this));Event.stop(b)},onMouseUp:function(a){Event.stopObserving(this.imageContainer,"mousemove")},onMouseMove:function(b){var a=document.getElementById(this.imageCouchImageWrapper);a.scrollTop=this.startDragScrollTop-(Event.pointerY(b)-this.startDragY);a.scrollLeft=this.startDragScrollLeft-(Event.pointerX(b)-this.startDragX);Event.stop(b)},updateCropper:function(){this.removeCropper();if(this.showCropper&&(this.imageContainer.complete||this.isWebKit)){this.cropper=new Cropper.Img(this.imageContainer,{onEndCrop:this.updateSelection.bind(this)})}},removeCropper:function(){if(this.cropper){this.cropper.remove()}this.cropper=undefined},updateSelection:function(i,a){var c=1/(this.displayImageSize/Math.max(this.actualImageSizeWidth,this.actualImageSizeHeight));var e=Math.floor(i.x1*c);var d=Math.floor(i.y1*c);var f=Math.floor(a.width*c);var h=Math.floor(a.height*c);if(this.selectionUnit){var g=$F(this.selectionUnit);if("cm"==g||"in"==g){var b=Number($F(this.selectionDPI));e/=b;d/=b;f/=b;h/=b;if("cm"==g){e*=2.54;d*=2.54;f*=2.54;h*=2.54}e=e.toFixed(3);d=d.toFixed(3);f=f.toFixed(3);h=h.toFixed(3)}}if(this.selectionX){this.selectionX.value=e}if(this.selectionY){this.selectionY.value=d}if(this.selectionWidth){this.selectionWidth.value=f}if(this.selectionHeight){this.selectionHeight.value=h}},updateLocalImageSize:function(e){this.actualImageSizeWidth=e.getAttribute("width");this.actualImageSizeHeight=e.getAttribute("height");this.displayImageSize=Math.floor((this.imageCouchSlider.sliderValueElement.slider.value/100)*Math.max(this.actualImageSizeWidth,this.actualImageSizeHeight));var d=$("imageCouchImageDetails");if(d){var a=d.innerHTML;var c=a.substring(a.indexOf(" "));var b=this.actualImageSizeWidth+"x"+this.actualImageSizeHeight;b+=c;d.innerHTML=b}},crop:function(b,f,c,a){var e=this.convertSelectionToPhysicalDimensions(b,f,c,a);var d="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=crop&convertParam="+e.x+":"+e.y+":"+e.width+":"+e.height;app.doRequest("imagecouch.jspx",d,function(g){this.updateLocalImageSize(g.responseXML.documentElement);this.updateImage();this.updateCropper()}.bind(this))},resize:function(b,a){var c="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=resize&convertParam="+$F(b)+":"+$F(a);app.doRequest("imagecouch.jspx",c,function(d){this.updateLocalImageSize(d.responseXML.documentElement);this.updateImage()}.bind(this))},keepRatio:function(c,d,b,a){if(c){a.value=parseInt(b*d,10)}},rotate:function(b){var a="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=rotate&convertParam="+b;app.doRequest("imagecouch.jspx",a,function(c){this.updateLocalImageSize(c.responseXML.documentElement);this.updateImage()}.bind(this))},flip:function(a){var b="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=flip&convertParam="+a;app.doRequest("imagecouch.jspx",b,function(c){this.updateLocalImageSize(c.responseXML.documentElement);this.updateImage()}.bind(this))},adjustBrightnessContrast:function(c,a,e,b){var d="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=brightnesscontrast&convertParam="+$F(c)+":"+$F(a)+":"+($F(e)?"true":"false")+":"+(b?"true":"false");if(b){this.isPreview=true}app.doRequest("imagecouch.jspx",d,function(){this.updateImage()}.bind(this))},adjustColors:function(b){var c="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=colors&convertParam="+(b?"true":"false");var a=$A(arguments);a.shift();a.each(function(d){c+=":"+$F(d)});if(b){this.isPreview=true}app.doRequest("imagecouch.jspx",c,function(){this.updateImage()}.bind(this))},adjustGamma:function(a,d,b){var c="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=gamma&convertParam="+$F(a)+":"+($F(d)?"true":"false")+":"+(b?"true":"false");if(b){this.isPreview=true}app.doRequest("imagecouch.jspx",c,function(){this.updateImage()}.bind(this))},filter:function(b,a,e,c){var d="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=filter&convertParam="+b+":"+a+":"+e+":"+(c?"true":"false");if(c){this.isPreview=true}app.doRequest("imagecouch.jspx",d,function(){this.updateImage()}.bind(this))},cancelCurrentPreview:function(){if(this.isPreview){var a="recordView="+this.recordView+"&processID="+this.processID+"&convertModule=cancelPreview";app.doRequest("imagecouch.jspx",a,function(){this.updateImage()}.bind(this));this.isPreview=undefined}},save:function(b,f,d){var g="recordView="+this.recordView+"&processID="+this.processID+"&encoding=UTF-8&convertModule=save&convertParam="+encodeURIComponent(f);g+=":"+encodeURIComponent(b||" ");g+=":"+(d?"true":"false");var e=$("save"+f+"ColorSpaceChooser");if(e){g+=":"+encodeURIComponent($F(e))}else{g+=":%20"}var a=$("save"+f+"JPEGQualityChooser");if(a){g+=":"+encodeURIComponent($F(a).substring(1))}else{g+=":0"}var c=$("save"+f+"CompressionChooser");if(c){g+=":"+encodeURIComponent($F(c))}else{g+=":none"}app.doRequest("imagecouch.jspx",g,function(h){app.hidePopupWindow();app.updateDownloadState()}.bind(this))},updateFilename:function(a,d){var c=a.value;if(c){var b=c;var e="."+d.toLowerCase();var f=c.lastIndexOf(".");if(f>-1){if(c.length-f<=5){b=b.substring(0,f)}b+=e}else{b+=e}a.value=b}},showWorker:function(){$("imageCouchWorkIndicator").src=this.currentTheme+"/images/working.gif"},hideWorker:function(){if(Ajax.activeRequestCount==0){$("imageCouchWorkIndicator").src=this.currentTheme+"images/transparent.gif"}},cancel:function(){Ajax.Responders.unregister(this.myWorkHandlers);this.removeCropper()}};var ImageCouchSlider=Class.create();ImageCouchSlider.prototype={initialize:function(a,c,b,d,e){this.sliderValueElement=$(a+"Value");this.sliderValueElement.slider=new Control.Slider(a+"SliderKnob",a+"SliderTrack",{range:c,sliderValue:b,onSlide:function(f){this.sliderValueElement.value=(d?Math.floor(f):f.toFixed(2))+(e||"")}.bind(this),onChange:function(f){this.sliderValueElement.value=(d?Math.floor(f):f.toFixed(2))+(e||"");if(this.listener){this.listener.apply(this,[f])}}.bind(this)});this.sliderValueElement.slider.setValue(b);this.sliderValueElement.imageCouchSlider=this},setValue:function(a,b){if(a.endsWith(b)){a=a.substring(0,a.lastIndexOf(b))}this.sliderValueElement.slider.setValue(Number(a))},setListener:function(a){if(typeof a=="function"){this.listener=a}}};
