(function($){$.fn.cdsZoom=function(options){var settings={selectorZoomWindow:$("body"),classZoomWindow:"cdsZoomWindow",classZoomCursorZone:"cdsZoomCursorZone",opacityCursorZone:.5};options=options||{};$.extend(settings,options);var LARGE_IMAGE_LOADED="largeLoaded";function MainImage(img){this.node=img[0];var imageWidth=0,imageHeight=0,imageOuterWidth=0,imageOuterHeight=0,$img=img;this.getWidth=function(){return imageWidth};this.getHeight=function(){return imageHeight};this.getOuterWidth=function(){return imageOuterWidth};this.getOuterHeight=function(){return imageOuterHeight};this.getOffset=function(){return $img.offset()};function initValues(){imageWidth=$img.width();imageHeight=$img.height();imageOuterWidth=$img.outerWidth();imageOuterHeight=$img.outerHeight()}this.loadImage=function(){this.node.src=img[0].src;initValues();return this};this.node.onload=function(){initValues()};return this}function LargeImage($zoomHref){this.node=new Image;var imageWidth=0,imageHeight=0,contentWidth=0,contentHeight=0;this.getWidth=function(){return imageWidth};this.getHeight=function(){return imageHeight};this.getContentWidth=function(){return contentWidth};this.getContentHeight=function(){return contentHeight};this.loadImage=function(imageUrl){$("<div/>").appendTo(settings.selectorZoomWindow).addClass(settings.classZoomWindow).css("overflow","hidden").append(this.node);this.node.style.position="absolute";this.node.src=imageUrl};this.node.onload=function(){var $this=$(this);imageWidth=$this.width();imageHeight=$this.height();contentWidth=$this.parent().width();contentHeight=$this.parent().height();$zoomHref.trigger(LARGE_IMAGE_LOADED)};this.move=function(x,y){$(this.node).css("left",x).css("top",y)};this.remove=function(){$(this.node).parent().remove();isImageLoaded=false};return this}function CursorZone(cursorWidth,cursorHeight,$parentElement){this.node=document.createElement("div");var contentWidth=cursorWidth,contentHeight=cursorHeight,outerWidth=0,outerHeight=0;this.getWidth=function(){return contentWidth};this.getHeight=function(){return contentHeight};this.getOuterWidth=function(){return outerWidth};this.getOuterHeight=function(){return outerHeight};$(this.node).css("position","absolute").width(cursorWidth).height(cursorHeight).addClass(settings.classZoomCursorZone).css("opacity",settings.opacityCursorZone).appendTo($parentElement);outerWidth=$(this.node).outerWidth();outerHeight=$(this.node).outerHeight();isCursorLoaded=true;this.changeSize=function(newCursorWidth,newCursorHeight){contentWidth=newCursorWidth;contentHeight=newCursorHeight;$(this.node).width(newCursorWidth).height(newCursorHeight);outerWidth=$(this.node).outerWidth();outerHeight=$(this.node).outerHeight();isCursorLoaded=true;return this};this.hide=function(){$(this.node).hide()};this.show=function(){$(this.node).show()};this.move=function(x,y){$(this.node).css("left",x).css("top",y)};return this}return this.each(function(){var $this=$(this),cursorZone=null,largeImage=new LargeImage($(this)),mainImage=new MainImage($("img",this)),ratioWitdth=0,ratioHeight=0,mouseX=0,mouseY=0;mainImage.loadImage();var largeImageLimits=[0,0,0,0],cursorZoneLimits=[0,0,0,0];$(this).bind(LARGE_IMAGE_LOADED,function(){largeImageLimits[1]=largeImage.getContentWidth()-largeImage.getWidth();largeImageLimits[3]=largeImage.getContentHeight()-largeImage.getHeight();var cursorZoneWidth=Math.min(Math.ceil(mainImage.getWidth()/largeImage.getWidth()*largeImage.getContentWidth()),mainImage.getWidth()),cursorZoneHeight=Math.min(Math.ceil(mainImage.getHeight()/largeImage.getHeight()*largeImage.getContentHeight()),mainImage.getHeight());cursorZone=cursorZone==null?new CursorZone(cursorZoneWidth,cursorZoneHeight,$this):cursorZone.changeSize(cursorZoneWidth,cursorZoneHeight);cursorZoneLimits[1]=mainImage.getOuterWidth()-cursorZone.getOuterWidth();cursorZoneLimits[3]=mainImage.getOuterHeight()-cursorZone.getOuterHeight();ratioWitdth=largeImage.getWidth()/mainImage.getWidth();ratioHeight=largeImage.getHeight()/mainImage.getHeight();updateZoomPostions(mouseX,mouseY);updateCursorZonePostions(mouseX,mouseY)});function updateZoomPostions(cursorX,cursorY){cursorX=cursorX-mainImage.getOffset().left;cursorY=cursorY-mainImage.getOffset().top;var largeImageX=Math.ceil(largeImage.getContentWidth()/2-ratioWitdth*cursorX);if(largeImageX>largeImageLimits[0])largeImageX=largeImageLimits[0];else if(largeImageX<largeImageLimits[1])largeImageX=largeImageLimits[1];var largeImageY=Math.ceil(largeImage.getContentHeight()/2-cursorY*ratioHeight);if(largeImageY>largeImageLimits[2])largeImageY=largeImageLimits[2];else if(largeImageY<largeImageLimits[3])largeImageY=largeImageLimits[3];largeImage.move(largeImageX,largeImageY)}function updateCursorZonePostions(cursorX,cursorY){cursorX-=mainImage.getOffset().left;cursorY-=mainImage.getOffset().top;if(cursorZone!=null){var cursorZoneX=Math.round(cursorX-cursorZone.getWidth()/2);if(cursorZoneX<cursorZoneLimits[0])cursorZoneX=cursorZoneLimits[0];else if(cursorZoneX>cursorZoneLimits[1])cursorZoneX=cursorZoneLimits[1];var cursorZoneY=Math.round(cursorY-cursorZone.getHeight()/2);if(cursorZoneY<cursorZoneLimits[2])cursorZoneY=cursorZoneLimits[2];else if(cursorZoneY>cursorZoneLimits[3])cursorZoneY=cursorZoneLimits[3];cursorZone.move(cursorZoneX,cursorZoneY)}}$this.hover(function(e){mouseX=e.pageX;mouseY=e.pageY;cursorZone!=null&&cursorZone.show();largeImage.loadImage($this.attr("href"))},function(){largeImage.remove();cursorZone!=null&&cursorZone.hide()});$this.mousemove(function(e){mouseX=e.pageX;mouseY=e.pageY;updateZoomPostions(mouseX,mouseY);updateCursorZonePostions(mouseX,mouseY)})});return this}})(jQuery);
