//===============================================================================
var Xoffset= 10;          
var Yoffset= 10;          
var MacIE50 = (IsMac && IsIE50)? 1 : 0;
var objChild;
var timerID = null;
var theBox = null;
var isLoaded = 0;
var theBoxs = new Array();
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=document.getElementById("box");
    theBox.style.visibility = 'hidden'; 
    theBox.style.position = 'absolute';
    theBox.style.zIndex=500;
    //document.body.appendChild(theBox);
  }
  theBox.Open=IsNN4? openNN : openDOM;
  theBox.Close=IsNN4? closeNN : closeDOM;
   for (i = 0; i <= 2; i++) {
        if (IsNN4) {
         theBoxs[i] = document.xpopup
        }
      else if (IsDOM) {
         //theBoxs[i] = document.createElement("DIV");
         theBoxs[i] = document.getElementById("box"+i);
          theBoxs[i].style.visibility = 'hidden'; 
          theBoxs[i].style.position = 'absolute';
          theBoxs[i].style.zIndex=200;
          //document.body.appendChild(theBoxs[i]);
        }
        theBoxs[i].Open=IsNN4? openNN : openDOM;
        theBoxs[i].Close=IsNN4? closeNN : closeDOM;
      theBoxs[i].name='box' + i;
  }
}
function showPopup(msg, msg2, fontColor, bgColor, fontSize, boxWidth, boxMode, boxIdx) {
   if (isNaN(boxIdx) || boxIdx < 0) {
      boxIdx = '';
   }
   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;
   aBox = (boxIdx < 0 || boxIdx == '')? theBox : theBoxs[boxIdx];
   aBox.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 popupHTML2(msg, boxIdx, make_tab, msg2, boxWidth, fontColor, bgColor, fontSize) {
   if (boxIdx >= 0) {
      closePopup();
   }
   popupHTML(msg, msg2, boxWidth, fontColor, bgColor, fontSize, boxIdx, make_tab);
}
function popupHTML(msg, msg2, boxWidth, fontColor, bgColor, fontSize, boxIdx, make_tab) {
   if (typeof boxIdx =="undefined" ||  boxIdx < 0) {
      boxIdx = ''
   }
   boxMode = 'html';
   site_nos = msg.match(/USGS .+?(\d+)/);
   if (typeof make_tab == "undefined") {make_tab = false;}
   if (site_nos.length) {   
      frm = document.getElementById('frmSelectType');
      dt = '';
      if (null != frm) {
         mt=frm.map_type.value;
         if (make_tab) {
            msg = updateMsgAsTab(msg, mt, site_nos, boxIdx);
            boxWidth = 305;
         }
         else {
            $msg = updateMsg(msg, mt, site_nos);
         }
      }
   }
   showPopup(msg, msg2, fontColor, bgColor, fontSize, boxWidth, boxMode, boxIdx);
   if (make_tab) {
      xwwtab=new ddtabcontent('wwtabs' + boxIdx)
      xwwtab.setpersist(true)
      xwwtab.setselectedClassTarget("link");
      xwwtab.init()
      xwwtab.expandit(0);
   }
   return true;
}
function doneLoadHydrograph() {
   tmp=document.getElementById("tmp");
   tmp.style.visibility="hidden";
}
function updateMsgAsTab(msg, mt, site_nos, boxIdx) {
   msg2 = '';
   xatr = 'style="width: 280px; height: 186px; border: none;" ';
   if (typeof boxIdx =="undefined" ||  boxIdx < 0) {
      boxIdx = ''
   }
   dt = (mt == 'real' || mt == 'flood')? 'uv' : 'dv';
   xmsgs = new Array();
   xtabs = {
     'NWIS': {'url': 'http://waterdata.usgs.gov/nwis/' + dt + '?site_no=', 'label': 'Hydrograph'},
     'NWIS2': {'url': 'http://waterdata.usgs.gov/nwis/' + dt + '?site_no=', 'label': 'Hydrograph'},
     'PEAK':  {'url': 'http://nwis.waterdata.usgs.gov/nwis/peak/?site_no=', 'label': 'Peak'}
   };
   if (dt == 'uv') {
      xtabs['AHPS'] = {'url': 'wwapps/ww_linkahps.php?site_no=', 'label': 'Forecast'};
   }
   for (xtab in xtabs) {
      re=new RegExp("<!--" + xtab + "-->");
      if (msg.match(re)) {
         url=xtabs[xtab]['url'];
         label=xtabs[xtab]['label'];
         src=xtab.toLowerCase(xtab);
         key=xstr.ucfirst(src);
           xmsgs[xtab]  = '<a href="' + url + site_nos[1] + '" target="_blank"><img src="wwapps/ww_chart.php?i=' + src + '&vt=' + dt + '&site_no=' + site_nos[1] + '" alt="hydrograph" ' + xatr +' /></a>';
      }
   }
   msg1='';
   if (IsFF) {
      xatr +='onload="doneLoadHydrograph()" ';
   }
   tab_title = "<div style='background-color: #000080;'><div style='float: left; color: white; font-weight: bold;'>WaterWatch: Water Resources Conditions</div><div style='text-align: right;'><input type='button' value='X' onclick='closePopup(" + boxIdx + ")' style='padding: 0px; font-weight:bold;'></div></div>"
   tab_list = '<ul id="wwtabs' + boxIdx + '" class="shadetabs">'
   tab_list += '<li><a href="#" rel="wwtab1' + boxIdx + '" class="selected">Summary</a></li>'
   xmsg='';
   tab_idx = 1;
   for (xtab in xmsgs) {
      tab_idx++;
      label = xtabs[xtab]['label'];
      tab_list += '<li><a href="#" rel="wwtab'+ tab_idx + boxIdx + '">' + label + '</a></li>';
      xmsg += '<div id="wwtab' + tab_idx + boxIdx + '" class="tabcontent">' + xmsgs[xtab] + '</div>';
   }
   tab_list += '</ul>'
   tab_content = '<div id="wwtab1' + boxIdx + '" class="tabcontent" style="text-align: left;">' + msg + '</div>';
   if (xmsg) {
      tab_content += xmsg;
   }
   omsg  = "<div class='xwwtab' style='width:305px; background-color: lightyellow; border: 0px solid black;'>"
   omsg += tab_title;
   omsg += tab_list;
   omsg += '<div style="border:1px solid gray; width:282px; margin-bottom: 1em; padding: 10px; text-align: center;">';
   omsg += tab_content;
   omsg += "</div>";
   omsg += "</div>";
   return omsg;
}
function updateMsg(msg, mt, site_nos) {
   msg = "<div style='text-align: right;'><input type='button' value='X' onclick='closePopup()'></div>" + msg
   msg2 = '';
   if (mt == 'real' || mt == 'flood') {
      dt = 'uv';
      msg2 = '<br /><img src="wwapps/ww_chart.php?i=ahps&site_no=' + site_nos[1] + '\" alt="" border="0" />';
   }
   else {
      dt = 'dv';
   }
   msg1 = '<br /><a href="http://waterdata.usgs.gov/nwis/' + dt + '?' + site_nos[1] + '" target="_blank"><img src="wwapps/ww_chart.php?i=nwis&site_no=' + site_nos[1] + "&vt=" + dt + '" alt="" border="" /></a>';
   msg += msg1 + msg2;
   return msg;
}
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 closePopup(boxIdx){
   if (boxIdx != null && boxIdx > 0) {
      theBoxs[boxIdx].Close();
   }
   else {
      theBox.Close();
   }
   if (IsMac) {window.status=''}
   clearTimeout(timerID);
   window.status='';   
}
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') {
      this.innerHTML = '';
      var objTextNode = window.document.createTextNode(msg);
      this.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 {
      this.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';
}

