var map2;
var mainMarker2;
var geocoder2;
var listeners2 = new Array();
var gdir2;

function load2(mapDiv){
    if(document.getElementById(mapDiv) == null) return false;
    if (GBrowserIsCompatible())
    {

        map2 = new GMap2(document.getElementById(""+mapDiv),{
            draggableCursor:"pointer"
        });
        geocoder2 = new GClientGeocoder();

        gdir2 = new GDirections(map2,document.getElementById('directions'));
        GEvent.addListener(gdir2, "load", onGDirectionsLoad2);
        GEvent.addListener(gdir2, "error", handleErrors2);

        if( typeof(x2)=="undefined" || isNaN(x2)) var xc = parseFloat("5.73349"); else var xc = x2;
        if( typeof(y2)=="undefined" || isNaN(y2)) var yc = parseFloat("45.19462"); else var yc = y2;
        if( typeof(z2)=="undefined" || isNaN(z2)) var zc = parseFloat("8"); else var zc = z2;

        //Initialisation de la carte
        map2.setCenter(new GLatLng(yc,xc),zc);
        map2.enableDoubleClickZoom();
        //map.enableScrollWheelZoom();

        //Controles de la carte (choix du type d'affichage et positionnement/zoom)
        map2.addControl(new GLargeMapControl3D(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5,5)));

    }
    return true;
}


//Ajoute un cercle
function addCircle2(latitude, longitude, radius, scolor, sweight, sopacity, fcolor, fopacity)
{
    var latlng = new GLatLng( parseFloat(latitude), parseFloat(longitude));
    var centre = map2.getCurrentMapType().getProjection().fromLatLngToPixel(latlng, map2.getZoom());
    return drawCircle2(parseFloat(latitude), parseFloat(longitude), centre, parseFloat(radius), scolor, sweight, sopacity, fcolor, fopacity);
}

//Dessine un cercle
function drawCircle2(lat, lng, centerPt, radiusKM, scolor, sweight, sopacity, fcolor, fopacity)
{
    var normalProj = map2.getCurrentMapType().getProjection();
    var zoom = map2.getZoom();
    var circlePoints = Array();
    with (Math)
    {
        var radiusEarth = 6378.2;
        var cosLat = cos(lat*(PI/180));
        var circ = 2*PI*cosLat*radiusEarth;
        var wZoom = 256 * pow(2,zoom)
        var radiusPixels = radiusKM * wZoom / circ;
        for (var a = 0 ; a < 361 ; a+=5 )
        {
            var aRad = a*(PI/180);
            var y = centerPt.y + radiusPixels * sin(aRad)
            var x = centerPt.x + radiusPixels * cos(aRad)
            var p = new GPoint(x,y);
            circlePoints.push(normalProj.fromPixelToLatLng(p, zoom));
        }
        circleLine2 = new GPolygon(circlePoints,scolor, sweight, sopacity, fcolor, fopacity, {
            clickable:false,
            draggableCursor:"crosshair"
        });
        map2.addOverlay(circleLine2);
        return circleLine2;
        }
        return false;
}

function handleErrors2(){
    if (gdir2.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir2.getStatus().code);
    else if (gdir2.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: " + gdir2.getStatus().code);

    else if (gdir2.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: " + gdir2.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 (gdir2.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: " + gdir2.getStatus().code);

    else if (gdir2.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir2.getStatus().code);

    else alert("Une erreur inconnue s'est produite.");

}

function onGDirectionsLoad2() {
    gpoly2=gdir2.getPolyline();
    gpoly2.color = "#00bbdd";
    gpoly2.weight=8;
    gpoly2.opacity = 0.7;
    setInfosGeoloc2();
}


