var nav_bounds;
var map;
var poligono;
var zoom_min = 10;
var zoom_max = 18;
var marker;
var geocoder;


function initializeHomeMap() {
	geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(44.088399492553606, 10.44868497851561);
    var myOptions = {
      zoom: 10,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
	  mapTypeControl: true,
	  //mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
	  navigationControl: true,
	  navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL}
    
	
	};
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	
	/*
	var latlngNE = new google.maps.LatLng(44.604196	, 10.693567	);
	var latlngSW = new google.maps.LatLng(43.871168 , 10.078833);
   */
   
   	var latlngNE = new google.maps.LatLng(44.36879876648653, 11.066552410156235);
	var latlngSW = new google.maps.LatLng(43.79761472736458, 9.926447429687485);
   
   
   nav_bounds = new google.maps.LatLngBounds(latlngSW,latlngNE);
	
	google.maps.event.addListener(map, 'center_changed', function() {
		checkBounds();
	});
	
	
	
	/*google.maps.event.addListener(map, 'dragend', function() {
		alert('bound: '+map.getBounds()+" \ncenter: "+map.getCenter());
	});
	*/
	
	google.maps.event.addListener(map, 'zoom_changed', function(event) {
		var actual = map.getZoom();
		if (actual < zoom_min)
			map.setZoom(zoom_min);
		else if (actual > zoom_max)
			map.setZoom(zoom_max);
			
		//checkBounds();
	});
  	
	placeMarkers();
}


function placeMarkers(){
	
	var image = new google.maps.MarkerImage('icona-segnalazione.png', new google.maps.Size(33,42), new google.maps.Point(0,0));
   
	
	for ( var i = 0; i < markArr.length; i++ ){
		// Create the marker...
	 
		var latlng = new google.maps.LatLng(markArr[i].lat,markArr[i].lng);
		var marker = new google.maps.Marker({
			position: latlng,
			visible: true,
			clickable: true,
			icon: image,
			map: map
		}); 
	 	
		
		markArr[i].marker = marker ;
		//markArr[i].html = "This is content for infoWindow " + i + " at " + markArr[i].lat + "," + markArr[i].lng ;
	 
		// Create the infoWindow...
		
		markArr[i].infoWindow = new google.maps.InfoWindow({
		 content: markArr[i].html
		});
	 
		// Create the listener with a closure...
	 
		markArr[i].listener = makeClosure(i, markArr[i].marker) ;
		
		
		
		//google.maps.event.addListener(marker, "click", function(e) {
      	//var infoBox = new InfoBox({latlng: marker.getPosition(), map: map, html:markArr[i].html });
    	//});

		
   }
	
}

lastI = -1 ;

// Make a simple closure with the listener... 
function makeClosure( i, marker )
{
	
	var listener = google.maps.event.addListener(marker, 'click', function() {
		openInfoWindow(i) ;		// <-- this is the key to making it work
	
	});
	return listener ;
}


// Open the infoWindow - called from the closure...
function openInfoWindow(i)
{
	/*alert(typeof(lastI)+" -- "+typeof(markArr[lastI].infoWindow));
	if ( typeof(lastI) == 'number' && typeof(markArr[lastI].infoWindow) == 'object' )
	{ 
		markArr[lastI].infoWindow.close() ;
	}
	*/
	if (markArr && markArr[lastI] && markArr[lastI].infoWindow)
		markArr[lastI].infoWindow.close() ;
	
	lastI = i ;    
	markArr[i].infoWindow.open(map,markArr[i].marker) ;    
}


var aberration = 0.2; // this value is a good choice for germany (?!)
			
function checkBounds() { 
	// Perform the check and return if OK 
	var currentBounds = map.getBounds();
	
	if (!currentBounds) return;
	
	var cSpan = currentBounds.toSpan(); // width and height of the bounds 
	var offsetX = cSpan.lng() / (2+aberration); // we need a little border 
	var offsetY = cSpan.lat() / (2+aberration); 
	var C = map.getCenter(); // current center coords 
	var X = C.lng(); 
	var Y = C.lat(); 
	// now check if the current rectangle in the allowed area 
	var checkSW = new google.maps.LatLng(C.lat()-offsetY,C.lng()- offsetX); 
	var checkNE = new google.maps.LatLng(C.lat()+offsetY,C.lng()+offsetX); 
	if (nav_bounds.contains(checkSW) && 
	   nav_bounds.contains(checkNE)) { 
	   return; // nothing to do 
	} 
	var AmaxX = nav_bounds.getNorthEast().lng(); 
	var AmaxY = nav_bounds.getNorthEast().lat(); 
	var AminX = nav_bounds.getSouthWest().lng(); 
	var AminY = nav_bounds.getSouthWest().lat(); 
	if (X < (AminX+offsetX)) {X = AminX + offsetX;} 
	if (X > (AmaxX-offsetX)) {X = AmaxX - offsetX;} 
	if (Y < (AminY+offsetY)) {Y = AminY + offsetY;} 
	if (Y > (AmaxY-offsetY)) {Y = AmaxY - offsetY;} 
	map.setCenter(new google.maps.LatLng(Y,X)); 
	return; 
 } 
