
var popupBoxOffsetFromMouse=[-30,-5]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset


////////////////////////////////////////////////////////////////////////////////
/// @name movePopupBox
/// @desc move a given popup box to the location defined by a click event
///
/// @TODO keep the popup box from extending beyond the right or bottom of the window
///
/// @param popupElement - element object - the popup box <DIV>
/// @param e event object - a click event that we will use to position the popup box near the cursor
////////////////////////////////////////////////////////////////////////////////
function movePopupBox(popupElement, e, width){

	//debugOnClickEventHandler(e);

	var xcoord = popupBoxOffsetFromMouse[0];
	var ycoord = popupBoxOffsetFromMouse[1];

	var docwidth = getDocWidth();
	var docheight = getDocHeight();

	if (typeof e != "undefined"){

//alert(e.pageY + ':' + popupElement.offsetHeight + ':' + docheight + ':' + getTrueBody().scrollTop);
//alert(ycoord);
/*
doc     ----------------------------
page    ------------------------
scroll  -------
pop            --------------
*/
		// ie doesn't like pageX
		var x = 0;
		var y = 0;	
		
		if(e.pageX || e.pageY) {
			x = e.pageX;
			y = e.pageY;
		} else if (e.clientX || e.clientY) {
			x = e.clientX;
			y = e.clientY;
			//x = e.clientX + document.body.scrollLeft;
			//y = e.clientY + document.body.scrollTop;
		}
        //changed popupElement.offsetWidth to passed in width
        // becuase offsetWidth is 0 before scriptaculous effects have been put on
		if (docwidth - x < width + xcoord) {
			xcoord = x - xcoord - width; // Move to the left side of the cursor
		} else {
			xcoord += x;
		} 
					
		if (docheight + getTrueBody().scrollTop - y < popupElement.offsetHeight + ycoord){
			ycoord = y - ycoord - popupElement.offsetHeight;
		} else {
			ycoord += y;
		}

	} else if (typeof window.event != "undefined"){
		//if (docwidth + getTrueBody().scrollLeft - e.pageX < popupElement.offsetWidth + xcoord){
		if (docwidth - event.clientX < width + xcoord){
			xcoord = event.clientX - xcoord - width + getTrueBody().scrollLeft; // Move to the left side of the cursor
		} else {
			xcoord += event.clientX + getTrueBody().scrollLeft;
		}
		if (docheight - event.clientY < popupElement.offsetHeight + ycoord){
			ycoord = event.clientY - ycoord - popupElement.offsetHeight + getTrueBody().scrollTop;
		} else {
			ycoord += event.clientY + getTrueBody().scrollTop;
		}

	}	else {
		return;

	}
	//var docwidth=document.all? getTrueBody().scrollLeft+getTrueBody().clientWidth : pageXOffset+window.innerWidth-15;
	//var docheight=document.all? Math.max(getTrueBody().scrollHeight, getTrueBody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight);
	popupElement.style.left = xcoord+"px"
	popupElement.style.top = ycoord+"px"

}

function getDocWidth(){
	return (document.all? getTrueBody().scrollLeft+getTrueBody().clientWidth : pageXOffset+window.innerWidth-15);
}

function getDocHeight(){
	return (document.all? Math.min(getTrueBody().scrollHeight, getTrueBody().clientHeight) : Math.min(window.innerHeight));
}

function getTrueBody(){
	return ( (!window.opera && document.compatMode && document.compatMode!="BackCompat") || window.opera)? document.documentElement : document.body
}