	/* Make the "map" variable global so that "cityDropDown( )" and
	 * "campusDropDown( )" can find it.
	 */
	var map;
	var rightside;
	var rightsideTitle;
	var directions;
    var marker_dict = {}; //Ark indexed list of markers, for lookup

    function gup( name ) {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
            return "";
        else
            return results[1];
    }


	function cityDropDown( ) {
	  var dropdown = document.getElementById("selcity");
	  var cityvalue = dropdown.options[dropdown.selectedIndex].value;
      document.getElementById("selcampus").selectedIndex = 0;

	  /* If we didn't get an actual city, don't do anything.  */
	  if (cityvalue.length == 0) return;

	  /* Separate the latitude, longitude, and zoom.  */
	  var latlong = cityvalue.split(",");

	  /* Renter on that city's coordinates.  If the custom zoom
	   * level is not present, use zoom level 11.)
	   */
	  if (GBrowserIsCompatible( )) {
	    if (latlong.length <= 2) {
	      map.setCenter(new GLatLng(latlong[0], latlong[1]), 12);
	    }
	    else {
	      map.setCenter(new GLatLng(latlong[0], latlong[1]),
		(latlong[2] - 0));
	    }
	  }
	  dropdown.blur( );
	}

	function campusDropDown( ) {
	  var dropdown = document.getElementById("selcampus");
	  var campusvalue = dropdown.options[dropdown.selectedIndex].value;
      document.getElementById("selcity").selectedIndex = 0;

	  /* If we didn't get an actual campus, don't do anything.  */
	  if (campusvalue.length == 0) return;

	  /* Separate the latitude, longitude, and zoom.  */
	  var ark = campusvalue;

	  /* Renter on that campus's coordinates.  If the custom zoom
	   * level is not present, use zoom level 13.)
	   */
	  if (GBrowserIsCompatible( )) {
          zoomToMarker(ark);
	  }
	  dropdown.blur( );
	}

    function loadDirectionsFromForm(latitude, longitude, onsubmitthis){
        loadDirections(latitude, longitude, onsubmitthis.from.value);
    }

    function loadDirections(latitude, longitude, address) {
      /* Empty out the right hand side, so the directions replace
       * what's there.
       */
      //NEED TO CHECK GEOCODING FOR ADDRESS BEFORE GETTING DIRECTIONS!
      removeChildren(rightside);
      /* Put in a title for the right hand side.  */
      writeTitle(rightsideTitle, "Directions");
      //alert("THERE:" + latitude + ','+ longitude + ' | ' + address);
      directions.lat = latitude;
      directions.lng = longitude;
      directions.address = unescape(address);
      directions.load(directions.address + " to " +
	                  directions.lat + "," + directions.lng);
      //alert("HERE:" + directions.lat+ ','+ directions.lng + ' | ' + directions.address);
      }

    function loadCollection(ark) {
      /* Empty out the right hand side, so the collection list replaces
       * what's there.
       */
      removeChildren(rightside);
      instName = window.institutes[ark].name
      writeTitle(rightsideTitle, instName + " Collections");
      /* Do a lookup of the indicated institution, using an HTTP request.  */
      //GXmlHttp creates in browser independent way  
      var instLookup = GXmlHttp.create();
      var href = "/search?style=oac4;map=map;Institution=" + instName
      instLookup.open("GET", href, false);
      instLookup.send('');
      rightside.innerHTML = instLookup.responseText;
    }

    function removeChildren(node) {
      var toRemove = node.childNodes;
      for (var i = toRemove.length - 1; i >= 0; i--)
	        node.removeChild(toRemove.item(i));
    }

    function writeTitle(rightside, title) {
      /* Construct the DOM child we want to put at the beginning of
       * the <div>.
       */
      var titleText = document.createTextNode(title);
      var strongNode = document.createElement("strong");
      strongNode.appendChild(titleText);
      rightside.innerHTML = strongNode.innerHTML;
    }

    function zoomToMarkerWithDeselect(ark) {
        zoomToMarker(ark);
        document.getElementById("selcampus").selectedIndex = 0;
        document.getElementById("selcity").selectedIndex = 0;
    }

    function zoomToMarker(ark) {
        //Zoom to marker for ark
        var i = 0;
        if (marker_dict[ark] != null) {
            marker = marker_dict[ark];
            map.setCenter(marker.getLatLng(), 9);
            marker.openInfoWindowHtml(window.institutes[ark].label);
        }
    }

    function getAddressSuggestionsHandler(response) {
        //alert('Geocoder status: ' +response.Status.code);
                    if (response.Status.code === 200) {
                        if (response.Placemark.length > 0) {
                            var x = '';
                            var suggests = '<strong>Did you mean?</strong><br/>';
                            for (i=0; i < response.Placemark.length; i += 1) {
                               suggests += '<div><a href="" onclick="loadDirections('
                                        + directions.lat + ','
                                        + directions.lng + ',\''
                                        + escape(response.Placemark[i].address)
                                        + '\');return false;" > '
                                        + response.Placemark[i].address
                                        + "</a></div>";
                               for (var member in response.Placemark[i]) {
                                    x = x+ "Name:" + member + " Value: " + response.Placemark[i][member];
                               }
            
                            }
                            //alert(x);
                            //alert(suggests);
                            rightside.innerHTML = suggests;
                        }
                    }
                    else { 
                        rightside.innerHTML = directions.errormsg;
                    }

    }

    function getAddressSuggestions(address, lat, lng) {
        //alert("Error for address: " + address);
        //make a call to google geocoder
        var geocoder = new GClientGeocoder();
        geocoder.getLocations(
                address,
                getAddressSuggestionsHandler
        );
    }

    function directionsError(){
      stat = directions.getStatus();
      //handle various direction errors
      msg = 'Error from Google Map site'
      switch (stat.code){
        case G_GEO_BAD_REQUEST:
            msg = 'A directions request could not be successfully parsed. For example, the request may have been rejected if it contained more than the maximum number of waypoints allowed.';
            break;
        case G_GEO_SERVER_ERROR:
            msg = 'A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.';
            break;
        case G_GEO_MISSING_QUERY:
            msg = 'The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.';
            break;
        case G_GEO_UNKNOWN_ADDRESS:
            msg = 'No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.';
            getAddressSuggestions(directions.address, directions.lat, directions.lng);
            break;
        case G_GEO_UNAVAILABLE_ADDRESS:
            msg = 'The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.';
            break;
        case G_GEO_UNKNOWN_DIRECTIONS:
            msg = 'The GDirections object could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region.';
            break;
        case G_GEO_BAD_KEY:
            msg = 'The given key is either invalid or does not match the domain for which it was given.';
            break;
        case G_GEO_TOO_MANY_QUERIES:
            msg = "The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time. If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the requests too quickly.";
            break;
        default:

      }
      msg = '<div>Your request could not be processed. Please check the address you entered and try again.' + '<br/><div>Address Entered: ' + escape(directions.address) + '</div>' + '<br/><br/>' + msg + '</div>';
      directions.errormsg = msg;
      if (stat.code !== G_GEO_UNKNOWN_ADDRESS) {
        rightside.innerHTML = msg;
      }
    }
        

    function load( ) {
      if (GBrowserIsCompatible() ) {
        map = new GMap2(document.getElementById("map"));
	    rightsideTitle = document.getElementById("row2-right-header");
	    rightside = document.getElementById("row2-right-content");
	    directions = new GDirections(map, rightside);
        GEvent.addListener(directions, "error", directionsError);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

        var centerLat = 37.301392;
        var centerLon = -120.486127;
        var zoomLevel = 6;

        var ark = gup('ark');
        if (ark) {
            //set center to institute location, with zoom in..
            if (window.institutes[ark]){
                centerLat = window.institutes[ark].lat;
                centerLon = window.institutes[ark].lng;
                zoomLevel = 11;
            }
        }  

        map.setCenter(new GLatLng(centerLat, centerLon), zoomLevel);
        
        // Creates a marker at the given point with the given number label
        var icon = new GIcon(G_DEFAULT_ICON);
        icon.image = "http://www.google.com/mapfiles/ms/micons/blue-dot.png";
        icon.iconSize = new GSize(32, 32);
        icon.iconAnchor = new GPoint(15, 31);
        icon.infoWindowAnchor = new GPoint(15, 2);
        
        function createMarker(point, label) {
        
          var marker = new GMarker(point, {draggable:false, icon : icon});
          marker.enableDragging();
          
          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(label);
            });
          /*
          GEvent.addListener(marker, "dragend", function() {
                  marker.openInfoWindowHtml(marker.getLatLng().toUrlValue());
                  });
                  */
          return marker;
        }

        /* Loop over the institutes object and create the markers and 
         * the righthandside list
         */
        var institute_html = '';// '<div>\n';
        var inChild = false;//for tracking child institutes
        for (institute in window.institutes) {
            var point = new GLatLng(window.institutes[institute].lat, 
                                    window.institutes[institute].lng);

            var marker = createMarker(point, window.institutes[institute].label);
            map.addOverlay(marker);

            marker_dict[institute] = marker; //Ark indexed list of markers, for lookup

            if (ark == institute) {
                //select current marker!
                marker.openInfoWindowHtml(window.institutes[institute].label);
            }

            institute_html += window.institutes[institute].html
        }
        //fill rhs block
        removeChildren(rightside);
        rightside.innerHTML = institute_html;
    }
}
