$(document).ready(function() {

	counter = 0;
	$('.crai_geocode').each(function() {
	
		counter++;
		
		var addresses = $(this).text().split(':');

		
		var id = 'crai_geocode_' + counter;
		$(this).html('<div style="width: 100%; height: 100%;" id="' + id + '"></div>');
		var map = new google.maps.Map(document.getElementById(id), { zoom: 18, mapTypeId: google.maps.MapTypeId.ROADMAP });
		var geocoder = new google.maps.Geocoder();
		
		//cAddr = 1;
		
		for (addrIdx in addresses) {
			address = addresses[addrIdx];
			address = address.replace("C.DA ", "CONTRADA ");
			address = address.replace("SCALO ", " ");
			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
						//image: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + cAddr + '|ff776b'
					});
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
			//cAddr++;
		}
		
	});
	
	counter = 0;
	$('#crai_geocode_list').each(function() {
	
		counter++;
		var id = 'crai_geocode_list_' + counter;
		$(this).html('<div style="width: 100%; height: 100%;" id="' + id + '"></div>');
		var map = new google.maps.Map(document.getElementById(id), { zoom: 7, mapTypeId: google.maps.MapTypeId.ROADMAP });
		var geocoder = new google.maps.Geocoder();
		
		$('.crai_geocode_list_data').each(function() {
			var address = $(this).find('.crai_geocode_list_addr').text();
			address = address.replace("C.DA ", "CONTRADA ");
			address = address.replace("SCALO ", " ");
			var info = $(this).find('.crai_geocode_list_info').html();
			var code = $(this).find('.crai_geocode_list_code').text();
			var coord = $(this).find('.crai_geocode_list_coord').text();
			coordArray = coord.split(',');
			if (coordArray[0] != '' && coordArray[1] != '') {
				var marker = new google.maps.Marker({
					map: map, 
					position: new google.maps.LatLng(coordArray[0],coordArray[1]),
					title: address,
					icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + code + '|cc0100|ffffff'
				});	
				google.maps.event.addListener(marker, 'click', function() {
					new google.maps.InfoWindow({content: info}).open(map,marker);
				});
			} else {
				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: address,
							icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + code + '|cc0100|ffffff'
						});
						google.maps.event.addListener(marker, 'click', function() {
							new google.maps.InfoWindow({content: info}).open(map,marker);
						});
					} else {
						//alert(status);
					}
				});
			}
			
		});
		
	});
	
	
	
});
