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( obj, e, anchor, preferredPlacement, onopen, cleanupPop){
	/// 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(obj)
	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

	minY=document.body.scrollTop
	maxY=document.body.clientHeight+document.body.scrollTop-(obj.clientHeight);

	tempX = originalX = (document.all) ? (event.clientX + document.body.scrollLeft) : (e.pageX);
	tempY = originalY = (document.all) ? (event.clientY + document.body.scrollTop) : (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 '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'
	}*/
	if(typeof cleanupPop == 'function'){cleanupPop()}
}
function _getElementNodes(popID){
	var nodes=[]
	var cn=$(popID).childNodes
	for(var i=0;i<cn.length;i++){
		if(cn[i].nodeType==1){ nodes.push(cn[i]) }
	}
	return nodes
}
function setDhtmlContentsTEMPLATE(popID){
	///begin prelim
	var nodes=_getElementNodes(popID)
	var header=nodes[1]
	var panel1=nodes[2]
	var panel2=nodes[3]
	///end prelim
	///begin your code ##########
	header.innerHTML=counterTest++
	
	return (popID)
}
function hideDhtmlPop(ref){
	if(_currentDhtmlPop){_currentDhtmlPop.style.display='none'}
	//if(ref){ref.style.display='none'}
	//$('mPopShadow').style.display='none'
}



