/* epm.js: Functions for South Florida EPM */

// Global variables
var EPMRunning = false;
var EPMLocOrParmWindow = null;
var EPMLoadWindow = null;
var EPMDownloadWindow = null;
var EPMPreprocWindow = null;
var EPMFragstatsWindow = null;
var EPMLocURL = "epmlocation.html";                       // URL for selecting EPM location
var EPMParmURL = "epmparms.html";                         // URL for entering EPM parms
var EPMLoadURL = "../python/epm/epmload/load_epm";
var EPMDownloadURL = "../python/download/get_download_list";
var EPMPreprocListURL = "../python/epm/preprocess/get_preproc_list";
var EPMFragstatsListURL = "../python/epm/preprocess/get_fragstats_list";
var PreprocWidth = 280;
var PreprocHeight = 240;
var epm_ulx = 0, epm_uly = 0, epm_lrx = 0, epm_lry = 0;   // For EPM bounding box

function OpenEPM() {
  if (EPMRunning) {
    alert("Please wait for current EPM process to complete.");
  }
  else if (EPMLocOrParmWindow ? EPMLocOrParmWindow.closed : true) {
    var ht = (IsChrome || IsSafari) ? 170 : 150;
    var features = "dependent,height=" + ht + ",width=210,left=" + (screen.width - 210) / 2 + ",top=" + (screen.height - ht) / 2;
    EPMLocOrParmWindow = window.open(EPMLocURL, "epmLoc", features);
    EPMLocOrParmWindow.focus();
  }
}

// only called via EPM Location window or selection box
function OpenEPMParms(ulx, uly, lrx, lry) {
  if (EPMLocOrParmWindow ? !EPMLocOrParmWindow.closed : false) EPMLocOrParmWindow.close();
  var ht = (IsChrome || IsSafari) ? 544 : 514;
  var wt = (IsIE || IsChrome || IsSafari) ? 750 : 690;
  var features = "dependent,height=" + ht + ",width=" + wt + ",left=" + (screen.width - wt) / 2 + ",top=" + (screen.height - 440) / 2;
  EPMLocOrParmWindow = window.open(EPMParmURL, "epmParm", features);
  epm_ulx = ulx;   // accessed by epmparms.html->RunEPM()
  epm_uly = uly;
  epm_lrx = lrx;
  epm_lry = lry;
  EPMLocOrParmWindow.focus();
}
      
function AddEPMLayer(runName, title, bio, tes, rare, buf, lpf, rest, ulx, uly, lrx, lry) {
  // Close any open EPM windows
  if (EPMLocOrParmWindow) EPMLocOrParmWindow.close();
  if (EPMLoadWindow) EPMLoadWindow.close();
  
  // Check if layers have already been added first
  if (map.getLayersByName("main_" + runName).length > 0) {
    alert("EPM Result " + title + " already on map.");
    document.getElementById("rdo_main_" + runName).checked = true;
    layerSwitcher.updateMap(true);
    return;
  }
  
  // Turn off current EPM layer (if any)
  document.getElementById("rdo_epmresults_none").checked = true;
  layerSwitcher.updateMap(true);
  
  // Add to EPM query list
  var epmQueryList = document.getElementById("epmQueryList");
  var lastOption = null;

  // Add the individual metric layers and construct the weight string
  var Metrics = ["Bio", "TES", "Rare", "Buf", "LPF", "Rest"];
  var MetricNames = ["Biodiversity", "T & E Species", "Rare & Unique Habitats",
                     "Water Quality Buffer", "Landscape Patterns", "Restoration"]; 
  var WtStr = new String();
  
  var newEPMMap = "results/res_" + runName;
  var newEPMLayers = new Array();    // new layers to add to map
  
  for (var m = Metrics.length - 1; m >= 0; m--) {   // reverse order so they are added to layer XML correctly
    var wt = eval(Metrics[m].toLowerCase());
    if (wt > 0) { 
      var layerName = Metrics[m].toLowerCase() + "_" + runName;
      
      // Remove .0 if it's unnecessary
      if (wt.length > 2) {
        if (wt.substr(-2) == ".0") {
          wt = wt.substr(0, wt.length - 2);
        }
      }
      
      // Create new layer
      newEPMLayers.push(
        NewMapserverLayer(layerName, newEPMMap, Metrics[m] + "=" + wt, "epmresults", false, true, overlayOpacity, true));
      
      WtStr = Metrics[m] + "=" + wt + ", " + WtStr;
      // Add to query dropdown list
      var option = document.createElement("option");
      option.value = layerName;
      option.text = title + " " + MetricNames[m];
      try {
        epmQueryList.add(option, lastOption);
      }
      catch(ex) {
        epmQueryList.add(option);    // IE
      }
      lastOption = option;
    }
  }
  var resOption = document.createElement("option");
  resOption.value = "res_" + runName;
  resOption.text = title + " Results";
  try {
    epmQueryList.add(resOption, lastOption);
  }
  catch(ex) {
    epmQueryList.add(resOption);    // IE
  }
  
  WtStr = WtStr.substring(0, WtStr.length - 2);    // remove final comma
  
  // Add new EPM layers to the map
  newEPMLayers.push(NewMapserverLayer("main_" + runName, newEPMMap, title, "epmresults", true, true, overlayOpacity, true));
  map.addLayers(newEPMLayers);
  
  EPMRunning = false;   // Make sure user can open EPM window again
  
  if (ulx == 0) {
    // use parameters from recent EPM run to determine new extent to zoom to
    if (epm_ulx == 0) {   // ran on whole county, so zoom to whole county
      map.zoomToMaxExtent();
    }
    else {
      map.zoomToExtent(new OpenLayers.Bounds(epm_ulx, epm_lry, epm_lrx, epm_uly));
    }
  }
  else {
    map.zoomToExtent(new OpenLayers.Bounds(ulx, lry, lrx, uly));
  }
  
  RefreshMarkers();
  layerSwitcher.openOrCollapse("epmresults", false);   // open EPM results group in layer list
  mainTabs.set("activeIndex", 0);                      // Set tabs to layer list
}

