var _currentDhtmlPop
var minX=0, maxX=0
var minY=0, maxY=0

function _snapWithinBounds(min,val1, val2, max){
	if(isNaN(parseFloat(val2))){val2=val1}
	//val2 gives a second possible placement. 
	if( ((val1>=max) & (val2<val1)) || ((val1<=min) & (val1<val2)) ){
		return Math.max( min, Math.min(val2,max) )
	}else{
		return Math.max( min, Math.min(val1,max) )
	}
}
function _snapX(val1,val2){
	return _snapWithinBounds(minX,val1,val2,maxX)
}
function _snapY(val1,val2){
	//if(_currentDhtmlPop.clientHeight > (maxY-minY)){
	//	//buggy, but may be worked with in the future to keep it onscreen.
	//	return _snapWithinBounds(minY,val1,null,minY)
	//}
	return _snapWithinBounds(minY,val1,val2,maxY)
}
function showDhtmlPop( id, e, anchor, preferredPlacement, onopen, title){
	/// sample usage: showDhtmlPop('dhtmlPopID',event,this,3)	
	/// second and third args MUST be event and this when called from an anchor
	/// There are 2 new extra args. References to functions to call on opening and after closing.
	// see the switch() below for the preferredPlacement options.
	if(typeof onopen == 'function'){onopen(anchor,obj)}

	hideDhtmlPop()
	
	obj=document.getElementById('info' + id);
	if (obj==null) return;
	obj.style.visibility='hidden'
	obj.style.display='inline'
	_currentDhtmlPop=obj

	var tempX = 0, originalX = 0;
	var tempY = 0, originalY = 0;
	var offset=10;
	
	minX=(document.body.clientWidth-964)/2 //For centered-content div pages.
	maxX= minX+964-obj.clientWidth

	var top = (document.documentElement && document.documentElement.scrollTop) ?
		document.documentElement.scrollTop : document.body.scrollTop;
	minY=top
	maxY=document.body.clientHeight+top-(obj.clientHeight);

	tempX = originalX = (document.all) ? (event.clientX + document.body.scrollLeft) : (e.pageX);
	tempY = originalY = (document.all) ? (event.clientY + top) : (e.pageY);

	switch(preferredPlacement.toLowerCase()){
		case 'side': /// right/left, mainly right, centered vertically. 
			tempX=_snapX(tempX + offset, tempX-obj.clientWidth - offset)
			tempY=_snapY(tempY-(obj.clientHeight/2))
			break;
		case 'sideandbelow': /// to the left of cell and level with it
			var td = anchor.parentNode.parentNode
			tempX=td.clientWidth + 144 + 15;
			tempY=tempY-anchor.offsetTop-3;
			break;
		case 'above': /// above. 
			tempX=_snapX(tempX-(obj.clientWidth/2)-offset)
			tempY=_snapY(tempY-(obj.clientHeight)-offset, tempY+offset)
			break;
		case 'below': /// below. 
			tempX=_snapX(tempX-(obj.clientWidth/2)+offset)
			tempY=_snapY(tempY+offset, tempY-(obj.clientHeight)-offset)
			break;
		case 'belowtallpage': /// belowTallPage. 
			tempX=_snapX(tempX-(obj.clientWidth/2)+offset)
			tempY=tempY+offset //_snapY(tempY+offset, tempY-(obj.clientHeight)-offset) //
			break;
		default:
	}
	obj.style.top  = (tempY) + 'px';
	obj.style.left = (tempX) + 'px';
	obj.style.visibility='visible'

	/*with($('mPopShadow').style){
		width=obj.clientWidth+ 'px';
		height=obj.clientHeight+ 'px';
		top=(tempY+8) + 'px';
		left=(tempX+8) + 'px';
		display='block'
	}*/
	
	oTitle = document.getElementById('infotitle' + id);
	if (oTitle==null) return;
	oTitle.innerHTML = title;
	
}

function hideDhtmlPop(ref){
	if(_currentDhtmlPop){_currentDhtmlPop.style.display='none'}
}


