String.prototype.toProperCase = function() {
	return this.toLowerCase().replace(/\w+/g,function(s){
		return s.charAt(0).toUpperCase() + s.substr(1);
	})
}

var nraAffiches = new Object();
var map;

function initMap(lat, lng, level)
{
	if (GBrowserIsCompatible())
	{
		var mapDiv = document.getElementById("map");
		mapDiv.style.display = "block";
		mapDiv.style.overflow = "hidden";
		mapDiv.style.backgroundImage = "";
		GEvent.addDomListener(mapDiv, "DOMMouseScroll",
			function(oEvent) {
				if (oEvent.preventDefault)
					oEvent.preventDefault();
			}
		);
		var opts = {mapTypes: new Array(G_PHYSICAL_MAP, G_NORMAL_MAP, G_HYBRID_MAP)};
		map = new GMap2(mapDiv, opts);
		map.setUIToDefault();
		/*
var hmc = new GHierarchicalMapTypeControl();
		hmc.addRelationship(G_NORMAL_MAP, G_HYBRID_MAP);
		map.addControl(hmc);
		map.addControl(new GScaleControl());
		map.enableScrollWheelZoom();
*/
		map.enableContinuousZoom();
		var mt = map.getMapTypes();
		for (var i = 0; i < mt.length; i++) {
            mt[i].getMinimumResolution = function() {return 9;}
            //mt[i].getMaximumResolution = function() {return 15;}
        }
        GEvent.addListener(map, "move", function() {
            checkBounds();
        });
		displayMap(map, new GLatLng(lat, lng), level);
		GEvent.addListener(map, "moveend", function() {
			updateMarkers(this);
		});
	}
	return map;
}

      var allowedBounds = new GLatLngBounds(new GLatLng(34.275649, -85.920879), new GLatLng(54.460225, 9.51214));
      
      // If the map position is out of range, move it back
      function checkBounds() {
        // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
          return;
        }
        // It`s not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));
      }

function displayMap(map, point, level)
{
	map.setCenter(point, level);
	updateMarkers(map);
}

function updateMarkers(map)
{
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var url = "/get-nras.php?sud=" + southWest.lat() + "&nord=" + northEast.lat() + "&est=" + northEast.lng() + "&ouest=" + southWest.lng();
	GDownloadUrl(url,
		function(data, responseCode) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("nra");
			for (var i = 0; i < markers.length; i++) {
				var code = markers[i].getAttribute("id");
    			var nom = markers[i].getAttribute("nom");
				if(!nraAffiches[code])
				{
				    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
										parseFloat(markers[i].getAttribute("lng")));
                    var lignes = markers[i].getAttribute("lignes");
                    var lignesC = lignes;
                    if(lignes == 0)
                        lignesC = 1;
                    if(lignesC < 1000)
                        rang = 1;
                    else if(lignesC < 5000)
                        rang = 3;
                    else
                        rang = 5;
                    var codeC = markers[i].getAttribute("code");
                    //var rang = Math.max(0, Math.floor(Math.log(lignesC)/Math.log(5.0) - 2));
                    var classe = "badge-nra fai" + markers[i].getAttribute("nbfai") + " taille" + rang;
    /* 				var texte = "<a onmouseover=\"alert('" + markers[i].getAttribute("ville"); */
                    var texte = "<a onmouseover='displayBox(this)' onmouseout='deleteBox(this)' infos=\"";
                    texte += "<b>" + codeC + "</b><br/>";
                    texte += markers[i].getAttribute("ville");
                    if(nom != null)
                        texte += " " + nom;
                    texte += "<br/>\n";
                    var presents = markers[i].getAttribute("presents");
                    if(presents != "")
                        texte += "Présents: " + presents + "<br/>\n";
                    var prevus = markers[i].getAttribute("prevus");
                    if(prevus != "")
                    {
                        texte += "Prévus: " + prevus + "<br/>\n";
                        classe += " prevu";
                    }
                    
                    texte += lignes + " lignes";
                    texte += "\" href='/" + code + "' nra='" + code + "'>" + markers[i].getAttribute("code") + "</a>";
                    var marqueur = new ELabel(point, texte, classe, new GSize(-19 - (rang * 2), 6 + rang), 100, true)
                    map.addOverlay(marqueur);
                    nraAffiches[code] = marqueur;
                }
			}
		}
	);
}

function displayBox(marker)
{
    //map.addOverlay(new ELabel(point, texte, classe, new GSize(-20, 8), 100, true));
    /*
var texte = document.createTextNode(marker.title);
    var box = document.createElement("div");
    box.appendChild(texte);
    box.className = "infobox";
*/
    marker.infoBox = new ELabel(nraAffiches[marker.getAttribute('nra')].getPoint(), marker.getAttribute('infos'), "infobox", new GSize(20, 30), 100, false)
    map.addOverlay(marker.infoBox);
//    marker.parentNode.parentNode.appendChild(box);
}

function deleteBox(marker)
{
    if(marker.infoBox != null)
        map.removeOverlay(marker.infoBox);
}