var geocoder;
var map;
var bounds;
var infowindow = null;
var aziende = new Array();
var texts = new Array();
var addresses = new Array();
var image;
var timer = 0;
function initialize_google_map() {
	geocoder = new google.maps.Geocoder();
	//lat_lng = codeAddress('Aosta');
	bounds = new google.maps.LatLngBounds();
	var latlng = new google.maps.LatLng(42.56262, 12.64207); //Terni - centro italia
	var myOptions = {
	  zoom: 8,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP,
	  scrollwheel: false
	};
	map = new google.maps.Map($("google_map"), myOptions);
	
	//alert(provincia_name);
	//center_map(provincia_name);
	setMarkers(map, provincia, provincia_name);
	
}

function setMarkers(map, sigla, location) {
  	var response = "";
	//alert('?provincia=' + sigla +  '&regione=' + location);
	//new Ajax.Request('/' + sf + 'ajax/google_map.asp?provincia=' + sigla + '&page=' + page + '&tipologia=' + tipologia + '&regione=' + regione + '&keyword=' + keyword,
	new Ajax.Request('/' + sf + 'ajax/google_map.asp?provincia=' + sigla + '&regione=' + location,
		{
		method:'get',
		onSuccess: function(transport){
			response = transport.responseText || "no response text";
			//alert("Success! \n\n" + response);
			
			var locations = new Array();
			locations = response.split("|");
		
			 // Add markers to the map
	
			// Marker sizes are expressed as a Size of X,Y
			// where the origin of the image (0,0) is located
			// in the top left of the image.
			
			// Origins, anchor positions and coordinates of the marker
			// increase in the X direction to the right and in
			// the Y direction down.
			image = new google.maps.MarkerImage('/' + sf + 'img/icon_map.gif',
				// This marker is 20 pixels wide by 32 pixels tall.
				new google.maps.Size(25, 25),
				// The origin for this image is 0,0.
				new google.maps.Point(0,0),
				// The anchor for this image is the base of the flagpole at 0,32.
				new google.maps.Point(0, 25));
			//var shadow = new google.maps.MarkerImage('img/paginations_off.png',
				// The shadow image is larger in the horizontal dimension
				// while the position and offset are the same as for the main image.
				//new google.maps.Size(37, 32),
				//new google.maps.Point(0,0),
				//new google.maps.Point(0, 32));
				// Shapes define the clickable region of the icon.
				// The type defines an HTML <area> element 'poly' which
				// traces out a polygon as a series of X,Y points. The final
				// coordinate closes the poly by connecting to the first
				// coordinate.
			var shape = {
				coord: [1, 1, 1, 25, 25, 25, 25 , 1],
				type: 'poly'
			};
			
			for (var i = 0; i < locations.length; i++) {
				str_loc = locations[i];
				loc_array = new Array();
				loc_array = str_loc.split("*");
				aziende[i] = loc_array[0];
				addresses[i] = loc_array[1];
				//z_index = parseInt(loc_array[2]);
				texts[i] = loc_array[3];
				timer = 600*i;
				//alert(addresses[i])
				setTimeout('create_marker(\'' + i + '\')', timer);
			} // for
		},
		onFailure: function(){
			//alert('Something went wrong...');
		}
	});
	
}

function center_map(location) {
	if (geocoder) {
		geocoder.geocode( { 'address': location}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				map.setCenter(results[0].geometry.location);
			} else {
				//alert("Geocode was not successful for the following reason: " + status);
			}
		});
	} // if (
}

//function create_marker(azienda, address, text, image) {
function create_marker(i) {

	
	if (geocoder) {
		geocoder.geocode( { 'address': addresses[i]}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				//map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					position: results[0].geometry.location,
					map: map,
					title: aziende[i],
					//shadow: shadow,
					icon: image
					//shape: shape,
					//zIndex: z_index
				});
				
				if (!infowindow) {
					//var g_size = new google.maps.Size(30, 30);
					infowindow = new google.maps.InfoWindow({
						content: texts[i]
						//pixelOffset: g_size
						//disableAutoPan: true
					});
				}
			
				google.maps.event.addListener(marker, 'click', function() {
					infowindow.setContent(texts[i]);
					infowindow.open(map,marker);
				});
				
				bounds.extend(results[0].geometry.location);

				map.fitBounds(bounds);
				
			} else {
				//$("map_status").update($("map_status").innerHTML + "<br />" + status)
				//alert("Geocode was not successful for the following reason: " + status);
			}
		});
	} // if (geocoder)
	
}
