var GOOGLE = {
	load: function(){
		if (GBrowserIsCompatible()){
			GOOGLE.geocoder = new GClientGeocoder();
			GOOGLE.map = new GMap2(document.getElementById('connect-map'));
			GOOGLE.map.addControl(new GLargeMapControl());
			GOOGLE.map.addControl(new GMapTypeControl());
			GOOGLE.map.setCenter(new GLatLng(37.0625, -95.677068), 4);
		} else {
			alert("It does not appear that your browser is compatible with Google Maps.");
		}
	},

	/**
	 * Searches for the locations that are near
	 */
	searchLocationsNear: function (center, radius){

		var path = window.location.pathname.split('/');
		url = '/ajax/google_maps/';
		url += ( path[1] == 'about' ) ? 'Patient' : 'HCP' ;

		$.post( url ,{ 'Latitude':center.lat(),'Longitude':center.lng() ,'Radius':radius }, function(data){

			GOOGLE.map.clearOverlays();
			$('#connect-sidebar').html('').hide();

			if(data == 'error'){
				$('#connect-sidebar').html('<p>Sorry, we couldn\'t find any information about <em>Conversation Map</em><sup>&#174</sup>' +
					'facilitators in the region you specified. Please try your search again, or contact us at ' + 
					'<a href="mailto:learn@healthyinteractions.com">learn@healthyinteractions.com</a></p>' ).fadeIn('normal');
			} else {
				$('#connect-sidebar').html('<h3>Your search found information on the following facilities. Click on a location below for more educator information.</h3>');
				var results = jsonParse(data);
				var len = results.markers.length;
				var bounds = new GLatLngBounds();

				for(var i=0; i<len; i++){
					var point = new GLatLng(parseFloat(results.markers[i].Latitude), parseFloat(results.markers[i].Longitude) );
					var marker = GOOGLE.createMarker(point, results.markers[i].Workplace, results.markers[i].Bubble, results.markers[i].Information);
					GOOGLE.map.addOverlay(marker);
					bounds.extend(point);
					GOOGLE.addSidebarEntry(marker, results.markers[i].Workplace, results.markers[i].Address, results.markers[i].Distance);
				}
				GOOGLE.map.setCenter(bounds.getCenter(), GOOGLE.map.getBoundsZoomLevel(bounds));
				$('#connect-sidebar').stripe({selector:'div:visible'}).fadeIn('normal');
			}

		});
	},

	createMarker: function(point, workplace, bubble, info) {
	  var marker = new GMarker(point);
	  var html =	'<div class="google-overlay"><p><strong>' + workplace + '</strong><br />' +
					bubble + '</p><p><strong>Educator(s):</strong><br />' + info + '</p></div>';

	  GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	  });
	  return marker;
	},

	addSidebarEntry: function(marker, name, address, distance) {
	  var div = document.createElement('div');
	  var html = '<strong>'+name + '</strong> (' + parseFloat(distance).toFixed(1) + ' Miles)<br />' + address;
	  div.innerHTML = html;
	  GEvent.addDomListener(div, 'click', function() {
		GEvent.trigger(marker, 'click');
	  });
	  $('#connect-sidebar').append(div);
	}

};

$( function(){
	$('#addy_form').submit(function(){
		var address = this.Address.value;
		GOOGLE.geocoder.getLatLng(address, function(latlng) {

		if (!latlng) {
		  alert(address + ' not found');
		} else {
		  GOOGLE.searchLocationsNear(latlng, $('select[name=Radius]').val());
		}

	  });

		return false;
	});
});

GOOGLE.load();