//**************************************************************	
// based on jqZoom version 2.1
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// First Release on Dec 05 2007
// mail: renzi.mrc@gmail.com
//**************************************************************

(function($){

		$.fn.jqueryzoom = function(options){

		var settings = {

				xzoom: 200,		//zoomed width default width
				yzoom: 200,		//zoomed div default width
				offset: 10,		//zoomed div default offset
				position: "right" ,//zoomed div default position,offset position is to the right of the image
				lens:1, //zooming lens over the image,by default is 1;
				preload: 1

			};

			if(options) {
				$.extend(settings, options);
			}

		    var noalt='';

			// Survol de l'élément de zoom
		    $(this).hover(function(){
				if( $.browser.msie && /6.0/.test(navigator.userAgent) ) { $('#zoom_box select').hide(); }

		    	var imageOffsetLeft	= $(this).children('img').get(0).offsetLeft;
		    	var imageOffsetTop	= $(this).children('img').get(0).offsetTop;
				var imageLeft		= $(this).get(0).offsetLeft	+ imageOffsetLeft;
				var imageWidth		= $(this).children('img').get(0).offsetWidth;
				var imageTop		= $(this).get(0).offsetTop	+ imageOffsetTop;
				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",'');
				if($("div.zoomdiv").get().length == 0){
					$(this).after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
					$(this).append("<div class='jqZoomPup'><img src='/lesblousesroses/images/_dot.gif' width='100' height='100'/></div>");
					
					//$('.jqPreload').jqm({overlay: 75, trigger: false});
	                $('.jqZoomPup img').click(function(){$('.jqPreload').jqmShow();});
				}

/*
				if(imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width){
				leftpos = imageLeft  - settings.offset - settings.xzoom;
				}else{
				leftpos = imageLeft + imageWidth + settings.offset;
				}
*/

				// 
				var zoomimageTop	=  document.getElementById('zoom_box').offsetTop;
				var zoomleftpos		= document.getElementById('zoom_box').offsetLeft;
			    
				if($.browser.msie){
				//	$(this).css('margin-bottom','10');
				}
				if($.browser.safari){
					// $("div.zoomdiv").css({ top: imageTop + 75,left: leftpos });
					$("div.zoomdiv").css({ top: zoomimageTop,left: zoomleftpos });
				}
				else{
					// $("div.zoomdiv").css({ top: imageTop + 80,left: leftpos });
					$("div.zoomdiv").css({ top: zoomimageTop,left: zoomleftpos });
				}
			    $("div.zoomdiv").width(settings.xzoom);
			    $("div.zoomdiv").height(settings.yzoom);
	            $("div.zoomdiv").show();
				
				// Mouvements de la souris
				$(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';
					if(isNaN(scalex)||isNaN(scaley)){
						var scalex = (bigwidth/imageWidth);
						var scaley = (bigheight/imageHeight);
						$(".jqZoomPup").width((settings.xzoom)/scalex );
						$(".jqZoomPup").height((settings.yzoom)/scaley);
						$(".jqZoomPup img").width((settings.xzoom)/scalex );
						$(".jqZoomPup img").height((settings.yzoom)/scaley);
						$(".jqZoomPup").css('visibility','visible'); 
				   	}

					var jqZoomPupWidth	= $(".jqZoomPup").width();
					var jqZoomPupHeight	= $(".jqZoomPup").height();
					
                    xpos = mouse.x - jqZoomPupWidth/2 - imageLeft + imageOffsetLeft;
                    ypos = mouse.y - jqZoomPupWidth/2 - imageTop  + imageOffsetTop;
					xpos = (xpos < imageOffsetLeft) ? imageOffsetLeft: (xpos + jqZoomPupWidth  > imageOffsetLeft + imageWidth)  ? (imageOffsetLeft + imageWidth  - jqZoomPupWidth  - 4): xpos;
					ypos = (ypos < imageOffsetTop)  ? imageOffsetTop:  (ypos + jqZoomPupHeight > imageOffsetTop  + imageHeight) ? (imageOffsetTop  + imageHeight - jqZoomPupHeight - 4): ypos;

					scrolly = ypos - imageOffsetTop;
					$("div.zoomdiv").get(0).scrollTop = (scrolly) * scaley;

					scrollx = xpos - imageOffsetLeft;
					$("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex ;
					
					$(".jqZoomPup").css({ top: ypos,left: xpos });

			    });
		    },function(){

				$(this).children("img").attr("alt",noalt);
				$(document.body).unbind("mousemove");
				$(".jqZoomPup").remove();
				$("div.zoomdiv").remove();

				if( $.browser.msie && /6.0/.test(navigator.userAgent) ) { $('#zoom_box select').show(); }

		    });

		}

})(jQuery);

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