var geograffiti=geograffiti||{};geograffiti.SearcherEvent={GoingToSearch:"geograffiti:goingToSearch",SearchStarted:"geograffiti:searchStarted",SearchCompleted:"geograffiti:searchCompleted",SearchCompletedWithFailure:"geograffiti:searchCompletedWithFailure"};geograffiti.Searcher=function(){var t={Content:0,Filter:1,UserVoiceMarks:2,Tag:3,AllWithTag:4,None:5};var f=t.None;var o=null;var e;var a=null;var A=null;var u=null;var z=null;var k=null;var l={botLeftLat:null,botLeftLon:null,topRightLat:null,topRightLon:null,zoom:null,searchWhat:null,searchWhere:null,offset:0,centerLat:null,centerLon:null,type:null,filter:null,vid:null,userid:null,uid:null,isAnonymous:null,userNameToSearch:null,gid:null};var p=function(B){return B.offset};var y=function(E){if(E){var F=E.page;if(F){l.offset=p(F)}}else{var D=gg.map.getBounds().southWest;var C=gg.map.getBounds().northEast;l.botLeftLat=D.latitude;l.botLeftLon=D.longitude;l.topRightLat=C.latitude;l.topRightLon=C.longitude;l.zoom=gg.map.getZoom();l.searchWhat=$F(a).strip().stripTags().stripScripts();l.searchWhere=$F(A).strip().stripTags().stripScripts()}var B=gg.map.getCenter();l.centerLat=B.latitude;l.centerLon=B.longitude};var m=function(B){y(B);o.fire(gg.SearcherEvent.GoingToSearch,{query:l});new Ajax.Request(gg.ApiUrl.ContentSearch,{parameters:$H(l),onSuccess:function(C){try{f=t.Content;k=C.responseText.evalJSON(true);if(gg.debug){gg.log("searchResults are %o",k)}if(k===null){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:C.status,statusText:C.statusText,responseText:C.responseText}});if(gg.debug){gg.log("AJAX call failed!!!")}}else{o.fire(gg.SearcherEvent.SearchCompleted,{query:l});gg.SideBar.displaySearchResults(k,{query:l});gg.map.displayResults(k)}}catch(D){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:C.status,statusText:C.statusText,responseText:C.responseText}});if(gg.debug){gg.log("AJAX call failed!!!")}}},onFailure:function(C){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:C.status,statusText:C.statusText,responseText:C.responseText}});if(gg.debug){gg.log("AJAX call failed!!!")}}});o.fire(gg.SearcherEvent.SearchStarted,{query:l})};var q=function(C){if(gg.debug){gg.log("SSSSEEEEAARRRRCHHH????? --->>>>> %o",C)}C.stop();var B=C.memo;l.offset=0;m(B)};var b=function(D){if(gg.debug){gg.log("!!!! filterSearchRequested with %o",D)}D.stop();var B=D.memo;var C=B.page;if(C){l.offset=C.offset}g(B)};var w=function(C){gg.log("user vms %o",C.memo);C.stop();var B=C.memo;if(typeof B.page==="undefined"){var D=C.memo.isAnonymous;l.uid=D?null:C.memo.id;l.vid=D?C.memo.id:null;l.isAnonymous=D;l.offset=0;l.userNameToSearch=C.memo.userName}else{l.offset=B.page.offset}d()};var n=function(C){C.stop();var B=C.memo;if(typeof B.page==="undefined"){l.offset=0;l.gid=C.memo.id}else{l.offset=B.page.offset}i()};var i=function(){o.fire(gg.SearcherEvent.GoingToSearch,{query:l});new Ajax.Request(gg.ApiUrl.GetGroupVoiceMarks,{parameters:$H(l),onSuccess:function(C){f=t.GroupVoiceMarks;o.fire(gg.SearcherEvent.SearchCompleted,{query:l});var B=C.responseText.evalJSON(true);gg.log("The response is %o",B);gg.map.displayGroupVoiceMarksResults(B);gg.SideBar.displayGroupVoiceMarksResults(B,{query:l})},onFailure:function(B){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:response.status,statusText:response.statusText,responseText:response.responseText}});gg.error("ERROOORRRR %o",B)}});o.fire(gg.SearcherEvent.SearchStarted,{query:l})};var d=function(){o.fire(gg.SearcherEvent.GoingToSearch,{query:l});new Ajax.Request(gg.ApiUrl.GetUserVoiceMarks,{parameters:$H(l),onSuccess:function(C){f=t.UserVoiceMarks;o.fire(gg.SearcherEvent.SearchCompleted,{query:l});var B=C.responseText.evalJSON(true);gg.log("The response is %o",B);gg.map.displayUserVoiceMarksResults(B);gg.SideBar.displayUserVoiceMarksResults(B,{query:l})},onFailure:function(B){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:response.status,statusText:response.statusText,responseText:response.responseText}});gg.error("ERROOORRRR %o",B)}});o.fire(gg.SearcherEvent.SearchStarted,{query:l})};var g=function(B){l.type="filter";o.fire(gg.SearcherEvent.GoingToSearch,{query:l});new Ajax.Request(gg.ApiUrl.FilterSearch,{parameters:$H(l),onSuccess:function(C){o.fire(gg.SearcherEvent.SearchCompleted,{query:l});f=t.Filter;if(gg.debug){gg.log("The response is %o",C)}var D=C.responseText.evalJSON(true);if(gg.debug){gg.log("The filter all result is %o",D)}gg.SideBar.displayFilterResults(D,{query:l});if(gg.debug){gg.log("about to call the map to filter")}gg.map.displayFilterResults(D);if(gg.debug){gg.log("done calling map to filte")}},onFailure:function(C){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:C.status,statusText:C.statusText,responseText:C.responseText}});if(gg.debug){gg.error("AJAX call for filter search failed with response %o",C)}}});o.fire(gg.SearcherEvent.SearchStarted,{query:l})};var x=function(C){C.stop();var B=C.memo;l.filter=B.filter;l.userid=B.userid;l.vid=B.vid;l.botLeftLat=B.botLeftLat;l.botLeftLon=B.botLeftLon;l.topRightLat=B.topRightLat;l.topRightLon=B.topRightLon;l.offset=0;g(B)};var v=function(){l.type="tag";o.fire(gg.SearcherEvent.GoingToSearch,{query:l});new Ajax.Request(gg.ApiUrl.TagSearch,{parameters:$H(l),onSuccess:function(B){f=t.Tag;if(gg.debug){gg.log("The response is %o",B)}var C=B.responseText.evalJSON(true);if(!C){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:B.status,statusText:B.statusText,responseText:B.responseText}})}else{o.fire(gg.SearcherEvent.SearchCompleted,{query:l})}gg.SideBar.displaySearchResults(C,{query:l});if(gg.debug){gg.log("about to call the map to filter")}gg.map.displayResults(C);if(gg.debug){gg.log("done calling map to filter")}},onFailure:function(B){if(gg.debug){gg.error("AJAX call for filter search failed with response %o",B)}o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:B.status,statusText:B.statusText,responseText:B.responseText}})}});o.fire(gg.SearcherEvent.SearchStarted,{query:l})};var h=function(C){C.stop();var B=C.memo;l.tag=B.tag;l.botLeftLat=B.botLeftLat;l.botLeftLon=B.botLeftLon;l.topRightLat=B.topRightLat;l.topRightLon=B.topRightLon;l.centerLat=B.centerLat;l.centerLon=B.centerLon;l.offset=0;v()};var s=function(){l.type="allwithtag";o.fire(gg.SearcherEvent.GoingToSearch,{query:l});new Ajax.Request(gg.ApiUrl.TagSearch,{parameters:$H(l),onSuccess:function(B){f=t.Tag;if(gg.debug){gg.log("The response is %o",B)}var C=B.responseText.evalJSON(true);if(!C){o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:B.status,statusText:B.statusText,responseText:B.responseText}})}else{o.fire(gg.SearcherEvent.SearchCompleted,{query:l})}gg.SideBar.displaySearchResults(C,{query:l});if(gg.debug){gg.log("about to call the map to filter")}gg.map.displayResults(C);if(gg.debug){gg.log("done calling map to filter")}},onFailure:function(B){if(gg.debug){gg.error("AJAX call for filter search failed with response %o",B)}o.fire(gg.SearcherEvent.SearchCompletedWithFailure,{query:l,error:{status:B.status,statusText:B.statusText,responseText:B.responseText}})}});o.fire(gg.SearcherEvent.SearchStarted,{query:l})};searchAllWithTagRequested=function(C){C.stop();var B=C.memo;l.tag=B.tag;l.botLeftLat=B.botLeftLat;l.botLeftLon=B.botLeftLon;l.topRightLat=B.topRightLat;l.topRightLon=B.topRightLon;l.centerLat=B.centerLat;l.centerLon=B.centerLon;l.offset=0;s()};var r=function(D){D.stop();var C=D.memo;var B=$H(l);B.update(D.memo);l=B.toObject();switch(f){case t.Content:m({});break;case t.Filter:g();break;case t.Tag:v();break;case t.UserVoiceMarks:d();break;default:gg.warn("lastSearchType invalid!-->%d",f);break}};var j=function(){o=$("searcher");e=$("searchform");a=e["searchbox-what"];A=e["searchbox-where"];u=$("sidebar");z=$("voicemarkmap")};var c=function(){e.observe("submit",q.bindAsEventListener(this));u.observe(gg.SideBarEvent.SearchRequested,q.bindAsEventListener(this));u.observe(gg.SideBarEvent.FilterSearchRequested,b.bindAsEventListener(this));u.observe(gg.SideBarEvent.ListenLinkClicked,w.bindAsEventListener(this));u.observe(gg.SideBarEvent.UserVoiceMarksSearchRequested,w.bindAsEventListener(this));u.observe(gg.SideBarEvent.GroupVoiceMarksSearchRequested,n.bindAsEventListener(this));u.observe(gg.SideBarEvent.ViewGroupVoiceMarksLinkClicked,n.bindAsEventListener(this));z.observe(gg.MapEvent.FilterSelected,x.bindAsEventListener(this));z.observe(gg.MapEvent.RepeatLastSearch,r.bindAsEventListener(this));z.observe(gg.MapEvent.TagLinkClicked,h.bindAsEventListener(this));z.observe(gg.MapEvent.SearchAllWithTagRequested,searchAllWithTagRequested.bindAsEventListener(this));z.observe(gg.MapEvent.UserVoiceMarksSearchRequested,w.bindAsEventListener(this))};j();c()}();