 //<![CDATA[
 
var map = null;
var wheelZooming = false;
var adr = "";
var adresse = "";
var nom = "";
var urlDetail = "";
var geocoder = new GClientGeocoder();
var gdir;

function load(adresse, nomOffre, url)
{
	
	adr = adresse;
    nom = nomOffre;
    urlDetail = url;
    
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById('map'));
        map.setCenter(new GLatLng(47.900808,1.904111), 11);
        map.addControl(new GMapTypeControl());
        map.addControl(new GLargeMapControl());
        map.enableContinuousZoom();
        map.enableDoubleClickZoom(); 
        GEvent.addDomListener(document.getElementById("map"), "DOMMouseScroll", wheelZoom);
        GEvent.addDomListener(document.getElementById("map"), "mousewheel", wheelZoom);

        showLocation();
    }
}

function wheelZoom(event)
{
    if (wheelZooming)
    {
        return;
    }

    wheelZooming = true;

    var pt = map.getCenter();

    if (event.cancelable)
    {
        event.preventDefault();
    }
    map.closeInfoWindow(); 
    if ((event.detail || -event.wheelDelta) < 0)
    {
        window.setTimeout(function()
        {
            map.zoomIn(pt, true, true);
            wheelZooming = false;
        }, 200);
    } 
    else
    {
        window.setTimeout(function()
        {
            map.zoomOut(pt,true);
            wheelZooming = false;
        }, 200);
    }
    return false; 
}

function addAddressToMap(response)
{
    map.clearOverlays();
    if (!response || response.Status.code != 200)
    {
        ville = adr.split(',');    
        if (ville.length != 1)
        {
            ville = ville[ville.length - 1];
            load(ville, nom, urlDetail);
        }
        else
        {
            //alert("\"" + adr + "\" n'a pas été trouvé.");
        }
    }
    else
    {
        place = response.Placemark[0];
        //document.getElementById("JSON").innerHTML = place.toString();
        point = new GLatLng(place.Point.coordinates[1],
        place.Point.coordinates[0]);
        var baseIcon = new GIcon();
        baseIcon.iconSize=new GSize(12,20);
   		baseIcon.shadowSize=new GSize(20,22);
   		baseIcon.iconAnchor=new GPoint(6,20);
   		baseIcon.infoWindowAnchor=new GPoint(5,1);
   				
   		var iconeTA = new GIcon(baseIcon, '/web/img/carto.png');
        
        var formulaire_itineraire ='<div style="width:310px; height:130px; border: 1px solid #ccc; background:#C3E6FF; font: 1em \'trebuchet ms\',verdana, helvetica, sans-serif;">' +
		'<form action="#" onsubmit="setDirections(this.from.value, this.to.value); return false">' +
			'<table align="center">' +
				'<tr>' +
					'<td colspan="2" align="center"><b>Indiquez votre adresse de d&eacute;part ci-dessous</b></td>' +
				'</tr>' +
				'<tr>' +
					'<td colspan="2" align="center"><input type="text" size="25" id="fromAddress" name="from" value="" style="width:300px; height:20px; border:1px solid grey;" /></td>' +
				'</tr>' +
				'<tr>' +
					'<td colspan="2" align="center"><b>Adresse d\'arriv&eacute;e</b></td>' +
				'</tr>' +
				'<tr>' +
					'<td colspan="2" align="center"><b><a href="'+urlDetail+'">'+nom + '</a></b><br/>'+place.address+'</td>' +
				'</tr>' +
				'<tr>' +
					'<td align="right" colspan="2" align="center"><input type="hidden" id="toAddress" name="to" value="'+place.address+'" /></td>' +
				'</tr>' +
				'<tr>' +
					'<td>&nbsp;</td>' +
					'<td align="center">' +
						'<input name="submit" type="submit" value="Afficher l\'itin&eacute;raire" />' +
					'</td>' +
				'</tr>' +
			'</table>' +
		'</form>' +
		'</div>';
        
        var marker = new GMarker(point,iconeTA);
        GEvent.addListener(marker, "click", function()
        {
            marker.openInfoWindowHtml(formulaire_itineraire);
        });
        map.addOverlay(marker);
        
        //affichage de la bulle tout de suite
        marker.openInfoWindowHtml(formulaire_itineraire);
        
        map.setCenter(point,11);
    }
}

function setDirections(fromAddress, toAddress) {
	
	document.getElementById("map").innerHTML = '<table id="resultat" style="background-color: white;">'+
		'<tr><td class="encadrement"><div id="EmplacementCarteItineraire" style="height:380px; width:250px;"></div></td>'+
		'<td class="encadrement"><div id="EmplacementTexteItineraire" style="background-color: white; margin:0; padding:0; width:auto; height:380px; width:250px; overflow:auto; text-align:left;"></div></td>'+
		'</tr></table>';

	if(gdir){gdir.clear();}

	CarteItineraire = new GMap2(document.getElementById("EmplacementCarteItineraire"));
	CarteItineraire.addControl(new GLargeMapControl());
	CarteItineraire.enableContinuousZoom();
	GEvent.addDomListener(document.getElementById("EmplacementCarteItineraire"), "DOMMouseScroll", wheelZoom);
       GEvent.addDomListener(document.getElementById("EmplacementCarteItineraire"), "mousewheel", wheelZoom);

	gdir = new GDirections(CarteItineraire, document.getElementById("EmplacementTexteItineraire"));

	GEvent.addListener(gdir, "error", EnCasdErreur);

	gdir.load("from: " + fromAddress + " to: " + toAddress);
}

function EnCasdErreur(){

	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
		//alert('Aucun endroit géographique ne correspond. Code d erreur : ' + gdir.getStatus().code);
		alert('Aucun lieu ne correspond à l\'adresse de départ.');
		load(adresse, nom, urlDetail);
	}else{
		alert('Une erreur inconnue s\'est produite.');
		load(adresse, nom, urlDetail);
	}
}


function showLocation()
{
    var address = adr+", France";
    geocoder.getLocations(address, addAddressToMap);
}


//]]>
