/**
 * ##########################################################################################
 * 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.3.2 January   2009 - Fixed deactivate method
 * v1.3.1 January   2009 - Added jQuery v 1.4 support
 * v1.3.0 January   2009 - Added ability to switch auto-hide off (added optional settings parameter), exitCallback function was added to settings
 * v1.2.3 December  2009 - Fixed viewfinder 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 alignment 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(h){function D(){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 E(b,d){var f=new D,g=new Image;h(g).load(function(){f.width=g.width;f.height=g.height;f.loaded=true;d(f)});h(g).attr("src",b);return f}function x(b,d,f,g){d=d+g;if(b<d)return d;f=f-g;if(b>f)return f;return b}function F(b){if(b==null||typeof b==undefined)b={};if(b.autoDeactivate==null||typeof b.autoDeactivate==undefined)b.autoDeactivate=true;if(b.zoomViewfinder== null||typeof b.zoomViewfinderName==undefined)b.zoomViewfinderName="zoomViewfinder";if(b.exitCallback==null||typeof b.exitCallback==undefined)b.exitCallback=function(){};return b}h.fn.zooma=function(b,d,f,g){function r(a){var j=a.pageX<s||a.pageX>y||a.pageY<t||a.pageY>z;if(G&&A&&j){h(document).unbind("mousemove",r);i.hide();e.hide();H()}else j||I(a)}function n(){A=true;if(!u){i.show();e.show();u=true;h(document).bind("mousemove",r)}}function I(a){if(c.isLoaded()){var j=a.pageY,k=c.viewfinderBorderTop+ c.viewfinderBorderBottom;a=x(a.pageX,c.thumbImgOffsetLeft,c.thumbImgOffsetRight-(c.viewfinderBorderLeft+c.viewfinderBorderRight),c.halfViewfinderWidth);j=x(j,c.thumbImgOffsetTop,c.thumbImgOffsetBottom-k,c.halfViewfinderHeight);k=Math.round(a-c.halfViewfinderWidth);var o=Math.round(j-c.halfViewfinderHeight);e.css("left",k);e.css("top",o);o=k=0;o=v?Math.round((J-c.getWidth())/2):Math.round((Math.round(a)-c.thumbImgOffsetLeft)*-c.widthDiff+K);k=w?Math.round((L-c.getHeight())/2):Math.round((Math.round(j)- c.thumbImgOffsetTop)*-c.heightDiff+M);i.css("backgroundPosition",o+"px "+k+"px")}}h.extend(this,{deactivate:function(){i.hide();e.hide();h(document).unbind("mousemove",r);B.unbind("mouseover",n);e.unbind("mouseover",n)},activate:function(){h(i).css({backgroundImage:"url("+f+")",backgroundRepeat:"no-repeat",backgroundPosition:"50% 50%"});i.show();u=false;B.bind("mouseover",n);e.bind("mouseover",n)}});var u=false;b=h("#"+b);var i=h("#"+d),B=b.find("a");d=b.find("img");var l=d.width(),m=d.height(),C= i.find("div.zoomLoading"),p=i.width(),q=i.height(),J=i.width(),L=i.height(),K=p/2,M=q/2,N=d.position().left,O=d.position().left+l,P=d.position().top,Q=d.position().top+m,s=d.offset().left,y=d.offset().left+l,t=d.offset().top,z=d.offset().top+m,A=false,v=false,w=false;g=F(g);var G=g.autoDeactivate,H=g.exitCallback,e=h("#"+g.zoomViewfinderName),c=new E(f,function(a){a.viewfinderBorderTop=parseInt(e.css("borderTopWidth"));a.viewfinderBorderLeft=parseInt(e.css("borderLeftWidth"));a.viewfinderBorderBottom= parseInt(e.css("borderBottomWidth"));a.viewfinderBorderRight=parseInt(e.css("borderRightWidth"));if(a.getWidth()!=0&&a.getHeight()!=0){if(p>a.getWidth()){p=a.getWidth();v=true}if(q>a.getHeight()){q=a.getHeight();w=true}a.viewfinderWidth=Math.round(l*p/a.getWidth());a.viewfinderHeight=Math.round(m*q/a.getHeight());if(v)a.viewfinderWidth-=a.viewfinderBorderLeft+a.viewfinderBorderRight;if(w)a.viewfinderHeight-=a.viewfinderBorderTop+a.viewfinderBorderBottom;a.halfViewfinderWidth=a.viewfinderWidth/2;a.halfViewfinderHeight= a.viewfinderHeight/2;a.widthDiff=a.getWidth()/l;a.heightDiff=a.getHeight()/m}a.thumbImgPositionLeft=N;a.thumbImgPositionRight=O;a.thumbImgPositionTop=P;a.thumbImgPositionBottom=Q;a.thumbImgOffsetLeft=s;a.thumbImgOffsetRight=y;a.thumbImgOffsetTop=t;a.thumbImgOffsetBottom=z;e.css("width",a.viewfinderWidth);e.css("height",a.viewfinderHeight);e.css("left",s+Math.round(l/2-(a.viewfinderWidth+a.viewfinderBorderLeft+a.viewfinderBorderRight)/2));e.css("top",t+Math.round(m/2-(a.viewfinderHeight+a.viewfinderBorderTop+ a.viewfinderBorderBottom)/2));e.show();C.hide()});c.isLoaded()||C.show();return this}})(jQuery);