(function($){

$.fn.jqueryzoom = function(options){
var settings = {
  lens:1, //zooming lens over the image,by default is 1;
  preload: 0
};

var noalt='';

$(this).hover(function(){
  var imageLeft = $(this).offset().left;    
  var imageTop = $(this).offset().top;
  var imageWidth = $(this).children('img').get(0).offsetWidth;
  var imageHeight = $(this).children('img').get(0).offsetHeight;

  noalt = $(this).children("img").attr("alt");
  var bigimage = $(this).children("img").attr("jqimg");
  $(this).children("img").attr("alt",'');
  $(this).append("<div class='jqZoomPup'>&nbsp;</div>");
  $(".jqzoom").width(imageWidth);
  $(".jqzoom").height(imageHeight);
  $(".zoomPop").height(Math.max(400, imageHeight));
  $(".zoomPop").show();

  if(!settings.lens) $(this).css('cursor','crosshair');
					   
  $(document.body).mousemove(function(e){
  mouse = new MouseEvent(e);

  var bigwidth = $(".bigimg").get(0).offsetWidth;
  var bigheight = $(".bigimg").get(0).offsetHeight;
  var scaley = 'x';
  var scalex = 'y';

// taille, position et déplacement de la loupe
  if (isNaN(scalex) | isNaN(scaley)){
    var scalex = (bigwidth/imageWidth);
    var scaley = (bigheight/imageHeight);
    var xzoom = $(".zoomPop").width(); 
    var yzoom = $(".zoomPop").height(); 
  
    $(".jqZoomPup").width(xzoom/scalex);
    $(".jqZoomPup").height(yzoom/scaley);
    if(settings.lens) $(".jqZoomPup").css('visibility','visible');
  }
  
  xpos=mouse.x-$(".jqZoomPup").width()/2-imageLeft;
  ypos=mouse.y-$(".jqZoomPup").height()/2-imageTop ;
  if(settings.lens){
    xpos=(mouse.x - $(".jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $(".jqZoomPup").width()/2 > imageWidth + imageLeft ) ?  (imageWidth -$(".jqZoomPup").width() -2)  : xpos;
    ypos=(mouse.y - $(".jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $(".jqZoomPup").height()/2  > imageHeight + imageTop ) ?  (imageHeight - $(".jqZoomPup").height() -2 ) : ypos;
    $(".jqZoomPup").css({ top: ypos,left: xpos });
  }

  scrolly = ypos;  $(".zoomPop").get(0).scrollTop = scrolly * scaley;
  scrollx = xpos;  $(".zoomPop").get(0).scrollLeft = (scrollx) * scalex ;
  });
}, function(){
  $(this).children("img").attr("alt",noalt);
  $(document.body).unbind("mousemove");
  if(settings.lens) $(".jqZoomPup").remove();
  $(".zoomPop").hide();
});

count = 0;
if(settings.preload){
		  $('body').append("<div style='display:none;' class='jqPreload"+count+"'>sdsdssdsd</div>");
		  $(this).each(function(){
          var imagetopreload= $(this).children("img").attr("jqimg");
          var content = jQuery('.jqPreload'+count+'').html();
          jQuery('.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');
});

}

}

})(jQuery);

function MouseEvent(e) {
  this.x = e.pageX;
  this.y = e.pageY;
}



