﻿var ImageMap = function()
{
	var hfMap;
	function SetValue(map, latlng)
	{
		hfMap.value = map.getZoom() + '#' + latlng.lat() + '#' + latlng.lng();
	};

	var map;

	var geocoder = new GClientGeocoder();

	function GetMap(ModeView, IndirizzoComune, Provincia, DivMap, Icon, ImageMap)
	{
		geocoder.getLatLng
		(
			IndirizzoComune,
			function(point)
			{
				if (!point)
					GetMap(ModeView, Provincia, Provincia, DivMap, Icon, ImageMap)
				else
				{
					map = new GMap2(DivMap);

					if (hfMap.value == '')
						hfMap.value = '15##';

					if (hfMap.value.split('#')[1] != '' && hfMap.value.split('#')[2] != '')
						point = new GLatLng(eval(hfMap.value.split('#')[1]), eval(hfMap.value.split('#')[2]));

					map.setCenter(point, eval(hfMap.value.split('#')[0]));

					var iconColor = new GIcon();
					iconColor.shadow = Icon;
					iconColor.iconSize = new GSize(70, 55);
					iconColor.shadowSize = new GSize(70, 55);
					iconColor.iconAnchor = new GPoint(32, 50);
					iconColor.infoWindowAnchor = new GPoint(30, 50);

					var marker = new GMarker(point, iconColor);

					map.addOverlay(marker);

					//if (!ImageMap)
					//{
						map.addControl(new GSmallMapControl());
						map.addControl(new GMapTypeControl());
						//map.addControl(new GLargeMapControl());
					//}

					if (!ModeView && !ImageMap)
					{
						SetValue(map, map.getCenter());

						GEvent.addListener
						(
							map,
							'zoomend',
							function(oldzoomlevel, zoomlevel)
							{
								hfMap.value = zoomlevel + '#' + hfMap.value.split('#')[1] + '#' + hfMap.value.split('#')[2];
							}
						);
						GEvent.addListener
						(
							map,
							'click',
							function(overlay, latlng)
							{
								SetValue(map, latlng);
								marker.setLatLng(latlng);
							}
						);
					}

					//if (ImageMap)
					//{
					//	DivMap.removeChild(DivMap.childNodes.item(2)); //logo Google
					//	DivMap.removeChild(DivMap.childNodes.item(1)); //link "termini e condizioni d'uso"
					//}
				}
			}
		);
	};

	return {
		GoogleMap: function(ModeView, Provincia, Comune, Indirizzo, GMap, Icon, ImageMap, HfID, PnlID)
		{
			if (document.getElementById("ContenitoreMap") != null) return;

			var mapold;
			if (ModeView)
			{
				hfMap = document.createElement("input");
				hfMap.setAttribute("type", "hidden");
				hfMap.value = GMap;
			}
			else
			{
				hfMap = document.getElementById(HfID);
				mapold = hfMap.value;
			}

			var DivMap = document.createElement("div");
			DivMap.style.width = "400px";
			DivMap.style.height = "400px";

			if (ImageMap)
				DivMap = document.getElementById(PnlID);

			GetMap(ModeView, Indirizzo + ' ' + Comune + ' ' + Provincia, Provincia, DivMap, Icon, ImageMap);

			if (ImageMap) return;

			var ContenitoreMap = document.createElement("div");
			ContenitoreMap.id = "ContenitoreMap";
			var objBody = document.getElementsByTagName("BODY")[0];
			ContenitoreMap.style.position = "fixed";
			ContenitoreMap.style.left = ((screen.availWidth / 2) - 200) + "px";
			ContenitoreMap.style.top = ((screen.availHeight / 2) - 200) + "px";
			ContenitoreMap.style.width = "400px";
			ContenitoreMap.style.height = "400px";
			ContenitoreMap.style.borderStyle = "solid";
			ContenitoreMap.style.borderWidth = "1px";
			objBody.appendChild(ContenitoreMap);

			var Bordo = document.createElement('div');
			Bordo.style.position = "absolute";
			Bordo.style.left = "-10px";
			Bordo.style.top = "-25px";
			Bordo.style.width = "420px";
			Bordo.style.height = "435px";
			Bordo.style.borderWidth = "1px";
			Bordo.style.borderStyle = "solid";
			ContenitoreMap.appendChild(Bordo);

			ContenitoreMap.appendChild(DivMap);

			var Chiudi = document.createElement('div');
			Chiudi.style.position = "absolute";
			Chiudi.style.left = "0px";
			Chiudi.style.top = "-20px";
			Chiudi.style.cursor = 'pointer';
			Chiudi.innerHTML = ModeView ? 'Chiudi' : 'Salva';
			Chiudi.onclick = function()
			{
				var objBody = document.getElementsByTagName("BODY")[0];
				objBody.removeChild(document.getElementById("ContenitoreMap"));
			}
			ContenitoreMap.appendChild(Chiudi);

			if (!ModeView)
			{
				var Annulla = document.createElement('div');
				Annulla.style.position = "absolute";
				Annulla.style.left = "50px";
				Annulla.style.top = "-20px";
				Annulla.style.cursor = 'pointer';
				Annulla.innerHTML = 'Annulla';
				Annulla.onclick = function()
				{
					var objBody = document.getElementsByTagName("BODY")[0];
					objBody.removeChild(document.getElementById("ContenitoreMap"));
					hfMap.value = mapold;
				}
				ContenitoreMap.appendChild(Annulla);
			}
		}
	};
} ();