var downx=0;
var downy=0;
var upx=0;
var upy=0;
var currx=0;
var curry=0;
var mtoggle=0;
var imgsizex=0;
var imgsizey=0;
var hover;
var driftcounter = 0;

function show_tab(selectedtab) {
  var currdiv;
  tablist = new Array ("layer","overview","help","search","q","about");
  for(tabidx in tablist) {
    divname = tablist[tabidx] + "div"
    currdiv = top.document.getElementById(divname);
    if ( tablist[tabidx] == selectedtab ) {
      currdiv.style.visibility = 'visible';
      top.document.getElementById(tablist[tabidx]).style.background="#c0c9c0";
      }
    else {
      currdiv.style.visibility = 'hidden';
      top.document.getElementById(tablist[tabidx]).style.background="#f0f0f0";
      }
  }
}

function downxy(e) {
if(mtoggle==0) {
  if(navigator.appName == 'Netscape') {
    downx=e.pageX;
    downy=e.pageY;
    button=e.which;
    }
  else {
    downx=event.clientX;
    downy=event.clientY;
    button=event.button;
    }

  if(button == '1') {
    mtoggle=1;
    if(document.mapform.z.value == '1' || document.mapform.z.value == '5' || document.mapform.z.value == '6') {
      rbox=document.getElementById("rboxov");
      rbox.style.left = downx;
      rbox.style.top = downy;
      rbox.style.width = 0;
      rbox.style.height = 0;
      rbox.style.visibility = 'visible';
      }
    }
  }
}

function upxy(e) {
  if(mtoggle == '1') {

  if(navigator.appName == 'Netscape') {
    upx=e.pageX;
    upy=e.pageY;
    button=e.which;
    }
  else {
    upx=event.clientX;
    upy=event.clientY;
    button=event.button;
    }

  mtoggle=0;
  func=document.mapform.z.value;
  rbox=document.getElementById("rboxov");
  rbox.style.visibility = 'hidden';
  ex=parseFloat(document.mapform.ex.value);
  ey=parseFloat(document.mapform.ey.value);
  scale=ex*2/imgsizex;
  ulx = parseFloat(document.mapform.x.value)-(scale*(imgsizex/2));
  uly = parseFloat(document.mapform.y.value)+(scale*(imgsizey/2));

  switch(func) {

    // zoom in box
    case '1': boxheight=parseFloat(rbox.style.height)*scale;
              newey = boxheight/2;
              newscale = boxheight/imgsizey;
              newex = imgsizex * newscale / 2;
              break;

    // zoom in fixed
    case '2': newex = ex/2; 
              newey = ey/2;
              break;

    // query & pan
    case '6': 
    case '3': newex=ex;
              newey=ey; 
              break;

    // zoom out fixed
    case '4': newex = ex*2; 
              newey = ey*2;
              break;

    // zoom out box
    case '5': boxheight = parseFloat(rbox.style.height)*scale;
              zoomratio = (ey*2)/boxheight;
              newey = ey * zoomratio;
              newex = ex * zoomratio;
              break;
    } 

  if(newex>150 && newex<800000) {
  
    if(func == '6') {
      document.mapform.target=top.frames['querywin'].name;
      document.mapform.action="/imap/query.php";
      show_tab("q");
      }
    else {
      document.getElementById("messagescrn").style.visibility = 'visible';
      document.mapform.target=top.frames['mapdisplay'].name;
      document.mapform.action="/imap/genmap.php";
      }

    if(func == '3') {
      document.mapform.x.value = parseFloat(document.mapform.x.value) - (scale*(currx-downx));
      document.mapform.y.value = parseFloat(document.mapform.y.value) + (scale*(curry-downy));
      }
    else if(func == '6') {
      x1=parseFloat(rbox.style.left);
      y1=parseFloat(rbox.style.top);
      x2=x1+parseFloat(rbox.style.width);
      y2=y1+parseFloat(rbox.style.height);
      document.mapform.qminx.value = ulx+(scale*x1);
      document.mapform.qminy.value = uly-(scale*y2);
      document.mapform.qmaxx.value = ulx+(scale*x2);
      document.mapform.qmaxy.value = uly-(scale*y1);
      document.mapform.sf.value = 4;
      }
    else if(func == '1' || func == '5') {
      cx=parseFloat(rbox.style.left)+(parseFloat(rbox.style.width)/2);
      cy=parseFloat(rbox.style.top)+(parseFloat(rbox.style.height)/2);
      document.mapform.x.value = ulx+scale*cx;
      document.mapform.y.value = uly-scale*cy;
      document.mapform.ex.value = newex;
      document.mapform.ey.value = newey;
      }
    else {
      document.mapform.x.value = ulx+(scale*downx);
      document.mapform.y.value = uly-(scale*downy);
      document.mapform.ex.value = newex;
      document.mapform.ey.value = newey;
      }
    document.mapform.submit();
    }
  else
    alert('attempted extent (' + newex + ' meters) exceeds limit');
  }
  }

