var project_ar;
function mapCreateCallback(pSet) {

  if (map1.identifyPopup) map1.identifyPopup.kill();

  //here's where i append this new layer to the map
  var baseUrl = 'http://' + appbase + '/' + pSet.getParameterValues('submit_to') + '?' + pSet.getParameterQueryString();

  var id = pSet.getParameterValues('layer_id');
  var name = pSet.getParameterValues('submit_name');
  var title = pSet.getParameterValues('submit_name');
  var zdepth = pSet.getParameterValues('zdepth');

  
  //see if this is in the mapobjects wmsLayers dictionary
  var found = false;
  var wms = map1.wmsLayers;
  for (var i = 0; i < wms.length; i++) {
    if (wms[i].id == id) {
      found = true;
      //change the baseurl if found
      wms[i].baseUrl = baseUrl;
      wms[i].title = title;
      wms[i].name = name;
      break;
    }
  }
  //if not found, push it on the stack  
  if (!found) {
    map1.wmsLayers.push(new WmsLayer(name, title, id, baseUrl, -1*zdepth, null, null, 0, 10, null,null,null,8307,'1.1.1','getmap','wms','image/png',100,true));
  }
  
  //remove old layer-
  map1.removeLayer(id);
  
  //append new layer
  map1.appendLayer(id);
  
  map1.currentParameterSet = pSet;
  
  
  var args = '';

  var t_args = pSet.getParameterQueryString().split('&query_id=map');
  for (var i = 0; i < t_args.length; i++) {
    args += t_args[i];
  }
  args += '&query_id=projectquery';


  if (project_ar) project_ar.abort();
  project_ar = new AjaxRequest();
  //fill in project list
  project_ar.asyncRequest('GET', 'projectquery', args, fillList);

}

function fillList() {
  document.getElementById('project_list-area').innerHTML = project_ar.getResponseText();
  project_ar = false;
}



function highlightTool(node) {

  var tools = getElementsByClassName(document, 'out_of_map_tool');

  for (var i = 0; i < tools.length; i++) {
    tools[i].className = 'out_of_map_tool';
  }

  node.className += ' selected';
}

function identify1(event, map, c) {

  event = event || window.event;

//lon lat map


  var coords = getRelativeCoords(event, map.pane);

  var ll = map.getLatLonFromPixel(coords[0], coords[1]);
  var lon = ll[0];
  var lat = ll[1];
  
  idshort(lon, lat, map, c);

  
  map.animateMove(event);  
}  
  
function idshort(lon, lat, map, c) {  
  
    
  var contents = 'Contents';
  var args = '';

  
 
  if (map.currentParameterSet) {
    var t_args = map.currentParameterSet.getParameterQueryString().split('&query_id=map');
    for (var i = 0; i < t_args.length; i++) {
      args += t_args[i];
    }
    
    args += '&BBOX=' + map.getViewportBoundingBoxString();
  }
 
  args += '&query_id=identify';
  args += '&mapscale=' + map.getScale();
  args += '&lat=' + lat;
  args += '&lon=' + lon;
  

  

  if (map.identifyPopup) map.identifyPopup.kill();
  var ajaxfill = false;
  if (!c) {
    c = 'Identifying...';
    ajaxfill = true;
  }
  
  map.identifyPopup = new IdentifyPopup(
    map,
    'Identify',
    c,
    200,
    200,
    lat,
    lon  
  );
  
  if (ajaxfill) map.identifyPopup.fillContentsAJAX('identify', args);
  
  return true;
}


function callIdentify1(lon, lat, id, title, project_number, chief) {
  var s = '<a style="font-weight:bold" href="#" onclick="';
  s += "popWindow('identifyProject.jsp?id=" + id + "')";
  s += '; return false;">' + project_number + ' - ' + title + '</a><br/>';
  s += chief.replace(/;/g,'<br/>').replace(/\^/g,'"') + '<br/>';
  idshort(lon, lat, map1, s);
}

var id_window = false;
function popWindow(url) {

  if (id_window) {
    id_window.close();
    id_window = false;
  }
  id_window = window.open(url, "IdentifyProject","scrollbars,menubar,resizable=yes,width=750,height=300");
}

function resetOptions() {
  document.getElementById('team_id').selectedIndex = 0;
  document.getElementById('chief_id').selectedIndex = 0;
  document.getElementById('cooperator_id').selectedIndex = 0;
  document.getElementById('congress_id').selectedIndex = 0;
  document.getElementById('county_id').selectedIndex = 0;
  document.getElementById('keyword').value = '';
}

function or_with(sel, with_sel) {

    document.onmouseup = function() {
      setTimeout(function() {
        if (sel.selectedIndex != 0) {
          with_sel.selectedIndex = 0;
        } else {
          sel.selectedIndex = 0;
        }
        document.onmouseup = null;
      }, 1)
    }
}