var geocoder;
var map;
var markersArray = [];
//var bounds;

function initialize_map()
{
	var myLatlng = new google.maps.LatLng(49.894634,13.886719);
	var myOptions = {
		zoom: 3,
		center: myLatlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false
	}
	
	geocoder = new google.maps.Geocoder();
	
	map = new google.maps.Map(document.getElementById("dealer_map"), myOptions);
	/*
	google.maps.event.addListener(map, 'idle', function() {
		bounds = map.getBounds();
	});
	*/
}

function add_spillo(indirizzo)
{
	var id = indirizzo.attr("id");
	var geo = indirizzo.attr("rel");
	var text = indirizzo.parent().html();
	var image = 'images/icoMap1.png';
	if (geo != "") {
		indirizzo = $.secureEvalJSON(geo);
		var myLatlng = new google.maps.LatLng(indirizzo.lat,indirizzo.long);
		var marker = new google.maps.Marker({
			position: myLatlng, 
			map: map,
			icon: image
		});
		var infowindow = new google.maps.InfoWindow({
			content: text
		});
		google.maps.event.addListener(marker, 'click', function () {
			infowindow.open(map, marker);
		});
		markersArray.push(marker);
		//bounds.extend(myLatlng);
    	//map.fitBounds(bounds);
	} else {
		indirizzo = indirizzo.val();
		geocoder.geocode({
			'address': indirizzo
		}, function(res, stato) {
			if (stato == google.maps.GeocoderStatus.OK)
			{
				var marker = new google.maps.Marker({
					position: res[0].geometry.location, 
					map: map,
					icon: image
				});
				var infowindow = new google.maps.InfoWindow({
					content: text
				});
				google.maps.event.addListener(marker, 'click', function () {
					infowindow.open(map, marker);
				});
				var latlong = {
					lat: res[0].geometry.location.lat(),
					long: res[0].geometry.location.lng()
				};
				$.post(
					"save_geo.php",
					{
						geocode: $.toJSON(latlong),
						id: id
					}
				);
				markersArray.push(marker);
				//bounds.extend(res[0].geometry.location);
    			//map.fitBounds(bounds);
			}
		});
	}
}

function centra_mappa(coords)
{
	var lat = coords.lat();
	var lon = coords.lng();
	
	var max_lon = map.getBounds().getNorthEast().lng();
	var max_lat = map.getBounds().getNorthEast().lat();
	var min_lon = map.getBounds().getSouthWest().lng();
	var min_lat = map.getBounds().getSouthWest().lat();
	
	max_lon = Math.max(lon, max_lon);
	min_lon = Math.min(lon, min_lon);
	max_lat = Math.max(lat, max_lat);
	min_lat = Math.min(lat, min_lat);
	
	var ne = new google.maps.LatLng(max_lat, max_lon);
	var sw = new google.maps.LatLng(min_lat, min_lon);
	
	var nuovi_bordi = new google.maps.LatLngBounds(sw, ne); console.log(nuovi_bordi);
	
	map.fitBounds(nuovi_bordi);
}

function deleteOverlays(indirizzo) {
	if (markersArray) {
		for (i in markersArray) {
			markersArray[i].setMap(null);
		}
		markersArray.length = 0;
	}
	
	/*setTimeout(function(){
		var start = new Date().getTime();
		while ((new Date().getTime() - start) < 10000){
		// Do nothing
		}
	},0);*/
	
	geocoder.geocode({
		'address': indirizzo
	}, function(res, stato) {
		if (stato == google.maps.GeocoderStatus.OK)
		{
			map.fitBounds(res[0].geometry.bounds);
			//map.setCenter(res[0].geometry.location);
		}
	});
}
