$(".variationPreviewBubble").each(
	function(e) {
		var t = $(this);
		var p = t.parent();
		t.css("top", p.position().top - 327);
		t.css("left", p.position().left - 180);
	}
);

	
var hint = $('<img src="/sys/resource.ashx?guid=38254716c8c94574a3d01eeca78a4669" class="zoomHint" />');
$("#productMainImageDisplayWrapper").append(hint);

var largeWidth =0;
var largeHeight = 0;
var zboxwidth = 0;
var zboxheight = 0;
var mmReady = false;
var hideDelay;
var showDelay;
var hideDelayIncrease;
var hideDelayTimeout = 0;

$("#productMainImageDisplayWrapper").mouseout(
	function() {

		//clearTimeout(showDelay);
	}
);

$("#productMainImageDisplayWrapper").mouseover(
	function() {
		if(pImages[currentImageID].large > 300) {
		hideDelayTimeout = 500;
		clearTimeout(hideDelay);
		
		if($(".imageViewerZoom").length == 0) {
	
		var pickImg = new Image();
											
		pickImg.onload = function(e) {
				var th = this.height;
				var tw = this.width;
				
				var largeSide = th;
				if(tw > largeSide) {
					largeSide = tw;
				}
				
				largeWidth = tw;
				largeHeight = th;
			 

				$(".imageViewerLarge").css("background-image", "url(" + this.src + ")");
				$(".imageViewerZoom").show();
				$("#productMainImageDisplayWrapper").mousemove();
			
			
				$("#productMainImageDisplayWrapper").bind("mouseout",
					function() {
						hideDelay = setTimeout(function() {
							hideDisplay();
						}, hideDelayTimeout);
					}
				);
				
					
		}
				
		largeWidth = 0; largeHeight = 0;
		var l = $("#prodMainImageDisplay").offset();
		$(".imageViewerLarge").remove();
		$(".imageViewerZoom").remove();
		var imgViewerLarge = $('<div class="imageViewerLarge"><div>');
		var il = pImages[currentImageID].large;
		zboxwidth = viewportWidth / (il / 300);
		zboxheight =  viewportHeight / (il / 300);
		$(".imageViewerZoom").css("width", zboxwidth + "px");
		$(".imageViewerZoom").css("height", zboxheight + "px");
		
		imgViewerLarge.mouseover(
			function() {
				clearTimeout(hideDelay);
				hideDisplay();
				//$(this).hide();
			}
		);
		
		imgViewerLarge.css("top", l.top).css("left", 328).css("background-position", "50%").css("background-image", "url(/sys/resource.ashx?guid=cbe62763cbf042ca988c97d8084ee6ef)");
		var imageViewerZoom = $('<div class="imageViewerZoom"></div>');
		if(zboxwidth > 300) zboxwidth = 300;
		if(zboxheight > 300) zboxheight = 300;
		imageViewerZoom.css("width", zboxwidth).css("height", zboxheight);
		$("#productMainImageDisplayWrapper").append(imageViewerZoom);
		imageViewerZoom.show();
		$("#bodyWrap").append(imgViewerLarge);
		pickImg.src = "/sys/resource.ashx?guid=" +  pImages[currentImageID].key + "&w=" + pImages[currentImageID].large + "&h=" +  pImages[currentImageID].large + "&p=" + Math.round((10*il)/300);
		}
		}
	}
);

function hideDisplay() {
	$(".imageViewerZoom").remove();
	$(".imageViewerLarge").fadeOut();
	$("#productMainImageDisplayWrapper").unbind("mouseout")
}

$("#productMainImageDisplayWrapper").mousemove(
	function(e) {
	try {
		 $(".imageViewerLarge").text("");
		 
	
		var l = $("#prodMainImageDisplay").offset();
		
		var zboxLeft = Math.round(e.pageX - l.left - (zboxwidth / 2));
		var zboxTop = Math.round(e.pageY - l.top - (zboxheight / 2));
		if(zboxLeft < 0) zboxLeft =0;
		if(zboxTop < 0) zboxTop =0;
		if(zboxLeft + zboxwidth > 300) zboxLeft = 300 - zboxwidth;
		if(zboxTop + zboxheight > 300) zboxTop = 300 - zboxheight;
		
		$(".imageViewerZoom").css("left", zboxLeft).css("top", zboxTop);
		
	
		if(largeWidth != 0 | largeHeight != 0) {
			var ll = Math.round((zboxLeft * largeWidth) / 300);
			var lt = Math.round((zboxTop * largeHeight) / 300);
			if(largeWidth > viewportWidth | largeHeight > viewportHeight) {
				var bpLeft = (-ll) + "px";
				var bpTop = (-lt) + "px";
				if(largeWidth < viewportWidth) {
					bpLeft = "50%";
				}
				if(largeHeight < viewportHeight) {
					bpTop = "50%";
				}
				$(".imageViewerLarge").css("background-position", bpLeft + " " + bpTop);
			} else {
				$(".imageViewerLarge").css("background-position", "50% 50%");
			}
		}
	} catch(e) {
	}
	}
);