﻿
var map;
function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("gMap"));
    map.setCenter(new GLatLng(45.0706029, 7.6867102), 10);
    //map.addControl(new GSmallMapControl());
    map.addControl(new GSmallZoomControl3D());
    //map.addControl(new GLargeMapControl3D());
    map.addControl(new GMapTypeControl());
  }
}

function  showResults(lat,lng,nome,address,comune,type)
{
    var elLat=new Array();
    elLat=lat.split("XX");

    var elLng=new Array();
    elLng=lng.split("XX");

    var elNome=new Array();
    elNome=nome.split("XX");
    
    var elAddress=new Array();
    elAddress=address.split("XX");
    
    var elComune=new Array();
    elComune=comune.split("XX");

    for (var i=0; i<elLat.length; i++) {
       if (elLat[i]!=0)
       { 
            addBaloon(elLat[i],elLng[i],elNome[i],elAddress[i],elComune[i],type);
        }
     }
}

function addBaloon(lat,lng,nome,address,comune,type,typeMarker)
{
    var iconP = new GIcon(G_DEFAULT_ICON);
    switch(typeMarker)
    {
        case 1:
        iconP.iconSize = new GSize(16, 16);
        break;
        
        case 2:
        iconP.iconSize = new GSize(12, 12);
        break;
		
		case 20:
        iconP.image='/images/ico_mappa_natura_generica.gif';
        break; 
    }
    
    iconP.shadowSize = new GSize(0, 0);
    
    switch(type)
    {
        case 1:
        iconP.image = '/images/gMap/ico_risorsaMUSEO.gif';
        break;
        
        case 6:
        iconP.image = '/images/gMap/ico_risorsaHOTEL.gif';
        break;  
        
        case 7:
        iconP.image = '/images/gMap/enogastronomia.gif';
        break;  
        
        case 8:
        iconP.image = '/images/gMap/ico_parchi.gif';
        break;  
        
        case 9:
        iconP.image = '/images/gMap/ico_risorse.gif';
        break;  
        
        case 10:
        iconP.image = '/images/gMap/prodotto.gif';
        break;  
        
        case 11:
        iconP.image = '/images/gMap/ico_risorse.gif';
        break;  
        
        case 12:
        iconP.image = '/images/gMap/ico_risorse.gif';
        break;  

		case 20:
        iconP.image='/images/ico_mappa_natura_generica.gif';
        break; 		
    }

    var latlng = new GLatLng(lat,lng);
    var marker = new GMarker(latlng, {icon:iconP});
    //alert("ok")
    map.addOverlay(marker);
    
    
        GEvent.addListener(marker, "click", function(){
        var strDet;
        
        strDet='<b>'+nome+'</b>'
        
        if (address!='')
            {
                strDet+='<br>'+address
            } 
        
        if (comune!='')
            {
                strDet+=' - <b>'+comune+'</b>'
            }  
        
        marker.openInfoWindowHtml(strDet);
        
    });
}
var latGen;
var lngGen;

function initializeMarker(lat,lng,nome,tel,fax,sito,email,indirizzo,comune,type) {
  if (GBrowserIsCompatible()) {
    if (nome!='SEDI VARIE')
    {
    latGen=lat;
    lngGen=lng;
    map = new GMap2(document.getElementById("gMap"));
    var directions = new GDirections(map,document.getElementById("sidebar"));
    //map.addControl(new GSmallMapControl());
    map.addControl(new GSmallZoomControl3D());
    //map.addControl(new GLargeMapControl3D());
    map.addControl(new GMapTypeControl());
    
    //GEvent.addListener(map, "click", plotRoute);

    var iconP = new GIcon(G_DEFAULT_ICON);
    iconP.shadowSize = new GSize(0, 0);
    if (lat!=0)
    {
        var iconP = new GIcon(G_DEFAULT_ICON);
        iconP.iconSize = new GSize(25, 25);
        iconP.shadowSize = new GSize(0, 0);
        
        switch(type)
        {
            case 1:
            iconP.image = '/images/gMap/ico_musei_mappa.gif';
            break;
            
            case 6:
            iconP.image = '/images/gMap/ico_ricettivita_mappa.gif';
            break; 
            
            case 7:
            iconP.image = '/images/gMap/ico_eno_mappa.gif';
            break;
            
            case 11:
            iconP.image = '/images/gMap/ico_eventi_mappa.gif';
            break;
			
			case 20:
			iconP.image='/images/ico_mappa_natura_generica.gif';
			break; 
        }
        
        
        var latlng = new GLatLng(lat,lng);
        var marker = new GMarker(latlng, {icon:iconP});
        map.addOverlay(marker);
        map.setCenter(latlng, 14);
        
        GEvent.addListener(marker, "click", function(){
            var strDet;
            var indirizzoV=0;
            
            strDet='<b>'+nome+'</b>'
            if (indirizzo!='')
            {
                strDet+='<br>'+indirizzo
                indirizzoV=1;
            }    
            
            if (comune!='')
            {
                if (indirizzoV==1)
                   strDet+='- <b>'+comune+'</b>'
                else
                   strDet+='<br><b>'+comune+'</b>'
            }
            
            if (tel!='')
                strDet+='<br>Tel: '+tel
            
            if (fax!='')    
                strDet+='<br>Fax: '+fax
                
            if (email!='')
                strDet+='<br>Email: <a href=\'mailto:'+email+'\'>'+email+'</a>'
            
            if (sito!='')
                strDet+='<br>Web: <a target=\'_blank\' href=\'http://'+sito+'\'>'+sito+'</a>'
        
            marker.openInfoWindowHtml(strDet);
        
        });
     }
     else
     {
        var latlng = new GLatLng(45.070562,7.686619);
        map.setCenter(latlng, 11);
     }
	 }
	 else
	  {
		initialize()
	  }
  }
}

