/**
 * ##########################################################################################
 * Image zoom (magnification) plugin for jQuery library http://alexeyenko.net/projects/zooma/
 *
 * Thanks to Justin Robert Wehrman (jwehrman@exertionist.com),
 * the developer of 'imageDetail' plugin http://exertionist.com/ImageDetail/
 *
 * Zooma jquery plugin page http://plugins.jquery.com/project/zooma
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * v1.2.3 December  2009 - Fixed viefinder issues when zoom thumb is placed in absolutely positioned blocks. viewfinder element now must be placed in the root of html document
 * v1.2.2 November  2009 - Fixed exit if zoom viewfinder has same width/height as original image
 * v1.2.1 October   2009 - Added loading... indicator. Fixed bug when large image is smaller then zoom div.
 * v1.1.1 October   2009 - Viewfinder block aligment was updated.
 * v1.1.0 October   2009 - Added viewfinder box. Code refactor (code becomes Object oriented).
 * v1.0.1 October   2009 - Zoom becomes faster: better mouse events management.
 * v1.0.0 October   2009.
 * author: Ignat Alexeyenko, http://alexeyenko.net
 * ##########################################################################################
 */
(function(f){function C(){this.viewfinderBorderRight=this.viewfinderBorderBottom=this.viewfinderBorderLeft=this.viewfinderBorderTop=this.thumbImgOffsetBottom=this.thumbImgOffsetTop=this.thumbImgOffsetRight=this.thumbImgOffsetLeft=this.thumbImgPositionBottom=this.thumbImgPositionTop=this.thumbImgPositionRight=this.thumbImgPositionLeft=this.heightDiff=this.widthDiff=this.halfViewfinderHeight=this.halfViewfinderWidth=this.viewfinderHeight=this.viewfinderWidth=this.height=this.width=0;this.loaded=false; this.getHeight=function(){return this.height};this.getWidth=function(){return this.width};this.isLoaded=function(){return this.loaded}}function D(c,i){var e=new C,g=new Image;f(g).load(function(){e.width=g.width;e.height=g.height;e.loaded=true;i(e)});f(g).attr("src",c);return e}function v(c,i,e,g){i=i+g;if(c<i)return i;e=e-g;if(c>e)return e;return c}f.fn.zooma=function(c,i,e){function g(a){if(w&&(a.pageX<q||a.pageX>x||a.pageY<r||a.pageY>y)){f(document).unbind("mousemove",g);h.hide();d.hide()}else E(a)} function z(){w=true;if(!s){h.show();d.show();s=true;f(document).bind("mousemove",g)}}function E(a){if(b.isLoaded()){var m=a.pageY,j=b.viewfinderBorderTop+b.viewfinderBorderBottom;a=v(a.pageX,b.thumbImgOffsetLeft,b.thumbImgOffsetRight-(b.viewfinderBorderLeft+b.viewfinderBorderRight),b.halfViewfinderWidth);m=v(m,b.thumbImgOffsetTop,b.thumbImgOffsetBottom-j,b.halfViewfinderHeight);j=Math.round(a-b.halfViewfinderWidth);var n=Math.round(m-b.halfViewfinderHeight);d.css("left",j);d.css("top",n);n=j=0;n= t?Math.round((F-b.getWidth())/2):Math.round((Math.round(a)-b.thumbImgOffsetLeft)*-b.widthDiff+G);j=u?Math.round((H-b.getHeight())/2):Math.round((Math.round(m)-b.thumbImgOffsetTop)*-b.heightDiff+I);h.css("backgroundPosition",n+"px "+j+"px")}}f.extend(this,{deactivate:function(){h.hide();d.hide();f(document).unbind("mousemove",g)},activate:function(){f(h).css({backgroundImage:"url("+e+")",backgroundRepeat:"no-repeat",backgroundPosition:"50% 50%"});h.show();s=false;A.find("a").hover(function(){z()}, function(){});d.hover(function(){z()},function(){})}});var s=false,A=f("#"+c),h=f("#"+i);c=f(A).find("img");var k=c.width(),l=c.height(),B=h.find("div.zoomLoading"),o=h.width(),p=h.height(),F=h.width(),H=h.height(),G=o/2,I=p/2,J=c.position().left,K=c.position().left+k,L=c.position().top,M=c.position().top+l,q=c.offset().left,x=c.offset().left+k,r=c.offset().top,y=c.offset().top+l,d=f("#zoomViewfinder"),w=false,t=false,u=false,b=new D(e,function(a){a.viewfinderBorderTop=parseInt(d.css("borderTopWidth")); a.viewfinderBorderLeft=parseInt(d.css("borderLeftWidth"));a.viewfinderBorderBottom=parseInt(d.css("borderBottomWidth"));a.viewfinderBorderRight=parseInt(d.css("borderRightWidth"));if(a.getWidth()!=0&&a.getHeight()!=0){if(o>a.getWidth()){o=a.getWidth();t=true}if(p>a.getHeight()){p=a.getHeight();u=true}a.viewfinderWidth=Math.round(k*o/a.getWidth());a.viewfinderHeight=Math.round(l*p/a.getHeight());if(t)a.viewfinderWidth-=a.viewfinderBorderLeft+a.viewfinderBorderRight;if(u)a.viewfinderHeight-=a.viewfinderBorderTop+ a.viewfinderBorderBottom;a.halfViewfinderWidth=a.viewfinderWidth/2;a.halfViewfinderHeight=a.viewfinderHeight/2;a.widthDiff=a.getWidth()/k;a.heightDiff=a.getHeight()/l}a.thumbImgPositionLeft=J;a.thumbImgPositionRight=K;a.thumbImgPositionTop=L;a.thumbImgPositionBottom=M;a.thumbImgOffsetLeft=q;a.thumbImgOffsetRight=x;a.thumbImgOffsetTop=r;a.thumbImgOffsetBottom=y;d.css("width",a.viewfinderWidth);d.css("height",a.viewfinderHeight);d.css("left",q+Math.round(k/2-(a.viewfinderWidth+a.viewfinderBorderLeft+ a.viewfinderBorderRight)/2));d.css("top",r+Math.round(l/2-(a.viewfinderHeight+a.viewfinderBorderTop+a.viewfinderBorderBottom)/2));d.show();B.hide()});b.isLoaded()||B.show();return this}})(jQuery);
