
To.api.Location={getInfo:function(l,s,f,c){this._request({method:'getInfo',location_id:l},s,f,c);},_request:function(p,s,f,c){Ext.Ajax.request({url:'/api/ajax/location/',method:'GET',params:p,success:s,failure:f,scope:c});}};To.location.Map=function(config){To.location.Map.superclass.constructor.call(this,config);this._markers=new To.MapMarkerManager(this);this.on('load',this._loadMarkers,this);};Ext.extend(To.location.Map,To.Map,{_markers:null,_onLoadSuccess:function(r){r=Ext.decode(r.responseText).response;for(var i=0,l=r.length;i<l;i++){var d=r[i];this._markers.createMarker(new GLatLng(d.latitude,d.longitude),{icon:To.MapMarker.createLocationIcon(d),tooltip:d.name,infoUrl:'/ajax/mapinfo/location/location_id/'+d.location_id},d.location_id);}},_onLoadFailure:function(r){To.util.Growl.error('Unable to load map markers');},_loadMarkers:function(){this._markers.createMarker(new GLatLng(this.lat,this.lng),{tooltip:this.location_name,icon:this.location_icon},this.location_id);Ext.Ajax.request({url:'/api/ajax/location/location_id/'+this.location_id+'/',method:'GET',params:{method:'getNeighbours',limit:20},scope:this,success:this._onLoadSuccess,failure:this._onLoadFailure});}});To.location.MapSearch=Ext.extend(To.map.Search,{bookmark:false,history:false,_type:null,_placemark:null,getType:function(){return this._type;},getPlacemark:function(){return this._placemark;},_onSearchSubmit:function(s,q){this.manager._showLoading();this._type='unknown';this._placemark=null;var p={};Ext.apply(p,{search:q},this.manager._params);Ext.Ajax.request({url:'/ajax/locations/search/',method:'GET',params:p,scope:this,success:this._onSearchSuccess,failure:this._onSearchFailure});},_onSearchSuccess:function(r,o){r=Ext.decode(r.responseText);this._cache.query=o.params.search;this._cache.locations=r;this._onSearchDone();},_onSearchFailure:function(){this._cache.locations=null;this._onSearchDone();},_onSearchReset:function(){var m=this.manager;this._type=null;this._placemark=null;this._saveHistory();if(m._params.search!==undefined){delete m._params.search;m._refreshResults();}},_onSearchGeocode:function(s,c,ps){this._cache.placemarks=(Ext.isArray(ps)?ps:[]);this._onSearchDone();},_onSearchSuggestion:function(s,p){var m=this.manager;this._type='address';this._placemark=p;this._saveHistory(this.getSearch(),'address');delete m._params.search;m._map.setCenter(p.getLatitude(),p.getLongitude(),To.MapUtility.getZoom(p.getAccuracy()));},_onSearchDone:function(){if(this._cache.locations===undefined||this._cache.placemarks===undefined){return;}
var m=this.manager;var l=this._cache.locations.metadata.locations;var p=this._cache.placemarks;var ll=To.util.Array.count(l);var pl=To.util.Array.count(p);if(ll===0&&pl===0){var a=this.manager._areaSelect;var c=this.manager._categorySelect;if(a.getCurrentValue()!=a.getDefaultValue()||c.getCurrentValue()!=c.getDefaultValue()){this.manager._ignoreRefresh=true;a.reset();c.reset();this.manager._ignoreRefresh=false;this.form.child('input[type="submit"]').dom.click();return;}}
if(ll===0&&pl===1){this._onSuggestion(p[0]);}else{if(ll>0&&pl===1){this._updateSuggestions(p);}
if(ll>0||pl===0){this._type='location';this._saveHistory(this.getSearch(),'location');m._params.search=this._cache.query;m._updateResults(this._cache.locations);if(ll>0){m._mapIgnoreMove=true;m._map.setCenterOnBounds(m._mapMarkers.getBounds());m._mapIgnoreMove=false;}}else{this._type='address';this._saveHistory(this.getSearch(),'address');}}
this._cache={};m._hideLoading();},_init:function(){To.location.MapSearch.superclass._init.call(this);var H=To.History;this._cache={};if(this.bookmark){var s=H.getLast('map_search');var t=H.getLast('map_search_type');if(s&&t){this._type=t;this._search.removeClass('to_hint');this._search.dom.value=s;if(t==='address'){this._button.removeClass('cancel');}else{this._button.addClass('cancel');}
this._saveState();}}
this.on('submit',this._onSearchSubmit,this);this.on('reset',this._onSearchReset,this);this.on('geocode',this._onSearchGeocode,this);this.on('suggestion',this._onSearchSuggestion,this);},_saveHistory:function(s,t){if(this.history||this.bookmark){if(s&&t){To.History.add('map_search',s).add('map_search_type',t).save();}else{To.History.remove('map_search').remove('map_search_type').save();}}}});To.manager.Location={location_id:0,mapSource:null,_vote:null,_tabs:null,_form:null,initialize:function(config){Ext.apply(this,config);this._initVote();this._initComments();this._initCalendar();this._initMap();this._initLiked();this._initOpenings();},_onFormOpen:function(){this._tabs.collapse();this._form.open();},_onFormClose:function(){this._form.close();this._tabs.expand();},_onFormSuccess:function(form){this._tabs.showTab('all');this._tabs.reloadComments();this._onFormClose();},_onCommentRemove:function(comment){if(this._form!==null){this._form.reset();}},_onCalendarChange:function(s,d){Ext.Ajax.request({url:'/ajax/location/events',params:{location_id:this.location_id,date:d},scope:this,success:this._onCalendarSuccess});},_onCalendarSuccess:function(r){Ext.DomHelper.overwrite(Ext.get('to_location_events'),r.responseText);},_onVoteToggle:function(e){e.stopEvent();var b=Ext.get('to_vote_toggle');if(b.hasClass('hide')){this._vote.hide();b.removeClass('hide');}else{this._vote.show();b.addClass('hide');}},_onVoteChange:function(o,v){if(v&&v.general!==undefined){var e=Ext.get('to_vote_general');if(e){e.setStyle('width',(v.general*20)+'%');}}},_initVote:function(){var c=Ext.get('to_vote');if(c){this._vote=new To.ui.OverlayVoteEdit({container:c,resource_type:'LOCATION',resource_id:this.location_id,url:'/api/ajax/location/location_id/'+this.location_id,fields:['general','service','price','ambient','music','food','drink'],listeners:{change:this._onVoteChange,scope:this}});c=Ext.get('to_vote_toggle');if(c){c.on('click',this._onVoteToggle,this);}
c=Ext.get('to_location_toolbar');if(c){c.on('click',this._onVoteToggle,this);}}},_initCalendar:function(){var id='to_location';if(Ext.get(id+'_calendar')){var s=new To.event.Selector({id:id,calendar:new To.event.Calendar({id:id+'_calendar',store:new To.event.CalendarStore({url:'/api/ajax/location/location_id/'+this.location_id,data:this.metadata})})});s.on('change',this._onCalendarChange,this);}},_initComments:function(){var c;c=Ext.get('to_location_comment');if(c!==null){this._form=new To.comment.Form({resource_type:'LOCATION',resource_id:this.location_id,container:c,listeners:{cancel:this._onFormClose,success:this._onFormSuccess,scope:this}});}
c=Ext.get('to_comments');if(c){this._tabs=new To.comment.Tabs({container:c,resource_type:'LOCATION',resource_id:this.location_id,listeners:{commentadd:this._onFormOpen,commentedit:this._onFormOpen,commentremove:this._onCommentRemove,scope:this}});}},_initMap:function(){var c=Ext.get('to_map_expandable');if(c!==null){var e=new To.MapExpandable({container:c,map:new To.location.Map({container:Ext.get('to_map'),location_id:this.location_id,location_name:this.location_name,location_icon:this.location_category,lat:this.latitude,lng:this.longitude,zoom:17,source:this.mapSource})});}},_initLiked:function(){var c=Ext.get('liked');if(c!==null){var l=new To.ui.ExpandableGallery({container:c,url:'/ajax/location/liked',params:{location_id:this.location_id}});}},_initOpenings:function(){var c=Ext.get('to_openings');if(c){var o=new To.MiniTabs({container:c});}}};To.manager.Locations={_config:{},_map:null,_mapSearch:null,_mapPaginator:null,_mapMarkers:null,_mapPolygons:null,_mapIgnoreMove:false,_areaSelect:null,_categorySelect:null,_task:null,_cache:{},_params:{},_transaction_id:null,_paginator:null,_spotlightItems:null,_resultsItems:null,_ignoreRefresh:false,initialize:function(config){this._config=config;To.History.initialize();this._task=new Ext.util.DelayedTask(this._refreshResults,this);this._spotlightItems=new To.ui.ItemManager();this._resultsItems=new To.ui.ItemManager();if(config.map_geocoder_suffix){To.map.GeocoderUtility.setConfig({type:config.map_geocoder,suffix:config.map_geocoder_suffix});}
this._areaSelect=new To.Select({container:'to_overlay_zones',name:'area_id',bookmark:true,listeners:{open:function(){this._categorySelect.close();},change:this._onAreaChange,scope:this}});this._categorySelect=new To.Select({container:'to_overlay_cats',name:'category_id',bookmark:true,listeners:{open:function(){this._areaSelect.close();},change:this._onCategoryChange,scope:this}});this._map=new To.Map({container:Ext.get('to_locations_map'),autoload:true,bookmark:true,lat:config.map_latitude,lng:config.map_longitude,zoom:config.map_zoom,source:config.map_source,controlOptions:{paginator:true},listeners:{scope:this,load:this._onMapLoad,moveend:this._onMapMoveEnd}});this._mapMarkers=new To.MapMarkerManager(this._map);this._mapPolygons=new To.MapPolygonManager(this._map);this._mapSearch=new To.location.MapSearch({container:Ext.get('to_map_search'),manager:this,bookmark:true});var c=Ext.get('to_comments_history');if(c){var h=new To.GrowableSimple({container:c,url:'/ajax/locations/comments/',start:10,limit:10});}},_onMapLoad:function(e){var c=this._map.getMapControl();if(c){var p=c.getMapPaginator();if(p){this._mapPaginator=p;p.on({prevPage:this._onMapPrevPage,nextPage:this._onMapNextPage,scope:this});}}
var v;var h=To.History;v=this._areaSelect.getCurrentValue();if(v!==null&&v!='0'){this._params.area_id=v;var polygon=this._mapPolygons.createPolygon(this._config.polygons[v],'area');if(!h.getLast('map_latitude')||!h.getLast('map_longitude')||!h.getLast('map_zoom')){this._mapIgnoreMove=true;this._map.setCenterOnBounds(polygon.getBounds());this._mapIgnoreMove=false;}}
v=this._categorySelect.getCurrentValue();if(v!==null&&v!='0'){this._params.category_id=v;}
v=this._mapSearch.getSearch();if(v!==null&&v!==''){var t=this._mapSearch.getType();if(t!=='address'){this._params.search=v;Ext.fly('to_map_search').child('input[type="submit"]').dom.click();this._ignoreRefresh=true;}}
v=h.getLast('page');if(!v){v=1;}
this._refreshResults(v);this._ignoreRefresh=false;},_onMapMoveEnd:function(e){var p;if(!this._mapIgnoreMove){this._task.delay(250);}
p=this._mapSearch.getPlacemark();if(p!==null){var mb=this._map.getBounds();if(mb&&!mb.containsLatLng(new google.maps.LatLng(p.getLatitude(),p.getLongitude()))){this._mapSearch.reset();}}
p=this._mapPolygons.getPolygon('area');if(p!==null){var pb=p.getBounds();if(!pb.intersects(this._map.getBounds())){this._mapPolygons.removePolygon('area');this._areaSelect.reset();}}},_onMapPrevPage:function(p){var c=this._map.getMapControl();if(this._paginator&&!c.hasLoading()){this._paginator.prev();}},_onMapNextPage:function(p){var c=this._map.getMapControl();if(this._paginator&&!c.hasLoading()){this._paginator.next();}},_onAreaChange:function(s,l,v){this._mapPolygons.clearPolygons();if(v!='0'){this._params.area_id=v;this._ignoreRefresh=true;this._mapSearch.reset();this._ignoreRefresh=false;if(this._map._isLoaded()){if(this._config.polygons[v]!==null&&this._config.polygons[v].length>0){var p=this._mapPolygons.createPolygon(this._config.polygons[v],'area');this._map.setCenterOnBounds(p.getBounds());}}}else{delete this._params.area_id;this._refreshResults();}},_onCategoryChange:function(s,l,v){if(v!='0'){this._params.category_id=v;this._ignoreRefresh=true;this._mapSearch.reset();this._ignoreRefresh=false;}else{delete this._params.category_id;}
this._refreshResults();},_showLoading:function(){if(this._map!==null){var c=this._map.getMapControl();if(c){c.showLoading();}}
if(this._paginator!==null){this._paginator.showLoading();}},_hideLoading:function(){if(this._map!==null){var c=this._map.getMapControl();if(c){c.hideLoading();}}
if(this._paginator!==null){this._paginator.hideLoading();}},_registerItems:function(c,locations,manager){c.select('.to_item',true).each(function(i){var id=i.id.split('_');id=id[id.length-1];if(typeof locations[id]!=='undefined'){manager.addItem(this._registerItem(i,id,locations[id]),id);}},this);},_registerItem:function(c,id,data){var marker=this._mapMarkers.getMarker(id);if(marker===null)
{marker=this._mapMarkers.createMarker(new google.maps.LatLng(data.latitude,data.longitude),{icon:To.MapMarker.createLocationIcon(data),tooltip:data.name,infoUrl:'/ajax/mapinfo/location/location_id/'+data.location_id},id);}
return new To.ui.Item({container:c,marker:marker});},_registerPaginator:function(c){var p=null;if(c!==null){p=new To.Paginator({container:c,bookmark:true,listeners:{gotoPage:function(e,page){this._refreshResults(page);},scope:this}});}
if(this._mapPaginator!==null){var t=this._mapPaginator;if(p===null){t.hide();}else{t.show();if(!p.hasPrev()){t.hidePrev();}else{t.showPrev();}
if(!p.hasNext()){t.hideNext();}else{t.showNext();}}}
return p;},_updateResults:function(r){var m=r.metadata;this._spotlightItems.clearItems();this._resultsItems.clearItems();Ext.DomHelper.overwrite(Ext.get('to_locations_results'),r.html);Ext.DomHelper.overwrite(Ext.get('to_locations_spotlight'),r.spotlight);Ext.get('to_locations_results_count').dom.innerHTML=m.count;Ext.get('to_locations_results_page').dom.innerHTML=m.page;Ext.get('to_locations_results_pages').dom.innerHTML=m.pages;var markers=this._mapMarkers.getMarkers();for(var key in markers){if(!m.locations[key]){this._mapMarkers.removeMarker(key);}}
this._registerItems(Ext.get('to_locations_results'),m.locations,this._resultsItems);this._registerItems(Ext.get('to_locations_spotlight'),m.locations,this._spotlightItems);mappabounds=this._map.getBounds();mappa_sw=mappabounds.getSouthWest();mappa_ne=mappabounds.getNorthEast();delta_lat=mappa_ne.lat()-mappa_sw.lat();delta_lng=mappa_ne.lng()-mappa_sw.lng();start_lat=mappa_sw.lat()+(delta_lat*25/100);start_lng=mappa_sw.lng()+(delta_lng*25/100);rnd_lat=start_lat+Math.random()*(delta_lat/2);rnd_lng=start_lng+Math.random()*(delta_lng/2);scarpamarker=this._mapMarkers.createMarker(new google.maps.LatLng(rnd_lat,rnd_lng),{icon:'shoe',tooltip:'',infoUrl:'/dateworld.html'},id);if(this._spotlightItems.count()>0){Ext.get('to_locations_spotlight_title').show();}else{Ext.get('to_locations_spotlight_title').hide();}
if(this._paginator){this._paginator.destructor();}
this._paginator=this._registerPaginator(Ext.get('to_locations_results').child('.to_pagination'));},_refreshResults:function(page){if(this._ignoreRefresh){return false;}
if(!this._map._isLoaded()){return false;}
if(!page){page=1;}
if(this._transaction_id!==null){Ext.Ajax.abort(this._transaction_id);this._transaction_id=null;}
this._showLoading();var params={};Ext.apply(params,this._params,{page:page,bounds:To.MapUtility.getBoundsUrl(this._map.getBounds([40,10,10,0]))});this._transaction_id=Ext.Ajax.request({url:'/ajax/locations/search/',method:'GET',params:params,scope:this,success:function(r){this._transaction_id=null;r=Ext.decode(r.responseText);this._updateResults(r);this._hideLoading();},failure:function(r){this._transaction_id=null;this._hideLoading();To.util.Growl.error('Unable to find locations');}});}};