function test()
{
    alert("CIAO")
}

function searchAddress() {
    var data = document.getElementById("gMap_cercaGmap").value;

    if (data == "" || data == "Inserisci indirizzo") {
        alert("Devi inserire un indirizzo!");
    }
    else {
        var geocoder;
        var lat;
        var lng;
        var place;
        
        if (GBrowserIsCompatible()) {
            geocoder = new GClientGeocoder();
            geocoder.getLocations(data, function(response){
	                 if(response){	         
		                 try {
		                    place = response.Placemark[0];
		                 }
		                 catch(error) {
		                    alert("Indirizzo non trovato!");
		                    return;
		                 }
		                 finally {
		                    lat = place.Point.coordinates[1];
		                    lng = place.Point.coordinates[0];
		                   
		                    //Icona personalizzata
		                    var iconP = new GIcon(G_DEFAULT_ICON);
                            iconP.iconSize = new GSize(15, 15);
                            iconP.shadowSize = new GSize(0, 0);
                            iconP.image = '/images/pallino.gif';
                  
		                    var latlng = new GLatLng(lat,lng);
		                    var marker = new GMarker(latlng, {icon:iconP});
		                    map.setCenter(latlng, 14);
		                    map.clearOverlays(); 
		                    map.addOverlay(marker);
		                 }
		             }
	        });
        }
    }
    return false;
}


function onClickTxtCerca(id) {

    var txt = document.getElementById(id).value;
    
    if (txt == "Inserisci indirizzo" || txt == "Insert address" || txt == "Recherche adresse") {
        document.getElementById(id).value = "";
        document.getElementById(id).style.color = "#000000";
    }
    
}

function clickButtonGMap(e,id) {
    
    /*var key = document.getElementById(id);
    var e;
    var keypress;
    
    key.onkeypress = function(e){
        e=window.event || e;
        keypress=e.charCode || e.keyCode;
        //alert(keypress);
        if (keypress == 13) {
            e.returnValue=false;
            e.cancel = true;
            document.getElementById("gMap_btnCercaGmap").click();
        }
    }*/

    var evt = e ? e : window.event;

    if (evt.keyCode == 13){
    var btn = document.getElementById(id);
        if (btn){
            btn.click();
            return false;
        }
    }  
}

var coordinates=new Array();

function myPlotRoute(lat,lng){
  var directions = new GDirections(map,document.getElementById("sidebar"));
            GEvent.addListener(directions, "load", function() {
              var duration = (directions.getDuration().seconds / 60).toFixed(2);
              document.getElementById("duration").innerHTML = "<b>Durata del viaggio:</b> " + duration + 
              " minuti";
           });
           
  // Create the new array element
  coordinates_array_element = lat + "," + lng;
 
  // Add the new array element to the map
  coordinates.push(coordinates_array_element);
 
  // If > one point on the map, plot the route between these two points
  if (coordinates.length < 3) {
    directions.loadFromWaypoints(coordinates);
  }
}

var latLng;
function geocodeAddress(address)
{
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(address,
    function(point)
    {
        if (!point) {
          alert(address + " not found");
        }
        else {
           document.getElementById("boxPrint").innerHTML="<a href='#' onclick='return print();'>Stampa percorso</a>"
        
           document.getElementById("sidebar").innerHTML='';
        
           var directions = new GDirections(map,document.getElementById("sidebar"));
           var arCoor=new Array();
           coordinates_array_element = point;
           coordinates.push(coordinates_array_element);
           
           if (coordinates.length > 1) {
             directions.loadFromWaypoints(coordinates);
           }
           
           myPlotRoute(latGen,lngGen);
           
           GEvent.addListener(directions, "load", function() {
              var duration = (directions.getDuration().seconds / 60).toFixed(2);
              document.getElementById("duration").innerHTML = "<b>Durata del viaggio:</b> " + duration + 
              " minuti";
           });
        }
    });
} 

function mylatLngRoute(){
    var address=document.getElementById("gMap_cercaGmap").value;

    geocodeAddress(address);
}


function plotRoute(overlay, latlng) {
  // Create the new array element
  coordinates_array_element = latlng.lat() + "," + latlng.lng();
 
  // Add the new array element to the map
  coordinates.push(coordinates_array_element);
 
  // If > one point on the map, plot the route between these two points
  if (coordinates.length > 1) {
    directions.loadFromWaypoints(coordinates);
  }
}

//function print()
//{
//    $(".boxinoTopGmap").jqprint();
//    return false;
//}

 
    
