//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
//
// jqZoom version 2.2
// 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: 338,		//zoomed width default width
				yzoom: 360,		//zoomed div default width
				offset: 53,		//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 (!window.XMLHttpRequest) {
				var settings = {
					xzoom: 338,		//zoomed width default width
					yzoom: 360,		//zoomed div default width
					offset: 53,		//zoomed div default offset
					position: "right" ,//zoomed div default position,offset position is to the right of the image
					lens:0, //zooming lens over the image,by default is 1;
					preload: 1

				};
			}

			if(options) {
				$.extend(settings, options);
			}

		    var noalt='';

		    $(this).hover(function(){
		    	var imageLeft = $(this).children('img').offset().left;     
		    	var imageTop = $(this).children('img').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",'');

		    	if($("div.zoomdiv").length == 0){

		    		//$(this).after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
		    		//$("#prdtdtlrgt").append("<div id='loader' class='zoomdiv loading'></div>");
		    		// DDP 20100830 - changed element id to fit new structure
		    		// DDP 20100830 - #col1 for wgdetail default page
		    		// DDP 20100830 - #desc-container for quickview
		    		$("#col1").append("<div id='loader' class='zoomdiv loading'></div>");
		    		//$("#desc-container").append("<div id='loader' class='zoomdiv loading'></div>");
		    		
		    		jQuery(function(){
		    			var img = new Image();
		    			jQuery(img)
		    				.load(function() {
		    					jQuery(this).hide();
		    					jQuery("#loader")
		    						.removeClass("loading")
		    						.append(this);
		    					jQuery(this).show();
		    				})
		    				.error(function(){
		    					//alert("something's wrong");
		    				})
		    				.attr("src", bigimage)
		    				.attr("id", "bigimg");
		    		});

					if(jQuery("div.jqZoomPup").length == 0)
					{
						$(this).append("<div class='jqZoomPup'>&nbsp;</div>");
					}

		    	}


		    	if(settings.position == "right"){

            		if(imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width){

            			leftpos = imageLeft  - settings.offset - settings.xzoom;

            		}else{

		    			leftpos = imageLeft + imageWidth + settings.offset;
            		}
		    	}else{
		    		leftpos = imageLeft - settings.xzoom - settings.offset;
		    		
		    		if(leftpos < 0){

            			leftpos = imageLeft + imageWidth  + settings.offset;

		    		}

		    	}
				
				var zoomtop = imageTop + 11;
		    	//$("div.zoomdiv").css({ top: zoomtop,left: leftpos });

		    	$("div.zoomdiv").width(settings.xzoom);

		    	$("div.zoomdiv").height(settings.yzoom);

            	$("div.zoomdiv").show();

            	if(!settings.lens){
            		$(this).css('cursor','crosshair');
				}

				$(document.body).mousemove(function(e){
	
					if(document.getElementById("bigimg") != null)
					{
						mouse = new MouseEvent(e);
	
	                   	/*$("div.jqZoomPup").hide();*/
	
						//var bigwidth = jQuery("#bigimg").get(0).offsetWidth;
						var bigwidth = document.getElementById("bigimg").offsetWidth;
	
					    //var bigheight = jQuery("#bigimg").get(0).offsetHeight;
					    var bigheight = document.getElementById("bigimg").offsetHeight;
	
					    var scaley ='x';
	
					    var scalex= 'y';


					    if(isNaN(scalex)|isNaN(scaley)){
	
					    	var scalex = (bigwidth/imageWidth);
	
					    	var scaley = (bigheight/imageHeight);
	
					    	$("div.jqZoomPup").width((settings.xzoom)/scalex );
	
			    			$("div.jqZoomPup").height((settings.yzoom)/scaley);
	
	                    	if(settings.lens){
	                    		$("div.jqZoomPup").css('visibility','visible');
							}
	
					   	}
	
	                    
						// DK - changed for centered product image
	                    //xpos = mouse.x - $("div.jqZoomPup").width()/2 - imageLeft;
	                    xpos = mouse.x - $("div.jqZoomPup").width()/2;
	
	                    //ypos = mouse.y - $("div.jqZoomPup").height()/2 - imageTop ;
	                    ypos = mouse.y - $("div.jqZoomPup").height()/2;
	                    	
	                    scrollx = xpos - imageLeft;
	                    
	                    scrolly = ypos - imageTop ;
	                    	
	                    if(settings.lens){
							
							scrolly = (mouse.y - $("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $("div.jqZoomPup").height()/2  > imageHeight + imageTop ) ?  (imageHeight - $("div.jqZoomPup").height() -2 ) : scrolly;
							scrollx = (mouse.x - $("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $("div.jqZoomPup").width()/2 > imageWidth + imageLeft ) ?  (imageWidth -$("div.jqZoomPup").width() -2)  : scrollx;
	
	                    	//xpos = (mouse.x - $("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $("div.jqZoomPup").width()/2 > imageWidth + imageLeft ) ?  (imageWidth -$("div.jqZoomPup").width() -2)  : xpos;
	                    	if(mouse.x - $("div.jqZoomPup").width()/2 < imageLeft)
	                    	{
	                    	
	                    		xpos = imageLeft;
	                    	}
	                    	else
	                    	{
	                    		if(mouse.x + $("div.jqZoomPup").width()/2 > imageWidth + imageLeft)
	                    		{
	                    			xpos = imageWidth -$("div.jqZoomPup").width() -2 + imageLeft;
	                    		}
	                    		else
	                    		{
	                    			xpos = xpos;
	                    		}
	                    	}
	                    	//xpos = (mouse.x - $("div.jqZoomPup").width()/2 < imageLeft ) ? imageLeft : (mouse.x + $("div.jqZoomPup").width()/2 > imageWidth + imageLeft ) ?  (imageWidth -$("div.jqZoomPup").width() -2 + imageLeft)  : xpos;
	
							//ypos = (mouse.y - $("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $("div.jqZoomPup").height()/2  > imageHeight + imageTop ) ?  (imageHeight - $("div.jqZoomPup").height() -2 ) : ypos;
							ypos = (mouse.y - $("div.jqZoomPup").height()/2 < imageTop ) ? imageTop : (mouse.y + $("div.jqZoomPup").height()/2  > imageHeight + imageTop ) ?  (imageHeight - $("div.jqZoomPup").height() -2 + imageTop) : ypos;
	
	                    }
	
	
	                    if(settings.lens){
							xpos = xpos - jQuery("#content-body").offset().left;
							ypos = ypos - jQuery("#content-body").offset().top;
	                    	$("div.jqZoomPup").css({ position: "absolute", top: ypos,left: xpos });
	
	                    }
	
	
						//scrolly = ypos;
	
						$("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
	
						//scrollx = xpos;
	
						$("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex ;

					}
				});
		    },function(){

               	$(this).children("img").attr("alt",noalt);
		       	$(document.body).unbind("mousemove");
		       	if(settings.lens){
		       		$("div.jqZoomPup").remove();
		       	}
		       	$("div.zoomdiv").remove();

		    });

        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('div.jqPreload'+count+'').html();

        		jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');

			});

		}

	}

})(jQuery);

function MouseEvent(e) {
	this.x = e.pageX;
	this.y = e.pageY;
}
