var popupBoxOffsetFromMouse=[-5,-5];
if (document.getElementById || document.all){
	document.write('<div id="popupBoxModalBackground" style="position: absolute; visibility: hidden; left: 0px; top: 0px; width: ' + getDocWidth() + 'px; height: ' + getDocHeight() + 'px; z-index: 50"></div>');
}
function showPopupBox(popupElement, e, popupResetCallback, isModal){

	if ( typeof popupElement == 'string' ){
		popupElement = document.getElementById(popupElement);
	}
	
	movePopupBox(popupElement, e);
	popupElement.style.visibility="visible";

	if ( isModal && popupResetCallback ){
		document.getElementById('popupBoxModalBackground').onclick = function (modalE){
			popupResetCallback(popupElement, modalE);
			popupElement.style.visibility = 'hidden';
			document.getElementById('popupBoxModalBackground').style.visibility = 'hidden';
		};
		document.getElementById('popupBoxModalBackground').style.visibility = 'visible';
	}

}
function hidePopupBox(popupElement){
	if ( typeof popupElement == 'string' ){
		popupElement = document.getElementById(popupElement);
	}
	popupElement.style.visibility="hidden";
	document.getElementById('popupBoxModalBackground').style.visibility = 'hidden';
}
function movePopupBox(popupElement, e){

	//debugOnClickEventHandler(e);

	var xcoord = popupBoxOffsetFromMouse[0];
	var ycoord = popupBoxOffsetFromMouse[1];

	var docwidth = getDocWidth();
	var docheight = getDocHeight();

	if (typeof e != "undefined"){
		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;
		}
		
				
		if (docwidth - x < popupElement.offsetWidth + xcoord){
			xcoord = x - xcoord - popupElement.offsetWidth; 
		} 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 - event.clientX < popupElement.offsetWidth + xcoord){
			xcoord = event.clientX - xcoord - popupElement.offsetWidth + getTrueBody().scrollLeft;
		} 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;

	}
	popupElement.style.left = xcoord+"px"
	popupElement.style.top = ycoord+"px"

}
var SIDE_TOP = 1;
var SIDE_BOTTOM = 2;
var SIDE_LEFT = 4;
var SIDE_RIGHT = 8;
var QUADRANT_TOP_LEFT = SIDE_TOP | SIDE_LEFT;
var QUADRANT_TOP_RIGHT = SIDE_TOP | SIDE_RIGHT;
var QUADRANT_BOTTOM_LEFT = SIDE_BOTTOM | SIDE_LEFT;
var QUADRANT_BOTTOM_RIGHT = SIDE_BOTTOM | SIDE_RIGHT;
function whichQuadrant(pageX, pageY, pointX, pointY){

	var quadrant = 0;
	var halfX = pageX/2;
	var halfY = pageY/2;

	if ( pointX < halfX ){
		quadrant |= SIDE_LEFT;
	} else {
		quadrant |= SIDE_RIGHT;
	}

	if ( pointY < halfY ){
		quadrant |= SIDE_TOP;
	} else {
		quadrant |= SIDE_BOTTOM;
	}
	return quadrant;
}
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
}
function debugOnClickEventHandler(e){
	s = typeof e + ":\n";
	s += 'type = ' + e.type + ":\n";
	s += 'target = ' + e.target + ":\n";
	s += 'layerX = ' + e.layerX + ":\n";
	s += 'layerY = ' + e.layerY + ":\n";
	s += 'pageX = ' + e.pageX + ":\n";
	s += 'pageY = ' + e.pageY + ":\n";
	s += 'screenX = ' + e.screenX + ":\n";
	s += 'screenY = ' + e.screenY + ":\n";
	s += 'which = ' + e.which + ":\n";
	s += 'modifiers = ' + e.modifiers + ":\n";

	alert(s);
}

