var map = null;
var geocoder = null;

//Load function for Google Maps
function load() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	//map.addControl(new GSmallMapControl());
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	//map.setMapType("hybrid");
	//map.setMapType('map');
    //map.setMapType('satellite');
	//map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(39.95222, -75.16417), 13);	
	geocoder = new GClientGeocoder();
  }
}

//Label class for markers
function markerLabel(label, address, phone, website, date, time, comment) {
	this.label = label;
	this.address = address;
	this.phone = phone;
	this.website = website;
	this.date = date;
	this.time = time;
	this.comment = comment;
	return this;
}

//Entry point when a link is clicked
function onEventClick(label, address, phone, website, date, time, comment) {
	var newLabel = markerLabel(label, address, phone, website, date, time, comment);
	map.clearOverlays();
	showAddress(newLabel);
	scroll(0,0);
}

//Geocode address and center map
function showAddress(label) {
  if (geocoder) {
	geocoder.getLatLng(
	  label.address,
	  function(point) {
		if (!point) {
		  alert(label.address + " not found");
		} else {
		  map.setCenter(point, 13);
		  createMarker(point, label);
		}
	  }
	);
  }
}

// Creates a marker at the given point with the given number label
function createMarker(point, label) { 
  var labelString = "";
  if(label.label != 'null') 	{	labelString += "<strong>" + label.label + "</strong><br/>";  						}
  if(label.address != 'null') 	{	labelString += label.address + "<br/>";						 						}
  if(label.phone != 'null') 	{	labelString += label.phone + "<br/>";						 						}
  if(label.website != 'null') 	{	labelString += "<a target='Event Website' href='" + label.website + "'>" + 
  													label.website + "</a><br/>";										}
  if(label.date != 'null') 		{	labelString += "<br/> Event Details: " + label.date;
									if(label.time != 'null') {	labelString += " (" + label.time + ")";	}				}
  if(label.comment != 'null') 	{	labelString += "<br/>" + label.comment;												}
 
  if(label.address != 'null')   {	
	//labelString += "<a href='http://www.google.com'>google</a><br>";
	labelString += "<a target='Directions' href='http://maps.google.com/maps?daddr=" + label.address + "&saddr= '>Driving Directions</a>";
	//labelString += "<br><a href='http://www.google.com'>google</a>";
  }
  
  var marker = new GMarker(point);
  map.addOverlay(marker);
  marker.openInfoWindowHtml(labelString);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(labelString);
  });
  
  //alert(labelString)
}
