var __markers = [], map = null;
$(document).ready(function(){
    var baseIcon = new GIcon();
    baseIcon.iconSize = new GSize(24, 49);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(0, 0);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);
    function createMarkerBis(point, index, text, depth) {
        // Create a lettered icon for this point using our icon class
        //baseIcon.iconAnchor = new GPoint(depth * 20, 0); // A voir
        var icon = new GIcon(baseIcon);
        // alert(depth);
        icon.image = "/FRONT/CRT_PIDF/images/common/gmap/marker" + index + ".png";
        // Set up our GMarkerOptions object
        markerOptions = {
            icon : icon
        };
        var marker = new GMarker(point, markerOptions);
        __markers.push(marker);
        GEvent.addListener(marker, 'click', function(){
            marker.openExtInfoWindow(
                map,
                "crt_window",
                text,
                {beakOffset: 11}
            );
        });
        return marker;
    }

    function createMarker(point, index, text, depth) {
        // Create a lettered icon for this point using our icon class
        baseIcon.iconAnchor = new GPoint(depth * 20, 0); // A voir
        var icon = new GIcon(baseIcon);
        // alert(depth);
        icon.image = "/FRONT/CRT_PIDF/images/common/gmap/marker" + index + ".png";
        // Set up our GMarkerOptions object
        markerOptions = {
            icon : icon
        };
        var marker = new GMarker(point, markerOptions);
        GEvent.addListener(marker, 'click', function(){
            marker.openExtInfoWindow(
                map,
                "crt_window",
                text,
                {beakOffset: 11}
            );
        });
        return marker;
    }

    // Google Map Fiche > Proximité
    if($('.ficheContent .proximite #googleMap').length){
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("googleMap"));
            map.addControl(new GLargeMapControl());
            map.setCenter(
                new GLatLng(
                    30,
                    2
                ),
                15
            );
            //map.addOverlay(new GMarker(new GLatLng(37.4425, -122.1420), ''));
            //map.addOverlay(createMarker(new GLatLng(37.4428, -122.1423), 1));
            /* UNIQUEMENT POUR LES TESTS */
            /**********************************/
            // alert(dataJSON);
            myJSONObject = dataJSON;
            var points_bounds;
            inc     = 1;
            for (i in myJSONObject) {
                depth   = 0;
                adress = myJSONObject[i];
                if (!points_bounds) {
                    points_bounds = {
                        min : {
                            latitude : parseFloat(adress.latitude),
                            longitude : parseFloat(adress.longitude)
                        },
                        max : {
                            latitude : parseFloat(adress.latitude),
                            longitude : parseFloat(adress.longitude)
                        }
                    };
                } else {
                    if (parseFloat(adress.latitude) < parseFloat(points_bounds.min.latitude))
                    points_bounds.min.latitude = parseFloat(adress.latitude);
                    if (parseFloat(adress.latitude) > parseFloat(points_bounds.max.latitude))
                    points_bounds.max.latitude = parseFloat(adress.latitude);
                    if (parseFloat(adress.longitude) < parseFloat(points_bounds.min.longitude))
                    points_bounds.min.longitude = parseFloat(adress.longitude);
                    if (parseFloat(adress.longitude) > parseFloat(points_bounds.max.longitude))
                    points_bounds.max.longitude = parseFloat(adress.longitude);
                }
                var latlng = new GLatLng(adress.latitude, adress.longitude);
                for (j in adress.fiches) {
                    fiche = adress.fiches[j];
                    var text = '<p class="nom">';
                    text += fiche.titre;
                    text += "</p>";
                    text += "<p>";
                    if(fiche.adr_s_numero)
                        text += fiche.adr_s_numero+' ';
                    if(fiche.adr_s_rue1)
                        text += fiche.adr_s_rue1;
                    if((fiche.adr_s_numero) || (fiche.adr_s_rue1))
                        text += "<br/>";
                    if(fiche.adr_s_cp)
                        text += fiche.adr_s_cp+" ";
                    if(fiche.adr_s_ville)
                        text += fiche.adr_s_ville;
                    text += "</p>";
                    if(fiche.fichesmulti_s_introcourte)
                        text += "<p>"+fiche.fichesmulti_s_introcourte+"</p>";
                    map.addOverlay(createMarker(latlng, inc, text, depth));
                    depth += 1;
                    inc++;
                }
            }

            map.setCenter(
                new GLatLng(
                    (points_bounds.min.latitude + points_bounds.max.latitude) / 2,
                    (points_bounds.min.longitude + points_bounds.max.longitude) / 2
                ),
                17
            );

            var zoomLevel = map.getBoundsZoomLevel(
                new GLatLngBounds(
                    new GLatLng(points_bounds.min.latitude, points_bounds.min.longitude),
                    new GLatLng(points_bounds.max.latitude, points_bounds.max.longitude)
                )
            );
            map.setZoom(zoomLevel);
        }
    }
    // Google Map Fiche > Infos pratiques
    if($('.ficheContent .infosPratiques #googleMap, #print.fiche #googleMap').length){
        if (GBrowserIsCompatible()) {
            if (dataJSON == null) {
                var geocoder = new GClientGeocoder();
                geocoder.getLatLng(address, function(point) {
                    if (point) {
                        var marker = new GMarker(point);
                        var latLng = marker.getLatLng();  
                        map = new GMap2(document.getElementById("googleMap"));
                        response = new GLatLng(latLng.lat(), latLng.lng());
                        map.setCenter(response, 16);
                        map.addOverlay(createMarkerBis(response, 'Big'));
                    }
                });
            } else {
                myJSONObject = dataJSON;
                var latitude    = myJSONObject['latitude'];
                var longitude   = myJSONObject['longitude'];
                map = new GMap2(document.getElementById("googleMap"));
                map.setCenter(new GLatLng(latitude, longitude), 16);
                map.addOverlay(createMarkerBis(new GLatLng(latitude, longitude), 'Big'));
                //map.setZoom(14);
            }
        }
    }
    
    // Google Map Fiche > Impression fiche
    if($('.fiche #googleMap').length){
        if (GBrowserIsCompatible()) {
            myJSONObject = dataJSON;
            var latitude    = myJSONObject['latitude'];
            var longitude   = myJSONObject['longitude'];

            map = new GMap2(document.getElementById("googleMap"));
            map.setCenter(new GLatLng(latitude, longitude), 16);
            map.addOverlay(createMarkerBis(new GLatLng(latitude, longitude), 'Big'));
            //map.setZoom(14);
        }
    }

	// Google Map Guide > Itinéraire
	if($('#inner .guideCarte #googleMap, #print.guideIT #googleMap, #modEspace #googleMap').length){
		if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("googleMap"));
            map.setCenter(new GLatLng(48.833, 2.333), 4);
            map.addControl(new GLargeMapControl());
            myJSONObject = dataJSON;
            var points_bounds;
            inc     = 1;
            for (i in myJSONObject) {
                depth   = 0;
                adress = myJSONObject[i];
                if (!points_bounds) {
                    points_bounds = {
                        min : {
                            latitude : parseFloat(adress.latitude),
                            longitude : parseFloat(adress.longitude)
                        },
                        max : {
                            latitude : parseFloat(adress.latitude),
                            longitude : parseFloat(adress.longitude)
                        }
                    };
                } else {
                    if (parseFloat(adress.latitude) < parseFloat(points_bounds.min.latitude))
                    points_bounds.min.latitude = parseFloat(adress.latitude);
                    if (parseFloat(adress.latitude) > parseFloat(points_bounds.max.latitude))
                    points_bounds.max.latitude = parseFloat(adress.latitude);
                    if (parseFloat(adress.longitude) < parseFloat(points_bounds.min.longitude))
                    points_bounds.min.longitude = parseFloat(adress.longitude);
                    if (parseFloat(adress.longitude) > parseFloat(points_bounds.max.longitude))
                    points_bounds.max.longitude = parseFloat(adress.longitude);
                }
                var latlng = new GLatLng(adress.latitude, adress.longitude);
                //alert(latlng);
                //for (j in adress.fiches) {
                    //fiche = adress.fiches[j];
                    /*var text = '<p class="nom">';
                    text = adress.titre;
                    text += "</p>";
                    text += '<p class="lieu"><strong>';
                    text += adress.titre;
                    text += "</strong>";*/
                    var text = '<p class="nom">';
                    text += adress.rubrique+"</p>";
                    text += '<p class="lieu"><strong>';
                    if((adress.coe_i_id != 0)&&(adress.coe_i_id != null))
                        text += '<a href="/index.php?coe_i_id='+adress.coe_i_id+'">';
                    text += adress.titre;
                    if((adress.coe_i_id != 0)&&(adress.coe_i_id != null))
                        text += '</a>';
                    text += '</strong>';
                    text += adress.adresse+"<br />";
                    if(adress.ville != null)
                    text += adress.ville;
                    if(adress.short_quartier != null)
                    text +=" "+adress.short_quartier;
                    text +="</p>";
                    if(adress.accroche != null)
                    text += "<p>"+adress.accroche+"</p>";

                    map.addOverlay(createMarkerBis(latlng, inc, text, depth));
                    depth += 1;
                    inc++;
                //}
            }

            map.setCenter(
                new GLatLng(
                    (points_bounds.min.latitude + points_bounds.max.latitude) / 2,
                    (points_bounds.min.longitude + points_bounds.max.longitude) / 2
                ),
                17
            );

            var zoomLevel = map.getBoundsZoomLevel(
                new GLatLngBounds(
                    new GLatLng(points_bounds.min.latitude, points_bounds.min.longitude),
                    new GLatLng(points_bounds.max.latitude, points_bounds.max.longitude)
                )
            );
            map.setZoom(zoomLevel);
			/*var map = new GMap2(document.getElementById("googleMap"));
	        map.setCenter(new GLatLng(48.833, 2.333), 4);
			map.addControl(new GLargeMapControl());*/

			//map.addOverlay(new GMarker(new GLatLng(37.4425, -122.1420), ''));
			//map.addOverlay(createMarker(new GLatLng(37.4428, -122.1423), 1));

			/* UNIQUEMENT POUR LES TESTS */
			/*var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			var lngSpan = northEast.lng() - southWest.lng();
			var latSpan = northEast.lat() - southWest.lat();*/
			/**********************************/

			/*for (var i = 0; i < 19; i++) {
				var latlng = new GLatLng(southWest.lat() + latSpan * Math.random(),
				southWest.lng() + lngSpan * Math.random());
				map.addOverlay(createMarker(latlng, i));
			}*/
	    }
	}
});
