		function HTMLrenderer(mapz){
			return ("martin");
		}
		function HTMLEmpty() {
			return ("obit");
		}

/********* PLACE *********/

		function Place(array,isJson,idx,user_id) {
			if (isJson) {
				this.id = array.id;
				this.user_id = array.user_id;
				this.lon = array.lon;
				this.lat = array.lat;
				this.name = array.name;
				this.idx = array.idx;
				this.categ = array.categ;
				this.photo = array.photo;
				this.tel = array.tel;
				this.address = array.address;
				this.rating = array.rating;
				this.nbcomments = array.nbcomments;
				this.desc = array.desc;
				this.user = array.user;
				this.from = array.from;
				this.avatar = array.avatar;
				this.status = array.status;
				this.distance = array.distance;
				this.privacy_lvl = array.privacy_lvl;
				this.created_at = array.created_at;
				this.attributes = array;
			} else {
				this.id = array.getAttribute('id');
				this.user_id = array.getAttribute('user_id');
				this.lon = array.getAttribute('lon');
				this.lat = array.getAttribute('lat');
				this.name = array.getAttribute('name');
				this.idx = array.getAttribute('idx');
				this.categ = array.getAttribute('categ');
				this.photo = array.getAttribute('photo');
				this.tel = array.getAttribute('tel');
				this.address = array.getAttribute('address');
				this.rating = array.getAttribute('rating');
				this.nbcomments = array.getAttribute('nbcomments');
				this.desc = array.getAttribute('desc');
				this.user = array.getAttribute('user');
				this.from = array.getAttribute('from');
				this.avatar = array.getAttribute('avatar');
				this.status = array.getAttribute('status');
				this.distance = array.getAttribute('distance');
				this.privacy_lvl = array.getAttribute('privacy_lvl');
				this.created_at = array.getAttribute('created_at');
				this.attributes = array.attributes;
			}
			this.isAuthor = (this.user_id==user_id)
			this.couleur = 1;
			this.marker=null;
			if (this.desc==null)
				this.desc="";
			if(this.photo != "" && this.photo!=null)
				this.photo  = "/proxy/img.php?url="+encodeURI(this.photo)+"&maxHeight=60&maxWidth=60";
			else
				this.photo = "";
			var namewospace = this.name.split(' ').join('');
			if(namewospace=="null" || namewospace=="")	this.name="Sans titre";
			if(this.tel=="undefined")	this.tel="";
			this.point  = new GLatLng(this.lat,this.lon);
			this.idx    = idx;
			if (this.address!="")	this.address = " @ " + this.address;
			
			this.getMarker = function() {
				if (this.marker==null) {
					var tinyIcon
					if(this.status != undefined){
						tinyIcon = new GIcon();
						tinyIcon.image = "/user/customMarker.php?name="+this.name+"&avatar="+this.avatar;
						tinyIcon.shadow  = "";
						tinyIcon.iconSize = new GSize(57, 67);
						tinyIcon.iconAnchor = new GPoint(5, 62);
						tinyIcon.infoWindowAnchor = new GPoint(5, 1);
						// Set up our GMarkerOptions object literal								
						markerOptions = { icon:tinyIcon };
					}
					else{
						tinyIcon = new GIcon();
						tinyIcon.image = "/images/sfr/categ_marker/"+this.categ+".gif";
						tinyIcon.shadow  = "";
						tinyIcon.iconSize = new GSize(30,45);
						tinyIcon.iconAnchor = new GPoint(15, 45);
						tinyIcon.infoWindowAnchor = new GPoint(15, 45);
						// Set up our GMarkerOptions object literal
						markerOptions = { icon:tinyIcon,draggable:true };
					}
					if (markerOptions!=null)
						this.marker = new GMarker(this.point,markerOptions);
					else
						this.marker = new GMarker(this.point);
					this.marker.disableDragging();
					this.marker.place = this;
					GEvent.addListener(this.marker,"click",function() {
						this.openInfoWindowHtml(this.place.toHTML2());
						//gmap.setCenter(this.getPoint());
						//gmap.panDirection(0,1);
					});
				}
				return this.marker;
			}
		
			this.toHTML = HTMLrenderer; /*function() {
				this.couleur = 1 - this.couleur;
				var content = "<div class=\"place_item\" style=\"margin-top:2px;margin-left:10px\"><div id=\"place_"+this.id+"\" class=\"place_item_"+this.couleur+"\">";
				content += "<TABLE class=\"place_item_title\">";
				content += "<TR><TD class=\"pi_title_left\" ROWSPAN=2><a href=\"#\" onclick=\"gmap.mapz.showPlace("+this.idx+",true)\" ><img src=\"/images/sfr/categ_marker/"+this.categ+".gif\" /></a></TD><TD class=\"pi_title_top\"><a href=\"#\" onclick=\"gmap.mapz.showPlace("+this.idx+",true)\" >";
				if(this.name.length>22)
					content += this.name.substr(0,19)+"...";
				else
					content += this.name;
				content += "</TD><td class=\"pi_title_right\" ROWSPAN=2 ><div class=\"border_styled\">Il y a <br/>"+this.from+"</div></td></TR>";
				content += "<TR><TD class=\"pi_title_bottom\"><div class=\"div_overflow\">"+this.address+"</div></TD></TR>";
				content += "</TABLE>";
				content += "<TABLE class=\"place_item_content\">";
				content += "<TR><TD class=\"pi_content_left\" ROWSPAN=2>";
				if(this.photo != "")
					content += "<img  src=\""+this.photo+"\"/>";
				if(this.desc.length>80)
					content += "</TD><TD class=\"pi_content_top\" colspan=\"2\">"+this.desc.substr(0,80)+"...</TD></TR>";
				else
					content += "</TD><TD class=\"pi_content_top\" colspan=\"2\">"+this.desc+"</TD></TR>";
				content += "<TR align=\"right\"  valign=\"bottom\"><TD class=\"pi_content_bottom\"><a href='/p/"+this.id+"'>"+displayCommentCount(this.nbcomments)+"</a></td><td width=\"65px\">"+displayRating(this.rating)+"</TD></TR>"
				content += "</TABLE><div class=\"pi_content_footer\"/></div></div></div>";
				return content;
				
			}
*/		
			this.toHTML2 = function() {
				var ago;
				var photoHtml="";
				var author="";
				var detail_link="";
				var privacy_lvl="";
				var modify="";
				var desc="";
				var created_at="";
				var rating="";

				if (this.status)
					photoHtml = '<img src='+unescape(this.avatar)+' class="photo_border" style="padding-right:8px;float:left;"/>';
				else
					if (this.photo)
						photoHtml = '<img src='+unescape(this.photo)+' class="photo_border" style="padding-right:8px;float:left;"/>'
					else
						photoHtml = '<img src="/images/cityFavorite/photo_no.png" class="photo_border" style="padding-right:8px;float:left;"/>';
				if (this.desc) {
					if (this.desc.length>100)
						desc='<i>'+this.desc.substring(0,97)+"..."+'</i>';
					else
						desc='<i>'+this.desc+'</i>';
				}
				if (this.status==undefined) {
					if (!this.rating)
						this.rating = -1;
					rating += "<div class=\"stars-small rank"+Math.round(this.rating)+"\"></div>";
				}
				if(this.user!="" && this.user!=null){
					author = 'Créé par <u><a href="/user/show/'+this.user+'" style="color:black;font-weight:bold;">' +this.user + "</a></u> ";
				}
				if (this.from!=null)
					ago = "il y a " + this.from;
				else
					ago = "";
				if (this.privacy_lvl!=null) {
					privacy_lvl = " - ";
					if (this.privacy_lvl=='1')
						privacy_lvl += 'Publique';
					if (this.privacy_lvl=='0')
						privacy_lvl += 'Privée';
					if (this.privacy_lvl=='2')
						privacy_lvl += 'Amis';
				}
				if (this.created_at)
					created_at = "inscrit depuis "+this.created_at;
				if(this.id != null) {
					if(this.status != undefined) {
						detail_link="<a href='/user/show/" + this.id + "' class='link' >Voir la fiche</a>";
					} else {
						detail_link="<a href='/place/show/" + this.id + "' class='link' >Voir la fiche</a>";
					}
				}
				if (this.status==undefined)
					modify = (this.isAuthor)?' - <a href="#" onclick="showModify(gmap.mapz.getPlace('+this.idx+'));">Modifier</a>':'';
				return "<div style=\"text-align:left;width:300px;overflow:hidden;\">"+ photoHtml +"<b>"+this.name+"</b><br/>"+created_at+desc+"<br/>"+rating+"<div style=\"margin-top:5px;\">"+author + ago + privacy_lvl+"</div><hr/>"+detail_link+modify+"</div>";
			}
	
	}

