//===============================================================================
// Name:     xww_popup.js
// Purpose:  Pop-up text box
// Date:     2005-07-06
// Version:  1.0.1
// Author:   X. Jian
// Usage:
//   1. Insert the below code into the <body> section 
//      <script language='JavaScript' src='scripts/xpopup.js'></script>
//   2. Add the following inside any link that will contain a popup:
//      onmouseover="showPopup('pop-up message','status-message', 
//                             'font-color', 'background-color', fontSize, boxWidth)"; 
//      onmouseout="hidePopup()";
//===============================================================================
var Xoffset= 10;          
var Yoffset= 10;          
var MacIE50 = (IsMac && IsIE50)? 1 : 0;
var objChild;
var timerID = null;
var theBox = null;
var isLoaded = 0;
getWinWH();
if (IsNN4) {
   window.document.write("<layer id=xpopup visibility='hide' z-index='200'></layer>");
}
setPopup();
function setPopup() {
  if (IsNN4) {
   theBox = document.xpopup
  }
  else if (IsDOM) {
    theBox = document.createElement("DIV");
    theBox.style.visibility = 'hidden'; 
    theBox.style.position = 'absolute';
    theBox.style.zIndex=200;
    document.body.appendChild(theBox);
  }
  theBox.Open=IsNN4? openNN : openDOM;
  theBox.Close=IsNN4? closeNN : closeDOM;
}
function showPopup(msg, msg2, fontColor, bgColor, fontSize, boxWidth, boxMode) {
   
   fontColor = fontColor? fontColor : 'black';
   bgColor = bgColor? bgColor : 'lightyellow';
   fontSize  = fontSize? fontSize : 12;
	if (boxWidth <= 0) {
		boxWidth = ''
	}
	else {
   	boxWidth = boxWidth?boxWidth:200;
	}

	boxMode = boxMode? boxMode : 'text';
	if (boxMode == 'text') {
	   msg2 = msg2?msg2:msg;
	}
   var winWH = getWinWH();
   var winW = winWH[0];
   var winH = winWH[1];
   var x0= DocX + Xoffset
   if ((x0 + boxWidth - ScrollX) > winW) {
      x = ScrollX + winW - boxWidth - 2 * Xoffset;
   }
   else { x = x0}
	
   var h='';
   var y = DocY + Yoffset;
   var optWidth = "width=" + boxWidth;
   var fontSizes = new Array(8, 10, 12, 14, 18, 24, 36);
   if (fontSize >= 1 && fontSize <= 7) {
      var msg_width = (msg.length) * fontSizes[fontSize-1];
      if (msg_width < boxWidth) optWidth = '';
   }
   var w = boxWidth;
 
   theBox.Open(msg, x, y, w, h, fontSize, fontColor, bgColor, boxMode);
   if (msg2) {

      if (IsNN4) {window.status=msg2}
      else {
			timerID = setTimeout("window.status='"+msg2+"'",100)
		}
   }
   return true;
}
function popupHTML(msg, msg2, boxWidth, fontColor, bgColor, fontSize) {
   boxMode = 'html';
	site_nos = msg.match(/USGS (\d+)/);
	if (site_nos.length) {	
	   msg += "<img src='wwapps/ww_chart.php?i=nwis&site_no=" + site_nos[1] + "' border=1 />";
	   msg += "<img src='wwapps/ww_chart.php?i=ahps&site_no=" + site_nos[1] + "' border=1 />";
		//alert(site_nos);
	}
	//alert(msg);
	showPopup(msg, msg2, fontColor, bgColor, fontSize, boxWidth, boxMode);
   return true;
}
function popupText(msg, fontSize, fontColor, boxWidth, bgColor) {
   boxMode = 'text';
	msg2='';
	showPopup(msg, msg2, fontColor, bgColor, fontSize, boxWidth, boxMode);
   return true;
}
function popupTable(msg, msg2, boxWidth, fontColor, bgColor, fontSize) {
   boxMode = 'table';
	showPopup(msg, msg2, fontColor, bgColor, fontSize, boxWidth, boxMode);
   return true;
}
function hidePopup(){
   theBox.Close();
   if (IsMac) {window.status=''}
   clearTimeout(timerID);
   window.status='';   
}
function openDOM(msg, x, y, w, h, fontSize, fontColor, bgColor, boxMode) {
	var blnBorder = 0;
   if (this.childNodes.length > 0) {
      for (var i = 0; i < this.childNodes.length; i++) {
          this.removeChild(this.childNodes[i])
      }
   }
	this.style.fontSize=fontSize+'px';
	this.style.display = '';
	this.style.color=fontColor;
   this.style.backgroundColor=bgColor;
	if (boxMode == 'text') {
		theBox.innerHTML = ''
      var objTextNode = window.document.createTextNode(msg);
      theBox.appendChild(objTextNode);
		if (w) {
		  	this.style.display = '';
   	   this.style.width= '';
   	   this.style.left = "0px";
		   this.style.top = "0px";	
		   var bW = this.offsetWidth;

			w = (bW > w)? w : '';
		}
   	h = '';
		blnBorder = 1;
   	if (MacIE50) {
     	 	x = ((x + boxWidth) > 670) ? (670 - boxWidth) : x;
   	}
	}
	else {
		theBox.innerHTML = msg;
	}

   this.style.textAlign='left';
	this.style.border=(blnBorder)? 'solid black 1px' : '';
	
	if (w) {
   	this.style.width= w + "px"
		boxWidth = w
	}
	else {
   	this.style.width= '';
   	this.style.left = "0px";
		this.style.top = "0px";	
		boxWidth = this.offsetWidth;
	}
   this.style.left = x + "px";
	this.style.top = y + "px";
	var winWH = getWinWH();
   var winW= winWH[0];
	var winH= winWH[1];
	if (x + boxWidth - ScrollX - winW > 0) {
		var tmp = ScrollX + winW - boxWidth  - Xoffset;
		this.style.left = tmp + "px" 
	}

	boxHeight = this.offsetHeight;
	if (y + boxHeight - ScrollY - winH > 0) {
		var tmp = ScrollY + winH - boxHeight  - 2 * Yoffset; 
		this.style.top = tmp + "px"
	}
	this.style.visibility = 'visible';
}
function closeDOM() {
  this.style.display = 'none';
}
function openNN(msg, x, y, w, h, fontSize, fontColor, bgColor) {
   fontSize *= 0.8;
   var content="<table width="+w+" BORDER=1 CELLPADDING=0 CELLSPACING=0 "+
               "BGCOLOR="+bgColor+"><tr><td ALIGN=left><FONT COLOR="+fontColor+
               " point-size="+fontSize+">"+
               msg+"</FONT></td></tr></table>";
   this.document.write(content);
   this.document.close();
   this.visibility='show'; 'inherit';
   this.moveTo(x, y);
}
function closeNN() {
   this.visibility='hidden';
}

