/*  G_NORMAL_MAP 
    G_SATELLITE_MAP 
    G_HYBRID_MAP 
*/

/*
    var indirizzo = "via - cap - Comune (Provincia)";
	var vistaZoom = 8;
	var latitudine = 0.0;
	var longitudine = 0.0;
	var descrizione = "<b>xyz</b><br />" + indirizzo ;
	var map = new GoogleMap("map",indirizzo, vistaZoom, latitudine, longitudine, descrizione);
	map.ShowMap();
*/
function GoogleMap(IDMap, Indirizzo, vistaZoom, Latitudine, Longitudine, Descrizione) {

    if (!IDMap)
        return null;

    this.Address = null;
    if (Indirizzo)
        this.Address = Indirizzo;

    this.Zoom = null;
    if (vistaZoom)
        this.Zoom = vistaZoom;

    this.Lat = null;
    if (Latitudine)
        this.Lat = Latitudine;

    this.Lng = null;
    if (Longitudine)
        this.Lng = Longitudine;

    this.Description = '';
    if (Descrizione)
        this.Description = Descrizione;

    this.Zoom = 7;
    if (vistaZoom)
        this.Zoom = vistaZoom;

	
	var directionsService = new google.maps.DirectionsService();

	var myLatlng = new google.maps.LatLng(this.Lat, this.Lng);  
	var myOptions = {	zoom:  this.Zoom,    
						center: myLatlng,
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						scaleControl : true,
						scrollwheel : true
					}  
	
	var map = new google.maps.Map(document.getElementById(IDMap), myOptions);
	map.setTilt(45);
    //var map = new GMap2(document.getElementById(IDMap));
    var gdir = null;
    



    function createMarker(point, description) {
    	//var marker = new GMarker(point);
    	var marker = new google.maps.Marker({ position: point, title: description, animation: google.maps.Animation.DROP });    // To add the marker to the map, call setMap();  marker.setMap(map);

    	google.maps.event.addListener(marker, 'click', function () {
    		var infowindow = new google.maps.InfoWindow({ content: description });
    		infowindow.open(map, marker);
    		//marker.openInfoWindowHtml(descri);
    	});

        /*GEvent.addListener(marker, "click",
			function() {
			    marker.openInfoWindowHtml(description);
			}
		);
		*/
        return marker;
    }

    function showAddress(address, zoom, descri, lat, lng) {

        if (address == null && (lat == null || lng == null))
            return;
        var point;
        if (lat != null && lng != null) {
        	//point = new GLatLng(lat, lng);
        	point = new google.maps.LatLng(lat, lng);
        	var marker = new google.maps.Marker({ position: point, title: descri, animation: google.maps.Animation.DROP });    // To add the marker to the map, call setMap();  marker.setMap(map);
        	google.maps.event.addListener(marker, 'click', function () {
        		var infowindow = new google.maps.InfoWindow({ content: descri });
        		infowindow.open(map, marker);
        	});
			marker.setMap(map);  
			
        }
        else {
            var geocoder = new GClientGeocoder();
            if (geocoder) {
                geocoder.getLatLng(
                    address,
                    function(point) {
                        if (!point) {
                            alert("NON E' POSSIBILE VISUALIZZARE LA MAPPA DI " + address);
                        }
                        else {
                            
                            map.setUIToDefault();
                            map.setCenter(point, zoom, G_NORMAL_MAP);
                           
                            //    map.addOverlay(createMarker(point, descri));
                        }
                    }
                );
            }
        }
    }

    this.ShowMap = function showMap() {
     //   if (GBrowserIsCompatible()) {
            //map.addControl(new GSmallMapControl());
    	var m = showAddress(this.Address, this.Zoom, this.Description, this.Lat, this.Lng);
		
            /*
			GEvent.addListener(map, 'click',
                function(overlay, point) {
                    if (point) {
                        document.getElementById('click_lat').value = point.y;
                        document.getElementById('click_long').value = point.x;
                    }
                }
            );
			*/
       // }
    }
    this.AddOverlay = function addOverlay(latitudine, longitudine, descri) {
    	//var point = new GLatLng(latitudine, longitudine);
    	point = new google.maps.LatLng(latitudine, longitudine);
    	var m = createMarker(point, descri);
    	m.setMap(map);
    	//map.addOverlay();
    }
    this.LoadDirections = function loadDir(directionsPanelId, directions) {
    	var segments = directions.split("%");

    	for (i = 0; i < segments.length; i++) {
    		var waypts = new Array();
    		var points = segments[i].split("#");
    		for (j = 0; j < points.length; j++) {
    			var item = points[j].split("-");
    			if (item[0] == "s" || item[0] == "sn" )
    				start = new google.maps.LatLng(item[1], item[2]);
    			else if (item[0] == "e" || item[0] == "en")
    				end = new google.maps.LatLng(item[1], item[2]);
    			else if (item[0] == "n")
    				waypts.push({ location: new google.maps.LatLng(item[1], item[2]), stopover: true });
				else if (item[0] == "st")
    				start = item[1];
    			else if (item[0] == "et")
    				end = item[1];
    			else if (item[0] == "t")
    				waypts.push({ location: item[1], stopover: true });
    		}
    		var request = {
    			origin: start,
    			destination: end,
    			waypoints: waypts,
    			optimizeWaypoints: false,
    			travelMode: google.maps.DirectionsTravelMode.DRIVING
    		};
    		directionsService.route(request, function (response, status) {
    			if (status == google.maps.DirectionsStatus.OK) {
    				var directionDisplay = new google.maps.DirectionsRenderer();
    				directionDisplay.setMap(map);
    				directionDisplay.setDirections(response);
    			}
    		});
    	}
    	/*
    	if (directionsPanelId == null)
    	gdir = new GDirections(map);
    	else
    	gdir = new GDirections(map, document.getElementById(directionsPanelId));

    	GEvent.addListener(gdir, "load", onGDirectionsLoad);

    	gdir.load(directions, { travelMode: G_TRAVEL_MODE_WALKING, locale: "it_IT" });
    	*/

    }
    function onGDirectionsLoad() {
        var gpol = gdir.getPolyline();
        try {
            gpol.color = "#00ff00";
        }
        catch (e) {
            alert(e.message);
        }
    }
}
