? 729648-31.patch
Index: hover_preview.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/hover_preview/hover_preview.js,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 hover_preview.js
--- hover_preview.js	22 Nov 2010 18:35:39 -0000	1.1.2.1
+++ hover_preview.js	28 Nov 2010 21:20:59 -0000
@@ -45,21 +45,39 @@ Drupal.behaviors.HoverPreview = function
   
   //keep track of mouse movement in an image and move preview element
   $("img.hover-preview").mousemove(function(e){
-  
+
+  //width & height of the popup with the larger image triggered on hover.
   var elementHeight = $("p#hover-preview").outerHeight(true);
+  var elementWidth = $("p#hover-preview").outerWidth(true);
+
+  //Size of the viewport, meaning the area of the page, that is completely visible.
   var winHeight = $(window).height();
-  var scrolledPixel = $(window).scrollTop();
+  var winWidth = $(window).width();
+
+  //If the page has been scolled, addidtional calculation must be done.
+  //Scroll position is the same as the number of pixels that are hidden from view above the scrollable area.
+  //If the scroll bar is at the very top, or if the element is not scrollable, this number will be 0.
+  var scrolledPixelTop = $(window).scrollTop();
+  var scrolledPixelLeft = $(window).scrollLeft();  //a site should not be vertically scrollable, but maybe...
+
+  //calculate if image would be loaded outside
+  var calcHeight = winHeight - e.pageY + yOffset + scrolledPixelTop;
+  var calcWidth = winWidth - e.pageX + xOffset + scrolledPixelLeft;
+
+  //set initial
+  var xPosition = e.pageX;
+  var yPosition = e.pageY;
+
+  if( calcHeight <= elementHeight ) {
+    var yPosition = winHeight - elementHeight + scrolledPixelTop;
+  }
+
+  if( calcWidth <= elementWidth ) {
+    var xPosition = winWidth - 2*elementWidth + scrolledPixelLeft;
+  }
 
-  if( ((winHeight - e.pageY + yOffset + scrolledPixel) <= elementHeight) ) {
-    var yPosition = winHeight - elementHeight + scrolledPixel;
-    $("#hover-preview")      
-        .css("top",(yPosition - yOffset) + "px")
-        .css("left",(e.pageX + xOffset) + "px");
-  } 
-   else {
-    $("#hover-preview")
-      .css("top",(e.pageY - yOffset) + "px")
-      .css("left",(e.pageX + xOffset) + "px");
-   }
+  $("#hover-preview")
+    .css("top",(yPosition - yOffset) + "px")
+    .css("left",(xPosition + xOffset) + "px");
   });  
 };
