function millier(nbre)
{
    var str_nbre = "";
   
    //    Découpage de la fin vers le début, par longueur de 3
    for (cpt = nbre.toString().length - 3; cpt >= 0; cpt = cpt - 3 )
    {
         str_nbre = nbre.toString().substr(cpt, 3) + "'" + str_nbre;
    }

    //    S'il y a un reste on traite
    if ( (nbre.toString().length % 3) != 0 )
        str_nbre = nbre.toString().substr(0, nbre.toString().length % 3) + "'" + str_nbre;
   
    //    Suppression du dernier .
    str_nbre = str_nbre.substr(0, str_nbre.length - 1);
   
    //    Retour du résultat
    return (str_nbre);
}

function WikipediaLayerCallback(json, wikiLayer) {
  for (var i = 0; i < json.geonames.length; i++) {
    var geoname = json.geonames[i];
    if (!wikiLayer.ids[geoname.title]) {
      var marker = this.createMarker(geoname, wikiLayer.markerIcon);
      wikiLayer.mgr.addMarker(marker, 0);
      wikiLayer.ids[geoname.title] = "exists";
    }
  }
}
 
WikipediaLayerCallback.prototype.formPageUrl = function(geoname) {
  return geoname.wikipediaUrl;
}

WikipediaLayerCallback.prototype.createMarker = function(geoname, baseIcon) {
  var markerIcon = new GIcon(baseIcon);

  markerIcon.iconSize = new GSize(24, 24);
  markerIcon.image = geoname.thumbnailImg;
  var marker = new GMarker(new GLatLng(geoname.lat, geoname.lng), {icon: markerIcon, title: geoname.title});

  if (geoname.title.length > 33) {
    geoname.title = geoname.title.substring(0, 33) + "&#8230;";
  }
  var html = "<div id='infowin' style='height:320px; width:470px;'>" +
            "<h1>" + geoname.title + ' <img src="' + geoname.thumbnailImg + '" \/><\/h1>' +
			"<p>" + (geoname.population != 0 ? "Population: " + millier(geoname.population) +"<br \/>" : "") + 
            geoname.summary + '<br \/><sup><a href="http://' + geoname.wikipediaUrl + '" target="wikipedia">Article complet<\/a></sup><br \/>' +
				'<\/p><p><a href="http://www.wikipedia.org" target="wikipedia"><img src="img/iw_wikipedia.png" border="0" \/><\/a></p><\/div>';

  marker.html = html;

  GEvent.addListener(marker, "click", function() {
    map.openInfoWindow(marker.getLatLng(), marker.html, {noCloseOnClick: true});
  });
 
  return marker;
}


function WikipediaLayer(map, opt_opts) {
  var me = this;
  me.ids = {};
  me.mgr = new MarkerManager(map, {maxZoom: 19});

  var icon = new GIcon();
  icon.image = "img/wikipedia_logo.png"; 
  icon.shadow = "img/image_ombre.pn";  
  icon.shadowSize = new GSize(50, 32); 
  icon.iconAnchor = new GPoint(9, 9);  
  icon.infoWindowAnchor = new GPoint(9, 0); 

  me.markerIcon = icon;
  me.enabled = false;

  GEvent.addListener(map, "moveend", function() {
    if (me.enabled) {
      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      var northEast = bounds.getNorthEast();
      me.load(me, {north: northEast.lat(), south: southWest.lat(), east: northEast.lng(), west: southWest.lng()});
    }
  });
}

WikipediaLayer.prototype.enable = function() {
  this.enabled = true;
  GEvent.trigger(map, "moveend");
}

WikipediaLayer.prototype.disable = function() {
  this.enabled = false;
  this.mgr.clearMarkers();
  this.ids = {};
}

WikipediaLayer.prototype.getEnabled = function() {
  return this.enabled;
}

