var map;
var gdir;
var geocoder;
var addressMarker;
var mostraItalia=0;
var zoom=0;

var disableControlMap=1;

var georef = new Array();
var htmlPoint = new Array();

var puntoVetrinaPoint = new Array();
var puntoVetrinaAddress = new Array();

var gmarkers = new Array();
var countMarkers = 1;
var tooltip = document.createElement("div");

var showFumetto=0;

var _mTerms = "Terms of Use";

function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		//map.setCenter(new GLatLng(42, 13), 5);
		map.setCenter(new GLatLng(44, 10), 4);
		
		if (disableControlMap) {
			map.disableDragging();
		} else {
			map.enableDoubleClickZoom();
			map.enableContinuousZoom();
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GLargeMapControl());					// per tooltip
			map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);		// per tooltip
		}
		//map.enableScrollWheelZoom();
		
		
		gdir = new GDirections(map, document.getElementById("directions")); // direzioni
        GEvent.addListener(gdir, "error", handleErrors);
		
		// georeferenziazione
		geocoder = new GClientGeocoder();
		if (georef.length>0) {
			for (i=0; i<georef.length; i++) {
				showAddress (georef[i],htmlPoint[i]);
			}
		}
		
		// crea marker da coord e fumetto esploso
		if (puntoVetrinaPoint.length>0) {
			for (i=0; i<puntoVetrinaPoint.length; i++) {
				var marker=showPoint (puntoVetrinaPoint[i][0],puntoVetrinaPoint[i][1],htmlPoint[i]);
				marker.openInfoWindowHtml('<div class="fumettoMap">'+htmlPoint[i]+'</div>');
			}
		}
		
		// crea marker da indirizzo e fumetto esploso
		if (puntoVetrinaAddress.length>0) {
			for (i=0; i<puntoVetrinaAddress.length; i++) {
				showFumetto=1;
				showAddress (puntoVetrinaAddress[i],htmlPoint[i]);
			}
		}
		
		if (zoom) map.setZoom(zoom);
	}
}
function createMarker(point, htmli) {
	var icon = new GIcon(); 
	icon.image = "../immagini/iconMap/icona.png"; 
	//icon.iconSize = new GSize(34, 37);
	//icon.iconAnchor = new GPoint(15, 34);
	//icon.infoWindowAnchor = new GPoint(10, 18);
	icon.iconSize = new GSize(20, 22);
	icon.iconAnchor = new GPoint(10, 22);
	icon.infoWindowAnchor = new GPoint(10, 18);

	var marker = new GMarker(point, icon);
	marker.tooltip = '<img src="../immagini/iconMap/icona.gif">';
	
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml('<div class="fumettoMap">'+htmli+'</div>');});
	
	gmarkers[countMarkers]=marker;
	countMarkers++;

	return marker;
}

function setDirections (fromIndirizzo,fromCitta,fromProvincia,toIndirizzo,toCitta,toProvincia) {
	gdir.load("from: " + fromIndirizzo + " " + fromCitta + " " + fromProvincia + " to: " + toIndirizzo + " " + toCitta + " " + toProvincia, { "locale": "it" });
	document.getElementById("sponsor").style.display="none"; // nascondo i banner
}

function handleErrors(){
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
		
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
		
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	
	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	 
	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
	else
		alert("An unknown error occurred.");
   
}


function showAddress(address,html) {
	if (geocoder) {
		geocoder.getLatLng(
		address,
		function (point) {
			if (point) {
				if (zoom)
					map.setCenter(point);
				markerTMP=createMarker(point,html);
				map.addOverlay(markerTMP);
				
				if (showFumetto) markerTMP.openInfoWindowHtml('<div class="fumettoMap">'+html+'</div>');
			} else {
				//alert(address + " non trovato");
				countMarkers++; // per non falsare le ricerche
			}
		}
		);
	}
}

function showPoint(coordX,coordY,html) {
	if (coordX && coordY) {
		var marker=createMarker(new GLatLng(coordX,coordY),html);
		map.addOverlay(marker);
	}
	return marker;
}

function showTooltipMap(marker) {
	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x, offset.y - point.y - anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
} 
function hideToolTipMap() {
	tooltip.style.visibility="hidden";
}

