var GeoPublisher=Class.create({suggestBox:false,lastSearch:false,input:false,options:{geo:{},location:0,live:true,suggestAddress:false,asklocation:true,homepoint:true,community:0,getmore:0,fakelock:false,lookupauto:null,map:null,zindex:1000,mappoints:new Array(),locationpoints:new Array(),radius:100,infowindow:false,locationpoint:false,listolds:new Array(),oldactivitycnt:0,stopaction:false,restartaction:false,eventtype:false,haswall:true,streamvalid:false,lng:false,lat:false,selectedLocation:"",locations:new Array(),additionalwalltabs:new Hash(),mapoptions:{zoom:12,disableDefaultUI:true,navigationControl:true,mapTypeId:null,center:null}},initialize:function(A){Object.extend(this.options,A||{});this.options.mapoptions.mapTypeId=google.maps.MapTypeId.ROADMAP;var B=new google.maps.LatLng(48,14);this.options.mapoptions.center=B;this.options.map=this.initializeMap();if(this.options.haswall&&$("geo_publisher_tabs")){this.options.tabs=new UI.Tabs("geo_publisher_tabs")}},initializeMap:function(){map=new google.maps.Map(document.getElementById("geomap"),this.options.mapoptions);google.maps.event.addListener(map,"zoom_changed",function(){geopublisher.recalcRadiusFromMap()});google.maps.event.addListener(map,"click",function(A){if(!geopublisher.options.fakelock){geopublisher.setLocationPoint(A.latLng.lng(),A.latLng.lat(),0,"")}});return map},recalcRadiusFromMap:function(){var A=this.options.map.getBounds().getSouthWest().lng();var B=this.options.map.getCenter().lng();this.options.radius=(B-A)*111.12;try{if(this.options.live){this.options.lng=this.options.locationpoint.position.lng();this.options.lat=this.options.locationpoint.position.lat();Stream.setMode("raw_geo:"+this.options.lng+":"+this.options.lat+":"+this.options.radius)}}catch(C){}},selectAPos:function(A){this.setLocationPoint(A.lng,A.lat,A.locationid,A.name)},closeTab:function(A){if(this.options.additionalwalltabs.get(A)){if(this.options.additionalwalltabs.get(A).hasClassName("active")){this.updateNews()}this.options.additionalwalltabs.get(A).remove();this.options.additionalwalltabs.unset(A);try{Stream.removeContext("30:"+A)}catch(B){}}},openMessage:function(E,D,A){if(this.options.live){$("wall_form").hide();this.options.fakelock=true;if(!this.options.additionalwalltabs.get(A)){var C='<span class="icon-16-delete-circle tooltip" onclick="geopublisher.closeTab('+A+');" title="entfernen"></span>';var B=this.options.tabs.newTab(E+C,D,"geopublisher.selectTab('message',"+A+")");$("geo_publisher_tabs").insert(B);this.options.additionalwalltabs.set(A,B);try{Stream.addContext("30:"+A)}catch(F){}}else{var B=this.options.additionalwalltabs.get(A)}this.options.tabs.select(B);this.updateMessage(A);return false}return true},checkSearch:function(B,A){if(!geopublisher.suggestAddress||!A){return }if(this.lastSearch==A||A.length<3){return }this.geocoder.geocode({address:A},function(D,C){geopublisher.suggestAddress.update();D.each(function(E){var F=new Element("li");F.update(E.formatted_address);myloc={lat:E.geometry.location.lat(),lng:E.geometry.location.lng(),name:E.formatted_address,id:0,typeid:0};F.locationdata=myloc;F.on("click",function(){geopublisher.selectAPos(F.locationdata)});geopublisher.suggestAddress.insert(F)})});if(this.locationajax){this.locationajax.transport.abort()}this.locationajax=new Ajax.Request("/ajax/timeline/geolookup",{method:"post",parameters:{name:A},onSuccess:function(C){geopublisher.suggestLocations.update();C.responseJSON.location.each(function(D){var E=new Element("li");E.update(D.name);if(D.distance){E.text+=" (Entfernung: "+D.distance+"km)"}E.locationdata=D;E.on("click",function(){geopublisher.selectAPos(E.locationdata)});geopublisher.suggestLocations.insert(E);geopublisher.addMapLocation(D.lng,D.lat,D.locationid,D.name)});if(C.responseJSON.newlocation){}}})},startFormObserver:function(A){if(!this.geocoder){this.geocoder=new google.maps.Geocoder()}this.observer=new GeoPublisher.Observer(this.input,0.6,this.checkSearch.bind(this))},stopFormObserver:function(A){this.observer.stop()},hideResBox:function(B,A){if(!this.input){return }if(B&&B.type=="click"){if((B.findElement()==this.input)||B.findElement()==$("my_current_location")||B.findElement().innerText==this.options.selectedLocation||(B.findElement("div#"+this.suggestBox.id)==this.suggestBox)){return }}this.suggestBox.hide();this.stopFormObserver();if(!A){$("my_current_location").update("<a>"+this.options.selectedLocation+"</a>")}this.input=false},searchFormLocation:function(){oldvalue=this.options.selectedLocation;if(!this.input){this.input=new Element("input",{id:"location_search",type:"text",value:oldvalue,style:"width:220px;"});$("my_current_location").update(this.input);var B=new Element("ul");B.addClassName("ul-default");this.suggestAddress=B;var A=new Element("ul");A.addClassName("ul-default");this.suggestLocations=A;this.options.locations.each(function(C){C.on("click",function(){geopublisher.selectAPos(C.locationdata)});A.insert(C)});if(!this.suggestBox){this.suggestBox=$("search_suggestions")}this.suggestBox.update(B);this.suggestBox.insert(A);this.suggestBox.show();this.startFormObserver();this.input.focus();this.checkSearch()}},displayPoint:function(A,B){$("message").hide();var C=GEvent.addListener(this.options.map,"moveend",function(){var D=this.options.map.fromLatLngToDivPixel(A.getLatLng());Effect.Appear($("message"),{duration:1});$("message").style.top=D.y;$("message").style.left=D.x;GEvent.removeListener(C)});this.options.map.panTo(A.getLatLng())},orderOfCreation:function(B,A){return 1},updateLocationByForm:function(C){for(var B=0;B<C.length;B++){if(C.options[B].selected){var A=C.options[B].locationdata;this.setLocationPoint(A.lng,A.lat,A.locationid,A.name);break}}},setLocationPoint:function(A,E,H,I){this.hideResBox(false,true);var F;if(!this.options.locationpoint){var G="http://thydzik.com/thydzikGoogleMap/markerlink.php?color=FC6355";F=new google.maps.LatLng(E,A);var B={text:"<span id=infotext>"+I+"</span>",icon:G,zIndexProcess:this.orderOfCreation};if(this.options.homepoint){t_map=this.options.map}else{t_map=null}var C=new google.maps.Marker({position:F,map:t_map,icon:G});google.maps.event.addListener(C,"dragend",function(){geopublisher.setLocationPoint(C.position.lng(),C.position.lat(),0,"")});C.setDraggable(true);this.options.locationpoint=C}else{F=new google.maps.LatLng(E,A);this.options.locationpoint.setPosition(F)}if(!H){new Ajax.Request("/ajax/timeline/geolookup",{method:"post",parameters:{lng:A,lat:E,radius:this.options.radius,getuser:true},onSuccess:function(K){this.options.locations=new Array();K.responseJSON.location.each(function(L){var M=new Element("li");M.update(L.name);if(L.distance){M.insert(" (Entfernung: "+L.distance+"km)")}M.locationdata=L;geopublisher.options.locations.push(M);if(L.selected){$("selected-loc").update(L.name);$("my_current_location").update("<a>"+L.name+"</a>");geopublisher.options.selectedLocation=L.name}else{geopublisher.addMapLocation(L.lng,L.lat,L.locationid,L.name)}});if(K.responseJSON.newlocation){}if(K.responseJSON.users&&$("people-posted")){$("people-posted").update(K.responseJSON.users)}}})}else{$("selected-loc").update(I);$("my_current_location").update("<a>"+I+"</a>");this.options.selectedLocation=I}try{if(this.options.haswall){var J=Timeline.instance();J.setGeo(A,E,this.options.radius);J.options.community=1;if(H){J.options.oType=8;J.options.oId=H}else{J.options.oType=23;J.options.oId=Config.str.uid}J.options.type="feed"}this.options.lng=A;this.options.lat=E;if(this.options.live){Stream.setMode("raw_geo:"+this.options.lng+":"+this.options.lat+":"+this.options.radius)}}catch(D){}this.loadOldEntries();this.updateNews();this.options.map.panTo(F)},addMapLocation:function(D,H,C,G){var E="http://thydzik.com/thydzikGoogleMap/markerlink.php?color=EBEBEB";var F=new google.maps.LatLng(H,D);var B=new google.maps.Marker({position:F,map:this.options.map,title:G,icon:E});B.content="<span id=infotext>"+G+"</span>";B.longitude=D;B.lat=H;B.location=C;google.maps.event.addListener(B,"click",function(I){geopublisher.setLocationPoint(B.longitude,B.lat,B.location,G)});this.options.locationpoints.push(B);if(this.options.locationpoints.length>100){var A=this.options.locationpoints.shift();A.setMap(null)}},addOldActivity:function(A){this.options.listolds.push(A);if(this.options.listolds.length>100){var B=this.options.listolds.shift()}},showOldActivity:function(){if(geopublisher.options.stopaction){if(geopublisher.options.restartaction){geopublisher.options.stopaction=false;geopublisher.options.restartaction=false}return }if(geopublisher.options.listolds.length<=geopublisher.options.oldactivitycnt){geopublisher.options.oldactivitycnt=0}if(geopublisher.options.listolds.length>0){try{var A=geopublisher.options.listolds[parseInt(Math.random()*geopublisher.options.listolds.length)];if(A){geopublisher.addMapActivity(A.lng,A.lat,A.activitiy,A.description,A.picurl,A.link,A.actorName,A.time);geopublisher.options.oldactivitycnt++}}catch(B){}}},addMapActivity:function(P,F,B,Q,O,I,E,G){var H,A;switch(B){case"wallpost":H="http://thydzik.com/thydzikGoogleMap/markerlink.php?color=FC6355";A="#aa0000";break;case"comment":H="http://thydzik.com/thydzikGoogleMap/markerlink.php?color=55D7D7";A="#00aa00";break;default:H="http://thydzik.com/thydzikGoogleMap/markerlink.php?color=5680FC";A="#070707"}var L="";var T=new Date();var R=new Date(G*1000);var M=(T-R)/1000;if(M<60){geopublisher.options.stopaction=true;geopublisher.options.restartaction=true;L="<br/>(<strong>gerade jetzt</strong>)"}else{if(M<60*60){L="<br/>(vor "+Math.round(M/60,2)+" Minuten)"}else{if(M<60*60*24){L="<br/>(vor "+Math.round(M/60/60,2)+" Stunden)"}else{if(G){L="<br/>(vor "+Math.round(M/60/60/24,1)+" Tagen)"}}}}var D=new google.maps.LatLng(F,P);var K=new Label({position:D,map:this.options.map,zIndex:(this.options.zindex++),optimized:false,text:'<a href="/user/'+E+'"><img src="'+O+'" class="thumb" alt="'+E+'" /></a><span class="content">'+Q+L+"</span>"});var J=new google.maps.Marker({position:D,map:this.options.map,icon:"/images/szene1/icons/space.gif"});J.content='<img src="'+O+'" class="thumb" /><span class="content">'+Q+"</span>";J.longitude=P;J.lat=F;J.location=location;J.label=K;try{var W=K.getProjection();var V=W.fromLatLngToDivPixel(D);var U=new google.maps.Point(V.x+75,V.y);var C=W.fromDivPixelToLatLng(U,this.options.mapoptions.zoom);this.options.map.panTo(C)}catch(S){this.options.map.panTo(D)}this.options.mappoints.push(J);if(this.options.mappoints.length>3){var N=this.options.mappoints.shift();N.label.setMap(null);N.setMap(null)}},loadOldEntries:function(){new Ajax.Request("/ajax/timeline/loadolds",{method:"post",parameters:{lng:this.options.locationpoint.position.lng(),lat:this.options.locationpoint.position.lat(),radius:this.options.radius,type:this.options.eventtype},onSuccess:function(A){geopublisher.options.listolds.clear();A.responseJSON.activities.each(function(D){var C=geopublisher.evaluateMessage(D);if(C){var B={lng:C.longitude,lat:C.latitude,activitiy:C.type,description:C.msg,link:C.link,picurl:C.picurl,actorName:C.actorName,time:C.time};geopublisher.addOldActivity(B)}})}})},updateNews:function(B){if(this.options.locationpoint==false){unotify("Keine Position ausgewählt!");return }$("loader").update("");$("loader").className="";if(this.options.haswall&&$("userpage_wall")){var A=$("geo_publisher_tabs").down("li");this.options.tabs.select(A);var C=A.down("a.tab-text");C.style.color="black";C.notify=0;C.update(C.readAttribute("title"));Timeline.instance().getStart(B)}},updateUser:function(){if(!this.options.haswall){return false}var A=Loader.get("24");$("userpage_wall").update(A);var B=Timeline.instance();new Ajax.Request("/ajax/timeline/getpeople",{method:"post",parameters:{geo:Object.toJSON(B.options.geo),radius:this.options.radius},onSuccess:function(C){$("userpage_wall").update(C.responseJSON.userHtml)},onComplete:function(C){Loader.remove()}})},updateMessage:function(D){var A=Loader.get("24");var C=new Element("div",{id:"wall_obj_"+D});C.update(A);$("userpage_wall").update(C);request=new Ajax.Request("/ajax/timeline/message",{parameters:{dataid:D},onSuccess:function(E){C.update(E.responseJSON.content);this.options.fakelock=false},onComplete:function(E){Loader.remove()},onFailure:function(E){C.update("Fehler beim Abrufen des Wallpostings :(")}});var B=geopublisher.options.additionalwalltabs.get(D).down("a.tab-text");B.style.color="black";B.notify=0;B.update(B.readAttribute("title"))},selectTab:function(A,B){switch(A){case"all":$("wall_form").show();this.updateNews({filter:["news"],tags:[]});break;case"media":$("wall_form").show();this.updateNews({filter:["news"],tags:[1]});break;case"user":$("wall_form").hide();this.updateUser();break;case"message":$("wall_form").hide();this.updateMessage(B);break}return false},evaluateMessage:function(C){var A={};A.longitude=C.longitude;A.latitude=C.latitude;A.picurl=C.picURL;A.link=C.link;A.actorName=C.raw.actorName;A.time=C.raw.time;if(C.raw.className=="TimelineEvent_WallPosting"){A.type="wallpost";A.msg='<a href="/user/'+C.raw.actorName+'"><strong class="user-name">'+C.raw.actorName+"</strong></a> hat gepostet: '<strong>"+C.raw.content+"</strong>' <br /><span><a href=\"/user/"+C.raw.actorName+"/walldata/"+C.dataid+'" onclick="return geopublisher.openMessage(\''+C.raw.actorName+"','/user/"+C.raw.actorName+"/walldata/"+C.dataid+"',"+C.dataid+')">sehen & antworten</a></span>'}else{if(C.raw.className=="TimelineEvent_Meetpoint"){A.type="meetpoint";A.msg='<a href="/user/'+C.raw.actorName+'"><strong class="user-name">'+C.raw.actorName+'</strong></a> hat sich im Meetpoint von <a href="'+C.link+'">'+C.raw.name+"</a> eingetragen."}else{if(C.raw.className=="TimelineEvent_Comment"){var B;B='<a href="/user/'+C.raw.actorName+'"><strong class="user-name">'+C.raw.actorName+"</strong></a>";if(C.raw.itemType==1){B+=' hat ein <a href="'+C.link+'">Userfoto</a> kommentiert.'}else{if(C.raw.itemType==8){B+=' hat <a href="'+C.link+'">eine Location</a> kommentiert.'}else{if(C.raw.itemType==9){B=' hat <a href="'+C.link+'">ein Event</a> kommentiert.'}else{if(C.raw.itemType==5){B=' hat <a href="'+C.link+'">ein Eventfoto</a> kommentiert.'}else{B=' hat <a href="'+C.link+'">irgendetwas</a> kommentiert.'}}}}A.type="comment";A.msg=B}else{A.type="something";A.msg="hier is igwas passiert: "+C.raw.className}}}return A}});$(document).observe("base:ready",function(){if(typeof geopublisher!="undefined"){options=geopublisher.options}else{if(typeof geooptions!="undefined"){options=geooptions}else{return }}if(options.live){RealTime.instance().ownNewEventHandler();Stream.addContext("23:0");if(typeof geopublisher=="undefined"){Stream.setMode("raw_geo:"+options.lng+":"+options.lat+":"+options.radius)}else{geopublisher.options.streamvalid=true}}});$(document).observe("dom:loaded",function(){Label.prototype=new google.maps.OverlayView;Label.prototype.onAdd=function(){var D=this.getPanes().overlayLayer;D.appendChild(this.div_);var C=this;this.listeners_=[google.maps.event.addListener(this,"position_changed",function(){C.draw()}),google.maps.event.addListener(this,"text_changed",function(){C.draw()}),google.maps.event.addListener(this,"zindex_changed",function(){C.draw()})]};Label.prototype.onRemove=function(){this.div_.parentNode.removeChild(this.div_);for(var D=0,C=this.listeners_.length;D<C;++D){google.maps.event.removeListener(this.listeners_[D])}};Label.prototype.draw=function(){var D=this.getProjection();var C=D.fromLatLngToDivPixel(this.get("position"));this.div_.up().style.zIndex=1000;var F=this.div_;F.style.left=C.x+"px";F.style.top=C.y+"px";F.style.display="block";var E=this.get("zIndex");F.style.zIndex=E;this.span_.innerHTML=this.get("text").toString()};if(typeof geooptions!="undefined"){geopublisher=new GeoPublisher(geooptions)}else{geopublisher=new GeoPublisher()}function B(C){geopublisher.setLocationPoint(C.coords.longitude,C.coords.latitude,null,"Aktuelle Position")}function A(C){$("loader").update("");$("loader").className=""}if(geopublisher.options.lng&&geopublisher.options.lat){geopublisher.setLocationPoint(geopublisher.options.lng,geopublisher.options.lat,null,"Aktuelle Position")}if(geopublisher.options.asklocation){if(navigator.geolocation){navigator.geolocation.getCurrentPosition(B,A)}else{A("not supported")}}$("geomap").on("mouseover",function(){geopublisher.options.stopaction=true});$("geomap").on("mouseout",function(){geopublisher.options.stopaction=false});if($("notifications_filter")){$("notifications_filter").on("change",function(C){$("notifications_filter").disable();new Ajax.Request("/ajax/timeline/filter",{method:"post",parameters:{filter:$("notifications_filter").value},onComplete:function(){$("notifications_filter").enable()},onSuccess:function(D){}})})}geopublisher.options.pexec=new PeriodicalExecuter(geopublisher.showOldActivity,5)});$(document).observe("stream:newevent",function(E){if(!geopublisher.options.live){return }var D=E.memo.message;var B=geopublisher.evaluateMessage(D);if(geopublisher.options.eventtype==false||D.raw.className==geopublisher.options.eventtype){geopublisher.addMapActivity(B.longitude,B.latitude,B.type,B.msg,B.picurl,B.link,B.actorName,B.time);var A={lng:B.longitude,lat:B.latitude,activitiy:B.type,description:B.msg,picurl:B.picurl,link:B.link,actorName:B.actorName,time:B.time};geopublisher.addOldActivity(A)}if(D.raw.className=="TimelineEvent_WallPosting"&&geopublisher.options.haswall){if(!RealTime.instance().haveEvDataId(D.dataid)){RealTime.instance().addEvDataId(D.dataid);if(!$("geo_publisher_tabs").down("li").hasClassName("active")){var C=$("geo_publisher_tabs").down("li").down("a.tab-text");C.style.color="green";if(!C.notify){C.notify=1}else{C.notify++}C.update(C.readAttribute("title")+" ("+C.notify+")")}else{Timeline.instance().container.insert({top:D.wallpost});new Effect.Highlight(Timeline.instance().container.firstDescendant())}}}if(D.obj_id&&D.obj_type==30&&geopublisher.options.additionalwalltabs.get(D.obj_id)){if(!geopublisher.options.additionalwalltabs.get(D.obj_id).hasClassName("active")){var C=geopublisher.options.additionalwalltabs.get(D.obj_id).down("a.tab-text");C.style.color="green";if(!C.notify){C.notify=1}else{C.notify++}C.update(C.readAttribute("title")+" ("+C.notify+")")}}});function Label(B){this.setValues(B);var A=this.span_=document.createElement("span");var C=this.div_=document.createElement("div");C.appendChild(A);C.style.cssText="position: absolute; display: none";C.setAttribute("class","geo-msg")}GeoPublisher.Observer=Class.create(Abstract.TimedObserver,{getValue:function(){return Form.Element.getValue(this.element).trim()}});