function outxy(e) {
  if(mtoggle==1 && document.mapform.z.value == '3')
    upxy(e);
 }

function mm(e) {

  // graceful disposal of popup query window after mouse starts moving again
  clearTimeout(hover);
  if(document.getElementById("iscrn").style.visibility == 'visible') {
    driftcounter += 1;
    if(driftcounter > 3) {
      document.getElementById("iscrn").style.visibility = 'hidden';
      driftcounter=0;
      }
    }

  if(navigator.appName == 'Netscape') {
    currx = parseFloat(e.pageX);
    curry = parseFloat(e.pageY);
    }
  else {
    currx = parseFloat(event.clientX);
    curry = parseFloat(event.clientY);
    }

  if(mtoggle==1) {
    if(document.mapform.z.value==3) {
      ml=document.getElementById("map");
      ml.style.left = currx - downx;
      ml.style.top  = curry - downy;
      }
    else if(document.mapform.z.value==1||document.mapform.z.value==5||document.mapform.z.value==6){
      rbox=document.getElementById("rboxov");
      if(currx>downx) {
         rbox.style.left = downx;
         rbox.style.width = currx-downx;
         }
      else {
         rbox.style.left = currx;
         rbox.style.width = downx-currx;
         }
      if(curry>downy) {
        rbox.style.top = downy;
        rbox.style.height = curry-downy;
        }
      else {
        rbox.style.top = curry;
        rbox.style.height = downy-curry;
         }
      }
    }
  else {
    var ex=parseFloat(document.mapform.ex.value);
    var ey=parseFloat(document.mapform.ey.value);
    var scale=ex*2/imgsizex;
    var ulx = parseFloat(document.mapform.x.value)-(scale*(imgsizex/2));
    var uly = parseFloat(document.mapform.y.value)+(scale*(imgsizey/2));
    var utmx = ulx+(scale*currx);
    var utmy = uly-(scale*curry);
    top.document.forms['coordform'].elements['xcoord'].value = Math.floor(utmx) + "E";
    top.document.forms['coordform'].elements['ycoord'].value = Math.floor(utmy) + "N";


    func=document.mapform.z.value;
    if(func == '6') {
      var timeoutaction = "check_hover(" + utmx + "," + utmy + "," + scale + ")";
      hover = setTimeout(timeoutaction,400);
      }

    }
  }

function check_hover(utmx,utmy,scale) {
  qurl = "http://gis.wvdep.org/imap/hquery.php?x=" + utmx + "&y=" + utmy + "&q=" + document.mapform.q.value + "&qradius=" + scale*4;
  q = new XMLHttpRequest();
  q.open("GET",qurl);
  q.onreadystatechange = return_hover;
  q.send(null);
  }

function return_hover() {
  if(q.readyState == 4) {
    msgdiv = document.getElementById("iscrn");
    if(q.responseText.length > 1) {
      mapwidth = document.getElementById("md").style.width;
      mapwidth = mapwidth.substr(0,mapwidth.indexOf("px"));
      iwidth= msgdiv.style.width;
      iwidth = iwidth.substr(0,iwidth.indexOf("px"));
      if(mapwidth-currx < iwidth)
        msgdiv.style.left = currx - iwidth;
      else
        msgdiv.style.left = currx;

      mapheight = document.getElementById("md").style.height;
      mapheight = mapheight.substr(0,mapheight.indexOf("px"));
      iheight= msgdiv.style.height;
      iheight = iheight.substr(0,iheight.indexOf("px"));
      if(curry < iheight)
        msgdiv.style.top = curry;
      else
        msgdiv.style.top = curry - iheight;

      msgdiv.innerHTML = q.responseText;
      msgdiv.style.visibility = 'visible';
      driftcounter=0;
      }
    }
  }

  


