var NewPopWin = null;
var Win_Mask = null;
function initPop()
{
	var bdy = getBodySize();
	var tmpEl = document.createElement("div");
	tmpEl.className = "pagemask";
	tmpEl.style.width = bdy.width + "px";
	tmpEl.style.height = bdy.height + "px";
	tmpEl.innerHTML = "<h1>To return here, close the viewer window or refresh this page.</h1>";
	Win_Mask = document.body.appendChild(tmpEl);
	Win_Mask.onclick = function() {ontop();};
	window.onbeforeunload = function(){POP_allout();};
}
function POP_allout()
{
	if(NewPopWin)
	{
		NewPopWin.close();
		NewPopWin = null;
	}
}
function popWindow(url, windowName, windowHeight, windowWidth)
{
  var desktopHeight = screen.availHeight;
  var desktopWidth  = screen.availWidth;
  var winheight     = windowHeight?windowHeight:desktopHeight-50; 
  var winwidth      = windowWidth?windowWidth:desktopWidth-50;
  var top   = (desktopHeight - winheight) / 2;
  var left  = (desktopWidth - winwidth) / 2;
  var urlPrefix = "";
  var winurl    = url;
  var winname   = windowName;
  var winopt    = "dependent=yes toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,copyhistory=0,resizable=0,alwaysRaised=yes,";
  winopt += "width="+winwidth+",height="+winheight+",left="+left+",top="+top;
  var newwin    = window.open(winurl,winname,winopt);
  return newwin;
}
function getBodySize()
{
	if ( window.innerHeight && window.scrollMaxY )
  {
    // Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY ;
		xWithScroll = window.innerWidth + window.scrollMaxX ;
	}
  else if ( document.body.scrollHeight > document.body.offsetHeight )
  { 
    // all but Explorer Mac
		yWithScroll = document.body.scrollHeight ;
		xWithScroll = document.body.scrollWidth ;
	}
  else
  { 
      // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		  yWithScroll = document.body.offsetHeight ;
		  xWithScroll = document.body.offsetWidth ;
  }

	return { width:xWithScroll,height:yWithScroll };
}
function launchWin(url,wname,h,w)
{
	window.scrollTo(0,0);
	NewPopWin = popWindow(url,wname,h,w);
	Win_Mask.style.visibility = "visible";
}
function ontop()
{
	if(NewPopWin && !NewPopWin.closed)
	{
		NewPopWin.focus();
	}
}
var API = {	
	reset:function(){if(Win_Mask){Win_Mask.style.visibility = "hidden";}}
};

/*
//Pop Win elements 
<link rel="stylesheet" href="css/popwin.css" type="text/css" media="all"></link>
<script src="js/popwin.js" type="text/javascript"></script>
<script type="text/javascript">
	dojo.addOnLoad(initPop);
	// OR add to body tag onload attribute
</script>
*/
