// JScript source code
// JScript File




var map;

function loadDirection(fromAddress,toAddress){

if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": "it_IT" });
        //setDirections("San Francisco", "Mountain View", "en_US");
      }
      
}

function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("Non è stato possibile localizzare l'indirizzo inserito");
	   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 onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}

function loadMappa() {
       
      if (GBrowserIsCompatible()) {
       var map = new GMap2(document.getElementById("map"));
      
       var point=new GLatLng(43.279681,13.491833)
       map.setCenter(point, 12);
       map.addControl(new GSmallMapControl());
       map.addControl(new GMapTypeControl());
       var marker = new GMarker(point);
       map.addOverlay(marker);
       var name="<b>FERRACUTI S.r.l.</b><br>Via Annibali, 21 - 62100 Piediripa (MC) Italy<br>tel.+39 0733 281277 - fax +39 0733 285345";
       if(name!='')
            GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml('<p style=color:black;font-weight:normal;font-size:11px>'+name+'</p>');
          });
       marker.openInfoWindowHtml('<p style=color:black;font-weight:normal;font-size:11px>'+name+'</p>');
       
      }
    }



function zoomStore(y,x,z) {
      if (GBrowserIsCompatible()) {
       var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(y,x), z);
        //map.addControl(new GSmallMapControl());
        //map.addControl(new GMapTypeControl());
        return map;
      }
    }


function loadCoordfromAddress(point,descr,z) {
     
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(point, z);
         map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var marker = new GMarker(point);
        map.addOverlay(marker);
        if(name!='')
            GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml('<p style=color:black;font-weight:bold>'+descr+'</p>');
          });
       marker.openInfoWindowHtml('<p style=color:black;font-weight:bold>'+descr+'</p>');     
            
        
      }
    }

function loadCoord(name,x,y,z) {
     
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        var point=new GLatLng(x,y);
        map.setCenter(point, z);
         map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var marker = new GMarker(point);
        map.addOverlay(marker);
        if(name!='')
            GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml('<p style=color:black;font-weight:bold>'+name+'</p>');
          });
       marker.openInfoWindowHtml('<p style=color:black;font-weight:bold>'+name+'</p>');     
       
       document.getElementById('txDaddr').value='';
       
        return map;
      }
    }

function SetPointFromAddress(address,descr,z,func) {
      var map = new GMap2(document.getElementById("map"));
      var geocoder = new GClientGeocoder();
      if (geocoder) {
            geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                        alert(address + " non trovato");
                        } else {
                        map.setCenter(point, z);
                        map.addControl(new GSmallMapControl());
                        map.addControl(new GMapTypeControl());
                        var marker = new GMarker(point);
                        map.addOverlay(marker);
                        if(name!='')
                            GEvent.addListener(marker, "mouseover", function() {
                            marker.openInfoWindowHtml('<p style=color:black;font-weight:bold>'+descr+'</p>');
                          });
                       marker.openInfoWindowHtml('<p style=color:black;font-weight:bold>'+descr+'</p>');  
                       if(func)func();
                        return point;
                        }
                  }
              );
        }
        return map;
}

function getPointFromAddress(address) {
      var geocoder = new GClientGeocoder();
      if (geocoder) {
            geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                        alert(address + " non trovato");
                        } else {
                    return point;
                        }
                  }
              );
        }
}

function pointToString(point)
{
    return point.x + ',' + point.y;
}

function calcolaCoordinateIndirizzo(address)
{
    return pointToString(getPointFromAddress(address));
}

function showAddress(address) {
      alert(address);
      var geocoder = new GClientGeocoder();
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 5);
              alert(point);
              
            }
          }
        );
      }
}

function markStore(map,addr,desc,y,x,tipo,imgpath)
{       //alert(imgpath);
        var point=new GLatLng(y,x);
        var bIcon = new GIcon(G_DEFAULT_ICON);
        bIcon.shadow ='';
        bIcon.iconSize = new GSize(17, 15); 
        bIcon.iconAnchor = new GPoint(8, 15); 
        
        //bIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
        bIcon.image = imgpath+"gmapblue.png";
        
        if(tipo.indexOf('corner')>=0)
        {
        //bIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/red/blank.png";
        bIcon.image = imgpath+"gmapgreen.png";
        }
        if(tipo.indexOf('new')>=0)
        {
        //bIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/green/blank.png";
        bIcon.image = imgpath+"gmapred.png";
        }
        if(tipo.indexOf('sel')>=0)
        {
        bIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png";
        bIcon.image = imgpath+"gmaporange.png";
        }
		
		markerOptions = {icon:bIcon};
        
        var marker = new GMarker(point,markerOptions);
        map.addOverlay(marker);
        GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml(desc);
          });
        GEvent.addListener(marker, "click",function() {
                     document.getElementById('txDaddr').value=addr;
                     document.getElementById('txDcoord').value=y + ',' + x;
                    //marker.setImage(imgpath+"gmaporange.png")
                    });
       if(tipo.indexOf('sel')>=0)marker.openInfoWindowHtml(desc);
}

function GoGMap()
{
if(document.getElementById('saddr').value=='')
{
    alert('Inserisci il tuo indirizzo !');
    return;
}


var saddr=document.getElementById('saddr').value;
var daddr="43.279681,13.491833";
var directions=document.getElementById('directions');
directions.innerHTML="";
loadDirection(saddr,daddr);

//var gmapurl='http://maps.google.com/maps?f=d&hl=it&&saddr='+saddr+'&daddr='+daddr;
//window.open(gmapurl);
	
}

function StoreClick(addr)
{
	//$get('divItenerarioGoogle').Show();
	//$get('txDestinazione').value=addr;
}


