/**
 * This JS file contains common functions used by both displayGoogleMap.js and displayCampusMap.js.
 * 
 * @author Momchil Kyurkchiev
 * @date March, 2007
 */

/* Common Display Code */

// This function is run when a label is clicked in the scrollable list of markers
// to the bottom of the map. 
function onMarkerClick(i) {
  markers[i].gmarker.openInfoWindowHtml('<div style="width: 300px;"><b>' + markers[i].label + 
      '</b><br/>' + unescape(markers[i].html) + '</div>');
  selectElemOnSidebar(i);
}    

// When a marker is clicked, the appropriate marker label in the scrollable list of markers 
// is highlighted in grey.
function selectElemOnSidebar(i) {
  // Deselect all current marker labels. 
  for (j = 0; j < markers.length; j++) {
    document.getElementById('item' + j).style.background = '#FFF';
  }
  // Select the marker we need. 
  document.getElementById('item' + i).style.background = '#EEE';
}

// display the either the campus or the world map depending on the supplied "maptype" 
// attribute in the markers XML file. 
function displayMap(mapType, markers) {
  gmap = new GMap2(document.getElementById("map"));
  if (mapType == 'googlemap') {
    displayGoogleMap(gmap, markers);
  } else if (mapType == 'campusmap') {
    displayCampusMap(gmap, markers);
  }
} 

// given the current set of marker, we set the center of the map so that all markers 
// are visible.
function setMapCenterViaBounds(gmap) {
  var centerLat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
  var centerLng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
  gmap.setCenter(new GLatLng(centerLat, centerLng)); 
}

// all markers are stored in a global markers array of Marker objects. 
function placeMarkersOnMap(map, markers) {  
  // no markers to place
  if (markers.length == 0)
  {
    return;
  }
  // Create a sidebar with a list of all items. 
  var sidebar_html = '<table width="100%" cellspacing="0px" cellpadding="0px" border="0px">';
  for (i = 0; i < markers.length; i++) {
    map.addOverlay(markers[i].gmarker);

    // Add a new item to the sidebar
    var letter = String.fromCharCode("A".charCodeAt(0) + i%26);
    sidebar_html += 
        '<tr><td align="center" valign="top" width="40px"><img alt="Item" src="http://www.google.com/mapfiles/marker' + letter + '.png"/></td>' + 
        '<td><div class="sidebarItem" id="item' + i + '">' + 
        '<span class="sidebarTitle"><a href="javascript:onMarkerClick(' + i + ')">' + markers[i].label + '</a></span>' +
        '</div></td></tr>';
  } 
  sidebar_html += '</table>';
  // put the assembled sidebar_html contents into the sidebar div
  document.getElementById("sidebar").innerHTML = sidebar_html;
}

/* End of Common Display Code */
