var map;
var geocoder;
var currentMarker;
var myPoints = new Array();

function showMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		GEvent.addListener(map, "load", manageMap);
		geocoder = new GClientGeocoder();
		map.setMapType(G_NORMAL_MAP);
		map.setCenter(new GLatLng(42, 10.5), 5);
		
		var customUI = map.getDefaultUI();
		customUI.controls.scalecontrol = false;
		customUI.maptypes.physical = false;
		map.setUI(customUI);
		//map.setUIToDefault();
		
	}
}


// va fino a un punto
function animateToPoint(p) {    
	map.panTo(p);
	map.setZoom(15);
}



function addMarkerFromAddress(address) {
	geocoder.getLatLng(address, addMarkerFromPoint);
}

function addDraggableMarkerFromAddress(address) {
	geocoder.getLatLng(address, addDraggableMarkerFromPoint);
}

function addMarkerFromPoint(p, htmlMessage, htmlMessageOpen, clickUrl) {
	if (!p) {
	}
	else {
		
		var clubIcon = new GIcon(G_DEFAULT_ICON);
		clubIcon.image = "http://www.freakclown.it/img/gmaps_marker5.png";
		clubIcon.shadow  = "http://www.freakclown.it/img/gmaps_shadow.png";
		clubIcon.iconSize = new GSize(14, 58);
		clubIcon.shadowSize = new GSize(60, 58);
		clubIcon.iconAnchor = new GPoint(5, 57);

		markerOptions = { draggable: false, icon:clubIcon };
		
		currentMarker = new GMarker(p, markerOptions);
		map.addOverlay(currentMarker);
		myPoints.push(p);
		//currentMarker.openInfoWindowHtml(address);
	}
	
	if (clickUrl) {
		GEvent.addListener(currentMarker, "click", function() {
			window.location.href=clickUrl;
		});
	}
	
	if (htmlMessage) {
		GEvent.addListener(currentMarker, "mouseover", function() {
			this.openInfoWindowHtml(htmlMessage);
		});
		if (htmlMessageOpen) {
			currentMarker.openInfoWindowHtml(htmlMessage);
		}
		else {
			GEvent.addListener(currentMarker, "mouseout", function() {
				map.closeInfoWindow();
			});
		}
	}
}

function addDraggableMarkerFromPoint(p) {
	if (!p) {
	}
	else {
		currentMarker = new GMarker(p, {draggable: true});
		GEvent.addListener(currentMarker, "dragend", function(pn) {
			if (document.getElementById('latitude_id')) {
				document.getElementById('latitude_id').value = pn.lat();
			}
			if (document.getElementById('longitude_id')) {
				document.getElementById('longitude_id').value = pn.lng();
			}
		});
		if (document.getElementById('latitude_id')) {
			document.getElementById('latitude_id').value = p.lat();
		}
		if (document.getElementById('longitude_id')) {
			document.getElementById('longitude_id').value = p.lng();
		}
		map.addOverlay(currentMarker);
		
		map.setCenter(p, 15);
		
		myPoints.push(p);
	}
}

function resizeMap() {
	var latlngbounds = new GLatLngBounds();
	for (var i = 0; i < myPoints.length; i++) {
		latlngbounds.extend(myPoints[i]);
	}
	map.setCenter(latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds));
}

