var a,GoogleMapsSupport={_maps:[],registerMap:function(c,b){b=b||c.getCenter();this._maps.push({map:c,point:b})},checkAllResize:function(){this._maps.each(function(c){var b=c.map;b.checkResize();(c=c.point)&&b.setCenter(c)})}};
if(typeof GIcon!="undefined"){ICONS={};ICONS.venue=new GIcon;ICONS.venue.iconSize=new GSize(21,31);ICONS.venue.iconAnchor=new GPoint(10,31);ICONS.venue.shadowSize=new GSize(21,31);ICONS.venue.infoWindowAnchor=new GPoint(11,5);ICONS.venue.image="/images/markers/green.png";ICONS.venue.shadow="/images/markers/color_marker_shadow.png";ICONS.bulletin=new GIcon;ICONS.bulletin.iconSize=new GSize(21,31);ICONS.bulletin.iconAnchor=new GPoint(10,31);ICONS.bulletin.shadowSize=new GSize(21,31);ICONS.bulletin.infoWindowAnchor=
new GPoint(11,5);ICONS.bulletin.image="/images/markers/orange.png";ICONS.bulletin.shadow="/images/markers/color_marker_shadow.png";ICONS.pro_con=ICONS.bulletin;ICONS.workgroup=new GIcon;ICONS.workgroup.iconSize=new GSize(21,31);ICONS.workgroup.iconAnchor=new GPoint(10,31);ICONS.workgroup.shadowSize=new GSize(21,31);ICONS.workgroup.infoWindowAnchor=new GPoint(11,5);ICONS.workgroup.image="/images/markers/blue.png";ICONS.workgroup.shadow="/images/markers/color_marker_shadow.png";ICONS.local_paper=ICONS.workgroup;
ICONS.faceless_user=new GIcon;ICONS.faceless_user.iconSize=new GSize(22,29);ICONS.faceless_user.iconAnchor=new GPoint(10,28);ICONS.faceless_user.shadowSize=new GSize(21,31);ICONS.faceless_user.infoWindowAnchor=new GPoint(11,5);ICONS.faceless_user.image="/images/markers/faceless_user_24.png";ICONS.faceless_user.shadow="/images/markers/color_marker_shadow.png"}
var MarkerNavigator={attach:function(c,b,d){this.baseUrl=b;this.map=c;this.markers=new Hash;this.options=d||{};GEvent.addListener(this.map,"movestart",this.hideBubbles.bind(this));GEvent.addListener(this.map,"moveend",this.update.bind(this));GEvent.addListener(this.map,"zoomend",function(){this.clear_markers()}.bind(this));this.progressControl=new LProgressControl(this.map);this.map.addControl(this.progressControl);this.update()},hideBubbles:function(){},open_info_url:function(c,b){c.openInfoWindowHtml('<img src="/images/progress/lights.gif"/>');
new Ajax.Request(b,{method:"get",onComplete:function(d){c.openInfoWindowHtml(d.responseText)}.bind(this)})},set_markers:function(c){c.each(function(b){if(!this.markers[b.id]){var d=new GLatLng(b.lat,b.lng);if(b.icon.type)icon=ICONS[b.icon.type];else{icon=new GIcon;dims=b.icon.dimensions;icon.iconSize=new GSize(dims[0],dims[1]);icon.iconAnchor=new GPoint(dims[2],dims[3]);icon.shadowSize=new GSize(dims[4],dims[5]);icon.infoWindowAnchor=new GPoint(dims[6],dims[7]);icon.image=b.icon.image;icon.shadow=
b.icon.shadow}var e=new GMarker(d,icon);GEvent.addListener(e,"click",function(){this.open_info_url(e,b.content_url)}.bind(this));this.map.addOverlay(e);this.markers[b.id]=e}}.bind(this))},_getExpandedMapBounds:function(){var c=this.map.getBounds(),b=c.getSouthWest();c=c.getNorthEast();var d=(c.lng()-b.lng())*1.015,e=(c.lat()-b.lat())*1.1;return new GLatLngBounds(new GLatLng(c.lat()-e,c.lng()-d),new GLatLng(b.lat()+e,b.lng()+d))},prune_markers:function(){var c=this._getExpandedMapBounds();this.markers.each(function(b){if(!c.containsLatLng(b.value.getPoint())){this.map.removeOverlay(b.value);
delete this.markers[b.key]}}.bind(this))},clear_markers:function(){this.markers=new Hash;this.map.clearOverlays()},update:function(){this.prune_markers();var c=this.map.getBounds();this.progressControl.loading();requestUrl=this.options.sandboxId?"/markers/"+this.options.sandboxId:"/markers";if(this.options.limit)requestUrl+="?limit="+this.options.limit;new Ajax.Request(this.baseUrl+requestUrl,{method:"get",parameters:$H({sw_lat:c.getSouthWest().lat(),sw_lng:c.getSouthWest().lng(),ne_lat:c.getNorthEast().lat(),
ne_lng:c.getNorthEast().lng(),format:"json"}).toQueryString(),onComplete:function(b){this.progressControl.done();try{eval("markerData = "+b.responseText);this.set_markers(markerData.markers)}catch(d){alert("Error: "+d)}}.bind(this)})}};
if(typeof GControl!="undefined"){function LProgressControl(){}LProgressControl.prototype=new GControl;LProgressControl.prototype.initialize=function(c){this.map=c;c=$(document.createElement("div"));this.map.getContainer().appendChild(c);var b=$(document.createElement("img"));b.src="/images/progress/lights.gif";b.style.display="block";this.spinner=b;c.appendChild(b);this.loading_status=0;return c};LProgressControl.prototype.loading=function(){this.loading_status+=1;if(this.loading_status>0)this.spinner.style.display=
"block"};LProgressControl.prototype.done=function(){this.loading_status-=1;if(this.loading_status<1)this.spinner.style.display="none"};LProgressControl.prototype.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(3,17))};function LGeoCoderControl(){}LGeoCoderControl.prototype=new GControl;a=LGeoCoderControl.prototype;a.initialize=function(c){this.map=c;this.defaultText="Stedsnavn eller adresse";c=$(document.createElement("div"));this.map.getContainer().appendChild(c);
var b=$(document.createElement("div"));b.className="map_search_controls";locationInputElement=$(document.createElement("input"));locationInputElement.size=20;locationInputElement.value=this.defaultText;locationInputElement.addClassName("unset");locationInputElement.onfocus=function(){if(locationInputElement.value==this.defaultText){locationInputElement.value="";locationInputElement.removeClassName("unset")}}.bind(this);locationInputElement.onblur=function(){if(locationInputElement.value==""){locationInputElement.value=
this.defaultText;locationInputElement.addClassName("unset")}}.bind(this);Event.observe(locationInputElement,"keypress",function(d){if(Event.getKey(d)==Event.KEY_RETURN){this.lookup(this.locationInputElement.value);Event.stop(d)}}.bind(this));b.appendChild(locationInputElement);this.locationInputElement=locationInputElement;b.appendChild(Button.linkToFunction(_("S\u00f8k"),function(){this.lookup(this.locationInputElement.value)}.bind(this),{size:"small",color:"white"}));this.onRemove&&b.appendChild(Button.linkToFunction(_("Fjern"),
function(){this.remove()}.bind(this),{size:"small",color:"white"}));c.appendChild(b);b=$(document.createElement("img"));b.src="/images/progress/spinner_16.gif";b.className="progress_indicator";b.style.display="none";this.spinner=b;c.appendChild(this.spinner);b=$(document.createElement("div"));b.style.display="none";b.className="map_search_result";this.messageDiv=b;c.appendChild(b);return c};a.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(5,5))};a.remove=function(){this.closeBalloon();
this.onRemove&&this.onRemove()};a.lookup=function(c){if(c!=this.defaultText&&c.length>0){this.locationInputElement.value=c;this.locationInputElement.removeClassName("unset");this.spinner.style.display="inline";this.map.closeInfoWindow();this.onInitiate&&this.onInitiate();var b=this.map.getBounds(),d=b.getSouthWest(),e=b.getNorthEast();b=e.lat()-d.lat();d=e.lng()-d.lng();new Ajax.Request("/-/map/geocode",{method:"get",parameters:$H({location:c,lng:this.map.getCenter().lng(),lat:this.map.getCenter().lat(),
lat_diff:b,lng_diff:d}).toQueryString(),onFailure:function(){this.onMiss&&this.onMiss();this.messageDiv.innerHTML=_("Fikk ikke kontakt med serveren. Pr\u00f8v igjen senere");new Effect.Appear(this.messageDiv,{duration:0.2});new Effect.Fade(this.messageDiv,{delay:4,duration:2})}.bind(this),onComplete:function(f){this.closeBalloon();this.spinner.hide();eval(f.responseText);if(geoCode.message){this.messageDiv.innerHTML=geoCode.message;new Effect.Appear(this.messageDiv,{duration:0.2});new Effect.Fade(this.messageDiv,
{delay:4,duration:2})}if(geoCode.length&&geoCode.length>0){f=geoCode;var g=f[0];this.onHit(new GLatLng(g.lat,g.lng),f);if(geoCode.length>1){this.balloon=new Balloon({origin:this.locationInputElement,className:"map_geocoder_balloon",content:this.createHitList(c,f),position:new Balloon.Position.Vertical(-75,-5),tail:null,rounded:true});this.balloon.open()}}else this.onMiss&&this.onMiss()}.bind(this),onException:function(f,g){Errors.log(g)}.bind(this)})}};a.createHitList=function(c,b){var d=b[0].matchText;
if(b[0].municipality){d+=" ";d+=_("i");d+=" ";d+=b[0].municipality}c=$(document.createElement("div"));var e=$(document.createElement("h4"));e.appendChild(document.createTextNode(_("Vi tror du mente %s. Andre steder som passet:",d)));c.appendChild(e);var f=$(document.createElement("ul"));b.slice(0,3).each(function(h){f.appendChild(this.createHitItem(h,b))}.bind(this));c.appendChild(f);if(b.length>3){d=$(document.createElement("a"));d.href="#";d.appendChild(document.createTextNode(_("(Vis alle %s treff\u2026)",
b.length)));d.observe("click",function(h){Event.element(h).remove();g.show();Event.stop(h)});c.appendChild(d);var g=$(document.createElement("ul"));g.style.display="none";b.slice(3,100).each(function(h){g.appendChild(this.createHitItem(h))}.bind(this));c.appendChild(g)}return c};a.createHitItem=function(c,b){var d=$(document.createElement("li")),e=$(document.createElement("a"));e.href="#";e.appendChild(document.createTextNode(_("%s (%s)",c.matchText,c.municipality)));e.observe("click",function(f){this.closeBalloon();
this.locationInputElement.value=c.matchText+", "+c.municipality;this.onHit(new GLatLng(c.lat,c.lng),b);Event.stop(f)}.bind(this));d.appendChild(e);return d};a.closeBalloon=function(){if(this.balloon){this.balloon.close();this.balloon=null}}};