/********* MAPZ *********/
		function Mapz() {

		  this.places = new Array();

		  this.addPlace = function(place) {
			this.places.push(place);
		  }

		  this.parse = function(request,isJson,user_id) {
			var array;
			if (isJson==true) {
			  array = eval(request);
			  
			  for(i=0;i<array.length;i++) {
				place = new Place(array[i],true,i,user_id);
				this.addPlace(place);
			  }
			} else {
			  var xml = GXml.parse(request);
			  array = xml.getElementsByTagName('place');
			  if(array != undefined){
				  for (i=0;i<array.length;i++) {
					place = new Place(array[i],false,i);
					this.addPlace(place);
				  }
			  }
			}
			
			
		  }

		  this.length = function() {
				return this.places.length;
		  }

		  this.getPlace = function(i) {
				return this.places[i];
		  }

			this.showPlace = function(pos_id) {
				place = this.getPlace(pos_id);
				if (place.lat!="")
					gmap.openInfoWindowHtml(place.point,place.toHTML2());
			}


			this.deletePlaces = function() {
				this.places.clear();
			}

		}

/********* MAP  *********/

		function Map(div) {

			this.mapz = new Mapz();
			this.gmap = new GMap2(div);
			this.gmap.addControl(new GOverviewMapControl());
			this.gmap.addControl(new GLargeMapControl());
			this.gmap.addControl(new GMapTypeControl());
			this.gmap.enableScrollWheelZoom();

			this.toHTMLEmpty = HTMLEmpty;

			this.setCenter = function(lat,lon,zoom) {
				if (arguments.length==1)
					this.gmap.setCenter(arguments[0]);
				else if (arguments.length==2)
					this.gmap.setCenter(arguments[0],arguments[1]);
				else
					this.gmap.setCenter(new GLatLng(arguments[0],arguments[1]),arguments[2]);
			}

			this.setMapType = function(mapType) {
				this.gmap.setMapType(mapType);
			}

			this.addListener = function(action,fun) {
				GEvent.addListener(this.gmap,action,fun);
			}

			this.getCurrentMapType = function() {
				return this.gmap.getCurrentMapType();
			}

			this.addOverlay = function(marker) {
				this.gmap.addOverlay(marker);
			}

			this.clearOverlays = function() {
				for (var i=0;i<this.mapz.length();i++) {
					var truc = this.mapz.getPlace(i).getMarker();
					this.gmap.removeOverlay(truc);
				}
				this.mapz.deletePlaces();
			}

			this.getBounds = function () {
				return this.gmap.getBounds();
			}

			this.openInfoWindowHtml = function(point,content) {
				this.gmap.openInfoWindowHtml(point,content);
			}

			this.closeInfoWindow = function() {
				this.gmap.closeInfoWindow();
			}

			this.panDirection = function(i,j) {
				this.gmap.panDirection(i,j);
			}

			function centerMapOnAdress() {
				address=$("centerMap").value;
				geocoder.getLatgLng(address,
				 function(point) {
						if (!point) {
							alert(address + " not found");
						} else {
							this.setCenter(point, 14);
						}
					}
				);
			}

			this.getPlaces = function(url,format,user_id) {
				GDownloadUrl(url,function(data,responseCode) {
					gmap.clearOverlays();
					gmap.mapz.parse(data,format,user_id); // true=au format Json
					//$("place_count").innerHTML = gmap.mapz.length();
					var place_list = new Array();
					var listHTML="";
					for(var i=0;i<gmap.mapz.length();i++) {
						place = gmap.mapz.getPlace(i);
						place_list[i]=place.id;
						if (place.status != "pending" && place.lat!="" && (place.lat != 0.0 || place.lon!= 0.0 ))
								gmap.addOverlay(place.getMarker());
						var html = place.toHTML(gmap.mapz);
						if(html)
							listHTML += html;
					}
					if (listHTML == "")
						$("place_list").innerHTML = gmap.toHTMLEmpty();
					else
						$("place_list").innerHTML = listHTML;
					//setCook("place_list",place_list);  // used to navigate between places in place/show
				
				});
			}

			this.markerAtAdress = function(point,adress) {
				this.setCenter(point, 16);
				var tinyIcon = new GIcon();
				tinyIcon.image = "/images/sfr/categ_marker/none.gif";
				tinyIcon.shadow  = "";
				tinyIcon.iconSize = new GSize(30,45);
				tinyIcon.iconAnchor = new GPoint(15, 45);
				tinyIcon.infoWindowAnchor = new GPoint(15, 45);
				var marker = new GMarker(point,{draggable:true, icon:tinyIcon});
				GEvent.addListener(marker, "click", function() {
				  this.openInfoWindowHtml(getPopupContent(adress,this.getPoint()));
				  gmap.setCenter(this.getPoint());
				});
				GEvent.addListener(marker, "dragend", function() {
				  var point=this.getPoint();
				  gmap.setCenter(point);
				});
				this.addOverlay(marker);
				this.openInfoWindowHtml(point,getPopupContent(adress,point));
			 }

			 		// Search point and set position Part
			this.showAdress = function(adress){
				if(adress=="la défense" || adress=="la defense" || adress=="La défense" || adress=="la Defense" || adress=="La Défense")
					adress="Paris, la défense";
				geocoder.getLatLng(adress,function(point) {
					if (!point)
						alert(adress + " n'a pas pu être trouvé");
					else
						gmap.markerAtAdress(point,adress);
				});
			}
		}



		/*
		 * Search point and set position Part
  function showAddress(){
	address=$("search_field").value;
	if(address=="la défense" || address=="la defense" || address=="La défense" || address=="la Defense" || address=="La Défense"){
	  address="Paris, la défense";
	  $("search_field").value=address;
	}
	geocoder.getLatLng(address,
	function(point) {
	  if (!point) {
		alert(address + " n'a pas pu être trouvé");
	  } else {
		markerAtAdress(point, address);
	  }
	}
  );
  }


  function markerAtAdress(point, adress){
	gmap.setCenter(point, 16);
	var tinyIcon = new GIcon();
	tinyIcon.image = "/images/sfr/categ_marker/none.gif";
	tinyIcon.shadow  = "";
	tinyIcon.iconSize = new GSize(30,45);
	tinyIcon.iconAnchor = new GPoint(15, 45);
	tinyIcon.infoWindowAnchor = new GPoint(15, 45);
	var marker = new GMarker(point,{draggable:true, icon:tinyIcon});
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(getPopupContent(adress,marker.getPoint()));
	  gmap.setCenter(marker.getPoint());
	});
	GEvent.addListener(marker, "dragend", function() {
	  var point=marker.getPoint();
	  gmap.setCenter(point);
	});
	gmap.addOverlay(marker);
	gmap.openInfoWindowHtml(point,getPopupContent(adress,point));
  }
		 *
		 */

/********* MAPZ CONTROLLER *********/



 	function loadMapz(service,isJson,fun) {
		new Ajax.Request(service+((isJson==true)?"&format=json":""),
		{method: 'post',onComplete: function(originalRequest) {mapz=new Mapz();mapz.parse(originalRequest,isJson);fun(mapz);}});
	}
	
/*************UTILS *******************/
 function setCook(name,value){
	document.cookie = name + "=" + escape(value) + ";path=/";
  }

  function getCook(name){
	var deb = document.cookie.indexOf(name + "=")
	if (deb >= 0){
	  deb += name.length + 1
	  var fin = document.cookie.indexOf(";",deb)
	  if (fin < 0) fin = document.cookie.length
	  return unescape(document.cookie.substring(deb,fin))
	}
	return ""
  }
