// JavaScript Document

legende=
{	
	name:["parkings","parking_free","poste","toilettes","boite_aux_lettres","distributeur","restaurants","hotels","hotels_restaurants","office_du_tourisme"],
	"parkings":
	{ 	
		icons:"parking.png",
		titre:"Les parkings",
		positions:	[
						[49.708190 , 0.205630],
						[49.708680 , 0.203000],
						[49.707510 , 0.203320],
						[49.707980 , 0.203690],
						[49.707581 , 0.200511]
						
				  	]
	},
	"parking_free":
	{
		icons:"parking-gratuit.png",
		titre:"Les parkings gratuits",
		positions:	[
				   		[49.701933 , 0.209405],
						[49.708880 , 0.215200],
						[49.704630 , 0.209560],
						[49.710850 , 0.207180],
						[49.703500 , 0.200080],
						[49.703380 , 0.200380]
					]
	},
	"poste":
	{
		icons:"poste.png",
		titre:"Les bureaux de poste",
		positions:	[
				   		[49.706240 , 0.204050]
					]
	},
	"toilettes":
	{
		icons:"WC.png",
		titre:"Les toilettes",
		positions:	[
				   		[49.704830 , 0.208750],
						[49.707370 , 0.205050],
						[49.708940 , 0.203140],
						[49.707095 , 0.200104]
					]
	},
	"boite_aux_lettres":
	{
		icons:"boite_aux_lettres.png",
		titre:"Les bo&icirc;tes aux lettres",
		positions:	[
				   		[49.707620 , 0.201690],
						[49.703100 , 0.211530],
						[49.703730 , 0.210660],
						[49.705150 , 0.207750],
						[49.707550 , 0.210160],
						[49.707160 , 0.205250],
						[49.708190 , 0.205180],
						[49.708130 , 0.203670]
					]
	},
	"distributeur":
	{
		icons:"GAB.png",
		titre:"Les distributeurs automatiques",
		positions:	[
				   		[49.706970 , 0.205330],
						[49.707610 , 0.203940],
						[49.708434 , 0.202399]
					]
	},
	"restaurants":
	{
		icons:"restaurant.png",
		titre:"Les restaurants",
		positions:	[
				   		[49.707330 , 0.200930],
						[49.707280 , 0.201740],
						[49.707100 , 0.202870],
						[49.707350 , 0.202010],
						[49.707050 , 0.203010],
						[49.707010 , 0.203130],
						[49.707860 , 0.201690],
						[49.707960 , 0.201860],
						[49.708580 , 0.202539],
						[49.709300 , 0.203510],
						[49.708420 , 0.203250],
						[49.708330 , 0.203370],
						[49.708090 , 0.204910],
						[49.708200 , 0.203570],
						[49.708240 , 0.203500]
					]
	},
	"hotels":
	{
		icons:"hotel.png",
		titre:"Les h&ocirc;tels",
		positions:	[
				   		[49.706560 , 0.204780],
						[49.707350 , 0.202020],
						[49.708110 , 0.203710],
						[49.707480 , 0.207120],
						[49.706010 , 0.203530],
						[49.708150 , 0.203640]
					]
	},
	"hotels_restaurants":
	{
		icons:"hotel-restaurant.png",
		titre:"Les h&ocirc;tels restaurants",
		positions:	[
				   		[49.707920 , 0.204020],
						[49.707770 , 0.204260],
						[49.708070 , 0.202010],
						[49.706490 , 0.212220],
						[49.706580 , 0.204820]
					]
	},
	"office_du_tourisme":
	{
		icons:"office_du_tourisme.png",
		titre:"L'office du tourisme",
		positions:	[
						[49.707510 , 0.205780]
					]
	}
};

function getMarker()
{
	array=(arguments.length==0)?legende.name:arguments;
	marker=[];
	for(i=0;i<array.length;i++)
	{
		marks=[];
		eval("var icone=legende."+array[i]+".icons");	
		eval("var positions=legende."+array[i]+".positions");
		for(p=0;p<positions.length;p++)
		{
			m=new GMarker(new GLatLng(positions[p][0], positions[p][1]), { icon: icone });
			marker.push(m);
			marks.push(m);
		}
		eval("legende."+array[i]+".markers=marks;");
	}
	return marker;
}
/*
<script type="text/javascript">
// création du tableau de test
var arr_0 = ['a', 'b', 'c','d','e','f',0,1,'null','null',null,''];

function test_ArrayIndexOf () {
	
	alert(arr_0.indexOf('d'));
	// 3
	
	alert(arr_0.indexOf(null));
	// -1
}
test_ArrayIndexOf();
</script>
*/
function hide()
{

	array=legende.name;
	args=[];
	for(i=0;i<arguments.length;i++)
		args.push(arguments[i]);
	for(i=0;i<array.length;i++)
	{
		eval("var marks=legende."+array[i]+".markers");
		hidden=(args.indexOf(array[i])==-1);
		for(p=0;p<marks.length;p++)
		{
			marks[p].show();	
			if(hidden)
				marks[p].hide();
		}
	}
}