function LoadEPM() {
  var openNewWindow = EPMLoadWindow ? EPMLoadWindow.closed : true;
  if (!openNewWindow && navigator.appName == "Netscape") {  // To get around Firefox Bug 355482 (if Raise & Lower Windows not on)
    EPMLoadWindow.close();
    openNewWindow = true;
  }
  if (openNewWindow) {
    var ht = (IsChrome || IsSafari) ? 200 : 180;
    var features = "dependent,height=" + ht + ",width=250,left=" + (screen.width - 250) / 2 + ",top=" + (screen.height - ht) / 2;
    EPMLoadWindow = window.open(EPMLoadURL, "epmLoad", features);
  }
  EPMLoadWindow.parentOpener = this;
  EPMLoadWindow.focus();
}
      
function GetDownloadList() {
  var openNewWindow = EPMDownloadWindow ? EPMDownloadWindow.closed : true;
  if (!openNewWindow && navigator.appName == "Netscape") {  // To get around Firefox Bug 355482 (if Raise & Lower Windows not on)
    EPMDownloadWindow.close();
    openNewWindow = true;
  }
  if (openNewWindow) {
    var ht = (IsChrome || IsSafari) ? 210 : 200;
    var features = "dependent,height=" + ht + ",width=200,left=" + (screen.width - 200) / 2 + ",top=" + (screen.height - ht) / 2;
    EPMDownloadWindow = window.open(EPMDownloadURL, "epmDownload", features);
  }
  EPMDownloadWindow.parentOpener = this;
  EPMDownloadWindow.focus();
}

function GetPreprocessList() {
  var openNewWindow = EPMPreprocWindow ? EPMPreprocWindow.closed : true;
  if (!openNewWindow && navigator.appName == "Netscape") {  // To get around Firefox Bug 355482 (if Raise & Lower Windows not on)
    EPMPreprocWindow.close();
    openNewWindow = true;
  }
  if (openNewWindow) {
    var features = "dependent,height=" + PreprocHeight + ",width=" + PreprocWidth + ",left=" + (screen.width - PreprocWidth) / 2 + 
                   ",top=" + (screen.height - PreprocHeight) / 2;
    EPMPreprocWindow = window.open(EPMPreprocListURL, "epmPreprocess", features);
  }
  EPMPreprocWindow.parentOpener = this;
  EPMPreprocWindow.focus();
}

function GetFragstatsList() {
  var openNewWindow = EPMFragstatsWindow ? EPMFragstatsWindow.closed : true;
  if (!openNewWindow && navigator.appName == "Netscape") {  // To get around Firefox Bug 355482 (if Raise & Lower Windows not on)
    EPMFragstatsWindow.close();
    openNewWindow = true;
  }
  if (openNewWindow) {
    var features = "dependent,height=" + PreprocHeight + ",width=" + PreprocWidth + ",left=" + (screen.width - PreprocWidth) / 2 + 
                   ",top=" + (screen.height - PreprocHeight) / 2;
    EPMFragstatsWindow = window.open(EPMFragstatsListURL, "epmFragstats", features);
  }
  EPMFragstatsWindow.parentOpener = this;
  EPMFragstatsWindow.focus();
}

function RunPreprocess(urlfxn, gridname, email) {
  var PreprocURL = "../python/epm/preprocess/run_" + urlfxn;
  var RunParms = "ingridname=" + gridname + "&emailaddr=" + email;
  var run_xmlhttp = GetNewXMLHttpRequest();
  run_xmlhttp.open("POST", PreprocURL, true);
  run_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  run_xmlhttp.setRequestHeader("Content-length", RunParms.length);
  run_xmlhttp.setRequestHeader("Connection", "close");
  run_xmlhttp.send(RunParms);
}