WikipediaLayer.prototype.load = function(wikiLayer, userOptions) {
  var options = {
    formatted: "true",
    east: "-180",
    south: "-90",
    west: "180",
    north: "90",
    style: "full",
    lang: "fr"
  };
 
  for (optionName in userOptions) {
    if (userOptions.hasOwnProperty(optionName)) {
      options[optionName] = userOptions[optionName];
    }
  }
 
  var url = "http://ws.geonames.org/wikipediaBoundingBoxJSON?";
 
  for (optionName in options) {
    if (options.hasOwnProperty(optionName)) {
      var optionVal = "" + options[optionName] + "";
      url += optionName + "=" + optionVal + "&";
    }
  }
  
  var callbackName = "WikipediaLayerCallback.loader"; //ask dion
  eval(callbackName + " = function(json) { var pa = new WikipediaLayerCallback(json, wikiLayer);}");
 
  var script = document.createElement('script');
  script.setAttribute('src', url + 'callback=' + callbackName);
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

/**************************************************/

function PanoramioLayerCallback(json, panoLayer) {
  for (var i = 0; i < json.photos.length; i++) {
    var photo = json.photos[i];
    if (!panoLayer.ids[photo.photo_id]) {
      var marker = this.createMarker(photo, panoLayer.markerIcon);
      panoLayer.mgr.addMarker(marker, 0);
      panoLayer.ids[photo.photo_id] = "exists";
    }
  }
}

PanoramioLayerCallback.prototype.formImgUrl = function(photoId, imgType) {
  return 'http://www.panoramio.com/photos/' + imgType + '/' + photoId + '.jpg';
}
 
PanoramioLayerCallback.prototype.formPageUrl = function(photoId) {
  return 'http://www.panoramio.com/photo/' + photoId;
}

PanoramioLayerCallback.prototype.createMarker = function(photo, baseIcon) {
  var markerIcon = new GIcon(baseIcon);
  markerIcon.image = this.formImgUrl(photo.photo_id, "mini_square");
  var marker = new GMarker(new GLatLng(photo.latitude, photo.longitude), {icon: markerIcon, title: photo.photo_title});

  if (photo.photo_title.length > 33) {
    photo.photo_title = photo.photo_title.substring(0, 33) + "&#8230;";
  }
  var html = "<div id='infowin' style='height:320px; width:240px;'>" +
            "<p><a href='http://www.panoramio.com/' target='_blank'>" + 
             "<img src='http://www.panoramio.com/img/logo-small.gif' border='0' width='119px' height='25px' alt='Panoramio logo' /><\/a></p>" +
             "<a id='photo_infowin' target='_blank' href='" + photo.photo_url + "'>" +                
             "<img border='0' width='" + photo.width + "' height='" + photo.height + "' src='" + photo.photo_file_url + "'/><\/a>" +
             "<div style='overflow: hidden; width: 240px;'>" +
             "<p><a target='_blank' class='photo_title' href='" + photo.photo_url +
             "'><strong>" + photo.photo_title + "<\/strong><\/a></p>" +
             "<p>Posted by <a target='_blank' href='" + photo.owner_url + "'>" +
             photo.owner_name + "<\/a></p><\/div>" +
             "<\/div>";

  marker.html = html;

  GEvent.addListener(marker, "click", function() {
    map.openInfoWindow(marker.getLatLng(), marker.html, {noCloseOnClick: true});
  });
 
  return marker;
}


function PanoramioLayer(map, opt_opts) {
  var me = this;
  me.ids = {};
  me.mgr = new MarkerManager(map, {maxZoom: 19});

  var icon = new GIcon();
  icon.image = "http://www.panoramio.com/img/panoramio-marker.png"; 
  icon.shadow = "img/image_ombre.png";  
  icon.iconSize = new GSize(24, 24); 
  icon.shadowSize = new GSize(50, 32); 
  icon.iconAnchor = new GPoint(9, 9);  
  icon.infoWindowAnchor = new GPoint(9, 0); 

  me.markerIcon = icon;
  me.enabled = false;

  GEvent.addListener(map, "moveend", function() {
    if (me.enabled) {
      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      var northEast = bounds.getNorthEast();
      me.load(me, {maxy: northEast.lat(), miny: southWest.lat(), maxx: northEast.lng(), minx: southWest.lng()});
    }
  });
}

PanoramioLayer.prototype.enable = function() {
  this.enabled = true;
  GEvent.trigger(map, "moveend");
}

PanoramioLayer.prototype.disable = function() {
  this.enabled = false;
  this.mgr.clearMarkers();
  this.ids = {};
}

PanoramioLayer.prototype.getEnabled = function() {
  return this.enabled;
}

PanoramioLayer.prototype.load = function(panoLayer, userOptions) {
  var options = {
    order: "popularity",
    set: "full",
    from: "0",
    to: "30",
    minx: "-180",
    miny: "-90",
    maxx: "180",
    maxy: "90",
    size: "small"
  };
 
  for (optionName in userOptions) {
    if (userOptions.hasOwnProperty(optionName)) {
      options[optionName] = userOptions[optionName];
    }
  }
 
  var url = "http://www.panoramio.com/map/get_panoramas.php?";
  var uniqueID = "";
 
  for (optionName in options) {
    if (options.hasOwnProperty(optionName)) {
      var optionVal = "" + options[optionName] + "";
      url += optionName + "=" + optionVal + "&";
      uniqueID += optionVal.replace(/[^\w]+/g,"");
    }
  }
  var callbackName = "PanoramioLayerCallback.loader" + uniqueID; //ask dion
  eval(callbackName + " = function(json) { var pa = new PanoramioLayerCallback(json, panoLayer);}");
 
  var script = document.createElement('script');
  script.setAttribute('src', url + 'callback=' + callbackName);
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}
/**************************************************/

/*
*  MenuMapTypeControl Class 
*  Copyright (c) 2008, Moutons sauvages 
*  Author: El Khalifa Karim, others
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class lets you add a control to the map which mimics GMapTypeControl
*  and allows for the addition of a traffic button/traffic key.
*/


function MenuMapTypeControl(name,opt_opts) {
	this.name = name;
	this.options = opt_opts || {};
	this.container = "";
}


MenuMapTypeControl.prototype = new GControl();

MenuMapTypeControl.prototype.getContainer = function(map) {
	return this.container;
}

MenuMapTypeControl.prototype.initialize = function(map) {
	me = this;
	me.container = document.createElement("div");
	me.container.style.cssFloat = "left";
	me.container.style.styleFloat = "left";
	me.DD = Array();
	
	terrecontain = me.makeMenu("Terre");
	
	terreDd = document.createElement("dd");
	terreDd.style.border = "1px solid black";
	terreDd.style.borderTop = "0px";
	terreDd.style.margin = "0px";
	terreDd.style.padding = "0px";
	terreDd.style.listStyleType = "none";
	terreDd.id = "menu_show_Terre"
	me.DD.push(terreDd.id);
	terreDd.style.visibility = "hidden";

	terreUl = document.createElement("ul");
	terreUl.style.margin = "0px";
	terreUl.style.padding = "0px";
	terreUl.style.listStyleType = "none";
	
	var eNormalli = me.addSubMenu(terreUl, "Normal",0,"terre");
	eNormalli.firstChild.firstChild.checked = "checked";
	GEvent.addDomListener(eNormalli, "click", function() {
		map.setMapType(G_NORMAL_MAP);
		eNormalli.firstChild.firstChild.checked = "checked";
	});
	map.setMapType(G_NORMAL_MAP);
	
	ephysli = me.addSubMenu(terreUl, "Physique",0,"terre");
	GEvent.addDomListener(ephysli, "click", function() {
		map.setMapType(G_PHYSICAL_MAP);
		ephysli.firstChild.firstChild.checked = "checked";
	});
	
	eSatli = me.addSubMenu(terreUl, "Satellite",0,"terre");
	GEvent.addDomListener(eSatli, "click", function() {
		map.setMapType(G_SATELLITE_MAP);
		eSatli.firstChild.firstChild.checked = "checked";
	});
	
	eMixteli = me.addSubMenu(terreUl, "Mixte",1,"terre");
	GEvent.addDomListener(eMixteli, "click", function() {
		map.setMapType(G_HYBRID_MAP);
		eMixteli.firstChild.firstChild.checked = "checked";
	});
	
	var wikili = me.addSubMenu(terreUl, "Wikipedia");
	GEvent.addDomListener(wikili, "click", function() {
	    if (me.wikiLayer) {
			if (me.wikiLayer.getEnabled()) {
				me.wikiLayer.disable();
				wikili.firstChild.firstChild.checked = "";
			} else {
				me.wikiLayer.enable();
				wikili.firstChild.firstChild.checked = "checked";
			}
	    } else {
			me.wikiLayer = new WikipediaLayer(map);
			me.wikiLayer.enable();
			wikili.firstChild.firstChild.checked = "checked";
	    }
	});
	
	var panoli = me.addSubMenu(terreUl, "Photo");
	GEvent.addDomListener(panoli, "click", function() {
	    if (me.panoLayer) {
			if (me.panoLayer.getEnabled()) {
				me.panoLayer.disable();
				panoli.firstChild.firstChild.checked = "";
			} else {
				me.panoLayer.enable();
				panoli.firstChild.firstChild.checked = "checked";
			}
	    } else {
			me.panoLayer = new PanoramioLayer(map);
			me.panoLayer.enable();
			panoli.firstChild.firstChild.checked = "checked";
	    }
	});
	
	var tremli = me.addSubMenu(terreUl, "Tremblements");
	me.tremblement = "undefined";
	GEvent.addDomListener(tremli, "click", function() {
		if(me.tremblement != "undefined") {
			map.removeOverlay(me.tremblement);
			me.tremblement = "undefined";
			tremli.firstChild.firstChild.checked = "";
		} else {
			me.tremblement = new GGeoXml("http://map.moutons.ch/kml/tremblements.php");
			map.addOverlay(me.tremblement);
			tremli.firstChild.firstChild.checked = "checked";
		}
	});
	
	var templi = me.addSubMenu(terreUl, "Météo");
	me.meteo = "undefined";
	GEvent.addDomListener(templi, "click", function() {
		if(me.meteo != "undefined") {
			map.removeOverlay(me.meteo);
			me.meteo = "undefined";
			templi.firstChild.firstChild.checked = "";
		} else {
			me.meteo = new GGeoXml("http://map.moutons.ch/kml/weather.kml");
			map.addOverlay(me.meteo);
			templi.firstChild.firstChild.checked = "checked";
		}
	});
	
	
	terreDd.appendChild(terreUl);
	terrecontain.firstChild.appendChild(terreDd);
	
	me.container.appendChild(terrecontain);
	
	
	marscontain = me.makeMenu("Mars");
	
	marsDd = document.createElement("dd");
	marsDd.style.border = "1px solid black";
	marsDd.style.borderTop = "0px";
	marsDd.style.margin = "0px";
	marsDd.style.padding = "0px";
	marsDd.style.listStyleType = "none";
	marsDd.id = "menu_show_Mars"
	me.DD.push(marsDd.id);
	marsDd.style.visibility = "hidden";

	marsUl = document.createElement("ul");
	marsUl.style.margin = "0px";
	marsUl.style.padding = "0px";
	marsUl.style.listStyleType = "none";
	
	var mvisili = me.addSubMenu(marsUl, "Visible",0,"terre");
	mvisili.firstChild.firstChild.checked = "checked";
	GEvent.addDomListener(mvisili, "click", function() {
		map.setMapType(G_MARS_VISIBLE_MAP);
		mvisili.firstChild.firstChild.checked = "checked";
	});
	
	var minfrali = me.addSubMenu(marsUl, "Infrarouge",0,"terre");
	GEvent.addDomListener(minfrali, "click", function() {
		map.setMapType(G_MARS_INFRARED_MAP);
		minfrali.firstChild.firstChild.checked = "checked";
	});
	
	marsDd.appendChild(marsUl);

	marscontain.firstChild.appendChild(marsDd);
	
	me.container.appendChild(marscontain);
	
	
	lunecontain = me.makeMenu("Lune");
	
	luneDd = document.createElement("dd");
	luneDd.style.border = "1px solid black";
	luneDd.style.borderTop = "0px";
	luneDd.style.margin = "0px";
	luneDd.style.padding = "0px";
	luneDd.style.listStyleType = "none";
	luneDd.id = "menu_show_Lune"
	me.DD.push(luneDd.id);
	luneDd.style.visibility = "hidden";

	luneUl = document.createElement("ul");
	luneUl.style.margin = "0px";
	luneUl.style.padding = "0px";
	luneUl.style.listStyleType = "none";
	
	var lvisili = me.addSubMenu(luneUl, "Visible",0,"terre");
	lvisili.firstChild.firstChild.checked = "checked";
	GEvent.addDomListener(lvisili, "click", function() {
		map.setMapType(G_MOON_VISIBLE_MAP);
		lvisili.firstChild.firstChild.checked = "checked";
	});
	
	var leleli = me.addSubMenu(luneUl, "Altitude",0,"terre");
	GEvent.addDomListener(leleli, "click", function() {
		map.setMapType(G_MOON_ELEVATION_MAP);
		leleli.firstChild.firstChild.checked = "checked";
	});
	
	luneDd.appendChild(luneUl);

	lunecontain.firstChild.appendChild(luneDd);
	
	me.container.appendChild(lunecontain);
	
	
	cielcontain = me.makeMenu("Ciel");
	GEvent.addDomListener(cielcontain, "click", function() {
		map.setMapType(G_SKY_VISIBLE_MAP);
	});
	
	me.container.appendChild(cielcontain);
	
	
	map.getContainer().appendChild(me.container);
	return me.container;
}

MenuMapTypeControl.prototype.makeMenu = function(titre) {
	var me = this;
	
	container = document.createElement("div");
	container.style.cssFloat = "left";
	container.style.styleFloat = "left";
	container.style.height = "300px";
	
	Dl = document.createElement("dl");
	Dl.style.padding = "0px";
	Dl.style.listStyleType = "none";
	Dl.style.margin = "0px";

	
	Dt = document.createElement("dt");
	bt = me.createButton(titre);//mouseover
	Dt.appendChild(bt);
	Dt.style.margin = "0px";
	Dt.style.padding = "0px";
	Dt.style.listStyleType = "none";
	Dt.style.border = "1px solid black";
	
	GEvent.addDomListener(Dt, "mouseover", function() {
		me.montre("menu_show_" + titre);
	});
	Dl.appendChild(Dt);
	container.appendChild(Dl);
	
	GEvent.addDomListener(container, "mouseover", function() {
		me.montre("menu_show_" + titre);
	});
	
	GEvent.addDomListener(container, "mouseout", function() {
		me.montre("");
	});
	
	return container;
}

MenuMapTypeControl.prototype.addSubMenu = function(menu, titre, bottom, radio) {
	me = this;
	var li = document.createElement("li");
	li.style.margin = "0px";
	li.style.padding = "0px";
	li.style.listStyleType = "none";
	if(bottom) {
		li.style.borderBottom="1px solid #555";
	}
	GEvent.addDomListener(li, "mouseover", function() {
		li.firstChild.style.backgroundColor = "violet";
	});
	GEvent.addDomListener(li, "mouseout", function() {
		li.firstChild.style.backgroundColor = "white";
	});
	li.appendChild(me.createButton(titre,true,radio));
	menu.appendChild(li);
	return li;
}

MenuMapTypeControl.prototype.montre = function(id) {
	var me = this;
	for (var i = 0; i < me.DD.length; i++) {
		if (me.DD[i] == id) {
			document.getElementById(me.DD[i]).style.visibility='visible';
		} else {
			document.getElementById(me.DD[i]).style.visibility='hidden';
			document.getElementById(me.DD[i]).style.visibility='hidden';
		}
	}
}

MenuMapTypeControl.prototype.createButton = function(name,checkbox,radio) {
	var textDiv = document.createElement("div");
	if(checkbox) {
		if(radio) {
			var input = document.createElement("input");
			input.type="radio";
			input.name=radio;
			textDiv.appendChild(input);
			textDiv.style.textAlign = "left";
		} else {
			var input = document.createElement("input");
			input.type="checkbox";
			textDiv.appendChild(input);
			textDiv.style.textAlign = "left";
		}
	} else {
		textDiv.style.textAlign = "center";
	}
	textDiv.appendChild(document.createTextNode(name));
	textDiv.style.width = "8em";
	textDiv.style.color = "#000000";
	textDiv.style.backgroundColor = "white";
	textDiv.style.font = "small Arial";
	textDiv.style.padding = "0px";
	textDiv.style.margin= "0px";
	textDiv.style.fontSize = "12px"; 
	textDiv.style.cursor = "pointer";
	return textDiv;
} 

MenuMapTypeControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

/*
function couleur() {
	RGB = new Array("#000000","#FAEBD7","#00FFFF","#7FFFD4","#F0FFFF","#F5F5DC","#FFE4C4","#F0F8FF","#FFEBCD","#0000FF","#8A2BE2","#A52A2A","#DEB887","#5F9EA0","#7FFF00","#D2691E","#FF7F50","#6495ED","#FFF8DC","#DC143C","#00FFFF","#00008B","#008B8B","#B8860B","#A9A9A9","#006400","#BDB76B","#8B008B","#556B2F","#FF8C00","#9932CC","#8B0000","#E9967A","#8FBC8F","#483D8B","#2F4F4F","#00CED1","#9400D3","#FF1493","#00BFFF","#696969","#1E90FF","#B22222","#FFFAF0","#228B22","#FF00FF","#DCDCDC","#F8F8FF","#FFD700","#DAA520","#808080","#008000","#ADFF2F","#F0FFF0","#FF69B4","#CD5C5C","#4B0082","#FFFFF0","#F0E68C","#E6E6FA","#FFF0F5","#7CFC00","#FFFACD","#ADD8E6","#F08080","#E0FFFF","#FAFAD2","#90EE90","#D3D3D3","#FFB6C1","#FFA07A","#20B2AA","#87CEFA","#778899","#B0C4DE","#FFFFE0","#00FF00","#32CD32","#FAF0E6","#FF00FF","#800000","#66CDAA","#0000CD","#BA55D3","#9370DB","#3CB371","#7B68EE","#00FA9A","#48D1CC","#C71585","#191970","#F5FFFA","#FFE4E1","#FFE4B5","#FFDEAD","#000080","#FDF5E6","#808000","#6B8E23","#FFA500","#FF4500","#DA70D6","#EEE8AA","#98FB98","#AFEEEE","#DB7093","#FFEFD5","#FFDAB9","#CD853F","#FFC0CB","#DDA0DD","#B0E0E6","#800080","#FFBB00","#BC8F8F","#416901","#8B4513","#FA8072","#F4A460","#2E8B57","#FFF5EE","#A0522D","#C0C0C0","#87CEEB","#6A5ACD","#708090","#FFFAFA","#00FF7F","#4682B4","#D2B48C","#008080","#D8BFD8","#FF6347","#40E0D0","#EE82EE","#F5DEB3","#FFFFFF","#F5F5F5","#FFFF00","#9ACD32");

	var nombre=140;

	g=Math.floor(Math.random()*nombre);
	return RGB[g]
}
*/
// Mélanie Bron
//   0795606334
