// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function add_fields(link, association, content) {
 	var new_id = new Date().getTime();
  	var regexp = new RegExp("new_" + association, "g")
  	$(link).up().insert({
    	before: content.replace(regexp, new_id)
  	});
}

function initialize(element, address, marker_title) { 
	var latlng = new google.maps.LatLng(-34.397, 150.644);
	var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false
	};
	var map = new google.maps.Map(document.getElementById(element), myOptions);
	codeAddress(map ,address, marker_title);	
}

function codeAddress(map, app_address, marker_title) {
	var address = app_address;
	var geocoder = new google.maps.Geocoder();
	if (geocoder) {
		geocoder.geocode({
			'address': address
		}, function(results, status){
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					map: map,
					position: results[0].geometry.location,
					title: marker_title,
					clickable: true
				});
				attachInfowindow(marker, map, marker_title);
			}
			else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
		
	}
} 

function attachInfowindow(marker, map, marker_title) {
	var infowindow = new google.maps.InfoWindow({ 
 		content: marker_title
 	});
	infowindow.open(map,marker);
 	google.maps.event.addListener(marker, 'click', function() {
 		infowindow.open(map,marker);
 	});
} 