var geocoder = null;
var m= null;
google.load("maps", "2");

function InitializeGoogleMap()
{
	var existsMap=document.getElementById("googleMap");
	if (existsMap &&  GBrowserIsCompatible())
	{
		geocoder = new GClientGeocoder();
		m = new google.maps.Map2(document.getElementById("googleMap"));
		//m.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
		var bounds = new google.maps.LatLngBounds();
		m.addControl(new google.maps.LargeMapControl());
		m.addControl(new google.maps.MapTypeControl());
		var dir=document.getElementById("CatalystLocation");
		showAddress(dir.innerHTML,bounds);
		m.setCenter(bounds.getCenter());
		m.setMapType(G_HYBRID_MAP);
		m.checkResize();
	}
}

google.setOnLoadCallback(InitializeGoogleMap);

function showAddress(address,bounds)
{
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              	m.setCenter(point,15);
		bounds.extend(point); 
              	//Create the marker setting the direction and the HTML to show in the info window
		var marker = createMarker(point,'110 Marina Drive Rochester NY, 14626','<b>Address:</b><br/>110 Marina Dr <br/> Rochester, NY 14626<br/>')
		m.addOverlay(marker);

            }
          }
        );
      }
}

var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function createMarker(point,name,html) {
	var marker = new GMarker(point);
				
	// The info window version with the "to here" form open
	to_htmls[i] = html + '<div class="endAddress"><br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
	'<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
	'<input type="text" SIZE=20 MAXLENGTH=40 name="saddr" id="saddr" value="" />' +
	'<INPUT value="Get Directions" TYPE="SUBMIT">' +
	'<input type="hidden" name="daddr" value="' + name + '"/><br><br></div><br><br/>';
		
	// The info window version with the "to here" form open
	from_htmls[i] = html + '<div class="endAddress"><br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
	'<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank"> ' +
	'<input type="text" SIZE=20 MAXLENGTH=40 name="daddr" id="daddr" value="" />' +
	'<INPUT value="Get Directions" TYPE="SUBMIT"> ' +
	'<input type="hidden" name="saddr" value="' + name + '"/><br><br></div><br><br/>';
			
	// The inactive version of the direction info
	html = html + '<br> Directions: <a href="javascript:tohere('+i+')"> To here</a> - <a href="javascript:fromhere('+i+')"> From here</a> ';
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	marker.openInfoWindowHtml(html);
	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	return marker;
}