//map
var map;
var geocoder;
var luoghi=new Array();	
var maxLatidute=41.889919;
var minLatidute=41.889919; 
var maxLongitude=12.469159;
var minLongitude=12.469159;
var porcessedPoint=0;
var baseIcon;

//var baseIcon = new GIcon();
/*baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);
*/

function loadMap() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(41.889919, 12.469159), 13);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());	
	baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 20);
	baseIcon.shadowSize = new GSize(40, 20);
	baseIcon.iconAnchor = new GPoint(0, 0);
	baseIcon.infoWindowAnchor = new GPoint(10, 10);
	baseIcon.infoShadowAnchor = new GPoint(0, 0);
	map.setMapType(G_SATELLITE_TYPE);
	geocoder = new GClientGeocoder();
	calculatePoint();			
  }
}
function getCoordinate(address){
  geocoder.getLocations(address, addPoint);
}

function addPoint(response){
	place = response.Placemark[0];
	labelStr=getPerformanceInfo(response.name,place.address);
	if(labelStr==-1){
		labelStr=place.address;
	}
	point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
	map.addOverlay(createMarker(point, labelStr));
	maxLatidute = (maxLatidute < place.Point.coordinates[1]) ? place.Point.coordinates[1] : maxLatidute;
	minLatidute = (minLatidute > place.Point.coordinates[1]) ? place.Point.coordinates[1] : minLatidute;
	maxLongitude = (maxLongitude < place.Point.coordinates[0]) ? place.Point.coordinates[0] : maxLongitude;
	minLongitude = (minLongitude > place.Point.coordinates[0]) ? place.Point.coordinates[0] : minLongitude;
	porcessedPoint++;
	if(porcessedPoint==luoghi.length){
		var myBounds=new GLatLngBounds(new GLatLng(maxLatidute, minLongitude),  new GLatLng(minLatidute, maxLongitude));
		map.setCenter(myBounds.getCenter(), map.getBoundsZoomLevel(myBounds));
	}
}

function calculatePoint(){
	for (key in luoghi)   {
		getCoordinate(luoghi[key][0]);
	}	
}
function createMarker(point, str_label) {
  var icon = new GIcon(baseIcon);
  icon.image = "http://www.fotografiafestival.it/img/ico.gif";
  icon.shadow = "http://www.fotografiafestival.it/img/shadow.png";
  var marker = new GMarker(point,icon);
  GEvent.addListener(marker, "mouseover", function() {
	marker.openInfoWindowHtml(str_label,{maxWidth:200});
  });
  return marker;
}

function getPerformanceInfo(addrs,myGaddrs){
	trovato=-1;
	for (key in luoghi){
		if( trim(luoghi[key][0].toLowerCase()) == trim(addrs.toLowerCase()) ){
			trovato="<b>"+luoghi[key][1]+"</b><br/>"+myGaddrs+"<br/>"+luoghi[key][2];
		}
	}
	return trovato;
}

function trim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;	
}

