Index: Popup.js
===================================================================
--- Popup.js	(revision 2339)
+++ Popup.js	(working copy)
@@ -8,8 +8,10 @@
 
 Popup.create = function(root,elem,theClass)
 {
-	Popup.remove();
+	var stackPosition = this.find(root,"popup");
+	Popup.remove(stackPosition+1);
 	var popup = createTiddlyElement(document.body,elem ? elem : "ol","popup",theClass ? theClass : "popup");
+	popup.stackPosition = stackPosition;
 	Popup.stack.push({root: root, popup: popup});
 	return popup;
 };
@@ -25,10 +27,16 @@
 	return true;
 };
 
-Popup.show = function(unused1,unused2)
+//# valign : 'top' or 'bottom' (optional)
+//#   defaults to 'bottom' for regular popups, 'top' for nested popups
+//# halign : 'left' or 'right' (optional)
+//#   defaults to 'left' for regular popups, 'right' for nested popups
+//# offset : {x: number, y: number} (optional)
+//#   defaults to {x:0,y:0}
+Popup.show = function(valign,halign,offset)
 {
 	var curr = Popup.stack[Popup.stack.length-1];
-	this.place(curr.root,curr.popup);
+	this.place(curr.root,curr.popup,valign,halign,offset);
 	addClass(curr.root,"highlight");
 	if(config.options.chkAnimate && anim && typeof Scroller == "function")
 		anim.startAnimating(new Scroller(curr.popup));
@@ -36,32 +44,55 @@
 		window.scrollTo(0,ensureVisible(curr.popup));
 };
 
-Popup.place = function(root,popup,offset)
-{
-	if(!offset) var offset = {x:0, y:0};
+Popup.place = function(root,popup,valign,halign,offset)
+{	
+	if(!offset)
+		var offset = {x:0,y:0};
+	if(popup.stackPosition>=0 && !valign && !halign){
+		offset.x = offset.x + root.offsetWidth;
+	}
+	else{
+		offset.x = (halign == 'right')? offset.x + root.offsetWidth : offset.x;
+		offset.y = (valign == 'top')? offset.y : offset.y + root.offsetHeight;	
+	}
 	var rootLeft = findPosX(root);
 	var rootTop = findPosY(root);
-	var rootHeight = root.offsetHeight;
 	var popupLeft = rootLeft + offset.x;
-	var popupTop = rootTop + rootHeight + offset.y;
+	var popupTop = rootTop + offset.y;
 	var winWidth = findWindowWidth();
 	if(popup.offsetWidth > winWidth*0.75)
 		popup.style.width = winWidth*0.75 + "px";
 	var popupWidth = popup.offsetWidth;
-	if(popupLeft + popupWidth > winWidth)
-		popupLeft = winWidth - popupWidth;
+	if(popupLeft + popupWidth > winWidth){
+		if(halign == 'right')
+			popupLeft = popupLeft - root.offsetWidth - popupWidth;
+		else
+			popupLeft = winWidth - popupWidth;
+	}
 	popup.style.left = popupLeft + "px";
 	popup.style.top = popupTop + "px";
 	popup.style.display = "block";
 }
 
-Popup.remove = function()
+Popup.find = function(item,node)
 {
-	if(Popup.stack.length > 0) {
-		Popup.removeFrom(0);
+	var pointer = -1;
+	for (var i=this.stack.length-1; i> -1; i--){
+		if(isDescendant(item,this.stack[i][node])){
+			pointer = i;
+		}
 	}
+	return pointer;
 };
 
+Popup.remove = function(pos)
+{
+	if (!pos) var pos = 0;
+	if(Popup.stack.length > pos) {
+		Popup.removeFrom(pos);
+	}
+};
+
 Popup.removeFrom = function(from)
 {
 	for(var t=Popup.stack.length-1; t>=from; t--) {
@@ -71,4 +102,3 @@
 	}
 	Popup.stack = Popup.stack.slice(0,from);
 };
-
