
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;
  
  var scrollTop = document.documentElement.scrollTop;
  if (scrollTop == 0)
    scrollTop = document.body.scrollTop;
	
	minX=(document.body.clientWidth-964)/2 //For centered-content div pages.
	maxX= minX+964-obj.clientWidth

	minY = scrollTop
	maxY = document.body.clientHeight + scrollTop - (obj.clientHeight);

	tempX = originalX = (document.all) ? (event.clientX + document.body.scrollLeft) : (e.pageX);
	tempY = originalY = (document.all) ? (event.clientY + scrollTop) : (e.pageY);
  
//  alert('tempY = ' + tempY + '\ne.pageY = ' + e.pageY + '\nobj.clientHeight = ' + obj.clientHeight + '\nevent.clientY = ' + event.clientY + '\ndocument.body.scrollTop = ' + document.body.scrollTop + '\nobj.top = ' + obj.top + '\ne.offsetY \ ' + e.offsetY + '\nobj.y = ' + obj.y + '\nobj.offsetParent = ' + obj.offsetParent + '\nanchor.style.posTop = ' + anchor.style.posTop + '\ne.srcElement.y = ' + e.srcElement.y + '\ndocument.documentElement.scrollTop = ' + document.documentElement.scrollTop);

	//code to position the popup when it is hiding in the bottom
	var mouseYpos = e.clientY;
	var bodyHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	if ((mouseYpos+obj.clientHeight+offset)>bodyHeight) {
		tempY=tempY-obj.clientHeight;
	}

	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;
		case 'centerinwindow':
			var winHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
			var scrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
			var winWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
			var scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
			//tempX = (document.body.clientWidth - obj.clientWidth)/2 + document.body.scrollLeft;
			tempX = (winWidth - 964)/2 + scrollX + 194;
			tempY = (winHeight - obj.offsetHeight)/2 + scrollY;
			//console.log('winWidth='+winWidth+', tempX='+tempX+', tempY='+tempY);
			break;
                                            case 'positiontop':
                                            var winWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
                                            var scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
                                            tempX = (winWidth - 964)/2 + scrollX + 194;
                                             tempY = 100;
                                           break;
		default:
		case 'anchorcenter': /// anchorCenter.
//      alert('before: tempX = ' + tempX);
//      alert('obj.clientWidth = ' + obj.clientWidth);
//      alert('offset = ' + offset);
      
      tempX=_snapX(tempX-(obj.clientWidth/2)+offset)
			tempY=tempY-(obj.clientHeight/2)+offset
      
//      alert('after: tempX = ' + tempX);

      // make sure the right of the popup is not off the main page
      if((tempX + obj.clientWidth) > 964) { tempX = 964 - obj.clientWidth - 80; }
      
      // make sure the left of the popup positive
      if(tempX < 30) { tempX = 30; }
      
			break;
      
      // make sure the left of the popup positive
      if(tempX < 30) { tempX = 30; }

      //Detect IE
      version=0
      if (navigator.appVersion.indexOf("MSIE")!=-1) {
        temp=navigator.appVersion.split("MSIE")
        version=parseFloat(temp[1])
      }

      // IE
      if (version > 0) {
        // make sure popup is below the start of the visible client area
        if(tempY < scrollTop) {
          tempY = scrollTop + 30
        }

        if (document.getElementById("tabSoaPageBgResize")) {
          // make sure the bottom of the popup is above the bottom of the visible client area
          if((tempY + obj.clientHeight) > (document.getElementById("tabSoaPageBgResize").clientHeight)) {
//            tempY = document.body.scrollHeight - obj.clientHeight - 240
            tempY -= obj.clientHeight
          }
        } else {
          // make sure the bottom of the popup is above the bottom of the visible client area
          if((tempY + obj.clientHeight) > (scrollTop  - 340)) {
            tempY = document.body.scrollHeight - obj.clientHeight - 380
          }
        }

      // Non-IE
      } else {
        // make sure popup is below the start of the visible client area
        if(tempY < window.scrollY) {
          tempY = window.scrollY + 30
        }

        // make sure the bottom of the popup is above the bottom of the visible client are
        if (document.getElementById("tabSoaPageBgResize")) {          
          if((tempY + obj.clientHeight) > (document.getElementById("tabSoaPageBgResize").clientHeight)) {
            tempY -= obj.clientHeight
          } else if((tempY + obj.clientHeight) > (window.scrollY + document.body.scrollHeight - 340)) {
            tempY = window.scrollY + document.body.scrollHeight - obj.clientHeight - 240
          }
        }
      }

      // make sure the top of the popup is below the global header
      if(tempY < 50)
        tempY = 50;
      
	}
	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(anchor)}
}
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=''}
	//if(ref){ref.style.display='none'}
	//$('mPopShadow').style.display='none'
}

var popMovieCrmEvent = function () {}; // do nothing

function startPopMovie(movieName, movieTitle, stopEvent){
  popMovieCrmEvent = stopEvent;
  $("movieCode").innerHTML = '<embed type="application/x-shockwave-flash" src="' + movieName + '" pluginspage="http://www.macromedia.com/go/getflashplayer" play="true" loop="false" menu="false" quality="high" scale="exactfit" wmode="transparent" width="490" height="400">';
  $("movieTitle").innerHTML = movieTitle;
  return false;
}

function stopPopMovie(){
  $("movieCode").innerHTML = "";
  if (popMovieCrmEvent) popMovieCrmEvent();
  return false;
}

