function createMarkerDepart(point)
{
	var icon = new GIcon();
	icon.image = "http://www.wanarun.net/i/depart.png";
	icon.iconSize = new GSize(16, 16);
	icon.iconAnchor = new GPoint(6, -8);
	icon.infoWindowAnchor = new GPoint(5, 1);
	
	var marker = new GMarker(point, icon);
	return marker;
}

function createMarkerArrive(point)
{
	var icon = new GIcon();
	icon.image = "http://www.wanarun.net/i/arrive.png";
	icon.iconSize = new GSize(16, 16);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	
	var marker = new GMarker(point, icon);
	return marker;
}
function createMarkerBorne(point,km)
{
	if (km!=0)
	{
		var icon = new GIcon();
		icon.image = 'http://www.wanarun.net/bornes/borne_'+km+'.gif';
		icon.iconSize = new GSize(16, 32);
		icon.iconAnchor = new GPoint(8,32);
		icon.infoWindowAnchor = new GPoint(0,0);
		var marker = new GMarker(point, icon);
		return marker;
	}
}

function saveCenterZoom(lat,lng,zoom,id) {
	/*dojo.xhrGet({
    	url: '/wanarun/htdocs/test/map/savecenterzoom.php?lat='+lat+'&lng='+lng+'&zoom='+zoom+'&id='+id,
    	method: "get",
	});*/
	
}

function WanarunControl() {
}
WanarunControl.prototype = new GControl();


// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
WanarunControl.prototype.initialize = function(map) {
	var container = document.createElement("div");
	var planDiv = document.createElement("div");
	this.setButtonStyle_(planDiv);
	container.appendChild(planDiv);
	planDiv.appendChild(document.createTextNode("Plan"));
	GEvent.addDomListener(planDiv, "click", function() {
			map.setMapType(G_NORMAL_MAP);
	});
	var mixteDiv = document.createElement("div");
	this.setButtonStyle_(mixteDiv);
	container.appendChild(mixteDiv);
	mixteDiv.appendChild(document.createTextNode("Mixte"));
	GEvent.addDomListener(mixteDiv, "click", function() {
			map.setMapType(G_HYBRID_MAP);
	});
	var satDiv = document.createElement("div");
	this.setButtonStyle_(satDiv);
	container.appendChild(satDiv);
	satDiv.appendChild(document.createTextNode("Satellite"));
	GEvent.addDomListener(satDiv, "click", function() {
			map.setMapType(G_SATELLITE_MAP);
	});
	var earthDiv = document.createElement("div");
	this.setButtonStyle_(earthDiv);
	container.appendChild(earthDiv);
	earthDiv.appendChild(document.createTextNode("Earth 3D"));
	GEvent.addDomListener(earthDiv, "click", function() {
			map.setMapType(G_SATELLITE_3D_MAP);
	});
	var distanceDiv = document.createElement("div");
	distanceDiv.name ='distance_div';
	distanceDiv.id ='distance_div';
	this.setButtonStyle_(distanceDiv);
	container.appendChild(distanceDiv);
	distanceDiv.innerHTML='0 m';
	map.getContainer().appendChild(container);
	return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
WanarunControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10));
}

// Sets the proper CSS for the given button element.
WanarunControl.prototype.setButtonStyle_ = function(button) {
	//button.style.textDecoration = "underline";
	button.style.color = "#FFFFFF";
	button.style.backgroundColor = "#009933";
	//probleme sous IE ????? pourquoi je sais pas !
	//button.style.font = 'Verdana, Arial, Helvetica, sans-serif';
	button.style.border = "1px solid #009933";
	button.style.padding = "2px";
	button.style.marginBottom = "3px";
	button.style.textAlign = "center";
	button.style.width = "6em";
	button.style.cursor = "pointer";
	
}

function showDistance(polyline_length)
{
	ddiv=document.getElementById("distance_div");
	ddiv.innerHTML=Math.floor(polyline_length)+" m";
}
var save_distance=false;
var id_parcours=0;

function loadJsonMap(src,map,id_map) {
	$.getJSON(src,
		function(data){
				points=new Array();
				//map.setCenter(new GLatLng(myObject.p[0]['t'],myObject.p[0]['n']),15);
				var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,160));
				//var mapTypeControl = new GMapTypeControl();
				//var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(200,200));
				//map.addControl(mapTypeControl, topRight);
				var topLeftWanarun = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
				map.addMapType(G_SATELLITE_3D_MAP);
				map.addControl(new GLargeMapControl(),topLeft);
				map.addControl(new WanarunControl());
				$.each(data.p, function(i,item){
					np=new GLatLng(item.t,item.n);
					points.push(np);
				});
				polyline=new GPolyline(points,'#009933',3,0.7);
				map.addOverlay(polyline);
				map.setCenter(polyline.getBounds().getCenter(),map.getBoundsZoomLevel(polyline.getBounds()));
				if(data.k.length>0) {
          $.each(data.k, function(i,item){
                 np=new GLatLng(item.t,item.n);
                 map.addOverlay(createMarkerBorne(np,item.k));
          });
        }
				map.addOverlay(createMarkerDepart(new GLatLng(data.p[0].t,data.p[0].n)));
				map.addOverlay(createMarkerArrive(new GLatLng(data.p[data.p.length-1].t,data.p[data.p.length-1].n)));
				//saveCenterZoom(polyline.getBounds().getCenter().lat(),polyline.getBounds().getCenter().lng(),map.getBoundsZoomLevel(polyline.getBounds()),id_map);
				mdistance=polyline.getLength();
				global_distance=polyline.getLength();
				showDistance(mdistance);
				if (save_distance) saveDistance(id_parcours,mdistance);
				//console.debug(map.getBoundsZoomLevel(polyline.getBounds()));
        map.getBoundsZoomLevel(polyline.getBounds());
				//alert(polyline.getLength());
		});
}

