﻿//<![CDATA[
var map;
var geocoder = null;
var gdir;
var addressMarker;

function initialize() {
    // define the crosshair tile layer and its required functions
    var crossLayer = new GTileLayer(new GCopyrightCollection(""), 0, 15);
    crossLayer.getTileUrl = function(tile, zoom) {
        return "./include/tile_crosshairs.png";
    }
    crossLayer.isPng = function() { return true; }

    // Create a new map type incorporating the tile layer
    var layerTerCross = [G_PHYSICAL_MAP.getTileLayers()[0],
                              crossLayer];
    var mtTerCross = new GMapType(layerTerCross,
                                      G_PHYSICAL_MAP.getProjection(), "Ter+");

/*

http://maps.google.it/maps?f=q&source=s_q&hl=it&geocode=&q=Pontecagnano,+Via+Carmine+Cal%C3%B2&sll=41.442726,12.392578&sspn=26.755531,39.506836&ie=UTF8&hq=&hnear=Via+Tenente+Colonnello+Carmine+Cal%C3%B2,+84098+Pontecagnano+Faiano+Salerno,+Campania&ll=40.625972,14.914337&spn=0.001657,0.002411&z=19

http://maps.google.it/maps?f=d&source=s_d&saddr=Via+Tenente+Colonnello+Carmine+Cal%C3%B2,+84098+Pontecagnano+Faiano+SA&daddr=Strada+sconosciuta&geocode=FY3sawIdNH7jACm3oVckYuc7EzG7fZEuiE7LUA%3BFfrnawId8pLjAA&hl=it&mra=ls&sll=40.625793,14.915035&sspn=0.006628,0.009645&ie=UTF8&ll=40.625722,14.914348&spn=0.001604,0.002411&z=19
*/

    map = new GMap2(document.getElementById("map_canvas"));
    gdir = new GDirections(map, document.getElementById("directions"));
    //map.setCenter(center,  zoom?,  type?)

    map.setCenter(new GLatLng(40.625722,14.914348));
    map.setZoom(14)
    map.addControl(new GLargeMapControl())
    geocoder = new GClientGeocoder();

    var mapControl = new GHierarchicalMapTypeControl();

    // Set up map type menu relationships
    mapControl.clearRelationships();
    mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
    mapControl.addRelationship(G_PHYSICAL_MAP, mtTerCross, "Crosshairs");

    // Add control after you've specified the relationships
    map.addControl(mapControl);

    //geocoder.getLocations('Italia', addAddressToMap);
    //showAddress('Salerno, Pontecagnano Faiano, VIA STATALE 18 KM.68,300')
    showCoord(40.625722,14.914348)

}



function showCoord(lat,lon){
        point = new GLatLng(lat,lon);
        marker = new GMarker(point);
        map.addOverlay(marker);
}



function showAddress(address) {
    if (geocoder) {
        geocoder.getLatLng(
	              address,
	              function(point) {
	                  if (!point) {
	                      alert(address + " not found");
	                  } else {
	                      map.setCenter(point, 16);
	                      var marker = new GMarker(point);
	                      map.addOverlay(marker);
	                      //marker.openInfoWindowHtml(address);
	                  }
	              }
	            );

    }
}


var a = 0;
function addAddressToMap(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {

        //se non trovo la via richiamo comune e provincia
        if (a == 0) {
            alert("Google maps attualmente non è in grado di visualizzare con esattezza la locazione dell’indirizzo:\n sulla mappa.")
            a = a + 1;
        }

        else {
            window.close()//se non trovo nemmeno il comune chiudo la finestra
            //alert("Google maps attualmente non è in grado di visualizzare con esattezza la locazione dell’indirizzo:\n sulla mappa.")

        }

    } else {

        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                                place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        //marker.openInfoWindowHtml(place.address);

        if (a == 0) {
            marker.openInfoWindowHtml('<strong>Test</strong>' + '<br/>' + place.address);
        }
        else
        { marker.openInfoWindowHtml(place.address); }

        document.getElementById('toAddress').value = place.address

    }
}


// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
    var address = document.forms[0].q.value;
    geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
    document.forms[0].q.value = address;
    showLocation();
}




function setDirections(fromAddress, toAddress, locale) {
    gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": locale });
        }


//]]>
