angular.module("location.module",["lta.services"]).controller("locationCtrl",["$scope","$rootScope","helper",function(n,t,i){"use strict";n.showLocations=!1,n.storeUserLocation=function(n){var t=JSON.stringify(n);i.setStorage("ltalocation",t,1,!0)},n.setUserLocation=function(i,r){t.userLocation=i,t.locationError=!1,n.showLocations=!1,r||n.storeUserLocation(i)},n.setLocations=function(t){n.locations=t,n.showLocations=n.showLocations?!1:!0,n.$apply()},n.presetLocation=function(t){for(var i=0;i<t.length;i++)t[i].types.indexOf("postal_town")>-1&&(n.setUserLocation(t[i]),i=t.length,n.$apply())},n.buildAutoComplete=function(t){var u=document.getElementById(t),i="uk",r=new google.maps.places.Autocomplete(u,{componentRestrictions:{country:i}}),f;google.maps.event.addListener(r,"place_changed",function(){n.setUserLocation(r.getPlace()),n.$apply()}),$(u).keyup(function(t){var u=$(this).val(),e,o;RegExp("(im)[0-9]").test(u)||u.toLowerCase().indexOf("isle of m")!=-1?i!="im"&&(i="im",r.setComponentRestrictions({country:i})):u.toLowerCase().indexOf("guernsey")!=-1?i!="gy"&&(i="gy",r.setComponentRestrictions({country:i})):RegExp("(je)[0-9]").test(u)||u.toLowerCase()=="jersey"?i!="je"&&(i="je",r.setComponentRestrictions({country:i})):i!="uk"&&(i="uk",r.setComponentRestrictions({country:i})),t.keyCode!=13?f=$(".pac-item-selected").length:f==0&&(e=$(this).val(),o=new google.maps.Geocoder,o.geocode({address:e,componentRestrictions:{country:i}},function(t){n.setUserLocation(t[0],!0),n.$apply()}))})},n.revGeocode=function(t){var i=new google.maps.Geocoder,r=new google.maps.LatLng(t.coords.latitude,t.coords.longitude);i.geocode({latLng:r},function(t){n.setPreset?n.presetLocation(t):n.setLocations(t)})},n.checkForLocation=function(r,u){var c=i.getBodyData("data-latitude"),l=i.getBodyData("data-longitude"),a,v,o,f,s,h;if(window.location.href.indexOf("tennis-in-")>-1)a=window.location.href.split("tennis-in-")[1].split("/")[0].replace(/-/g," "),o=new google.maps.Geocoder,o.geocode({address:a,componentRestrictions:{country:"UK"}},function(t){n.setUserLocation(t[0],!1),n.$apply()});else if(c!=undefined&&l!=undefined)v=new google.maps.LatLng(c,l),o=new google.maps.Geocoder,o.geocode({latLng:v},function(t){n.setUserLocation(t[0],!1),n.$apply()});else if(t.stored.cookie!=undefined)f=JSON.parse(t.stored.cookie),f.geometry&&(s=f.geometry.location[Object.keys(f.geometry.location)[0]],h=f.geometry.location[Object.keys(f.geometry.location)[1]],f.geometry.location=new google.maps.LatLng(s,h),n.setUserLocation(f));else if(t.stored.profile){var e=JSON.parse(t.stored.profile),s=e.geometry.location[Object.keys(e.geometry.location)[0]],h=e.geometry.location[Object.keys(e.geometry.location)[1]];e.geometry.location=new google.maps.LatLng(s,h),n.setUserLocation(e)}else n.findLocation(r,u)},n.findLocation=function(i,r){n.setPreset=i,r&&r.preventDefault(),navigator.geolocation?navigator.geolocation.getCurrentPosition(function(i){n.revGeocode(i),t.locationError=!1},function(i){switch(i.code){case 1:t.locationError="Sorry, your location is not accessible. Please enable geolocation or enter a location into the box above.",t.disableGeolocate=!0;break;case 2:t.locationError="Sorry, your location is not available. Please enter a location into the box above.",t.disableGeolocate=!0;break;case 3:t.locationError="Sorry, it has taken too long to find you. Please enter a location into the box above.",t.disableGeolocate=!0}n.$apply()},{enableHighAccuracy:!1,timeout:1e4}):(t.locationError="Your browser doesn’t support geolocation. Please enter a location into the box above.",t.disableGeolocate=!0)},n.toggleFilters=function(n){n.preventDefault(),t.showFilters=!t.showFilters},n.handleKeypress=function(n){n.keyCode===13&&(n.stopPropagation(),n.preventDefault())}}]).run(["$rootScope","helper",function(n,t){n.userLocation,n.locationError="Please enable geolocation or enter a location into the box above.",n.disableGeolocate=!1,n.stored={cookie:t.getStorage("ltalocation",!0),profile:profilelocation}}])