var map_options, map, cses_marker, cses_infowindow, vcuc_marker, vcuc_infowindow;
var markers = Array();
var info_windows = Array();

jQuery(document).ready(
  function () {
    // Define the map options
    map_options = {
      zoom: 15,
      center: new google.maps.LatLng(37.545428, -77.450266),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: false
    };
    // Generate with the options
    map = new google.maps.Map(document.getElementById("gmap"), map_options);
    
    // Generate the Clark Springs Elementary School marker
    markers[0] = new google.maps.Marker({
      position: new google.maps.LatLng(37.539643, -77.45739), 
      map: map, 
      title:"Clark Springs Elementary School"
    });
    
    var cses_content = "<strong>Clark Springs Elementary School</strong><br />1101 Dance St.<br />Richmond, VA 23220";
    
    info_windows[0] = new google.maps.InfoWindow({
        content: cses_content
    });
    
    google.maps.event.addListener(markers[0], 'click', function() {
      info_windows[0].open(map, markers[0]);
    });
    
    // Generate the VCU Student Commons Marker
    markers[1] = new google.maps.Marker({
      position: new google.maps.LatLng(37.54806, -77.44969),
      map: map,
      title: "Pace Center"
    });
    
    var vcuc_content = "<strong>Pace Center</strong><br />700 West Franklin St.<br />Richmond, VA 23220";

    info_windows[1] = new google.maps.InfoWindow({
        content: vcuc_content
    });
    
    google.maps.event.addListener(markers[1], 'click', function() {
      info_windows[1].open(map, markers[1]);
    });
    
    // Assign click events to the links on the page.
    jQuery("#main-content a.map_marker_link").click(marker_click);
  }
);

/**
 * Executed when a marker link is selected
 * 
 * @param e
 *   The event from the click
 * @return
 *   FALSE to stop the event chain
 */
function marker_click(e) {
  var classes = jQuery(this).attr("class").split(" ");
  
  for (var i = 0; i < classes.length; i++) {
    var class = classes[i];
    
    if (class.substring(0, 7) == "marker_") {
      var index = Number(class.substring(7));
      
      info_windows[index].open(map, markers[index]);
    }
  }
  
  return false;
}