--- context_menu.js.orig	2008-04-02 21:19:25.000000000 -0400
+++ context_menu.js	2008-04-02 21:25:48.000000000 -0400
@@ -93,14 +93,53 @@
   },
   
   showMenu: function(e) {
-		$('context-menu').style['left'] = (Event.pointerX(e) + 'px');
-		$('context-menu').style['top'] = (Event.pointerY(e) + 'px');		
-		Element.update('context-menu', '');
-		new Ajax.Updater({success:'context-menu'}, this.url, 
+    var mouse_x = Event.pointerX(e);
+    var mouse_y = Event.pointerY(e);
+    var render_x = mouse_x;
+    var render_y = mouse_y;
+    var dims;
+    var menu_width;
+    var menu_height;
+    var window_width;
+    var window_height;
+    var max_width;
+    var max_height;
+
+    $('context-menu').style['left'] = (render_x + 'px');
+    $('context-menu').style['top'] = (render_y + 'px');		
+    Element.update('context-menu', '');
+
+    new Ajax.Updater({success:'context-menu'}, this.url, 
       {asynchronous:true,
        evalScripts:true,
        parameters:Form.serialize(Event.findElement(e, 'form')),
        onComplete:function(request){
+	 dims = $('context-menu').getDimensions();
+	 menu_width = dims.width;
+	 menu_height = dims.height;
+
+	 max_width = mouse_x + 2*menu_width;
+	 max_height = mouse_y + menu_height;
+
+	 var ws = window_size();
+	 window_width = ws.width;
+	 window_height = ws.height;
+
+	 if (max_width > window_width) {
+	   render_x -= menu_width;
+	 }
+	 if (max_height > window_height) {
+	   render_y -= menu_height;
+	 }
+	 if (render_x <= 0) render_x = 1;
+	 if (render_y <= 0) render_y = 1;
+	 $('context-menu').style['left'] = (render_x + 'px');
+	 $('context-menu').style['top'] = (render_y + 'px');		
+
+// this code can be used for debugging
+//	 alert("menu_width=" + menu_width + "\nmenu_height=" + menu_height + "\nmouse_x=" + mouse_x + "\nmouse_y=" + mouse_y +
+//	 "\nwindow_width=" + window_width + "\nwindow_height=" + window_height + "\nrender_x=" + render_x + "\nrender_y=" + render_y);
+
          Effect.Appear('context-menu', {duration: 0.20});
          if (window.parseStylesheets) { window.parseStylesheets(); } // IE
       }})
@@ -159,3 +198,19 @@
 		}
 	}
 }
+
+function window_size() {
+    var w;
+    var h;
+    if (window.innerWidth) {
+	w = window.innerWidth;
+	h = window.innerHeight;
+    } else if (document.documentElement) {
+	w = document.documentElement.clientWidth;
+	h = document.documentElement.clientHeight;
+    } else {
+	w = document.body.clientWidth;
+	h = document.body.clientHeight;
+    }
+    return {width: w, height: h};
+}