function TextualZoomControl() {}
    TextualZoomControl.prototype = new GControl();

    TextualZoomControl.prototype.initialize = function(map) 
	{
		var container = document.createElement("div");
	
		var iconeP=document.createElement("img");
		iconeP.src="../../_icones/zoom_plus.png";
		Element.setStyle(iconeP,{cursor:"pointer"});
		Event.observe(iconeP, "mouseover", function(){this.src="../../_icones/zoom_plus_over.png";});
		Event.observe(iconeP, "mouseout", function(){this.src="../../_icones/zoom_plus.png";});
		
		var zoomInDiv = document.createElement("div");
		container.appendChild(zoomInDiv);
		zoomInDiv.appendChild(iconeP);
		GEvent.addDomListener(iconeP, "click", function() 
		{
		   map.zoomIn();
		   hide(selected);
		});
		
		
		var iconeM=document.createElement("img");
		iconeM.src="../../_icones/zoom_moins.png";
		Element.setStyle(iconeM,{cursor:"pointer",marginTop:"5px"});
		Event.observe(iconeM, "mouseover", function(){this.src="../../_icones/zoom_moins_over.png";});
		Event.observe(iconeM, "mouseout", function(){this.src="../../_icones/zoom_moins.png";});
		
		var zoomOutDiv = document.createElement("div");
		container.appendChild(zoomOutDiv);
		zoomOutDiv.appendChild(iconeM);
		GEvent.addDomListener(iconeM, "click", function() 
		{
			//if(map.getZoom()<14)
			if(map.getZoom()>14)
		   		map.zoomOut();
		   hide(selected);
		});
	
		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.
    TextualZoomControl.prototype.getDefaultPosition = function() {
      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
    }
	
function rePosition()
{
	var n,s,e,o;
	so=map.getBounds().getSouthWest();
	ne=map.getBounds().getNorthEast();	
	lso=limites.getSouthWest();
	lne=limites.getNorthEast();	
	
	n=(ne.y>lne.y)?ne.y-lne.y:0;
	s=(so.y<lso.y)?lso.y-so.y:0;
	y=(n==0)?-s:n;
	
	e=(ne.x>lne.x)?ne.x-lne.x:0;
	o=(so.x<lso.x)?lso.x-so.x:0;
	x=(e==0)?-o:e;
	
	center=new GLatLng(map.getCenter().y-y,map.getCenter().x-x);
	map.panTo(center);
	//map.setCenter(center);

}

var map;
var mgr;
var selected;
var limites;
function loadMap() 
{
      if (GBrowserIsCompatible()) 
	  {
			map = new GMap2($("interactiveMap"));  
			//map.addControl(new GSmallMapControl());
			map.addControl(new TextualZoomControl());
			
			map.setCenter(new GLatLng(49.707608, 0.209899), 14);
			GEvent.addListener(map,"zoomend",function(){setTimeout("hide('"+selected+"')",100);});
			GEvent.addListener(map,"moveend",function(){setTimeout("hide('"+selected+"')",100); if(!limites.containsBounds(map.getBounds()))rePosition();});
			limites=map.getBounds();
			//		Creation de Icones  
			
			for(i=0;i<legende.name.length;i++)
			{
				eval("var url=legende."+legende.name[i]+".icons;");
				var icon = new GIcon();
					icon.image = "../../_icones/"+url;
					icon.iconAnchor = new GPoint(16, 16);
					icon.infoWindowAnchor = new GPoint(16, 0);
					icon.iconSize = new GSize(25, 25);
				eval("legende."+legende.name[i]+".icons=icon;");
			}
			mgr = new GMarkerManager(map);
			marker=getMarker();
			mgr.addMarkers(marker,14);
			mgr.refresh();
			selected="office_du_tourisme";
			hide(selected);
			
			//alert();
			html="";
			for(i=0;i<legende.name.length;i++)
			{
				zeclass=(legende.name[i]==selected)?"select":"item";
				eval("var icone=legende."+legende.name[i]+".icons;");
				eval("var titre=legende."+legende.name[i]+".titre;");
				html+="<div onclick=\"changeLengend(this);\" id=\"legende_"+legende.name[i]+"\" class=\""+zeclass+"\">";
					html+="<img src=\""+icone.image+"\">";
					html+="<span>"+titre+"</span>";
				html+="</div>";
			}
		//alert(html);
		$("legende").innerHTML=html+"<br style=\"clear:both;\" />";
      }
	  
}
function changeLengend(t)
{
	if(t.id=="legende_"+selected)
		return false;
	Element.removeClassName($("legende_"+selected),"select");
	Element.addClassName($("legende_"+selected),"item");
	selected=t.id.replace("legende_","");
	Element.removeClassName(t,"item");
	Element.addClassName(t,"select");
	hide(selected);
}
onload=loadMap;
