//var centerLatitude = 42.3146845;
var centerLatitude = 42;

//var centerLongitude = 11.4740692;
var centerLongitude = 12.8;
var startZoom = 6;

var map;

function addMarker(latitude, longitude, description,showLogoLega) {

	if(showLogoLega){
		var legaIcon = new GIcon();
		legaIcon.iconSize = new GSize(25,25);
		legaIcon.iconAnchor = new GPoint(14,25);
		legaIcon.infoWindowAnchor = new GPoint(14,14);
		legaIcon.image = "http://reporter.puliamoilmondo.it/la.png";
	}
	else{
		var legaicon = new GIcon(G_DEFAULT_ICON);
	}

    var marker = new GMarker(new GLatLng(latitude, longitude),legaIcon);
    // var marker = new GMarker(new GLatLng(latitude, longitude));

    GEvent.addListener(marker, 'click',
        function() {
            marker.openInfoWindowHtml(description);
        }
    );

    map.addOverlay(marker);
}

function init() {
    if (GBrowserIsCompatible()) {	

		// A TextualZoomControl is a GControl that displays textual "Zoom In"
		// and "Zoom Out" buttons (as opposed to the iconic buttons used in
		// Google Maps).
		
		// We define the function first
		function TextualZoomControl() {
		}
		
		// To "subclass" the GControl, we set the prototype object to
		// an instance of the GControl object
		TextualZoomControl.prototype = new GControl();
		
		// Creates a one DIV for each of the buttons and places them in a container
		// DIV which is returned as our control element. We add the control to
		// to the map container and return the element for the map class to
		// position properly.
		TextualZoomControl.prototype.initialize = function(map) {
		  var container = document.createElement("div");
		
		  var zoomInDiv = document.createElement("div");
		  this.setButtonStyle_(zoomInDiv);
		  container.appendChild(zoomInDiv);
		  zoomInDiv.appendChild(document.createTextNode("Aumenta zoom"));
		  GEvent.addDomListener(zoomInDiv, "click", function() {
		    map.zoomIn();
		  });
		
		  var zoomOutDiv = document.createElement("div");
		  this.setButtonStyle_(zoomOutDiv);
		  container.appendChild(zoomOutDiv);
		  zoomOutDiv.appendChild(document.createTextNode("Diminuisci zoom"));
		  GEvent.addDomListener(zoomOutDiv, "click", function() {
		    map.zoomOut();
		  });
		
		  map.getContainer().appendChild(container);
		  return container;
		}
		
		// By default, the control will appear in the top left corner of the
		// map with 7 pixels of padding.
		TextualZoomControl.prototype.getDefaultPosition = function() {
		  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
		}
		
		// Sets the proper CSS for the given button element.
		TextualZoomControl.prototype.setButtonStyle_ = function(button) {
		  button.style.textDecoration = "underline";
		  button.style.color = "#0000cc";
		  button.style.backgroundColor = "white";
		  button.style.font = "small Arial";
		  button.style.border = "1px solid black";
		  button.style.padding = "2px";
		  button.style.marginBottom = "3px";
		  button.style.textAlign = "center";
		  button.style.width = "6em";
		  button.style.cursor = "pointer";
		}




        map = new GMap2(document.getElementById("mappa"));
        // map.addControl(new GSmallMapControl());
		map.addControl(new TextualZoomControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        map.addControl(new GMapTypeControl());

        for(id in markers) {
        	if(markers[id].latitude != undefined){
            	addMarker(markers[id].latitude, markers[id].longitude, markers[id].name,true);
        	}
        }
    }
}


function init_cercapunto(finded,lat,long,cod) {
	
	if(finded==true){
		centerLatitude=lat;
		centerLongitude=long;
		startZoom=12;
	}
	var codice = cod;
	//alert(centerLatitude + " " + centerLongitude);

    if (GBrowserIsCompatible()) {	

		// A TextualZoomControl is a GControl that displays textual "Zoom In"
		// and "Zoom Out" buttons (as opposed to the iconic buttons used in
		// Google Maps).
		
		// We define the function first
		function TextualZoomControl() {
		}
		
		// To "subclass" the GControl, we set the prototype object to
		// an instance of the GControl object
		TextualZoomControl.prototype = new GControl();
		
		// Creates a one DIV for each of the buttons and places them in a container
		// DIV which is returned as our control element. We add the control to
		// to the map container and return the element for the map class to
		// position properly.
		TextualZoomControl.prototype.initialize = function(map) {
		  var container = document.createElement("div");
		
		  var zoomInDiv = document.createElement("div");
		  this.setButtonStyle_(zoomInDiv);
		  container.appendChild(zoomInDiv);
		  zoomInDiv.appendChild(document.createTextNode("Aumenta zoom"));
		  GEvent.addDomListener(zoomInDiv, "click", function() {
		    map.zoomIn();
		  });
		
		  var zoomOutDiv = document.createElement("div");
		  this.setButtonStyle_(zoomOutDiv);
		  container.appendChild(zoomOutDiv);
		  zoomOutDiv.appendChild(document.createTextNode("Diminuisci zoom"));
		  GEvent.addDomListener(zoomOutDiv, "click", function() {
		    map.zoomOut();
		  });
		
		  map.getContainer().appendChild(container);
		  return container;
		}
		
		// By default, the control will appear in the top left corner of the
		// map with 7 pixels of padding.
		TextualZoomControl.prototype.getDefaultPosition = function() {
		  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 360));
		}
		
		// Sets the proper CSS for the given button element.
		TextualZoomControl.prototype.setButtonStyle_ = function(button) {
		  button.style.textDecoration = "underline";
		  button.style.color = "#0000cc";
		  button.style.backgroundColor = "white";
		  button.style.font = "small Arial";
		  button.style.border = "1px solid black";
		  button.style.padding = "2px";
		  button.style.marginBottom = "3px";
		  button.style.textAlign = "center";
		  button.style.width = "6em";
		  button.style.cursor = "pointer";
		}




        map = new GMap2(document.getElementById("mappa"));
        map.addControl(new GSmallMapControl());
		map.addControl(new TextualZoomControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        map.addControl(new GMapTypeControl());

		addMarker(centerLatitude, centerLongitude, 'Posizione attuale del punto di pulizia',false);
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);


	    GEvent.addListener(map, "click", function(overlay, latlng) {
	        //only perform the click if the window is closed and the click was directly on the map.
			map.clearOverlays();
	        if(!overlay) {
		        //retrieve the longitude and lattitude of the click point
		        var lng = latlng.lng();
		        var lat = latlng.lat();
		        map.addOverlay(new GMarker(latlng));
		        document.getElementById("conferma-punto").innerHTML = "<span class=\"tasto\"><a href=\"/registra-punto/"+ codice + "/" + lat + "/" + lng + "\"><span class=\"istruzioni\">Conferma la posizione indicata e</span> procedi</a></span>";
		        document.getElementById("conferma-punto-2").innerHTML = "<span class=\"tasto\"><a href=\"/registra-punto/"+ codice + "/" + lat + "/" + lng + "\"><span class=\"istruzioni\">Conferma la posizione indicata e</span> procedi</a></span>";
	
	        }
	    });
    }
}


function map_show_point(latitudine, longitudine) {
	//map.clearOverlays();
	// addMarker(latitudine, longitudine, 'Questo &egrave; il punto che hai cercato',false);
	map.setCenter(new GLatLng(latitudine, longitudine), 8);

}
function show_minimap(lat,lon) {
    if (GBrowserIsCompatible()) {	
        map = new GMap2(document.getElementById("mappa"));
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(lat, lon), 7);
		addMarker(lat, lon, 'Punto di pulizia',false);
    }
}


// window.onload = init;
window.onunload = GUnload;