// Start of Region Map Code

  var Map_WIDTH = 200;
  var Map_HEIGHT = 183;

  function Map_max(a,b) { 
    return (a>b) ? a : b;
  }
  function Map_min(a,b) { 
    return (a<b) ? a : b;
  }


  /*
   * Parses fields from the location.search (URL) get string.
   * ie: getFromURL("target"), finds target=abc and returns abc
   */
  function Map_getFromURL(field)
  {
    var loc = location.search;
    var i;

    if(loc.length == 0) { return ""; }
    loc = "&" + loc.substr(1);

    i = loc.indexOf("&"+field+"=");
    if(i < 0) { return ""; }

    loc = loc.substr(i + field.length + 2);

    i = loc.indexOf("&");
    if(i >= 0) { loc = loc.substr(0,i); }

    return loc;
  }

  function Map_resetRegionMenu()
  {
    var i = 1;
    var el = document.getElementById("Map_Sub"+i);
    while(el) {
      el.style.display = "none";
      i++; el = document.getElementById("Map_Sub"+i);
    }
  }

  function Map_resetProductMenu()
  {
    var i = 1;
    var el = document.getElementById("Map_Prod"+i);
    
    while(el) { 
      el.style.display = "none";
      i++; el = document.getElementById("Map_Prod"+i);
    }
  }


  /*
   * Strictly to toggle product menus.
   */
  function Map_setProduct(pid)
  {
    if(pid == '0') { Map_resetProductMenu(); return; }
    var el = document.getElementById("Map_Prod"+pid);

    if(!el) { return; }
    var targType = "none";
    if(el.style.display == "none") {
      if(el.tagName == "TR") {
        targType = "";
      } else {
        targType = "inline";
      }
    }
    el.style.display = targType;
  }


  /*
   * Toggles Sub-Regions, whether to display
   * a region or hide it.
   */
  function Map_setRegion(region)
  {
    if(region == '0') { Map_resetRegionMenu(); return; }
    var el = document.getElementById("Map_Sub" + region);


    if(!el) { return; }

    var targType = "none";
    if(el.style.display == "none") {
      if(el.tagName == "TR") { 
        targType = "";
      } else { 
        targType = "inline";
      }
    }
    el.style.display = targType;
  }


  var Map_thisRegion = 0;
  var Map_thisProduct = -1;
  function Map_doPageLoad() 
  {
    var loc = Map_getFromURL("r");
    if(!isNaN(loc) && loc != "") { 
      if( (location.href).indexOf("interactive-map") < 0 
        && (location.href).indexOf("data") < 0
        && (location.href).indexOf("outreach") < 0)
      { location.href = "/interactive-map/"+location.search; }
      Map_thisRegion = loc;
    }

    loc = Map_getFromURL("p");
    if(!isNaN(loc) && loc != "") { Map_thisProduct = loc; }

    if(Map_thisRegion > 0) { Map_setRegion(Map_thisRegion); }
    if(Map_thisProduct > 0) { Map_setProduct(Map_thisProduct); }
  }
  
  // window.onload = Map_doPageLoad;
