var map;
var mainMarker;
var geocoder;
var listeners = new Array();
var mapDiv;
var gdir;
//Fonction executee au chargement de la page
function load(mapDiv){
    if(document.getElementById(mapDiv) == null) return false;
    if (GBrowserIsCompatible())
    {

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

        gdir = new GDirections(map,document.getElementById('directions'));
        //GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

        if( typeof(x)=="undefined" || isNaN(x)) var xc = parseFloat("5.73349"); else var xc = x;
        if( typeof(y)=="undefined" || isNaN(y)) var yc = parseFloat("45.19462"); else var yc = y;
        if( typeof(z)=="undefined" || isNaN(z)) var zc = parseFloat("8"); else var zc = z;
     
        //Initialisation de la carte
        map.setCenter(new GLatLng(yc,xc),zc);
        map.enableDoubleClickZoom();
        //map.enableScrollWheelZoom();

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

//Fonction red�finissant la position et le zoom de la carte afin d'afficher les latitudes
// et longitudes fournies en param�tres
function resizeToBounds(minLat,minLng,maxLat,maxLng){
    var bounds = new GLatLngBounds();
    bounds.extend(new GLatLng(minLat,minLng));
    bounds.extend(new GLatLng(maxLat,maxLng));
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
}


//D�finition de la position et le zoom de la carte selon les param�tres
//Et m�morise la position comme 'position initiale" de la carte
function setCenter(latitude,longitude,zoom){
    map.setCenter(new GLatLng(latitude,longitude), zoom);
    map.savePosition();
}

//Zoom avant et arri�re
function zoomIn() {
    map.zoomIn();
}
function zoomOut() {
    map.zoomOut();
}



//R�cup�ration du facteur de zoom
function getZoom() {
    return map.getZoom();
}

//Modification du facteur de zoom
function setZoom(level) {
    map.setZoom(level);
}



//Efface les calques (pointeurs, polygones...)
function clearOverlays(){
    removeListeners();
    map.clearOverlays();
}

//Supprime les gestionnaires d'�v�nements
function removeListeners()
{
    try
    {
        var  n = listeners.length;
        for(var i=0; i<n; i++)
        {
            GEvent.removeListener(listeners[i]);
        }
        listeners=[];
    }
    catch(ex){}
}




/* Les fonctions ci-dessous permettent à l'utilisateur de dessiner un polygone ou un cercle directement sur la carte
        Elles ne sont utilis�es que pour la définition de secteurs g�ographiques ou de p�rim�tres de recherche
    */

//Definition de polygone
var polygoneEnCours;
var cercleEnCours;
function cancelPolygon(){
    polygoneEnCours.disableEditing();
    map.removeOverlay(polygoneEnCours);
    if($("input[name=searchGeo]")) $("input[name=searchGeo]").val("");
}
function startPolygon(scolor, sweight, sopacity, fcolor, fopacity)
{
    searchOpenTown = false;
    $("#town").val("");
    if(polygoneEnCours)cancelPolygon();
    polygoneEnCours = new GPolygon([], scolor, sweight, sopacity, fcolor, fopacity);
    map.addOverlay(polygoneEnCours);
    polygoneEnCours.enableDrawing({});
    polygoneEnCours.enableEditing({
        onEvent: "mouseover"
    });
    polygoneEnCours.disableEditing({
        onEvent: "mouseout"
    });
    GEvent.addListener(polygoneEnCours, "endline", function()
    {
        if($("input[name=searchGeo]")) $("input[name=searchGeo]").val(getPolygon(polygoneEnCours));
        GEvent.addListener(polygoneEnCours, "click", function(latlng, index)
        {
            if (typeof index == "number")
            {
                polygoneEnCours.deleteVertex(index);
            }
        });
    });
    GEvent.addListener(polygoneEnCours, "lineupdated", function(latlng, index){
        if($("input[name=searchGeo]")) $("input[name=searchGeo]").val(getPolygon(polygoneEnCours));
        if($("input[name=searchVille]")) $("input[name=searchVille]").val("");
    });
}

//ajoute un poligone éditable a partir des coordonée
function loadPolygon(mycoord, scolor, sweight, sopacity, fcolor, fopacity){
    var tabmycoord = mycoord.split(",");
    var coords = new Array();
    for (var i=0; i<tabmycoord.length; i++){
        var tempcoord = tabmycoord[i].split(" ");
        coords[coords.length] = new GLatLng(parseFloat(tempcoord[1]), parseFloat(tempcoord[0]));
    }
    if(polygoneEnCours)cancelPolygon();
    polygoneEnCours = new GPolygon(coords,scolor,sweight,sopacity,fcolor,fopacity);
    map.addOverlay(polygoneEnCours);
    polygoneEnCours.enableEditing({
        onEvent: "mouseover"
    });
    polygoneEnCours.disableEditing({
        onEvent: "mouseout"
    });
    if($("input[name=searchGeo]")) $("input[name=searchGeo]").val(getPolygon(polygoneEnCours));
    GEvent.addListener(polygoneEnCours, "lineupdated", function(latlng, index){
        if($("input[name=searchGeo]")) $("input[name=searchGeo]").val(getPolygon(polygoneEnCours));
        if($("input[name=searchVille]")) $("input[name=searchVille]").val("");
    });
    GEvent.addListener(polygoneEnCours, "click", function(latlng, index){
        if (typeof index == "number")
        {
            polygoneEnCours.deleteVertex(index);
        }
        if($("input[name=searchGeo]")) $("input[name=searchGeo]").val(getPolygon(polygoneEnCours));
    });
    


}

//ajoute un poligone non éditable a partir des coordonée
function loadPolygonNormal(mycoord, scolor, sweight, sopacity, fcolor, fopacity){
    var tabmycoord = mycoord.split(",");
    var coords = new Array();
    for (var i=0; i<tabmycoord.length; i++){
        var tempcoord = tabmycoord[i].split(" ");
        coords[coords.length] = new GLatLng(parseFloat(tempcoord[1]), parseFloat(tempcoord[0]));
    }
    if(polygoneEnCours)cancelPolygon();
    polygoneEnCours = new GPolygon(coords,scolor,sweight,sopacity,fcolor,fopacity);
    map.addOverlay(polygoneEnCours);
    if($("input[name=searchGeo]")) $("input[name=searchGeo]").val(getPolygon(polygoneEnCours));
}


function getPolygon(name){

    var points = "";
    var nb = name.getVertexCount();
    for(var i=0;i<nb;i++)
    {
        var latlng = name.getVertex(i);
        if(points.length > 0) {
            points = points + ",";
        }
        points = points + latlng.lng() + " " + latlng.lat();
    }
    return points;
}

function PolygonToCircle(addKm){
    if(cercleEnCours) map.removeOverlay(cercleEnCours);
    var bounds = polygoneEnCours.getBounds();
    var lat = bounds.getCenter().lat();
    var lng = bounds.getCenter().lng();
    var maxi = 0;
    for(var i=0;i<polygoneEnCours.getVertexCount();i++){
        var distvertex = bounds.getCenter().distanceFrom(polygoneEnCours.getVertex(i));
        if( distvertex > maxi){
            maxi = distvertex;
        }
    }
    cercleEnCours = addCircle(lat, lng, ((maxi/1000)+parseInt(addKm)), "#735961", 2, 1, "#735961",0.30);
    if(polygoneEnCours) map.removeOverlay(polygoneEnCours);
    goPolygone(cercleEnCours);
    if($("input[name=searchGeo]")) $("input[name=searchGeo]").val(getPolygon(cercleEnCours));
}


function goPolygone(name){
    var bounds=name.getBounds();
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
    map.savePosition();

}


function getPolygonArea(){
    return polygoneEnCours.getArea();
}


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

//Dessine un cercle
function drawCircle(lat, lng, centerPt, radiusKM, scolor, sweight, sopacity, fcolor, fopacity)
{
    var normalProj = map.getCurrentMapType().getProjection();
    var zoom = map.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"
        });
        map.addOverlay(circleLine2);
        return circleLine2;
        }
        return false;
}

function handleErrors(){
    if (gdir.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: " + gdir.getStatus().code);
    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("Une erreur inconnue s'est produite.");

}

function onGDirectionsLoad() {
    gpoly=gdir.getPolyline();
    gpoly.color = "#00bbdd";
    gpoly.weight=8;
    gpoly.opacity = 0.7;
    setInfosGeoloc();
}

