/**
 * ##########################################################################################
 * 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.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($)
{function ImageData()
{this.width=0;this.height=0;this.viewfinderWidth=0;this.viewfinderHeight=0;this.halfViewfinderWidth=0;this.halfViewfinderHeight=0;this.widthDiff=0;this.heightDiff=0;this.thumbImgLeft=0;this.thumbImgRight=0;this.thumbImgTop=0;this.thumbImgBottom=0;this.viewfinderBorderTop=0;this.viewfinderBorderLeft=0;this.viewfinderBorderBottom=0;this.viewfinderBorderRight=0;this.loaded=false;this.getHeight=function()
{return this.height;},this.getWidth=function()
{return this.width;},this.isLoaded=function()
{return this.loaded;};}
function LoaderHeleper(src,calllbackFunction)
{var imageData=new ImageData();var imgToLoad=new Image();$(imgToLoad).load(function()
{imageData.width=imgToLoad.width;imageData.height=imgToLoad.height;imageData.loaded=true;calllbackFunction(imageData);});$(imgToLoad).attr('src',src);return imageData;}
function correctPositionByEpsilonBorder(position,min,max,epsilon)
{var allowedMin=min+epsilon;if(position<allowedMin)
{return allowedMin;}
var allowedMax=max-epsilon;if(position>allowedMax)
{return allowedMax;}
return position;}
$.fn.zooma=function(thumbnailID,fullID,fullImageUrl)
{$.extend(this,{deactivate:function()
{IDfull.hide();viewAreaDiv.hide();$(document).unbind('mousemove',zoomExitListener);},activate:function()
{$(IDfull).css({backgroundImage:'url('+fullImageUrl+')',backgroundRepeat:'no-repeat',backgroundPosition:'50% 50%'});IDfull.show();var isVisibleZoomBlock=false;var IDa=IDthumb.find('a');IDa.hover(function()
{mouseEnteredZoomArea=true;if(!isVisibleZoomBlock)
{IDfull.show();viewAreaDiv.show();isVisibleZoomBlock=true;$(document).bind('mousemove',zoomExitListener);}},function()
{});}});var IDthumb=$('#'+thumbnailID);var IDfull=$('#'+fullID);var imgElement=$(IDthumb).find('img');var thumbWidth=imgElement.width();var thumbHeight=imgElement.height();var loadingDiv=IDfull.find('div.zoomLoading');var mutableFullWidth=IDfull.width();var mutableFullHeight=IDfull.height();var originalFullWidth=IDfull.width();var originalFullHeight=IDfull.height();var MidDisplayWidth=mutableFullWidth/2;var MidDisplayHeight=mutableFullHeight/2;var thumbImgLeft=imgElement.position().left;var thumbImgRight=imgElement.position().left+thumbWidth;var thumbImgTop=imgElement.position().top;var thumbImgBottom=imgElement.position().top+thumbHeight;var viewAreaDiv=$('#zoomViewfinder');var mouseEnteredZoomArea=false;var widthCutted=false;var heightCutted=false;var loadedImgData=new LoaderHeleper(fullImageUrl,function(imageData)
{imageData.viewfinderBorderTop=parseInt(viewAreaDiv.css('borderTopWidth'));imageData.viewfinderBorderLeft=parseInt(viewAreaDiv.css('borderLeftWidth'));imageData.viewfinderBorderBottom=parseInt(viewAreaDiv.css('borderBottomWidth'));imageData.viewfinderBorderRight=parseInt(viewAreaDiv.css('borderRightWidth'));if(imageData.getWidth()!=0&&imageData.getHeight()!=0)
{if(mutableFullWidth>imageData.getWidth())
{mutableFullWidth=imageData.getWidth();widthCutted=true;}
if(mutableFullHeight>imageData.getHeight())
{mutableFullHeight=imageData.getHeight();heightCutted=true;}
imageData.viewfinderWidth=Math.round(thumbWidth*mutableFullWidth/(imageData.getWidth()));imageData.viewfinderHeight=Math.round(thumbHeight*mutableFullHeight/(imageData.getHeight()));if(widthCutted)
{imageData.viewfinderWidth=imageData.viewfinderWidth-(imageData.viewfinderBorderLeft+imageData.viewfinderBorderRight);}
if(heightCutted)
{imageData.viewfinderHeight=imageData.viewfinderHeight-(imageData.viewfinderBorderTop+imageData.viewfinderBorderBottom);}
imageData.halfViewfinderWidth=imageData.viewfinderWidth/2.0;imageData.halfViewfinderHeight=imageData.viewfinderHeight/2.0;imageData.widthDiff=imageData.getWidth()/thumbWidth;imageData.heightDiff=imageData.getHeight()/thumbHeight;}
imageData.thumbImgLeft=thumbImgLeft;imageData.thumbImgRight=thumbImgRight;imageData.thumbImgTop=thumbImgTop;imageData.thumbImgBottom=thumbImgBottom;viewAreaDiv.css('width',imageData.viewfinderWidth);viewAreaDiv.css('height',imageData.viewfinderHeight);viewAreaDiv.css('left',thumbImgLeft+Math.round(thumbWidth/2.0-(imageData.viewfinderWidth+imageData.viewfinderBorderLeft+imageData.viewfinderBorderRight)/2.0));viewAreaDiv.css('top',thumbImgTop+Math.round(thumbHeight/2.0-(imageData.viewfinderHeight+imageData.viewfinderBorderTop+imageData.viewfinderBorderBottom)/2.0));viewAreaDiv.show();loadingDiv.hide();});if(!loadedImgData.isLoaded())
{loadingDiv.show();}
function zoomExitListener(event)
{if(mouseEnteredZoomArea&&event.pageX<thumbImgLeft||event.pageX>thumbImgRight||event.pageY<thumbImgTop||event.pageY>thumbImgBottom)
{/* do not close magnifier */return;$(document).unbind('mousemove',zoomExitListener);IDfull.hide();viewAreaDiv.hide();}
else
{mouseMoveListener(event);}}
function mouseMoveListener(event)
{if(loadedImgData.isLoaded())
{var mouseX=event.pageX;var mouseY=event.pageY;var verticalViewfinderBorder=loadedImgData.viewfinderBorderTop+loadedImgData.viewfinderBorderBottom;var horizontalViewfinderBorder=loadedImgData.viewfinderBorderLeft+loadedImgData.viewfinderBorderRight;var mouseXFixed=correctPositionByEpsilonBorder(mouseX,loadedImgData.thumbImgLeft,loadedImgData.thumbImgRight-horizontalViewfinderBorder,loadedImgData.halfViewfinderWidth);var mouseYFixed=correctPositionByEpsilonBorder(mouseY,loadedImgData.thumbImgTop,loadedImgData.thumbImgBottom-verticalViewfinderBorder,loadedImgData.halfViewfinderHeight);var viewfinderLeft=Math.round(mouseXFixed-loadedImgData.halfViewfinderWidth);var viewfinderTop=Math.round(mouseYFixed-loadedImgData.halfViewfinderHeight);viewAreaDiv.css('left',viewfinderLeft);viewAreaDiv.css('top',viewfinderTop);var bgy=0;var bgx=0;if(widthCutted)
{bgx=Math.round((originalFullWidth-loadedImgData.getWidth())/2.0);}
else
{bgx=Math.round((Math.round(mouseXFixed)-loadedImgData.thumbImgLeft)*(-loadedImgData.widthDiff)+MidDisplayWidth);}
if(heightCutted)
{bgy=Math.round((originalFullHeight-loadedImgData.getHeight())/2.0);}
else
{bgy=Math.round((Math.round(mouseYFixed)-loadedImgData.thumbImgTop)*(-loadedImgData.heightDiff)+MidDisplayHeight);}
IDfull.css('backgroundPosition',bgx+'px'+' '+bgy+'px');}}
return this;};})(jQuery);