
MSG['geo_search_error']="Geocoder error";MSG['geo_search_nan']="Wrong coordinates"+"\n"+"Please enter: <b>latitude, longitude</b>";MSG['geo_search_warn']="Invalid or incomplete address";MSG['geo_search_nok']="This address has not been found."+"\n"+"Check the address or try another one around";MSG['geo_search_quota']="Geocoder is currently unavailable."+"\n"+"Please try again later.";MSG['geo_search_ok']="Ok, let's go!";MSG['whereami_start']='<b>'+"Type an address or search by zooming in the map"+'</b><br/>'+"and move this point to your location";MSG['whereami_confirm']="This point is not precise enough."+"\n"+"Save anyway?";MSG['usermap_user_zoom']="Zoom in";MSG['usermap_user_network']="View network";var whereami=Class.create();whereami.prototype.initialize=function(){if(!GBrowserIsCompatible()){$('nomap').style.display="block";return;}
Event.observe(window,"unload",function(){GUnload();});var map=new GMap2($("map"));this.map=map;map.enableDoubleClickZoom();map.addControl(new GMapTypeControl());map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(10,30)));this.zoom_observer=this.check_zoom.bindAsEventListener(this);GEvent.addListener(map,"zoomend",this.zoom_observer);this.geo_init();this.create_marker();var form=document.fw;(form.mz&&form.mz.value)?this.mz=_pi(form.mz.value):0;var self=this;form.rst.onclick=function(){var f=document.fw;f.lat.value=0;f.lng.value=0;f.zoom.value=0;self.save_position();};form.sav.onclick=function(){var z=_pi(document.fw.zoom.value);if(!document.fw.lat.value)return false;else if((isNaN(z)||z<10)&&(self.lat!=self._lat||self.lng!=self._lng))
{modal.say(MSG['whereami_confirm'],{icon:"warn"},{onclick:function(){self.save_position()}},{});}
else
{self.save_position();}
return false;};if(parseInt(form.zoom.value))
{this.lat=parseFloat(form.lat.value);this.lng=parseFloat(form.lng.value);this.zoom=parseInt(form.zoom.value);this._lat=this.lat;this._lng=this.lng;map.setCenter(new GLatLng(this.lat,this.lng),this.zoom);this.place_marker(new GLatLng(this.lat,this.lng));this.show_latlng();}
else
{map.setCenter(new GLatLng(30.4486,-1.40625),1);this.autoplace_marker();}};whereami.prototype.check_zoom=function(){var map=this.map;var z=map.getZoom();if(this.mz&&z>this.mz){z=this.mz;map.setZoom(z);}
if(z<1){z=1;map.setZoom(z);}
var form=document.fw;form.zoom.value=z;this.show_latlng();};whereami.prototype.create_marker=function(){var icon=new GIcon();icon.image=URL['L']+'/map/userpoint.png';icon.shadow=URL['L']+'/map/userpoint.bg.png';icon.iconSize=new GSize(30,40);icon.shadowSize=new GSize(45,30);icon.iconAnchor=new GPoint(15,40);icon.infoWindowAnchor=new GPoint(15,15);this.icon=icon;};whereami.prototype.autoplace_marker=function(){var map=this.map;var b=map.getBounds();var cw=b.getSouthWest();var xy=map.fromLatLngToDivPixel(cw);xy.x+=200;xy.y-=30;var point=map.fromDivPixelToLatLng(xy);if(this.marker)
{GEvent.clearInstanceListeners(this.marker);map.removeOverlay(this.marker);}
var marker=new GMarker(point,{icon:this.icon,draggable:true});map.addOverlay(marker);GEvent.addListener(marker,"drag",function(){map.closeInfoWindow()});GEvent.addListener(marker,"dragend",function(){this.place_marker(marker.getPoint())}.bind(this));if(!this.wai_started)
{marker.openInfoWindowHtml('<p>'+MSG['whereami_start']+'</p>');this.wai_started=true;}
this.marker=marker;};whereami.prototype.place_marker=function(point){var map=this.map;if(this.marker)
{GEvent.clearInstanceListeners(this.marker);map.removeOverlay(this.marker);}
var marker=new GMarker(point,{icon:this.icon,draggable:true});map.addOverlay(marker);GEvent.addListener(marker,"dragend",function(){this.set_position()}.bind(this));this.marker=marker;this.set_position();};whereami.prototype.set_position=function(){var point=this.marker.getPoint();var map=this.map;var z=map.getZoom();if(z<6)z=6;map.setCenter(point,z);this.lat=point.lat();this.lng=point.lng();var form=document.fw;form.lat.value=this.lat;form.lng.value=this.lng;form.zoom.value=z;this.show_latlng();};whereami.prototype.save_position=function(){var form=document.fw;modal.say(_MSG['saving']+"...",{flash:"wait"});this._lat=form.lat.value;this._lng=form.lng.value;IOL.API.request("user.set.geo",{lat:form.lat.value,lng:form.lng.value,zoom:form.zoom.value},this);};whereami.prototype.user_set_geo_onApi=function(success,rsp,txt,params){if(success)
{modal.say(_MSG['saved'],{icon:"ok",timeout:2000});}
else
{modal.complain(rsp,{},1);}};whereami.prototype.show_latlng=function()
{if(!this.lat||!this.lng)return;$('u_latlng').style.display="block";$('u_lat').innerHTML=new Number(this.lat).toPrecision(8)+'°';$('u_lng').innerHTML=new Number(this.lng).toPrecision(8)+'°';$('u_zoo').innerHTML=this.map.getZoom();};whereami.prototype.geo_init=function(){var form=document.fgeosearch;form.address.undo_value=form.address.value;form.address.style.color="#888";form.search.onclick=function(){this.blur();};form.onsubmit=function(){this.geo_search();return false;}.bind(this);form.address.onfocus=function(){if(this.value==this.undo_value){this.value='';this.style.color="#000";};this.select();};form.address.onblur=function(){if(!this.value.trim()){this.value=this.undo_value;this.focus();this.style.color="#888"}};};whereami.prototype.geo_search=function(force){var form=document.fgeosearch;var query=form.address.value.trim();if(!query||query==form.address.undo_value){alert(MSG['geo_search_warn']);return false;};modal.say(_MSG['processing'],{icon:"wait"});var m,p;if(m=query.match(/^([\-\d\.]+)[\s\,]+([\-\d\.]+)$/)){p=[parseFloat(m[1]),parseFloat(m[2])];}
if(typeof p=="object")
{if(isNaN(p[0])||isNaN(p[1])||p[0]<-90||p[0]>90||p[1]<-180||p[1]>180)
{return modal.say(MSG['geo_search_nan'],{},1);}
modal.say(MSG['geo_search_ok'],{icon:"ok",timeout:1000});var point=new GLatLng(p[1],p[0]);this.place_marker(point);return;}
IOL.API.request("loc.geocoder",{query:query,callback:"geo_search"},this);};whereami.prototype.geo_search_onApi=function(success,rsp,txt,params){if(!success)
{if(rsp.error==1)return modal.say(MSG['geo_search_warn'],{icon:"warn"},1);else if(rsp.error==2)return modal.say(MSG['geo_search_nok'],{icon:"warn"},1);else if(rsp.error==3)return modal.say(MSG['geo_search_quota'],{icon:"nok"},1);else return modal.say(MSG['geo_search_error'],{icon:"nok"},1);}
modal.shutup();var addresses=rsp.addrs;if(addresses.length){var address=addresses[0];var point=new GLatLng(parseFloat(address.lat),parseFloat(address.lng));return this.place_marker(point);}
else
{return modal.say(MSG['geo_search_error'],{},1);}};var usermap=Class.create();usermap.prototype.initialize=function(network){if(!GBrowserIsCompatible()){$('nomap').style.display="block";return;}
Event.observe(window,"unload",function(){GUnload();});var map=new GMap2($("map"));this.map=map;map.enableDoubleClickZoom();map.addControl(new GMapTypeControl());map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(10,30)));var points=[];map.setCenter(new GLatLng(30.4486,-1.40625),2);for(var i=0;i<network.length;i++)
{var u=network[i];if(!u.lat||!u.lng)continue;var point=new GLatLng(parseFloat(u.lat),parseFloat(u.lng));points.push(point);var icon=new GIcon();icon.image=URL['L']+'/map/userpoint.png';icon.shadow=URL['L']+'/map/userpoint.bg.png';icon.iconSize=new GSize(30,40);icon.shadowSize=new GSize(45,30);icon.iconAnchor=new GPoint(15,40);var marker=new GMarker(point,{icon:icon});map.addOverlay(marker);var photo=new GIcon();photo.image=u.doc_url;photo.iconSize=new GSize(25,25);photo.iconAnchor=new GPoint(13,37);photo.infoWindowAnchor=new GPoint(13,37);var thumb=new GMarker(point,{icon:photo});thumb._user=u;thumb.self=this;GEvent.addListener(thumb,"click",function(){this.self.info(this)});map.addOverlay(thumb);}
if(points.length>1)
{var rect=new GBounds(points);var sw=new GLatLng(rect.minY,rect.minX);var ne=new GLatLng(rect.maxY,rect.maxX);var bound=new GLatLngBounds(sw,ne);var c=bound.getCenter();var z=map.getBoundsZoomLevel(bound)-2;if(z<2)z=2;map.setCenter(c,z);}
else
{map.setCenter(points[0],2);}};usermap.prototype.info=function(marker){var map=this.map;var user=marker._user;var div=document.createElement("div");div.className="userinfowin";var img=document.createElement("img");img.src=user.doc_url;var tit=document.createElement("h4");var lnk=document.createElement("a");lnk.style.color="#000";lnk.href='/home/'+user.user_id;lnk.appendChild(document.createTextNode(user.title));tit.appendChild(lnk);var zoo=document.createElement("a");zoo.className="small";zoo.href="#";zoo.onclick=function(){var z=map.getZoom();map.setCenter(marker.getPoint(),(z<15)?z+5:z);};zoo.innerHTML=MSG['usermap_user_zoom'];var net=document.createElement("a");net.className="small";net.href='/user/'+user.user_id+'/network/map';net.innerHTML=MSG['usermap_user_network'];div.appendChild(img);div.appendChild(tit);div.appendChild(zoo);div.appendChild(document.createTextNode(' / '));div.appendChild(net);marker.openInfoWindow(div);};var geopointer=Class.create();geopointer.prototype.initialize=function(town,country){if(!GBrowserIsCompatible())return;Event.observe(window,"unload",function(){GUnload();});this.search(town,country);};geopointer.prototype.search=function(town,country){var query=town+', '+country;IOL.API.request("loc.geocoder",{query:query,callback:"search"},this);};geopointer.prototype.search_onApi=function(success,rsp,txt,params){if(success&&rsp.addrs&&rsp.addrs.length==1){var address=rsp.addrs[0];IOL.API.request("user.set.geo",{lat:address.lat,lng:address.lng,zoom:1,noback:true});}};