jQuery.fn.RotateZoom=function(r){var L;var A;var g;var e;var F;var P;var N;var M;var w;var y;var Q;var a;var f="An error occurred.";function i(S,R,T){return[Math.round(T[0]/(S[0]/R[0]/M.current)),Math.round(T[1]/(S[1]/R[1]/M.current))]}function s(S,R){var T;T=$(S).offset();if(L){return[N.current.x,N.current.y]}else{if(R){return[R.pageX-T.left,R.pageY-T.top]}else{return[(e.dimensions.small[0]/2),(e.dimensions.small[1]/2)]}}}function h(R){var V,U,T,S;V=s(g,R);U=i(e.dimensions.small,e.dimensions.large,V);T=(0-(U[1]-V[1]))+"px";S=(0-(U[0]-V[0]))+"px";return[S,T]}function d(R){coords=h(R);e.css({top:coords[1],left:coords[0]});E(R)}function G(S,R){$.each(R,function T(U){S.addEventListener(U,this,false)})}function E(R){R.cancelable&&R.preventDefault();return false}function p(R){if(R>0){return 1}else{if(R<0){return -1}else{return 0}}}function H(R){E(R);if(M.current==0){if(L){if(R.touches.length==1){w.delta.x=0;w.delta.index=0;w.delta.previoustouchx=0;v(R);F.pause=setTimeout(function(){w.paused=true},80)}}else{v(R);F.pause=setTimeout(function(){w.paused=true},100)}}else{if(L){N.zooming=true;N.previous.x=N.current.x;N.previous.y=N.current.y}}if(L){if(R.touches.length>1){N.start.x=(R.touches[0].screenX+R.touches[1].screenX)/2;N.start.y=(R.touches[0].screenY+R.touches[1].screenY)/2}else{N.start.x=R.touches[0].screenX;N.start.y=R.touches[0].screenY}}}function n(R){E(R);if(M.current==0){if(w.rotating==true){clearTimeout(F.pause);if(L){if(w.paused){N.dragcount++;if(N.dragcount>3){N.dragcount=0;w.paused=false}}w.delta.x=R.touches[0].screenX-w.start.x;w.delta.index=Math.round(parseFloat(w.delta.x)/w.speed);w.delta.previoustouchx=w.delta.index;l(w.start.index+w.delta.index);F.pause=setTimeout(function(){N.dragcount=0;w.paused=true},80)}else{w.paused=false;w.delta.x=R.pageX-w.start.x;w.delta.index=Math.round(parseFloat(w.delta.x)/w.speed);l(w.start.index+w.delta.index);F.pause=setTimeout(function(){w.paused=true},100)}}}else{if(L){if(N.zooming){if(R.touches.length>1){N.current.x=((N.start.x-((R.touches[0].screenX+R.touches[1].screenX)/2))+N.previous.x);N.current.y=((N.start.y-((R.touches[0].screenY+R.touches[1].screenY)/2))+N.previous.y)}else{N.current.x=((N.start.x-R.touches[0].screenX)+N.previous.x);N.current.y=((N.start.y-R.touches[0].screenY)+N.previous.y)}B(R)}}}}function I(R){E(R);if(M.current==0){if(w.rotating){clearTimeout(F.pause);K();if(w.paused!=true){m(R)}w.paused=false}}else{if(L){N.zooming=false}}}function D(R,S){E(R);if(S>0){if(M.current>0){if(M.current<M.maximum){M.current=Math.round(M.current*M.multiplier*100)/100;B(R)}}else{M.current=M.minimum;u(w.current.index);B(R)}}else{if(M.current>0){if(M.current<=M.minimum){j()}else{M.current=Math.round(M.current/M.multiplier*100)/100;B(R)}}}}function J(R){E(R);K();if(M.current==0){N.previous.x=e.dimensions.small[0]/2;N.previous.y=e.dimensions.small[1]/2;N.current.x=N.previous.x;N.current.y=N.previous.y}N.zooming=true;M.touchstart=M.current}function O(R){E(R);if(M.current==0){if(R.scale>1){M.current=M.minimum;u(w.current.index);B(R)}}else{if(N.zooming){M.current=M.touchstart+(R.scale-1)/P.touchzoomfactor;if(M.current>M.maximum){M.current=M.maximum}if(M.current<M.minimum){M.current=M.minimum}B(R)}}}function q(R){E(R);N.zooming=false;if(M.current<=M.minimum){j()}}function u(R){b();w.rotating=false;w.slowing=false;$(".rotate").hide();if(L){Q.html("Pan")}g.bind("mousemove",d);if(e.images.large[R]&&e.images.large[R].loaded_==true){e.attr("src",e.srcs.large[R])}else{y.show();if(!(e.images.large[R]&&e.images.large[R].loading_==true)){e.images.large[R]=new Image();e.images.large[R].loading_=true;e.images.large[R].onload=function(S){e.images.large[R].loaded_=true;e.images.large[R].loading_=false;if(w.current.index==R){y.hide();e.attr("src",e.srcs.large[R])}};e.images.large[R].onerror=function(S){this.loading_=false};e.images.large[R].src=e.srcs.large[R]}}}function B(R){var S;S=h(R);e.css({top:S[1],left:S[0],width:e.dimensions.large[0]*M.current,height:e.dimensions.large[1]*M.current})}function j(){M.current=0;e.css({top:"0px",left:"0px",width:e.dimensions.small[0],height:e.dimensions.small[1]});e.attr("src",e.srcs.small[w.current.index]);g.unbind("mousemove",d);y.hide();$(".rotate").show();if(L){Q.html("Rotate")}}function v(R){b();w.slowing=false;w.paused=false;w.rotating=true;if(L){w.start.x=R.touches[0].screenX}else{w.start.x=R.pageX}w.start.index=w.current.index}function K(){b();w.rotating=false}function l(R){w.current.index=R;while(w.current.index>e.total-1){w.current.index=w.current.index-e.total}while(w.current.index<0){w.current.index=w.current.index+e.total}e.attr("src",e.srcs.small[w.current.index])}function m(R){if(L){w.delta.x=w.delta.previoustouchx;w.delta.index=Math.round(parseFloat(w.delta.x))}else{w.delta.x=R.pageX-w.start.x;w.delta.index=Math.round(parseFloat(w.delta.x/10))}w.slowdown.magnitude=Math.abs(w.delta.index);if(w.slowdown.magnitude>w.slowdown.cutoff){F.slowdown=setTimeout(x,Math.abs(w.slowdown.delayfactor/w.slowdown.magnitude));w.slowing=true}}function x(){clearTimeout(F.slowdown);if(w.slowing==true){w.slowdown.magnitude=w.slowdown.magnitude-1;l(w.current.index+p(w.delta.x));if(w.slowdown.magnitude>w.slowdown.cutoff-1){F.slowdown=setTimeout(x,Math.abs(w.slowdown.delayfactor/w.slowdown.magnitude))}else{w.slowing=false}}}function C(R){e.loadcount+=1;if(e.failcount==0){$("#rotatezoom-loading-inner").css("width",((P.loadingwidth/e.total)*e.loadcount).toString()+"px");if(e.loadcount==e.total){if(P.showanimations==true){$("#rotatezoom-loading").fadeOut("fast",function(){$(this).remove();e.fadeIn("fast",function(){A.animate({height:e.dimensions.small[1]+P.toolbar.height},function(){if(L){t();A.append(toolbar);toolbar.fadeIn("fast")}else{o();A.append(toolbar);toolbar.fadeIn("fast",function(){g.bind("mousedown",function(S){H(S)}).bind("mousemove",function(S){n(S)}).bind("mouseup",function(S){I(S)}).bind("mouseleave",function(S){I(S)}).bind("mousewheel",function(S,T){D(S,T)})})}})})})}else{$("#rotatezoom-loading").remove();e.fadeIn();A.css({height:e.dimensions.small[1]+P.toolbar.height});if(L){t();A.append(toolbar);toolbar.show()}else{o();A.append(toolbar);toolbar.show();g.bind("mousedown",function(S){H(S)}).bind("mousemove",function(S){n(S)}).bind("mouseup",function(S){I(S)}).bind("mouseleave",function(S){I(S)}).bind("mousewheel",function(S,T){D(S,T)})}}}}}function z(R){e.failcount+=1;if(e.failcount==1){$("#rotatezoom-loading").hide();A.append('<div class="error">'+f+"</div>")}}function b(){clearTimeout(F.slowdown);clearTimeout(F.pause);clearTimeout(F.autorotation)}function o(){toolbar=$('<div class="toolbar"></div>');toolbar.css("height",P.toolbar.height);var R=$('<div class="right zoomout"></div>');var U=$('<div class="right zoomin"></div>');var T=$('<div class="right rotate rotateright"></div>');var S=$('<div class="right rotate rotateleft"></div>');U.click(function(){if(M.current>0){if(M.current<M.maximum){M.current=Math.round(M.current*M.multiplier*100)/100;B()}}else{M.current=M.minimum;u(w.current.index);B()}});R.click(function(){if(M.current>0){if(M.current<=M.minimum){j()}else{M.current=Math.round(M.current/M.multiplier*100)/100;B()}}});T.mousedown(function(){b();k(1)}).mouseup(function(){b()}).mouseleave(function(){c()});S.mousedown(function(){b();k(-1)}).mouseup(function(){b()}).mouseleave(function(){c()});toolbar.append(R);toolbar.append(U);toolbar.append(T);toolbar.append(S);toolbar.append(y);toolbar.append('<div style="clear:both"></div>')}function t(){toolbar=$('<div class="toolbar touchy"></div>');toolbar.css("height",P.toolbar.height);toolbar.append(y);Q=$('<div class="touchdrag">Rotate</div>');a=$('<div class="touchpinch">Zoom</div>');toolbar.append(Q);toolbar.append(a);toolbar.append('<div style="clear:both"></div>')}function k(R){l(w.current.index+R);F.autorotation=setInterval(function(){l(w.current.index+R)},60)}function c(){clearTimeout(F.autorotation)}return this.each(function(){A=$(this);A.bind("contextmenu",function(T){return false});document.ondragstart=function(T){return false};L=(/iphone|ipod|ipad|android/i).test(navigator.userAgent);if(r==null||r.urlssmall==null||r.urlslarge==null||r.widthsmall==null||r.widthlarge==null||r.heightlarge==null){A.append('<div class="error">'+f+"</div>");return}M={current:0,minimum:0.16,maximum:1,multiplier:1.3};w={start:new Object(),delta:new Object(),current:{index:(r.startindex!=null)?r.startindex:0},paused:false,rotating:false,slowing:false,speed:8,slowdown:{cutoff:4,delayfactor:500,magnitude:0}};F={slowdown:null,pause:null,autorotation:null};e=$('<img src="'+r.urlssmall[w.current.index]+'" />');e.total=r.urlssmall.length;e.loadcount=0;e.failcount=0;e.srcs={small:r.urlssmall,large:r.urlslarge};e.dimensions={small:[r.widthsmall,r.heightsmall],large:[r.widthlarge,r.heightlarge]};e.images={small:new Array(r.urlssmall.length),large:new Array(r.urlslarge.length)};N={previous:new Object(),start:new Object(),current:new Object(),dragcount:0};P=new Object();P.loadingwidth=Math.round(e.dimensions.small[0]*0.4);P.loadingheight=Math.round((e.dimensions.small[1]/2)-6);P.touchzoomfactor=2;if(L){P.toolbar={height:53}}else{P.toolbar={height:33}}P.showanimations=(r.animations!=null)?r.animations:true;g=$('<div class="imagecontainer"></div>');g.css("width",e.dimensions.small[0]);g.css("height",e.dimensions.small[1]);A.append(g);var S=$('<div id="rotatezoom-loading"></div>');S.append('<div id="rotatezoom-loading-inner"></div>');S.css({width:P.loadingwidth+"px",margin:P.loadingheight+"px "+(((e.dimensions.small[0]-P.loadingwidth)/2)-2)+"px "+P.loadingheight+"px "+(((e.dimensions.small[0]-P.loadingwidth)/2)-2)+"px"});g.append(S);if(L){y=$('<div class="spinner-touch"></div>')}else{y=$('<div class="spinner">Loading...</div>')}for(var R=0;R<e.total;R++){e.images.small[R]=new Image(e.dimensions.small[0],e.dimensions.small[1]);e.images.small[R].onload=function(){C()};e.images.small[R].onerror=function(){z()};e.images.small[R].src=e.srcs.small[R]}g.append(e);if(L){M.minimum=e.dimensions.small[0]/e.dimensions.large[0];G(this,{touchstart:H,touchmove:n,touchend:I,touchcancel:I,click:E,gesturestart:J,gesturechange:O,gestureend:q})}})};
